I'm interested in updating a field in geodatabase feature classes based on their location in relation to another feature class in the geodatabase, but without having to perform a join (and therefore creating another feature class).
I've got a polygon feature class ("boundaries") and then multiple point and line features that fall within those boundaries ("fieldData," "streams," "origins"). All of the features, once loaded to the database, have a common field labeled "siteCode" that I would like to populate programmatically rather than manually doing a Select by Location and calculating the fields (or creating extra files like performing a join). Better yet, I would like this to occur as the point and line files are loaded to the geodatabase, but that's not necessary. None of the files have this information stored in their attributes prior to being loaded to the database, but the siteCode is the first two letters in the name of each source shapefile.
As it stands now, I select a feature in the "boundaries" feature class, Select by Location on each of the other files one at a time, and populate the fields using Calculate Field.
I am open to any suggestions for workflow improvement here! My experience is limited with Python but I am interested in learning. If people respond with Python suggestions I would just ask that they include as much information as possible! I feel like this must have been answered a million times but I'm seeing a lot about using a join and not many ways to avoid it.
Also: There is this posting that appears to be accomplishing something similar but I'm too much of a novice to understand exactly what it's doing and if it will do what I need. If someone can confirm that some variation on that code will work for me I will dive in and figure it out!
#Make a feature layer for the neighourhoods
arcpy.MakeFeatureLayer_management(neighbourhood, "neighbourhoodLayer")
arcpy.MakeFeatureLayer_management(parcel, "parcelLayerTest1")
#Setup cursor to update new NBHD field
with arcpy.da.UpdateCursor(parcel, (parcelIDField,nbhdField)) as parRows:
for par in parRows:
# Create a query string for the current patrol zone
#parcelIDstring = par[0]
#queryString = '"' + str(parcelIDField) + '" =' + "'" + str(parcelIDstring) + "'"
#Select parcels based on location contained within neighbourhood
arcpy.SelectLayerByLocation_management("parcelLayerTest1", "HAVE_THEIR_CENTER_IN", neighbourhood)
nbhdTable = arcpy.SearchCursor(neighbourhood)
for nbhd in nbhdTable:
nbhdValue = nbhd.getValue("Neighbourh")
if nbhdValue:
par[1] = nbhdValue
parRows.updateRow(par)
Thanks in advance for any assistance.
Answer
This answer will be similar to the one I gave here...
If I understand your question right, you have parcels (and maybe some other layers) and neighborhoods. You want to calculate a field in the Parcels layer (named SITE_CODE) based on what Neighborhood it's in... if so, try this code...
import arcpy
# Set overwrite option
arcpy.env.overwriteOutput = True
# KEEPING ORIGINAL LAYERS (NOT CREATING ADDITIONAL "JOINED" LAYER)
# Create FeatureLayers
arcpy.MakeFeatureLayer_management("C:/YourFolder/Parcels.shp", "lyr_Parcels")
arcpy.MakeFeatureLayer_management("C:/YourFolder/Neighborhoods.shp", "lyr_Neighborhoods")
# Add an "SIDE_CODE" field (remove this if the field already exists...)
arcpy.AddField_management("lyr_Parcels", "SITE_CODE", "TEXT", "", "", "20")
# Create a search cursor for the states
rows = arcpy.SearchCursor("lyr_Neighborhoods")
for row in rows:
# What you'll do is select each neighborhood one at a time, and then select all the parcels in that neighborhood and calculate the SITE_CODE field
# NOTE: If you are using not using shapefiles, then you'll have to change the FID in the line below to OBJECTID (or similar)
arcpy.SelectLayerByAttribute_management("lyr_Neighborhoods", "NEW_SELECTION", "\"FID\" = " + str(row.getValue("FID")))
arcpy.SelectLayerByLocation_management("lyr_Parcels", "INTERSECT", "lyr_Neighborhoods", "", "NEW_SELECTION")
arcpy.CalculateField_management("lyr_Parcels", "SITE_CODE", "'{0}'".format(str(row.getValue("Neighbourh"))), "PYTHON_9.3", "")
print "Finished processing " + str(row.getValue("Neighbourh"))
No comments:
Post a Comment