【问题标题】:Python: Only using part of a shapefilePython:仅使用 shapefile 的一部分
【发布时间】:2017-07-07 17:02:44
【问题描述】:

我正在使用 Python 3.6 创建显示北美降水数据的地图。到目前为止,我的代码将我的数据提取到 shapefile 中,然后将其绘制在地图上。

我的问题是我下载的shapefile 包含所有大陆的形状。我想知道有没有一种方法只能通过我的shapefile 在北美大陆阅读?

如果这不可能,那么有谁知道我可以在哪里下载只有北美的shapefile?谢谢!

这是我的代码中读取shapefile的部分:

import shapefile
sf=shapefile.Reader('continents')

这是 shapefile 的 link

【问题讨论】:

  • 请添加更多详细信息以便为您提供帮助。你试过什么了?该 shapefile 看起来如何,是否有任何可能有帮助的属性(不只是共享链接)?
  • 我尝试将大陆的名称添加到 shapefile 的末尾。例如 sf=shapefile.Reader('continents','North America') 和 sf=shapefile.Reader('continents/North America')。这样做没有任何改变,我仍然只看到一张包含所有大陆的地图。我在我的问题中包含了链接,以便人们可以下载我正在使用的 shapefile 并阅读随附的显示所有属性的 txt 文件,因为它太大而无法在此处复制和粘贴。 @GrayCygnus
  • 我下载了你的 shapefile,我目前正在处理一个 aswer,很快就会发布它
  • 完成了,您可能还想查看GIS Stack Exchange 那里有与GIS相关的主题的问答

标签: python python-3.x gis shapefile


【解决方案1】:

查看您提到的 shapefile,我们可以看到它在其属性表中有一个属性(即“CONTINENT”)。因此,我们需要搜索所有形状并选择与您想要的大陆(北美)相匹配的形状,如下所示:

import shapefile

#read the shapefile
sf=shapefile.Reader('continent.shp')

#obtain shapes and its records
#the shapes are the actual coordinate points 
#the record contains all attributes related to each shape
shape_records = sf.shapeRecords()

#search for the ones with desired records
#this shapefile has only one attribute calles CONTINENT
desired_shapes = []
for s in shape_records:
    if s.record[0] == 'North America':
        desired_shapes.append(s.shape)
        #or do whatever you want with that element s.shape is the actual shape

如果您想查看描述shapefile 用法的this 页面。使用工具(如 QGis)来查看您的数据具有哪些属性很有用,这样您就可以继续以编程方式检测它们。

【讨论】:

  • 太好了,您还可以查看OSGEO 来处理 shapefile,这是我目前使用的,而不是 pyshp,因为它有更多选项和文档。
猜你喜欢
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
相关资源
最近更新 更多