您可能想看看Shapely和Fiona。Fiona是gdal的包装器,可简化空间文件的导入和导出。匀称地提供几何功能。这是一个非常简单的示例,可以为您提供想法。它将多边形属性连接到该多边形内的所有点。
我使用的示例数据是这些多边形和这些点。
import fiona
from shapely.geometry import shape
from copy import deepcopy
with fiona.open("planning_neighborhoods.shp", "r") as n:
with fiona.open("Schools_Private_Pt.shp", "r") as s:
# create a schema for the attributes
outSchema = deepcopy(s.schema)
outSchema['properties'].update(n.schema['properties'])
with fiona.open ("Schools_withNbhd.shp", "w", s.driver, outSchema, s.crs) as output:
for school in s:
for neighborhood in n:
# check if point is in polygon and set attribute
if shape(school['geometry']).within(shape(neighborhood['geometry'])):
school['properties']['neighborho'] = neighborhood['properties']['neighborho']
# write out
output.write({
'properties': school['properties'],
'geometry': school['geometry']
})
Join attributes by location
来自fTools
plugin:的实际命令的源代码doSpatialJoin.py
,尤其是compute()
方法。从其中消除任何UI代码并将其剥离为简单的python函数应该不太困难。