How to calculate the corner coordinates (bounding box) of a static Google Map image, fetched with e.g. http://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=12&size=400x400&sensor=false ?
-
can you explain why you need that corner coordinates of each image?– Mapperz ♦Commented Jan 10, 2013 at 14:31
-
I'm not OP, but have a similar need. I have GPS coords that I want to plot, and need a background image to plot the data on. For the plots to work I need to ensure that I have the accurate coords of the bounding box from the google map image.– JoonCommented Jan 26, 2020 at 12:44
1 Answer
I think this is not so hard a problem to solve but I have my doubts about whether the accuracy is absolutely correct. First of all, you have to convert your center lat/lon to pixels with gdal2tiles codes. If I find some time and if you want, I can convert it to stable code for finding corner coordinates.
This is a Python code:
tileSize = 256
initialResolution = 2 * math.pi * 6378137 / tileSize
# 156543.03392804062 for tileSize 256 pixels
originShift = 2 * math.pi * 6378137 / 2.0
# 20037508.342789244
def LatLonToMeters( lat, lon ):
"Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913"
mx = lon * originShift / 180.0
my = math.log( math.tan((90 + lat) * math.pi / 360.0 )) / (math.pi / 180.0)
my = my * originShift / 180.0
return mx, my
def MetersToPixels( mx, my, zoom):
"Converts EPSG:900913 to pyramid pixel coordinates in given zoom level"
res = Resolution( zoom )
px = (mx + originShift) / res
py = (my + originShift) / res
return px, py
# Dont forget you have to convert your projection to EPSG:900913
mx = -8237494.4864285 #-73.998672
my = 4970354.7325767 # 40.714728
zoom = 12
pixel_x, pixel_y = MetersToPixels( *LatLonToMeters( mx, my ), zoom)
Then you can use addition or subtraction by looking the following image:
If you want to find point A :
x = pixel_x - 200
y = pixel_y + 200
or you want to find point B:
x = pixel_x + 200
y = pixel_y + 200
and the last thing is that you have to do is convert your pixels to lat/lon.
def PixelsToMeters( px, py, zoom):
"Converts pixel coordinates in given zoom level of pyramid to EPSG:900913"
res = Resolution(zoom)
mx = px * res - originShift
my = py * res - originShift
return mx, my
def MetersToLatLon( mx, my ):
"Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum"
lon = (mx / originShift) * 180.0
lat = (my / originShift) * 180.0
lat = 180 / math.pi * (2 * math.atan(math.exp(lat * math.pi / 180.0)) - math.pi / 2.0)
return lat, lon
#Result
llx, lly = MetersToLatLon( *PixelsToMeters( x, y, zoom) )
so the result I have:
point A - UpperLeftLatLon = 40.7667530977 -74.0673365509
point B - UpperRightLatLon = 40.7667530977 -73.9300074493
point C - LowerRightLatLon = 40.6626622172 -73.9300074493
point D - LowerLeftLatLon = 40.6626622172 -74.0673365509
I hope it helps you....
-
Thanks. The accuracy is OK. Any easy way to calculate
Resolution(zoom)
? This is known for OSM, but I can't find it for Google maps.– BoockoCommented Jan 10, 2013 at 20:37 -
-
I've a doubt with the last part, how do you convert your projection (-73.998672, 40.714728) to EPSG:900913 (-8237494.4864285, 4970354.7325767)? Commented Nov 26, 2013 at 18:06
-
Here it is hinted that Resolution(zoom) depends on latitude: gis.stackexchange.com/questions/108373/…– ariveroCommented Sep 22, 2016 at 13:47