Python script to find out if two layers are overlapping

652
13
01-07-2020 10:57 AM
GarthGroshans
New Contributor

As part of QAQC, I have to routinely check if all vector line data is buffered on the buffered layer. The layers are: vector line layer, buffer layer, and grid number layer. As of right now, I scan the entire county boundary to see if there is any vector line data that is not buffered on the buffer layer. If it is not buffered, I need to find out which grid number the problem resides. 

I would like to make a python script that would check to see if all vector line data (layer 1) is overlaying the buffered layer (layer 2) and if not, which grid number (layer 3) is the problem. 

Does anyone have any experience trying to write a python script that can tell if one layer is spatially overlaying another layer?

Code examples would be much appreciated. 

0 Kudos
13 Replies
JoeBorgione
MVP Esteemed Contributor

I haven't done it specifically, but a spatial selection or a series of them ought to do the trick

That should just about do it....
XanderBakker
Esri Esteemed Contributor

Hi ggroshans_GreenvilleWater ,

Can you provide a screenshot showing the lines, buffers and grids or provide a sample of your data? There are a couple of things that might complicate this and I would like to see some specific situations to see what would be possible.

0 Kudos
GarthGroshans
New Contributor

Hi Xander, 


Thank you for your help. I have attached three screenshots. 1) A view of the vector line data, the buffer layer, and an example of how the vector line data may not correspond to the buffer layer, 2) A screenshot of my table of contents, and 3) A zoomed out view to show the grid layer (e.g., screenshot 1 is within grid 38).

A view of the vector line data, the buffer layer, and an example of how the vector line data may not correspond to the buffer layer

A screenshot of my table of contents

A zoomed out view to show the grid layer (e.g., screenshot 1 is within grid 38).

0 Kudos
AndrewLaws2
New Contributor

What's the field name used in mapnum_copy for labelling?

0 Kudos
GarthGroshans
New Contributor

Thanks for all your help so far.  The field name used in mapnum_copy for labeling is called MapNum

0 Kudos
AndrewLaws2
New Contributor
import arcpy

arcpy.MakeFeatureLayer_management('water_main_copy', 'water_main_copy_lyr')

arcpy.SelectLayerByLocation_management('water_main_copy_lyr', "WITHIN", 'water_main_buffer')

arcpy.SelectLayerByLocation_management('water_main_copy', "", "", "", "SWITCH_SELECTION")

arcpy.SelectLayerByLocation_management('mapnum_copy', "INTERSECT", 'water_main_copy')

fc = 'mapnum_copy'
field = 'MapNum'
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    print(row.getValue(field))

This would be a way to make a feature layer. Give it a try and if it gives you errors just follow the similar naming convention for feature layers for water_main_buffer and mapnum_copy.

The text that comes will be the Map Numbers with unbuffered water mains.

Hope this helps!

0 Kudos
AndrewLaws2
New Contributor

Hi, I use a similar script from the Python window in ArcMap

##running from Python window in ArcMap with layers already MXD and using layer names as per your post

import arcpy

arcpy.SelectLayerByLocation_management('layer1', "WITHIN", 'layer2')

arcpy.SelectLayerByLocation_management('layer1', "", "", "", "SWITCH_SELECTION")

arcpy.SelectLayerByLocation_management('layer3', "INTERSECT", 'layer1')

fc = 'layer3'
field = 'layer3_field_that_contains_grid_name'
cursor = arcpy.SearchCursor(fc)
for row in cursor:
     print(row.getValue(field))

0 Kudos
AndrewLaws2
New Contributor

What's the name of the labeling field in mapnum_copy?

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Using the syntax highlighter makes code reading much easier:

##running from Python window in ArcMap with layers already MXD and using layer names as per your post

import arcpy

arcpy.SelectLayerByLocation_management('layer1', "WITHIN", 'layer2')

arcpy.SelectLayerByLocation_management('layer1', "", "", "", "SWITCH_SELECTION")

arcpy.SelectLayerByLocation_management('layer3', "INTERSECT", 'layer1')

fc = 'layer3'
field = 'layer3_field_that_contains_grid_name'
cursor = arcpy.SearchCursor(fc)
for row in cursor:
That should just about do it....
0 Kudos