Skip to content

Commit 96c87d4

Browse files
authored
Merge pull request #319 from mapswipe/dev
Google format custom tile servers
2 parents 48b5cc8 + 461f13e commit 96c87d4

File tree

1 file changed

+22
-121
lines changed

1 file changed

+22
-121
lines changed

mapswipe_workers/mapswipe_workers/utils/tile_functions.py

Lines changed: 22 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -36,56 +36,23 @@ def __init__(self, x=0, y=0):
3636

3737

3838
def lat_long_zoom_to_pixel_coords(lat, lon, zoom):
39-
"""
40-
The function to compute pixel coordinates from lat-long point at a given zoom level
41-
42-
Parameters
43-
----------
44-
lat : float
45-
latitude in degree
46-
lon : float
47-
longitude in degree
48-
zoom : int
49-
tile map service zoom level
50-
51-
Returns
52-
-------
53-
p : Point
54-
"""
39+
"""Compute pixel coordinates from lat-long point at a given zoom level."""
5540

5641
p = Point()
5742
sinLat = math.sin(lat * math.pi / 180.0)
5843
x = ((lon + 180) / 360) * 256 * math.pow(2, zoom)
5944
y = (
6045
(0.5 - math.log((1 + sinLat) / (1 - sinLat)) / (4 * math.pi))
61-
* 256
62-
* math.pow(2, zoom)
46+
* 256 # noqa: W503
47+
* math.pow(2, zoom) # noqa: W503
6348
)
6449
p.x = int(math.floor(x))
6550
p.y = int(math.floor(y))
6651
return p
6752

6853

6954
def pixel_coords_zoom_to_lat_lon(PixelX, PixelY, zoom):
70-
"""
71-
The function to compute latitude, longituted from pixel coordinates at a given zoom level
72-
73-
Parameters
74-
----------
75-
PixelX : int
76-
x coordinate
77-
PixelY : int
78-
y coordinate
79-
zoom : int
80-
tile map service zoom level
81-
82-
Returns
83-
-------
84-
lon : float
85-
the longitude in degree
86-
lat : float
87-
the latitude in degree
88-
"""
55+
"""Compute latitude, longituted from pixel coordinates at a given zoom level."""
8956

9057
MapSize = 256 * math.pow(2, zoom)
9158
x = (PixelX / MapSize) - 0.5
@@ -97,20 +64,7 @@ def pixel_coords_zoom_to_lat_lon(PixelX, PixelY, zoom):
9764

9865

9966
def pixel_coords_to_tile_address(PixelX, PixelY):
100-
"""
101-
The function to compute a tile address from pixel coordinates of point within tile.
102-
103-
Parameters
104-
----------
105-
PixelX : int
106-
x coordinate
107-
PixelY : int
108-
y coordinate
109-
110-
Returns
111-
-------
112-
t : Tile
113-
"""
67+
"""Compute a tile address from pixel coordinates of point within tile."""
11468

11569
t = Tile()
11670
t.x = int(math.floor(PixelX / 256))
@@ -121,25 +75,7 @@ def pixel_coords_to_tile_address(PixelX, PixelY):
12175
def tile_coords_zoom_and_tileserver_to_url(
12276
tile_x: int, tile_y: int, tile_z: int, tile_server: dict
12377
) -> str:
124-
"""
125-
The function to Create a URL for a tile based on tile coordinates, zoom and given tile server
126-
127-
Parameters
128-
----------
129-
tile_x : int
130-
x coordinate of tile
131-
tile_y : int
132-
y coordinate of tile
133-
tile_z : int
134-
tile map service zoom level
135-
tile_server : dict
136-
the tile server dictionary containing name and url
137-
138-
Returns
139-
-------
140-
URL : string
141-
the url for the specific tile image
142-
"""
78+
"""Create a URL for a tile based on tile coordinates, zoom and given tile server."""
14379

14480
if tile_server["name"] == "bing":
14581
quadKey = tile_coords_and_zoom_to_quadKey(tile_x, tile_y, tile_z)
@@ -153,12 +89,22 @@ def tile_coords_zoom_and_tileserver_to_url(
15389
layer=tile_server["wmtsLayerName"],
15490
)
15591
elif "maxar" in tile_server["name"]:
156-
# maxar uses not the standard TMS tile y coordinate, but the Google tile y coordinate
157-
# more information here: https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/
92+
# maxar uses not the standard TMS tile y coordinate,
93+
# but the Google tile y coordinate
94+
# more information here:
95+
# https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/
15896
tile_y = int(math.pow(2, tile_z) - tile_y) - 1
15997
url = tile_server["url"].format(
16098
key=tile_server["apiKey"], x=tile_x, y=tile_y, z=tile_z,
16199
)
100+
elif "{-y}" in tile_server["url"]:
101+
# this uses not the standard TMS tile y coordinate,
102+
# but the Google tile y coordinate
103+
# more information here:
104+
# https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/
105+
tile_y = int(math.pow(2, tile_z) - tile_y) - 1
106+
url = tile_server["url"].replace("{-y}", "{y}")
107+
url = url.format(key=tile_server["apiKey"], x=tile_x, y=tile_y, z=tile_z,)
162108
else:
163109
url = tile_server["url"].format(
164110
key=tile_server["apiKey"], x=tile_x, y=tile_y, z=tile_z,
@@ -168,22 +114,7 @@ def tile_coords_zoom_and_tileserver_to_url(
168114

169115

170116
def tile_coords_and_zoom_to_quadKey(TileX, TileY, zoom):
171-
"""
172-
The function to create a quadkey for use with certain tileservers that use them, e.g. Bing
173-
174-
Parameters
175-
----------
176-
TileX : int
177-
x coordinate of tile
178-
TileY : int
179-
y coordinate of tile
180-
zoom : int
181-
tile map service zoom level
182-
183-
Returns
184-
-------
185-
quadKey : str
186-
"""
117+
"""Create a quadkey for use with certain tileservers that use them, e.g. Bing."""
187118

188119
quadKey = ""
189120
for i in range(zoom, 0, -1):
@@ -198,47 +129,17 @@ def tile_coords_and_zoom_to_quadKey(TileX, TileY, zoom):
198129

199130

200131
def quadKey_to_Bing_URL(quadKey, api_key):
201-
"""
202-
The function to create a tile image URL linking to a Bing tile server
203-
204-
Parameters
205-
----------
206-
quadKey : str
207-
the quad key for Bing for the given tile
208-
api_key : str
209-
the Bing maps api key
210-
211-
Returns
212-
-------
213-
tile_url : str
214-
the url for the specific Bing tile image
215-
"""
132+
"""Create a tile image URL linking to a Bing tile server."""
216133

217-
tile_url = "https://ecn.t0.tiles.virtualearth.net/tiles/a{}.jpeg?g=7505&mkt=en-US&token={}".format(
134+
tile_url = "https://ecn.t0.tiles.virtualearth.net/tiles/a{}.jpeg?g=7505&mkt=en-US&token={}".format( # noqa: E501
218135
quadKey, api_key
219136
)
220137

221138
return tile_url
222139

223140

224141
def geometry_from_tile_coords(TileX, TileY, zoom):
225-
"""
226-
The function to compute the polygon geometry of a tile map service tile
227-
228-
Parameters
229-
----------
230-
TileX : int
231-
x coordinate of tile
232-
TileY : int
233-
y coordinate of tile
234-
zoom : int
235-
tile map service zoom level
236-
237-
Returns
238-
-------
239-
wkt_geom : str
240-
the polygon geometry of the tile in WKT format
241-
"""
142+
"""Compute the polygon geometry of a tile map service tile."""
242143

243144
# Calculate lat, lon of upper left corner of tile
244145
PixelX = TileX * 256

0 commit comments

Comments
 (0)