我想学习如何使用Python中的国家和居住区的数据集从使用OGR http://www.naturalearthdata.com/downloads/50m-cultural-vectors/。我试图使用过滤器和缓冲区在命名国家/地区的指定缓冲区(从ne_50m_admin_0_countries.shp中的要素类ADMIN过滤)中查找点(ne_50m_populated_places.shp)。问题似乎是我不了解用于buffer()的单位。在脚本中,我只是使用任意值10来测试脚本是否有效。该脚本会运行,但会从加勒比海地区返回命名国家为“安哥拉”的人口稠密的地方。理想情况下,我希望能够指定一个缓冲距离,例如500km,但是由于我的理解是buffer()使用的是country.shp单位(将采用wgs84纬度/经度格式),因此无法解决该问题。对此方法的建议将不胜感激。
# import modules
import ogr, os, sys
## data source
os.chdir('C:/data/naturalearth/50m_cultural')
# get the shapefile driver
driver = ogr.GetDriverByName('ESRI Shapefile')
# open ne_50m_admin_0_countries.shp and get the layer
admin = driver.Open('ne_50m_admin_0_countries.shp')
if admin is None:
print 'Could not open ne_50m_admin_0_countries.shp'
sys.exit(1)
adminLayer = admin.GetLayer()
# open ne_50m_populated_places.shp and get the layer
pop = driver.Open('ne_50m_populated_places.shp')
if pop is None:
print 'could not open ne_50m_populated_places.shp'
sys.exit(1)
popLayer = pop.GetLayer()
# use an attribute filter to restrict ne_50m_admin_0_countries.shp to "Angola"
adminLayer.SetAttributeFilter("ADMIN = ANGOLA")
# get the Angola geometry and buffer it by 10 units
adminFeature = adminLayer.GetFeature(0)
adminGeom = adminFeature.GetGeometryRef()
bufferGeom = adminGeom.Buffer(10)
# use bufferGeom as a spatial filter on ne_50m_populated_places.shp to get all places
# within 10 units of Angola
popLayer.SetSpatialFilter(bufferGeom)
# loop through the remaining features in ne_50m_populated_places.shp and print their
# id values
popFeature = popLayer.GetNextFeature()
while popFeature:
print popFeature.GetField('NAME')
popFeature.Destroy()
popFeature = popLayer.GetNextFeature()
# close the shapefiles
admin.Destroy()
pop.Destroy()