im trying to write a script that allows me to iterate through my map layers, and for each layer, iterate over each feature and add a field that gives the location of the feature using my index layer as reference. In other words, I want to know where every single feature is in relation to the index layer. The index layer is comprised of many polygons called "zones". So every feature should have a field that tells me which zone it is in. I need some help with the expression this is what I have so far :

            import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Users\Daimon Nurse\Desktop\Zonetrial.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Main Map")[0]
lyrList = arcpy.mapping.ListLayers(mxd,"",df)
fieldname = "Zone"
type = "TEXT"
inlayer = "layeritem2"
inlayer1 = "Zoneitem"

#for layeritem in lyrList:
    #if layeritem.name == "Zone of Interest":
        #Zones = set([r[0] for r in arcpy.da.SearchCursor(layeritem, "ZoneName")])
       #print Zones
for layeritem in lyrList:
    if layeritem.name == "Zone of Interest":
        with arcpy.da.SearchCursor(layeritem, "ZoneName") as cursor:  
            for Zone in cursor:
                print Zone
                print Zone
                for layeritem in lyrList:
                    if layeritem.name == "AllZones_Topology":
                        print "Nothing will happen to " + layeritem.name
                    elif layeritem.name == "UWI Campus Boundary":
                        print "Nothing will happen to " + layeritem.name
                    elif layeritem.name == "Zone of Interest":
                       print "Nothing will happen to " + layeritem.name
                    elif layeritem.name == "polytrial selection 5":
                        print "Nothing will happen to " + layeritem.name 
                        print layeritem
                        print Zone
                        arcpy.AddField_management (layeritem,fieldname,type)

print "Script Finished!"
  • Have you made any attempts to do this using Python? Ii so, can you post what you've tried already so we can get an idea of where you need help?
    – MrBubbles
    Jan 25 '15 at 15:12

Here's what you need to do, with comments:

    # Reference the MXD containing your layers. 
# If you're doing this in the Python Window of the MXD, 
# use the "CURRENT" keyword instead of providing a path 
# to the MXD you already have open.
mxd = arcpy.mapping.MapDocument(r"C:\Users\Daimon Nurse\Desktop\Zonetrial.mxd")
# mxd = arcpy.mapping.MapDocument("CURRENT")
# Reference the first dataframe in the MXD
df = arcpy.mapping.ListDataFrames(mxd)[0]
# Create a Layer Object from the Layer in your MXD containing your Zone features
ZoneIndexLayer = arcpy.mapping.ListLayers(mxd, "Zone of Interest", df)[0]
# Create a list of all of the unique zone values in the 'ZoneIndexLayer'
Zones = set([r[0] for r in arcpy.da.SearchCursor(ZoneIndexLayer, "ZoneName")])
print "Found " + str(len(Zones)) + " unique zones"
# Get a list of Layer objects of all the other layers in the dataframe of the mxd
Layers = arcpy.mapping.ListLayers(mxd, "", df)

# Iterate through each of the Layers
for Layer in Layers:
    # If the current Layer's name is the 'ZoneIndexLayer' or one of the other layers you do not wish to process:
    if Layer.name == ZoneIndexLayer.name or Layer.name == "UWI Campus Boundary" or Layer.name == "Zone of Interest" or Layer.name == "polytrial selection 5" or Layer.name == "AllZones_Topology":
        # Skip the Layer
    # Otherwise
        print Layer
        # Add a field named "Zone" to the layer
        arcpy.AddField_management(Layer, "Zone", "TEXT")
        # for each Zone in the list of Zones
        for Zone in Zones:
            print "Selecting Zones " + str(Zone)
            # Select the features in the 'ZonesIndexLayer' which are of the current Zone
            arcpy.SelectLayerByAttribute_management(ZoneIndexLayer, "NEW_SELECTION", '"ZoneName" = ' + "'" + Zone + "'")
            # Select the features in the current Layer which intersect the currently selected Zone features in the ZoneIndexLayer
            print "Selecting features in Layer '" + Layer.name + "' which intersect Zone " + str(Zone) + "..."
            arcpy.SelectLayerByLocation_management(Layer, "INTERSECT", ZoneIndexLayer)
           # Calculate the 'Zone' field of the current layer to that of the currently selected Zone
            print "Calculating Zone field..."
            arcpy.CalculateField_management(Layer, "Zone", Zone, "PYTHON_9.3")

print "Script Finished!"
  • I haven't understood everything you have, I edited my original question to include the code I have written so far Jan 25 '15 at 16:25
  • My understanding is you have two layers. One which serves as your index layer containing the zones which want to intersect against, and another which contains the features which you want to calculate the zone which each feature falls inside. Is that correct?
    – MrBubbles
    Jan 25 '15 at 16:38
  • yes, that is the idea Jan 25 '15 at 16:41
  • i think I have most of it, but I not sure what my expression should be in the "calculateField_management". I edited my original post to include what code I have so far. Jan 25 '15 at 17:07
  • arcpy.CalculateField_management(inlayer,fieldname, Zone)
    – MrBubbles
    Jan 25 '15 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.