显示形状文件
Displaying a Shapefile
我有一个 shapefile 想要显示。我尝试使用 matplotlib 来显示它,但我得到了这个:
但是,当我尝试使用在线网站进行展示时,我得到了这个;
如何获取第二张图片?
这是我的代码:
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 内部有多个形状,我需要绘制所有这些形状。由此可见,这是有效的:
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()
使用 GeoPandas:
import geopandas as gpd
shape=gpd.read_file('shapefile')
shape.plot()
使用 pyshp 和笛卡尔:
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()
,如 this answer 中所述。
我有一个 shapefile 想要显示。我尝试使用 matplotlib 来显示它,但我得到了这个:
如何获取第二张图片?
这是我的代码:
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 内部有多个形状,我需要绘制所有这些形状。由此可见,这是有效的:
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()
使用 GeoPandas:
import geopandas as gpd
shape=gpd.read_file('shapefile')
shape.plot()
使用 pyshp 和笛卡尔:
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()
,如 this answer 中所述。