2

I have an address point layer and would like to spatially query this layer using various polygon shapefiles to intersect this information so that each address contains the attributes from these polygon layers.

Is this possible using non ESRI Python? I have used ArcPy but don't know if this kind of thing is possible with open source Python options? If so is the Python script fairly simple? Possible examples?

1

there are a few possiblities for non-ESRI vector-based geoprocessing - i see fiona and shapely mentioned a lot, though i don't have experience with either. I tend to simply use OGR.

a simple implementation of intersecting a point feature with a polygon feature might be something like:

from osgeo import ogr

drv=ogr.GetDriverByName('ESRI Shapefile')
ds=drv.Open('yourpointshapefile.shp',1) #1 open point shapefile for editing
ptlyr=ds.GetLayer(0)

#open polygon shapefile
drv2=ogr.GetDriverByName('ESRI Shapefile') #actually unsure if you have to use 2 drivers, but better safe
ds2=drv2.Open('yourpolyshapefile.shp')
polylyr=ds2.GetLayer(0)

polyfeatdef=ogr.Feature(polylyr.GetLayerDefn())
feat1=polylyr.GetNextFeature()
for i in range(feat1.GetFieldCount()):
    fldDef=feat1.GetFieldDefnRef(i)
    ptlyr.CreateField(fldDef) #add polygon fields to point shapefile

polylyr.ResetReading() #start from beginning of polygons
for polyfeat in polylyr:
    polyGeom=polyfeat.GetGeometryRef()
    for ptfeat in ptlyr:
        ptGeom=ptfeat.GetGeometryRef()
        if polyGeom.Contains(ptGeom):
            print ptfeat.GetFID()
            for i in range(polyfeat.GetFieldCount()):
                ptfeat.SetField(polyfeat.GetFieldDefnRef(i).name,polyfeat.GetField(i))
                ptlyr.SetFeature(ptfeat)
        ptfeat=None
    polyfeat=None

if there are many points and polygons, the process may be slow - you can also add a SpatialFilter to help speed things up which basically looks for all points with the extent of the polygon of interest

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.