2

I am new to Python programming.

How do I check the intersection of two shapefiles using ArcPy?

If the shapefiles intersect, it should return True else False.

Below is the code that I have written :

import arcpy

arcpy.env.workspace = "D:/Qasim/mn_corcoran"

arcpy.Intersect_analysis(r'D:/Qasim/mn_hopkins/abc.shp #;D:/Qasim/mn_corcoran/xyz.shp #', r'D:/Qasim/mn_hopkins/asd.shp', 'ALL', '#', 'INPUT')

if arcpy.Intersect_analysis == True:
  
 print("True")

else: arcpy.Intersect_analysis == False

print("False")
1
  • ArcGIS is geared toward comparing features not feature classes. You should review documentation associated with the spatial selection tools, and selection set management.
    – Vince
    Aug 31, 2020 at 12:08

2 Answers 2

1

You can "catch" the result object returned by Intersect.

For example:

import arcpy
arcpy.env.workspace = r'C:\shapefile_folder'
result = arcpy.Intersect_analysis(["ak_riks.shp","ak_riks_Dissolve.shp"],r'in_memory\intersection')

And then use getMessages():

result.getMessages()
u'Executing: Intersect "ak_riks #;ak_riks_Dissolve #" in_memory\\intersection ALL # INPUT\nStart Time: Mon Aug 31 12:32:47 2020\nReading Features...\nCracking Features...\nAssembling Features...\nWARNING 000117: Warning empty output generated.\nSucceeded at Mon Aug 31 12:32:47 2020 (Elapsed Time: 0,14 seconds)'

'empty output' not in result.getMessages()
False

enter image description here

2
  • 1
    Using Intersect_analysis to test for dataset overlap is like crashing your car into a shopping mall to see if the Pier 1 Imports is still open. Instead of creating a new dataset to be discarded, the OP's stated goal could be achieved with an envelope test.
    – Vince
    Aug 31, 2020 at 12:28
  • 1
    I know. It does not matter if the analysis take 2 min or 30 s unless you have to do the comparison often
    – BERA
    Aug 31, 2020 at 12:29
0

You can do it by counting intersect features. If intersect returns some features, then result is True, else False. The code can be:

import arcpy

arcpy.env.workspace = "D:/Qasim/mn_corcoran"

intersect_layer = arcpy.Intersect_analysis(r'D:/Qasim/mn_hopkins/abc.shp #;D:/Qasim/mn_corcoran/xyz.shp #',
                         r'D:/Qasim/mn_hopkins/asd.shp', 'ALL', '#', 'INPUT')

if int(arcpy.GetCount_management(intersect_layer)[0]) > 0:
    print("True")
else:
    print("False")
7
  • This could be a heinously expensive way to know that a contour feature class overlays with a soils feature class. I know the OP used the Intersect_analysis tool, but it's the wrong tool for the task, so answers should address the root problem, which could be as simple as testing envelopes before attempting a spatial selection.
    – Vince
    Aug 31, 2020 at 12:16
  • @Vince Then please elaborate , what should be done in order to check intersection of two shapefiles using ArcPy? if you have a sample code discussing this type of problem , share it here then. Aug 31, 2020 at 12:27
  • @david_p It gives FALSE in return in every condition even if there are intersected features. It should give TRUE also when there are intersected features between two shapefiles. Aug 31, 2020 at 12:38
  • 1
    @muhammad It depends on "intersect_layer". If its empty, it should return False. If it contains some features (even only one), it should return True. Try to inspect this layer if it is empty or not.
    – david_p
    Aug 31, 2020 at 13:17
  • @david_p The layer has some features , still FALSE is returning everytime. Have you tried this method with any shapefiles??? Sep 1, 2020 at 4:08

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.