【问题标题】:Python: How to use matplotlib basemap with shapefilesPython:如何将 matplotlib 底图与 shapefile 一起使用
【发布时间】:2018-03-24 14:24:15
【问题描述】:

对于我大学的一个项目,我想使用 Python 2.7 显示一个(德国)城市的地图,主要是道路,最后包括几个服务点的地理位置。我有 Python 的基本知识,但我觉得我无法访问为实现目标而需要执行的步骤。

我使用底图来限制城市所在的经度和纬度区域。然后我想使用 shapefile 来包含已提供的道路,但我收到错误消息“RuntimeError:无法将单个艺术家放入多个图中”。尽管搜索我找不到解决方案。这是我使用的代码:

import matplotlib.pyplot as plt
import matplotlib.cm
import numpy as np
import shapefile as shp

from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.collections import LineCollection
from matplotlib.collections import PolyCollection
from matplotlib.colors import Normalize

fig, ax=plt.subplots(figsize=(10,20))

m = Basemap(resolution='c', # c, l, i, h, f or None
            projection='merc',
           #lat lowleft: llcrnrlon, lon low right llcrnrlat
            #lat upleft urcrnrlon=47.81,, lon up right urcrnrlat=10.5
            llcrnrlon=8.45195, llcrnrlat=49.477816, urcrnrlon=8.491432, urcrnrlat=49.50117)    

drawmapboundary(fill_color='aqua')
m.fillcontinents(color='firebrick')
m.drawrivers(color='aqua')
m.drawcoastlines()

plt.show()

m.readshapefile('roads', 'roads')

然后我得到了 Google Maps API,但在我看来,我不能用它们来分发地图。另外,加载 pygmaps 包装器崩溃:“ImportError: No module named pygmaps”,反正我不知道怎么用。

这里有人知道我可以采取哪些步骤来实现我的目标吗?我将非常感谢您的建议。提前致谢!

【问题讨论】:

  • 请分享您使用的代码,这将澄清您的问题。
  • 我在原帖中编辑过
  • 查看this post 以获取有关如何在Basemap 中使用shapefile 的非常基本的示例,并可能查看this post 以获取更复杂的示例。此外,请参阅this post 了解绘制街道网络的不同方法。如果这些都不能帮助您,请告诉我们在哪里可以找到您尝试使用的 shapefile。

标签: python google-maps-api-3 shapefile matplotlib-basemap


【解决方案1】:

使用docs,读取shapefile就像这样简单:

m.readshapefile('path_to/shapefile/shapefile', 'name')

请注意,第一个参数不需要 .shp 在 shapefile 名称的末尾。如果要访问 shapefile 的元素:

name_info = m.readshapefile('path_to/shapefile/shapefile', 'name')
for info, shape in zip(m.name_info, m.name):
    print(info)

这将在该 shapefile 中打印出包含您的属性和值的字典。从那里,您可以使用 shapefile。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2013-08-22
    • 2022-08-06
    • 2017-09-14
    • 2020-11-19
    相关资源
    最近更新 更多