首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示Shapefile

显示Shapefile
EN

Stack Overflow用户
提问于 2015-05-26 08:46:23
回答 2查看 9.9K关注 0票数 3

我有一个想要显示的shapefile。我尝试使用matplotlib来显示它,但得到的结果如下:

然而,当我尝试使用在线网站显示时,我得到了这样的结果;

怎样才能得到第二张图片?

下面是我的代码:

代码语言:javascript
复制
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()

提前谢谢你。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-26 10:18:58

原来一个shapefile里面有多个形状,我需要把它们都画出来。从那开始,这就是工作原理:

代码语言:javascript
复制
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()
票数 5
EN

Stack Overflow用户

发布于 2018-01-02 14:26:16

使用GeoPandas:

代码语言:javascript
复制
import geopandas as gpd
shape=gpd.read_file('shapefile')
shape.plot()

使用pyshp和笛卡尔:

代码语言:javascript
复制
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有多个形状,那么你可以像this answer中讨论的那样在sf.shapes()上循环。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30447790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档