Google静态地图图块的角坐标


12

如何计算通过http://maps.googleapis.com/maps/api/staticmap获取的静态Google Map图像的角坐标(边界框)?center = 40.714728,-73.998672zoom = 12&size = 400x400&sensor = false


您能解释为什么需要每个图像的角坐标吗?
Mapperz

我不是OP,但是有类似的需求。我有要绘制的GPS坐标,并且需要背景图像才能绘制数据。为了使这些图起作用,我需要确保我具有Google地图图像中边界框的准确坐标。
Joon

Answers:


9

我认为这并不是一个很难解决的问题,但我对准确性是否绝对正确表示怀疑。首先,您必须使用gdal2tiles代码将中心纬度/经度转换为像素。如果我愿意的话,可以将其转换为稳定的代码以查找角坐标。

这是一个Python代码:

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 = LatLonToMeters(MetersToPixels( mx, my, zoom))

然后,您可以通过查看下图使用加法或减法:

数学

如果要找到A点:

x = pixel_x - 200
y = pixel_y + 200

或者您想找到B点:

x = pixel_x + 200
y = pixel_y + 200

最后要做的就是将像素转换为经/纬度。

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) )

所以我得到的结果是:

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

希望对您有帮助。


谢谢。精度还可以。有任何简单的计算方法Resolution(zoom)吗?这以OSM闻名,但我在Google地图上找不到它。
Boocko

尝试使用initialResolution / (2**zoom)
阿拉贡

我对最后一部分感到怀疑,如何将投影(-73.998672,40.714728)转换为EPSG:900913(-8237494.4864285,4970354.7325767)?
norman784

在这里暗示分辨率(缩放)取决于纬度: gis.stackexchange.com/questions/108373/…–
arivero
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.