Verificando a interseção de dois shapefiles usando ArcPy

Aug 31 2020

Eu sou novo em programação Python.

Como eu verifico a interseção de dois shapefiles usando ArcPy?

Se os shapefiles se cruzam, ele deve retornar Trueelse False.

Abaixo está o código que escrevi:

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

Respostas

1 BERA Aug 31 2020 at 17:37

Você pode "capturar" o objeto de resultado retornado pelo Intersect.

Por exemplo:

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

E então 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

david_p Aug 31 2020 at 17:46

Você pode fazer isso contando os recursos de interseção. Se intersect retornar alguns recursos, o resultado será Trueoutro False. O código pode ser:

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