I have two feature classes one with points one with polygons (in ArcGis 10.2). Now, I want to write into a String Field of the polygon layer a list of all the Point-FIDs that are within each polygon (often more than one, which is why join doesn't help). I was hoping that there is a possibility using Field Calculator and somehow concatenate the FIDs. But I don't know how to access the other feature class within the calculation code - is that possible? Also a Python code would be appreciated or anything else...
@PolyGeo's solution will work, but here is how to do it with just Spatial Join (and Add/Calculate Field if you need the FID, which needs to be copied to a real field for this to work). This will almost certainly be faster than using nested cursors.
- If you need a FID from a shapefile, use Add Field followed by Calculate Field to first copy the FID field to a new user-defined field, because Spatial Join does not allow you to join FID because it is not a real field.
- In the Spatial Join tool, select the
ONE_TO_ONErelationship type (this still lets us concatenate all the spatially related records, just with one output record per target record).
- In the Field Map, for the field(s) that you want to get a concatenated list of values, right-click the output field and click Properties.
- Select 'Join' for the merge rule and enter the desired delimiter, e.g. a comma.
Beware that if there are many values being concatenated this can very easily go over field length limits, resulting in an error.
Use the Spatial Join tool with Match Option set to CONTAINS, instead of the Field Calculator.
Spatial Join (Analysis)
Joins attributes from one feature to another based on the spatial relationship. The target features and the joined attributes from the join features are written to the output feature class.
This Python script should do what you are after once you have substituted your feature class and field names into it.
import arcpy arcpy.MakeFeatureLayer_management(r"C:\temp\test.gdb\PolyFC", "PolyLayer") arcpy.MakeFeatureLayer_management(r"C:\temp\test.gdb\PointFC", "PointLayer") rows = arcpy.UpdateCursor("PolyLayer") # iterate through each polygon in PolyLayer for row in rows: polygon = row.SHAPE # intersect polygon geometry with points to get list of point OBJECTIDs arcpy.SelectLayerByLocation_management("PointLayer","INTERSECT",polygon) # use search cursor to read selected points selectedPoints = arcpy.SearchCursor("PointLayer") objectidList = "" for selectedPoint in selectedPoints: objectidList = objectidList + "," + str(selectedPoint.OBJECTID) # write OBJECTID list to the polygon attribute # called (PointIDs which is text field added) # lstrip() used to get rid of leading comma row.PointIDs = objectidList.lstrip(",") rows.updateRow(row) del row,rows
To do the same thing using Data Access (arcpy.da) cursors, which were new at 10.1 and thus available to you, is slightly more complicated and probably gives little performance gain in this instance so I stayed with this code which will also work at 10.0.