Python:仅使用shapefi的一部分

2024-06-01 10:16:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的问题是我下载的shapefile包含了所有大陆的形状。我在想,有没有办法我只能在北美大陆阅读我的shapefile?在

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

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

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

这是形状文件的link。在


Tags: 文件数据代码import地图绘制linksf
1条回答
网友
1楼 · 发布于 2024-06-01 10:16:23

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

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)来查看数据的属性是很有用的,这样您就可以继续以编程方式检测它们。在

相关问题 更多 >