2017-07-07 35 views
1

我使用Python 3.6创建了一个在北美显示降水数据的地图。我目前使用的代码在shapefile之内提取了我的数据,然后将其绘制在地图上。Python:仅使用shape文件的一部分

我的问题是,我下载的shapefile包含所有大洲的形状。我想知道在我的shapefile中只能在北美洲读到吗?

如果这不是一种可能性,那么是否有人知道我可以在哪里下载只有北美地区的shapefile?谢谢!

这是我的代码的一部分,它读取shapefile

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

这是link为shape文件。

+0

请添加更多细节以帮助您。你试过什么了?这个shapefile如何看起来,它有任何可能有帮助的属性(不要只是分享一个链接)? – DarkCygnus

+0

我已经尝试将大陆的名称添加到shapefile的末尾。例如sf = shapefile.Reader('continents','North America')和sf = shapefile.Reader('continents/North America')。这样做并没有改变,我仍然只能看到所有大陆的地图。我在我的问题中包含链接,以便人们可以下载我正在使用的shapefile,并阅读随附的txt文件,该文件显示所有属性,因为它太大而无法在此处复制和粘贴。 @GrayCygnus – CPG

+0

我下载了你的shapefile,我目前正在做一个aswer,很快发布 – DarkCygnus

回答

2

看看你提到的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 

如果你想看看this页描述的shapefile使用。使用工具(如QGis)查看数据的属性非常有用,因此您可以继续以编程方式检测它们。

+1

This works,thank you! – CPG

+1

太好了,你也可以检查[OSGEO](https://pcjericks.github.io/py-gdalogr-cookbook/)来处理shapefile,这是我目前使用的,而不是'pyshp',因为它有更多选项和文件。 – DarkCygnus