44from urllib .parse import urlparse , parse_qs
55
66from mercantile import bounds
7- from pyproj import Proj , transform
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,8 +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 = Proj ({'init' : 'epsg:4326' })
53- p2 = Proj (** src .crs )
5455
5556 # offset our imagery in the "destination pixel" space
5657 offset_bound = dict ()
@@ -63,8 +64,12 @@ def get_tile_tif(tile, imagery, folder, kwargs):
6364 offset_bound ['south' ] = bound .south + imagery_offset [1 ] * deg_per_pix_y
6465
6566 # project tile boundaries from lat/lng to source CRS
66- tile_ul_proj = transform (p1 , p2 , offset_bound ['west' ], offset_bound ['north' ])
67- tile_lr_proj = transform (p1 , p2 , 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+
6873 # get origin point from the TIF
6974 tif_ul_proj = (src .bounds .left , src .bounds .top )
7075
@@ -106,16 +111,12 @@ def get_tile_wms(tile, imagery, folder, kwargs):
106111
107112 # find our tile bounding box
108113 bound = bounds (* [int (t ) for t in tile .split ('-' )])
109- p1 = Proj ({'init' : 'epsg:4326' })
110- p2 = Proj ({'init' : wms_srs })
114+ xmin , ymin , xmax , ymax = transform_bounds (WGS84_CRS , CRS .from_string (wms_srs ), * bound , densify_pts = 21 )
111115
112116 # project the tile bounding box from lat/lng to WMS SRS
113- tile_ll_proj = transform (p1 , p2 , bound .west , bound .south )
114- tile_ur_proj = transform (p1 , p2 , bound .east , bound .north )
115- if wms_version == '1.3.0' :
116- bbox = tile_ll_proj [::- 1 ] + tile_ur_proj [::- 1 ]
117- else :
118- 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+ )
119120
120121 # request the image with the transformed bounding box and save
121122 wms_url = imagery .replace ('{bbox}' , ',' .join ([str (b ) for b in bbox ]))
@@ -136,7 +137,7 @@ def is_tif(imagery):
136137 valid_tif = False
137138 else :
138139 valid_tif = True
139- except rasterio ._err . CPLE_HttpResponseError : #pylint: disable=protected-access
140+ except rasterio .errors . RasterioIOError :
140141 # rasterio cannot open the path. this is the case for a
141142 # tile service
142143 valid_tif = False
0 commit comments