显示一个Shapefile

3
我有一个shapefile想要展示。我尝试使用matplotlib来展示它,但是得到了这个结果: What I Get 然而,当我尝试使用在线网站展示时,我得到了这个结果: What I Want 我该如何得到第二张图片?
以下是我的代码:
import shapefile
import matplotlib.pyplot as plt

print("Initializing Shapefile")
sf = shapefile.Reader("ap_abl")
apShapes = sf.shapes()
points = apShapes[3].points
print("Shapefile Initialized")

print("Initializing Display")
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([78, 79])
plt.ylim([19, 20])
print("Display Initialized")

print("Creating Polygon")
ap = plt.Polygon(points, fill=False, edgecolor="k")
ax.add_patch(ap)
print("Polygon Created")

print("Displaying polygon")
plt.show()

提前感谢您。


Shapefile是一个几何数据的数据库。您必须渲染每一个几何对象。 - James Mills
你知道我怎么访问几何图形吗? - Ace
Shapefile数据库中的每个形状都是一种几何体。您已经访问了其中至少一个!您是从其他地方复制/粘贴此代码而没有理解它在做什么或Shapefile是什么吗? - James Mills
我搞定了!sf.shapes具有所有几何形状的列表。文档使用第三个索引作为演示。 - Ace
1
太好了!阅读文档真的很有帮助! - James Mills
显示剩余2条评论
2个回答

7
使用GeoPandas:
import geopandas as gpd
shape=gpd.read_file('shapefile')
shape.plot()

使用pyshp和Descartes:
from descartes import PolygonPatch
import shapefile
sf=shapefile.Reader('shapefile')
poly=sf.shape(1).__geo_interface__
fig = plt.figure() 
ax = fig.gca() 
ax.add_patch(PolygonPatch(poly, fc='#ffffff', ec='#000000', alpha=0.5, zorder=2 ))
ax.axis('scaled')
plt.show()

如果 shapefile 有多个形状,则可以像这个答案中所讨论的那样循环遍历 sf.shapes()

4
原来shapefile文件中包含多个形状,我需要绘制所有的形状。以下是可行的方法:
import shapefile
import matplotlib.pyplot as plt

sf = shapefile.Reader("ap_abl")

print("Initializing Display")
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([76, 85])
plt.ylim([12, 21])
print("Display Initialized")

for shape in sf.shapes():
    print("Finding Points")
    points = shape.points
    print("Found Points")    

    print("Creating Polygon")
    ap = plt.Polygon(points, fill=False, edgecolor="k")
    ax.add_patch(ap)
    print("Polygon Created")

print("Displaying Polygons")
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接