Monday 22 April 2019

arcpy - How to write FIDs of points within each polygon to polygon field?


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...



Answer



@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.



  1. 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.

  2. In the Spatial Join tool, select the ONE_TO_ONE relationship type (this still lets us concatenate all the spatially related records, just with one output record per target record).

  3. 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.

  4. 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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...