1

I am trying to automate a process using python and arcpy. I have a scenario where a user will open an mxd and edit a polygon layer drawing a new polygon. They will then want to use just this new polygon to undertake a select by location against every other layer (except the polygon itself obviously) in the table of contents.

I need help enabling my code to export one spreadsheet/csv of every layer that is within the search distance (the individual rows of data associated with each feature are not required, simply a yes or no). Below is the code I have so far.

#Imports the modules
import arcpy
#Sets the Map Document
mxd = arcpy.mapping.MapDocument("CURRENT")
#Sets the dataframe
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Overwrites the Output
arcpy.env.overwriteOutput = True
#Output location for the new polygon feature class that will be used for the select by location.
Search_Area = "U:\\Test.gdb\\Search_Area"
#Selects the polygon with the MAX ObjectID from the Convey feature class.
arcpy.Select_analysis(Convey_L, Search_Area, "\"OBJECTID\" = (SELECT MAX( \"OBJECTID\" ) FROM Convey)")
#Defines the layers as everything in the table of contents.
layers = arcpy.mapping.ListLayers(mxd)
#Loops through the layers in the MXD and select everything that is within 200m of the Relevant Area (Search Area)
for layer in layers:
    if layer.name != Convey_L:
        arcpy.SelectLayerByLocation_management(layer, "INTERSECT", Convey_L, "200 meters", "ADD_TO_SELECTION", "NOT_INVERT")
for layer in layers:
     desc = arcpy.Describe(layer)
     if desc.fidSet:
         print layer.name, "has {0} features selected:".format(len(desc.fidSet.split(';')))
         for row in arcpy.da.SearchCursor(layer, "*"):
             print row
for lyr in arcpy.mapping.ListLayers(mxd,"",df):
    arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
2
  • Can you indicate (by editing your question), where it is it that your script is going wrong or where you are stuck? It might be useful for you to look at the csv module that is part of the standard Python Library. You can import that and take advantage of that. docs.python.org/2/library/csv.html Jul 5, 2017 at 14:13
  • Try focusing your question on I or II first.
    – PolyGeo
    Jul 5, 2017 at 19:32

1 Answer 1

2

I'd suggest using the Near tool. One caveat is that the Near tool calculates distance in the units of the layers so you'll want to use projected layers.

#Imports the modules
import arcpy
#Sets the Map Document
mxd = arcpy.mapping.MapDocument("CURRENT")
#Sets the dataframe
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Overwrites the Output
arcpy.env.overwriteOutput = True
#open file for writing and write header row
my_file = open(r'U:\layers.csv', 'a')
my_file.write("Layer, Distance_from_Target \n")

#Output location for the new polygon feature class that will be used for the select by location.
Search_Area = "U:\\Test.gdb\\Search_Area"
Search_Area = "U:\\Test.gdb\\Search_Area"

#Selects the polygon with the MAX ObjectID from the Convey feature class and assigns it to target
target=arcpy.Select_analysis(Convey_L, Search_Area, "\"OBJECTID\" = (SELECT MAX( \"OBJECTID\" ) FROM Convey)")
#Defines the layers as everything in the table of contents.
layers = arcpy.mapping.ListLayers(mxd)
#Loops through the layers in the MXD, calculates distance to target and writes layer name and distance to file if closer than threshold
for layer in layers:
    if layer.name != Convey_L:
        arcpy.Near_analysis(target, layer, "200 meters")
        with arcpy.da.SearchCursor(target, ["NEAR_FID", "NEAR_DIST"]) as rows:
            for row in rows:
                if row[1] <= 200
                    my_file.write("%s, %f \n" % (layer, row[1]))

#close file
my_file.close()
for lyr in arcpy.mapping.ListLayers(mxd,"",df):
    arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")

Your Answer

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

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