44from urllib .parse import urlparse , parse_qs
55
66from mercantile import bounds
7- from pyproj import Transformer
87from PIL import Image
98import numpy as np
109import requests
1110import rasterio
11+ from rasterio .crs import CRS
12+ from rasterio .warp import transform , transform_bounds
13+
14+ WGS84_CRS = CRS .from_epsg (4326 )
1215
1316def url (tile , imagery ):
1417 """Return a tile url provided an imagery template and a tile"""
@@ -49,9 +52,6 @@ def get_tile_tif(tile, imagery, folder, kwargs):
4952 imagery_offset = kwargs .get ('imagery_offset' ) or [0 , 0 ]
5053 with rasterio .open (imagery ) as src :
5154 x_res , y_res = src .transform [0 ], src .transform [4 ]
52- p1 = 'epsg:4326'
53- p2 = str (src .crs )
54- transformer = Transformer .from_crs (p1 , p2 )
5555
5656 # offset our imagery in the "destination pixel" space
5757 offset_bound = dict ()
@@ -64,8 +64,12 @@ def get_tile_tif(tile, imagery, folder, kwargs):
6464 offset_bound ['south' ] = bound .south + imagery_offset [1 ] * deg_per_pix_y
6565
6666 # project tile boundaries from lat/lng to source CRS
67- tile_ul_proj = transformer .transform (offset_bound ['west' ], offset_bound ['north' ])
68- tile_lr_proj = transformer .transform (offset_bound ['east' ], offset_bound ['south' ])
67+ x , y = transform (WGS84_CRS , src .crs , [offset_bound ['west' ]], [offset_bound ['north' ]])
68+ tile_ul_proj = x [0 ], y [0 ]
69+
70+ x , y = transform (WGS84_CRS , src .crs , [offset_bound ['east' ]], [offset_bound ['south' ]])
71+ tile_lr_proj = x [0 ], y [0 ]
72+
6973 # get origin point from the TIF
7074 tif_ul_proj = (src .bounds .left , src .bounds .top )
7175
@@ -107,17 +111,12 @@ def get_tile_wms(tile, imagery, folder, kwargs):
107111
108112 # find our tile bounding box
109113 bound = bounds (* [int (t ) for t in tile .split ('-' )])
110- p1 = 'epsg:4326'
111- p2 = wms_srs
112- transformer = Transformer .from_crs (p1 , p2 )
114+ xmin , ymin , xmax , ymax = transform_bounds (WGS84_CRS , CRS .from_string (wms_srs ), * bound , densify_pts = 21 )
113115
114116 # project the tile bounding box from lat/lng to WMS SRS
115- tile_ll_proj = transformer .transform (bound .west , bound .south )
116- tile_ur_proj = transformer .transform (bound .east , bound .north )
117- if wms_version == '1.3.0' :
118- bbox = tile_ll_proj [::- 1 ] + tile_ur_proj [::- 1 ]
119- else :
120- bbox = tile_ll_proj + tile_ur_proj
117+ bbox = (
118+ [ymin , xmin , ymax , xmax ] if wms_version == "1.3.0" else [xmin , ymin , xmax , ymax ]
119+ )
121120
122121 # request the image with the transformed bounding box and save
123122 wms_url = imagery .replace ('{bbox}' , ',' .join ([str (b ) for b in bbox ]))
0 commit comments