This is kind of from memory as I cannot access my specific code… I am using 10.3.1 and this will be run inside ArcMap.

Overall goal: Find which layers in my active dataframe intersect a specific layer in the dataframe, then for each unique layer name that intersects the specific layer, output a line of text to a text file (don't need help with that part). I need to do this 5 times, each with a different intersecting layer. The dataframe will have on average 20 layers but in very rare circumstances could have up to 90 or so layers. The list of possible layers present will almost always be the same, however if the layer is empty it will not be present in the dataframe (for context in case that helps).

So, for the SelectLayerByLocation part for the first run of the tool - I think this code will accomplish that (I’ve already imported arcpy and set the workspace etc, my intersect layers are defined elsewhere in the code but I showed it again here so as not to confuse)

mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.ActiveDataFrame
intersectlayer = “Layer 1” 
layerlist = arcpy.mapping.ListLayers(df)
for layer in layerlist:
  if layer.name != intersectlayer    
    arcpy.SelectLayerByLocation_management(layer, "INTERSECT", intersectlayer)

(Can I add more layer names to the “if layer.name != intersectlayer” line to weed out some returns? Or will that not matter too much in terms of efficiency?)

I am stuck on how to take the results of this query, remove the duplicate layer names (because I’m sure intersectlayer will overlap another layer multiple times) or conversely keep only unique layer names, and then pass the unique layer names with no duplicates to the part of the code where the output text is written…. Is it possible to get the results of the query, and if a result is also in the layerlist then print the message? Not sure how to accomplish that if it's possible...

A suggestion was to getcount but that result will always be > 0 because intersectlayer would not exist if it didn't intersect anything (this is covered in an earlier part of the script that I didn't show here).

For additional context: if intersectlayer intersects Marsh once or multiple times, I’ll print one message “Marsh is wet” and so forth with nested If statements. I've written this code separately and it works as expected as far as I can tell - I am not requesting help with this part of the task (yet), just providing it for context.

  • Sounds like you want to run a get count tool on the layer, if the intersectlayer intersected then layer will have a selection count greater than zero. – Hornbydd Mar 17 '19 at 15:58
  • @Hornbydd there will always be a selection - the 'intersectlayer' won't be created in the earlier part of the script (not shown here) if it is not needed. Hope that makes sense. Will edit question to add this context. Thanks :) – DavidA Mar 17 '19 at 16:21
  • 1
    This is a workflow to find if a layer has a selection.. if not layer.isBroken: (First be sure that the layer points to *something) if layer.isFeatureLayer: (make sure the layer is a feature layer and not a raster or group layer etc.) D = arcpy.Describe(layer) followed by if len(D.FIDSet) > 0: to test if the layer has a selection, testing validity and isFeatureLayer should precede select by location, describe should follow the intersection. I think that's what you're after. – Michael Stimson Mar 18 '19 at 0:46

Your Answer

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

Browse other questions tagged or ask your own question.