轻松显示Python的OpenStreetMap磁贴 [英] Easy OpenStreetMap tile displaying for Python

查看:113
本文介绍了轻松显示Python的OpenStreetMap磁贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的python代码中包含开放式街道地图(OSM).

I want to include the open street map (OSM) in my python code.

我已经阅读了很多有关OSM的网页.但是不幸的是,我对于使用哪种软件包最满意感到迷茫.

I have read through lots of webpages regarding to OSM. But unfortunately I'm a bit lost, regarding which package I use best.

我正在寻找一种在我的应用中获取OSM映像的简单方法.作为起点,我在想类似的东西:

I'm looking for an easy way to get an OSM image in my app. As I starting point I'm thinking of something like:

import matplotlib.pyplot as plt

# Pseudo - Code for required function 'GetOSMImage'
Map = GetOSMImage(lat,long,delta_lat,delta_long)

imgplot = plt.imshow(Map)

稍后我想在此plt中添加绘图我的其他数据. (我知道我需要处理投影等).

Later I want to add plot my additional data in this plt. (I'm aware that I'll need to deal with projections etc.)

我不需要/想要的东西:

What I don't need/want:

  • 在我自己的网站上显示
  • 要将我的数据上传到某些Internet服务器
  • 互动功能,例如缩放,滚动(首先)
  • 手动处理和呈现OSM中的.xml数据
  • 首先,我不想定义渲染样式的每个细节.我希望/期望存在一些默认样式.

您对我有一个很好的起点吗? 还是我低估了这个主题的复杂性?

Do you have a good starting point for me? Or do I underestimate the complexity of this topic?

推荐答案

根据您的输入,我能够实现目标.这是我给其他人的代码,它们正在寻找OSM的起点. (当然,还有很多改进的空间.)

Based on your input, I was able to achive my target. Here is my code for others, which are searching a starting point to OSM. (Of course there is still much room for improvements).

import matplotlib.pyplot as plt
import numpy as np

import math
import urllib2
import StringIO
from PIL import Image



def deg2num(lat_deg, lon_deg, zoom):
  lat_rad = math.radians(lat_deg)
  n = 2.0 ** zoom
  xtile = int((lon_deg + 180.0) / 360.0 * n)
  ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
  return (xtile, ytile)

def num2deg(xtile, ytile, zoom):
  n = 2.0 ** zoom
  lon_deg = xtile / n * 360.0 - 180.0
  lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
  lat_deg = math.degrees(lat_rad)
  return (lat_deg, lon_deg)



def getImageCluster(lat_deg, lon_deg, delta_lat,  delta_long, zoom):
    smurl = r"http://a.tile.openstreetmap.org/{0}/{1}/{2}.png"
    xmin, ymax =deg2num(lat_deg, lon_deg, zoom)
    xmax, ymin =deg2num(lat_deg + delta_lat, lon_deg + delta_long, zoom)

    Cluster = Image.new('RGB',((xmax-xmin+1)*256-1,(ymax-ymin+1)*256-1) ) 
    for xtile in range(xmin, xmax+1):
        for ytile in range(ymin,  ymax+1):
            try:
                imgurl=smurl.format(zoom, xtile, ytile)
                print("Opening: " + imgurl)
                imgstr = urllib2.urlopen(imgurl).read()
                tile = Image.open(StringIO.StringIO(imgstr))
                Cluster.paste(tile, box=((xtile-xmin)*256 ,  (ytile-ymin)*255))
            except: 
                print("Couldn't download image")
                tile = None

    return Cluster



if __name__ == '__main__':

    a = getImageCluster(38.5, -77.04, 0.02,  0.05, 13)
    fig = plt.figure()
    fig.patch.set_facecolor('white')
    plt.imshow(np.asarray(a))
    plt.show()

这篇关于轻松显示Python的OpenStreetMap磁贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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