I have a set of polygons and I want to add a field with the name of the streets (from another feature class of streets) that are within each polygon. Using Select Features by Location, I select streets that are within the polygons, but I can't think of a way (using python) to automate the process of populating each polygon with the name of the street that falls within it. I don't have a matching field for either feature class to base a join on. Is this possible or is it something that would have to be done manually?
Try something to do with this, it is the idea, but it mostly untested, but i am sure it good and it's should work: This "your_streets" and "your_polygons" are your feature classe.
for street in cursor:
name_of_street=street.Name ID_line=street.OBJECTID if arcpy.Exists(street_layer): arcpy.Delete_management(street_layer) arcpy.MakeFeatureLayer_management(your_streets,street_layer) arcpy.SelectLayerByAttribute_management(street_layer, "NEW_SELECTION", "\"OBJECTID\" = "+str(ID_line)) if arcpy.Exists(polygons_layer): arcpy.Delete_management(polygons_layer) arcpy.MakeFeatureLayer_management(your_polygons,polygons_layer) arcpy.SelectLayerByLocation_management(polygons_layer,"WITHIN",street_layer) cursor_1=arcpy.UpdateCursor(polygons_layer) for polygon in cursor_1: polygon.Name_street=name_of_street
The analysis that you want to do, might be a bit strange, as you may have lots of streets passing trough your polygons, and it might be complicated put all that in a single field, but I do not know your dataset, so it might be perfectly reasonable.
A quite easy way would be to use the following algorithm that you can process in a Python Script:
- Use Spatial join using Polygons as "Target features" and street as "Join Features", set the merge rule for streets field name to join (use ", " as separator) and intersects in the "matching";
- Make Feature Layer from your Polygons
- Add Join between Polygons Layer and the output of the Spatial Join using the original FID
- Add field Streets_Name (text 255)
- Calculate Field Streets_Name from the joined table Join_Street_Name Field
- Remove Join
- Delete (Management) Spatial Join Output
Remember that if the join of the street names from any polygon is bigger than 155 character you will have problems...