使用Python捕获嵌入的Google地图图像,而不使用浏览器 [英] Capture embedded google map image with Python without using a browser

查看:176
本文介绍了使用Python捕获嵌入的Google地图图像,而不使用浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在Google地图页面中,您可以获得一个嵌入链接,将其放入iframe并在浏览器中加载地图。 (这里没有新闻)



图片大小可以调整得非常大,所以我很想将som大图像作为单个.PNGs。



更具体地说,我想从边界框(右上角和左下角坐标)定义一个矩形区域,并以适当的缩放系数获取相应的图像。 p>

但我的问题是:如何使用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&amp;ll=-30.027489,-51.229248&amp;spn=1.783415,2.745209& ;安培; Z = 10&放大器;放大器;输出=嵌入/>

编辑:我按照Ned Batchelder的建议操作,并阅读 urllib.urlopen()使用上面iframe中的 src 地址进行调用。其结果是很多JavaScript代码,我认为这与Google Maps JavaScript API有关。所以,这个问题仍然存在:我怎么能从Python中的所有东西中做一些有用的东西来获得地图图像?

编辑:这个链接似乎包含一些有关Google地图如何贴图地图的相关信息:
http:// www。 codeproject.com/KB/scrapbook/googlemap.aspx



也是:
http://econym.org.uk/gmap/howitworks.htm

解决方案

我感谢所有的答案。我最终以另一种方式解决了这个问题,使用Google Maps Static API和一些公式将坐标空间转换为像素空间,这样我就可以获得精确拼接的精确图像。



对于任何感兴趣的人,这里是代码。如果有人帮忙,请评论!



=========================== ==

  import image,urllib,StringIO 
从数学导入日志,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

######################### ###################

#在巴西Lajeado的邻居:

upperleft ='-29.44, - 52.0'
lowerright ='-29.45,-51.98'

zoom = 18#小心不要让太多图片!

########################################## ##

ullat,ullon = map(float,upperleft.split(','))
lrlat,lrlon = map(float,lowerright.split(','))

#设置一些重要参数
scale = 1
maxsize = 640

#将所有这些坐标转换为像素
ulx,uly = latlontopixels( ulaon,zoom)
lrx,lry = latlontopixels(lrlat,lrlon,zoom)

#计算最终图像的总像素尺寸
dx,dy = lrx - ulx, uly - lry

#计算行和列
cols,rows = int(ceil(dx / maxsize)),int(ceil(dy / maxsize))

#计算每个小图像的像素尺寸
bottom = 120
largura = int(ceil(dx / cols))
altura = int(ceil(dy / rows))
alturaplus = altura +底部


final = Image.new(RGB,(int(dx),int(dy)))
for range in(cols) :
在范围(行)中为y:
dxn = largura *(0.5 + x)
dyn = altura *(0.5 + y)
lat n,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()


I have noticed that, from Google Maps page, you can get an "embed" link to put inside an iframe and load the map in a browser. (no news here)

The image size can be adjusted to be very large, so I am interested in getting som big images as single .PNGs.

More specifically, I would like to define a rectangular area from a bounding box (upper-right and lower-left coordinates), and get the corresponding image, with an appropriate zoom factor.

But my question is: How can I use Python to get the "pixel content" of this map as an image object?

(My rationale is: if the browser can get and render such image content, then Python should be capable of doing it, too).

EDIT: this is the content of the HTML file that shows my sample map:

<iframe 
    width="2000"
    height="1500"
    frameborder="0"
    scrolling="yes"
    marginheight="0"
    marginwidth="0"
    src="http://maps.google.com.br/maps?hl=pt-BR&amp;ll=-30.027489,-51.229248&amp;spn=1.783415,2.745209&amp;z=10&amp;output=embed"/>

EDIT: I did as suggested by Ned Batchelder, and read the content of an urllib.urlopen() call using the src address taken from the iframe above. The result was a lot of javascript code, which I think has to do with the Google Maps JavaScript API. So, the question lingers: how could I do some useful stuff from all this stuff in Python in order to get the map image?

EDIT: this link appears to contain some pretty relevant info on how Google Maps tiles their maps: http://www.codeproject.com/KB/scrapbook/googlemap.aspx

also: http://econym.org.uk/gmap/howitworks.htm

解决方案

I thank for all the answers. I ended up solving the problem another way, using Google Maps Static API and some formulas to convert from Coordinate space to Pixel space, so that I can get precise images that "stitch" nicely together.

For anyone interested, here is the code. If it helps someone, please comment!

=============================

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

这篇关于使用Python捕获嵌入的Google地图图像,而不使用浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆