Get features attribute from one shape to another

517
2
04-18-2012 06:21 AM
ManuHarchies
New Contributor
Dear all,

I have two shapefiles : a first one with big polygones and a second one with smaller polygones.
I would like to create a text field in the first shape that would concatenate the ID fields of all the small polygones of the second shape that are included (at least partially) in the polygone of the first shape.
Any idea how I could do that ?

Thanks !

Manu
0 Kudos
2 Replies
MarcinGasior
Occasional Contributor III
Try this code:
import arcpy
arcpy.env.overwriteOutput = True

bigPolygonLayer = r"C:\tmp\Test.gdb\BigPolygons"
smallPolygonLayer = r"C:\tmp\Test.gdb\SmallPolygons"
idField = "YourIDField"

#add new field to target feature class if not exists:
fieldList = arcpy.ListFields(bigPolygonLayer)
fieldNameList = []
for field in fieldList:
    fieldNameList.append(field.name)
if not idField in fieldNameList:
    arcpy.AddField_management(bigPolygonLayer, idField, "TEXT","","",1023) #you can adjust field length

#loop throug big polygons
bigPolyUC = arcpy.UpdateCursor(bigPolygonLayer)
for bigPolyRow in bigPolyUC:
    bigPolyGeom = bigPolyRow.Shape
    concatIDLst =[]

    #loop through all small polygons, take ID values if small polygon OVERLAPS the big ...
    #and add to concat string; (You can change relation to CONTAINS to restrict results)
    smallPolySC = arcpy.SearchCursor(smallPolygonLayer)
    for smallPolyRow in smallPolySC:
        smallPolyGeom = smallPolyRow.Shape
        if bigPolyGeom.overlaps(smallPolyGeom):
            concatIDLst.append(str(smallPolyRow.getValue(idField)))
            concatID = "".join(concatIDLst) #between "" you can add any ID separator: space, comma, etc.

    bigPolyRow.setValue(idField, concatID)
    bigPolyUC.updateRow(bigPolyRow)

del smallPolySC, smallPolyRow, bigPolyUC, bigPolyRow, bigPolyGeom


For more info how OVERLAPS work check Geometry class.
0 Kudos
NobbirAhmed
Esri Regular Contributor

I would like to create a text field in the first shape that would concatenate the ID fields of all the small polygones of the second shape that are included (at least partially) in the polygone of the first shape.


Use Spatial Join tool with INTERSECT option.

In the Field Mapping control of the Spatial Join add a new output field of type TEXT with the merge rule of Join.
0 Kudos