0

I'm interested in updating a field in N-geodatabase feature classes based on their location to another feature class in the geodatabase, but without having to perform join.

I am new to Python, and have tried to use the similar answer to my question in : Assigning field value based on location without join

I have a feature layer "zone - M" and a feature layer "Zone - Z", these features have a field called "name_M" and "name_Z". I will draw several others layers "on" these zones. These others layers have all a field "name_zone_M" and "name_zone_Z". I would like that if the layer is contained completely inside a zone, the value "name" of each zone get assigned to the field "name_zone_M" and "name_zone_Z". These feature class are all polygons.

I have tried the following code to try to get the first one right, and then to try to loop inside the others layers, but I did not succeed.

Any toughts?

import arcpy

# Set overwrite option
arcpy.env.overwriteOutput = True

# KEEPING ORIGINAL LAYERS (NOT CREATING ADDITIONAL "JOINED" LAYER)
# Create FeatureLayers
arcpy.MakeFeatureLayer_management(r"Zone-M")
arcpy.MakeFeatureLayer_management(r"DRAW1")

# Create a search cursor for the states
rows = arcpy.SearchCursor(r"Zone-M")
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(r"Zone-M", "NEW_SELECTION", "\"FID\" = " + str(row.getValue("FID")))
    arcpy.SelectLayerByLocation_management(r"DRAW1", "COMPLETELY_CONTAINS", r"Zone-M", "", "NEW_SELECTION")
    arcpy.CalculateField_management(r"DRAW1", "name_zone_M", "'{0}'".format(str(row.getValue("name_M"))), "PYTHON_9.3", "")
    print "Finished processing " + str(row.getValue("name_M")) 
3
  • Please explain what you mean by, "did not succeed". Post full text of any errors. Or if nothing at all happened, post that. If something unexpected happened, what was it? Feb 18, 2020 at 22:24
  • Why do you wish to avoid a join? This is what joins are for. Feb 18, 2020 at 22:25
  • Thanks for your repply. Nothing happened. I am trying to avoid join because I got the "join" function to work, but I couldn't get the "Spatial Relationship If, else" to work... Also, I have a lot of different feature classes, it seemed that it would be a lot of work to join each single one.
    – CAM
    Feb 19, 2020 at 9:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.