|
"""test.""" |
|
|
|
import os |
|
from rio_tiler.utils import array_to_image, get_vrt_transform |
|
|
|
import mercantile |
|
|
|
import rasterio |
|
from rasterio.enums import Resampling |
|
from rasterio.transform import from_bounds |
|
from rasterio.vrt import WarpedVRT |
|
|
|
input = "https://s3-us-west-2.amazonaws.com/remotepixel-pub/data/Int16_nodata9999.tif" |
|
tile_z = 12 |
|
tile_x = 676 |
|
tile_y = 1620 |
|
|
|
|
|
def tile_read(src_path, bounds, tilesize, warp_resampling, read_resampling): |
|
"""~Copy from rio-tiler.""" |
|
with rasterio.open(src_path) as src_dst: |
|
vrt_transform, vrt_width, vrt_height = get_vrt_transform(src_dst, bounds) |
|
vrt_params = dict( |
|
crs="epsg:3857", |
|
transform=vrt_transform, |
|
width=vrt_width, |
|
height=vrt_height, |
|
nodata=src_dst.nodata, |
|
src_nodata=src_dst.nodata, |
|
add_alpha=False, |
|
resampling=Resampling[warp_resampling], |
|
) |
|
|
|
out_shape = (len(src_dst.indexes), tilesize, tilesize) |
|
with WarpedVRT(src_dst, **vrt_params) as vrt: |
|
data = vrt.read( |
|
out_shape=out_shape, |
|
resampling=Resampling[read_resampling], |
|
) |
|
mask = vrt.dataset_mask(out_shape=(tilesize, tilesize)) |
|
return data, mask |
|
|
|
|
|
def main(warp_resampling, read_resampling): |
|
mercator_tile = mercantile.Tile(x=tile_x, y=tile_y, z=tile_z) |
|
bounds = mercantile.xy_bounds(mercator_tile) |
|
w, s, e, n = bounds |
|
|
|
tilesize = 256 |
|
|
|
dst_crs = {'init': 'EPSG:3857'} |
|
dst_transform = from_bounds(w, s, e, n, tilesize, tilesize) |
|
|
|
tile, mask = tile_read( |
|
input, bounds, tilesize, warp_resampling, read_resampling |
|
) |
|
with open(f"{tile_z}-{tile_x}-{tile_y}_{warp_resampling}_{read_resampling}.tif", "wb") as f: |
|
f.write(array_to_image( |
|
tile, |
|
dtype=tile.dtype, |
|
img_format="GTiff", |
|
crs=dst_crs, |
|
transform=dst_transform |
|
)) |
|
|
|
|
|
if __name__ == '__main__': |
|
main("nearest", "nearest") |
|
main("nearest", "bilinear") |
|
main("bilinear", "bilinear") |
|
main("bilinear", "nearest") |
|
|
|
# fout = f"{tile_z}-{tile_y}-{tile_y}_nearest_gdal.tif" |
|
# os.remove(fout) |
|
# cmd = f"gdalwarp {input} " + \ |
|
# f"{fout} -t_srs EPSG:3857 " + \ |
|
# "-te -13423565.159329513 4177742.217954592 -13413781.219709009 4187526.1575750955 " + \ |
|
# "-te_srs EPSG:3857 -r near -ts 256 256" |
|
# os.system(cmd) |
|
|
|
# fout = f"{tile_z}-{tile_y}-{tile_y}_bilinear_gdal.tif" |
|
# os.remove(fout) |
|
# cmd = f"gdalwarp {input} " + \ |
|
# f"{fout} -t_srs EPSG:3857 " + \ |
|
# "-te -13423565.159329513 4177742.217954592 -13413781.219709009 4187526.1575750955 " + \ |
|
# "-te_srs EPSG:3857 -r bilinear -ts 256 256" |
|
# os.system(cmd) |