TransWikia.com

Checking intersection of two shapefiles using ArcPy

Geographic Information Systems Asked by Muhammad Qasim on September 6, 2020

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")

2 Answers

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_memoryintersection')

And then use getMessages():

result.getMessages()
u'Executing: Intersect "ak_riks #;ak_riks_Dissolve #" in_memoryintersection ALL # INPUTnStart Time: Mon Aug 31 12:32:47 2020nReading 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

Correct answer by BERA on September 6, 2020

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")

Answered by david_p on September 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP