我注意到,在Google地图页面中,您可以获得一个“嵌入”链接以放入iframe并在浏览器中加载地图。 (这里没有消息)
图像尺寸可以调整得非常大,所以我有兴趣将单个大图像作为单个.PNGs。
更具体地说,我想从边界框(右上角和左下角坐标)定义一个矩形区域,并获得相应的图像,并具有适当的缩放系数。
但我的问题是:如何使用Python将此地图的“像素内容”作为图像对象?
(我的理由是:如果浏览器可以获取并呈现这样的图像内容,那么Python也应该能够做到这一点。)
编辑:这是显示我的示例地图的HTML文件的内容:
<iframe
width="2000"
height="1500"
frameborder="0"
scrolling="yes"
marginheight="0"
marginwidth="0"
src="http://maps.google.com.br/maps?hl=pt-BR&ll=-30.027489,-51.229248&spn=1.783415,2.745209&z=10&output=embed"/>
编辑:我按照Ned Batchelder的建议做了,并使用从上面的iframe中获取的urllib.urlopen()
地址阅读src
来电的内容。结果是很多javascript代码,我认为这与Google Maps JavaScript API有关。所以,问题依然存在:为了得到地图图像,我怎样才能从Python中的所有这些东西中做一些有用的东西?
编辑:此链接似乎包含一些关于Google地图如何平铺地图的相关信息: http://www.codeproject.com/KB/scrapbook/googlemap.aspx
答案 0 :(得分:23)
我感谢所有答案。我最后用另一种方式解决了这个问题,使用谷歌地图静态API和一些公式从坐标空间转换为像素空间,这样我就可以得到精确的“拼接”在一起的图像。
对于任何感兴趣的人,这是代码。如果它对某人有帮助,请发表评论!
=============================
import Image, urllib, StringIO
from math import log, exp, tan, atan, pi, ceil
EARTH_RADIUS = 6378137
EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS
INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0
ORIGIN_SHIFT = EQUATOR_CIRCUMFERENCE / 2.0
def latlontopixels(lat, lon, zoom):
mx = (lon * ORIGIN_SHIFT) / 180.0
my = log(tan((90 + lat) * pi/360.0))/(pi/180.0)
my = (my * ORIGIN_SHIFT) /180.0
res = INITIAL_RESOLUTION / (2**zoom)
px = (mx + ORIGIN_SHIFT) / res
py = (my + ORIGIN_SHIFT) / res
return px, py
def pixelstolatlon(px, py, zoom):
res = INITIAL_RESOLUTION / (2**zoom)
mx = px * res - ORIGIN_SHIFT
my = py * res - ORIGIN_SHIFT
lat = (my / ORIGIN_SHIFT) * 180.0
lat = 180 / pi * (2*atan(exp(lat*pi/180.0)) - pi/2.0)
lon = (mx / ORIGIN_SHIFT) * 180.0
return lat, lon
############################################
# a neighbourhood in Lajeado, Brazil:
upperleft = '-29.44,-52.0'
lowerright = '-29.45,-51.98'
zoom = 18 # be careful not to get too many images!
############################################
ullat, ullon = map(float, upperleft.split(','))
lrlat, lrlon = map(float, lowerright.split(','))
# Set some important parameters
scale = 1
maxsize = 640
# convert all these coordinates to pixels
ulx, uly = latlontopixels(ullat, ullon, zoom)
lrx, lry = latlontopixels(lrlat, lrlon, zoom)
# calculate total pixel dimensions of final image
dx, dy = lrx - ulx, uly - lry
# calculate rows and columns
cols, rows = int(ceil(dx/maxsize)), int(ceil(dy/maxsize))
# calculate pixel dimensions of each small image
bottom = 120
largura = int(ceil(dx/cols))
altura = int(ceil(dy/rows))
alturaplus = altura + bottom
final = Image.new("RGB", (int(dx), int(dy)))
for x in range(cols):
for y in range(rows):
dxn = largura * (0.5 + x)
dyn = altura * (0.5 + y)
latn, lonn = pixelstolatlon(ulx + dxn, uly - dyn - bottom/2, zoom)
position = ','.join((str(latn), str(lonn)))
print x, y, position
urlparams = urllib.urlencode({'center': position,
'zoom': str(zoom),
'size': '%dx%d' % (largura, alturaplus),
'maptype': 'satellite',
'sensor': 'false',
'scale': scale})
url = 'http://maps.google.com/maps/api/staticmap?' + urlparams
f=urllib.urlopen(url)
im=Image.open(StringIO.StringIO(f.read()))
final.paste(im, (int(x*largura), int(y*altura)))
final.show()
答案 1 :(得分:14)
您应该直接使用Google API将图片作为静态图片,而不是尝试使用嵌入式链接。这是Google Maps static image API的链接 - 看起来你可以像传统的可嵌入参数一样传入URL中的long / lat参数。例如:
http://maps.googleapis.com/maps/api/staticmap?center=-30.027489,-51.229248&size=600x600&zoom=14&sensor=false
为您提供600x600街道级概述,其中心是您在上面给出的坐标,这似乎是巴西的Porto Alegre。现在,您可以使用urlopen
和PIL
作为Ned建议:
from cStringIO import StringIO
import Image
import urllib
url = "http://maps.googleapis.com/maps/api/staticmap?center=-30.027489,-51.229248&size=800x800&zoom=14&sensor=false"
buffer = StringIO(urllib.urlopen(url).read())
image = Image.open(buffer)
答案 2 :(得分:6)
对于使用python 3.x的人来说,这是Daniel Roseman's answer:
Python 3.x代码:
from io import BytesIO
from PIL import Image
from urllib import request
import matplotlib.pyplot as plt # this is if you want to plot the map using pyplot
url = "http://maps.googleapis.com/maps/api/staticmap?center=-30.027489,-51.229248&size=800x800&zoom=14&sensor=false"
buffer = BytesIO(request.urlopen(url).read())
image = Image.open(buffer)
# Show Using PIL
image.show()
# Or using pyplot
plt.imshow(image)
plt.show()
答案 3 :(得分:6)
根据heltonbiker对BenElgar更改的优秀答案,下面是Python 3的一些更新代码以及API密钥访问的添加,希望它对某些人有用:
"""
Stitch together Google Maps images from lat, long coordinates
Based on work by heltonbiker and BenElgar
Changes:
* updated for Python 3
* added Google Maps API key (compliance with T&C, although can set to None)
* handle http request exceptions
"""
import requests
from io import BytesIO
from math import log, exp, tan, atan, pi, ceil
from PIL import Image
import sys
EARTH_RADIUS = 6378137
EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS
INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0
ORIGIN_SHIFT = EQUATOR_CIRCUMFERENCE / 2.0
GOOGLE_MAPS_API_KEY = None # set to 'your_API_key'
def latlontopixels(lat, lon, zoom):
mx = (lon * ORIGIN_SHIFT) / 180.0
my = log(tan((90 + lat) * pi/360.0))/(pi/180.0)
my = (my * ORIGIN_SHIFT) /180.0
res = INITIAL_RESOLUTION / (2**zoom)
px = (mx + ORIGIN_SHIFT) / res
py = (my + ORIGIN_SHIFT) / res
return px, py
def pixelstolatlon(px, py, zoom):
res = INITIAL_RESOLUTION / (2**zoom)
mx = px * res - ORIGIN_SHIFT
my = py * res - ORIGIN_SHIFT
lat = (my / ORIGIN_SHIFT) * 180.0
lat = 180 / pi * (2*atan(exp(lat*pi/180.0)) - pi/2.0)
lon = (mx / ORIGIN_SHIFT) * 180.0
return lat, lon
def get_maps_image(NW_lat_long, SE_lat_long, zoom=18):
ullat, ullon = NW_lat_long
lrlat, lrlon = SE_lat_long
# Set some important parameters
scale = 1
maxsize = 640
# convert all these coordinates to pixels
ulx, uly = latlontopixels(ullat, ullon, zoom)
lrx, lry = latlontopixels(lrlat, lrlon, zoom)
# calculate total pixel dimensions of final image
dx, dy = lrx - ulx, uly - lry
# calculate rows and columns
cols, rows = int(ceil(dx/maxsize)), int(ceil(dy/maxsize))
# calculate pixel dimensions of each small image
bottom = 120
largura = int(ceil(dx/cols))
altura = int(ceil(dy/rows))
alturaplus = altura + bottom
# assemble the image from stitched
final = Image.new("RGB", (int(dx), int(dy)))
for x in range(cols):
for y in range(rows):
dxn = largura * (0.5 + x)
dyn = altura * (0.5 + y)
latn, lonn = pixelstolatlon(ulx + dxn, uly - dyn - bottom/2, zoom)
position = ','.join((str(latn), str(lonn)))
print(x, y, position)
urlparams = {'center': position,
'zoom': str(zoom),
'size': '%dx%d' % (largura, alturaplus),
'maptype': 'satellite',
'sensor': 'false',
'scale': scale}
if GOOGLE_MAPS_API_KEY is not None:
urlparams['key'] = GOOGLE_MAPS_API_KEY
url = 'http://maps.google.com/maps/api/staticmap'
try:
response = requests.get(url, params=urlparams)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
im = Image.open(BytesIO(response.content))
final.paste(im, (int(x*largura), int(y*altura)))
return final
############################################
if __name__ == '__main__':
# a neighbourhood in Lajeado, Brazil:
NW_lat_long = (-29.44,-52.0)
SE_lat_long = (-29.45,-51.98)
zoom = 18 # be careful not to get too many images!
result = get_maps_image(NW_lat_long, SE_lat_long, zoom=18)
result.show()
答案 4 :(得分:5)
@ 4Oh4的答案是正确的,但数学比他们需要的更复杂。度和弧度之间的转换比他们需要的更频繁地发生。根本没有任何理由调用地球的半径 - 它在所有计算中都取消了。无理由地将偏移添加到像素坐标。徽标截止值比它需要的大。还有一些其他的可能性和目的,这些都是在变化中写成的。这是我的版本:
#!/usr/bin/env python
"""
Stitch together Google Maps images from lat, long coordinates
Based on work by heltonbiker and BenElgar
Changes:
* updated for Python 3
* added Google Maps API key (compliance with T&C, although can set to None)
* handle http request exceptions
With contributions from Eric Toombs.
Changes:
* Dramatically simplified the maths.
* Set a more reasonable default logo cutoff.
* Added global constants for logo cutoff and max image size.
* Translated a couple presumably Portuguese variable names to English.
"""
import requests
from io import BytesIO
from math import log, exp, tan, atan, ceil
from PIL import Image
import sys
# circumference/radius
tau = 6.283185307179586
# One degree in radians, i.e. in the units the machine uses to store angle,
# which is always radians. For converting to and from degrees. See code for
# usage demonstration.
DEGREE = tau/360
ZOOM_OFFSET = 8
GOOGLE_MAPS_API_KEY = None # set to 'your_API_key'
# Max width or height of a single image grabbed from Google.
MAXSIZE = 640
# For cutting off the logos at the bottom of each of the grabbed images. The
# logo height in pixels is assumed to be less than this amount.
LOGO_CUTOFF = 32
def latlon2pixels(lat, lon, zoom):
mx = lon
my = log(tan((lat + tau/4)/2))
res = 2**(zoom + ZOOM_OFFSET) / tau
px = mx*res
py = my*res
return px, py
def pixels2latlon(px, py, zoom):
res = 2**(zoom + ZOOM_OFFSET) / tau
mx = px/res
my = py/res
lon = mx
lat = 2*atan(exp(my)) - tau/4
return lat, lon
def get_maps_image(NW_lat_long, SE_lat_long, zoom=18):
ullat, ullon = NW_lat_long
lrlat, lrlon = SE_lat_long
# convert all these coordinates to pixels
ulx, uly = latlon2pixels(ullat, ullon, zoom)
lrx, lry = latlon2pixels(lrlat, lrlon, zoom)
# calculate total pixel dimensions of final image
dx, dy = lrx - ulx, uly - lry
# calculate rows and columns
cols, rows = ceil(dx/MAXSIZE), ceil(dy/MAXSIZE)
# calculate pixel dimensions of each small image
width = ceil(dx/cols)
height = ceil(dy/rows)
heightplus = height + LOGO_CUTOFF
# assemble the image from stitched
final = Image.new('RGB', (int(dx), int(dy)))
for x in range(cols):
for y in range(rows):
dxn = width * (0.5 + x)
dyn = height * (0.5 + y)
latn, lonn = pixels2latlon(
ulx + dxn, uly - dyn - LOGO_CUTOFF/2, zoom)
position = ','.join((str(latn/DEGREE), str(lonn/DEGREE)))
print(x, y, position)
urlparams = {
'center': position,
'zoom': str(zoom),
'size': '%dx%d' % (width, heightplus),
'maptype': 'satellite',
'sensor': 'false',
'scale': 1
}
if GOOGLE_MAPS_API_KEY is not None:
urlparams['key'] = GOOGLE_MAPS_API_KEY
url = 'http://maps.google.com/maps/api/staticmap'
try:
response = requests.get(url, params=urlparams)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
im = Image.open(BytesIO(response.content))
final.paste(im, (int(x*width), int(y*height)))
return final
############################################
if __name__ == '__main__':
# a neighbourhood in Lajeado, Brazil:
NW_lat_long = (-29.44*DEGREE, -52.0*DEGREE)
SE_lat_long = (-29.45*DEGREE, -51.98*DEGREE)
zoom = 18 # be careful not to get too many images!
result = get_maps_image(NW_lat_long, SE_lat_long, zoom=18)
result.show()
答案 5 :(得分:3)
更简洁的Python 2.x兼容方法
from io import BytesIO
import Image
import urllib
url = "http://maps.googleapis.com/maps/api/staticmap?center=52.50058,13.31316&size=800x800&zoom=14"
buffer = BytesIO(urllib.urlopen(url).read())
image = Image.open(buffer)
image.save("map.png")
答案 6 :(得分:1)
获取/保存Google静态地图图像的最简单方法(作为png):
import requests
img = open('tmp.png','wb')
img.write(requests.get('https://maps.googleapis.com/maps/api/staticmap?center=33.0456,131.3009&zoom=12&size=320x385&key=YOUR_API_KEY').content)
img.close()
答案 7 :(得分:0)
urllib.urlopen
会打开一个网址,结果会有一个.read()
方法,可以用来获取图片字节。 cStringIO
具有基于内存中字符串的类文件对象。 PIL有一个Image.open
函数,可以打开一个类似文件的东西,为你提供一个图像对象。可以询问图像对象的像素值。