Pi-Hellz

Looping Intersection Analysis

Discussion created by Pi-Hellz on Jan 7, 2013
Hello,

I'm trying to create a loop that will allow me to intersect 50 files with each other.

My first input is a polygon layer resulting from coverage analysis (Analysis Network Layer) containing all the fire station

My second input is a layer polygon layer resulting coverage analysis (Analysis Network Layer) containing all the fire station - 1

From the result of the intersection, I compare two columns together from a condition and remove the selection will be my input to the next intersection operation.

Until there everything goes well, the problem is that I want to create a loop to automate the process. I find it difficult to figure out how to use a layer resulting and input at the sametime to do an intersection analysis.


import arcpy
from arcpy import env
env.overwriteOutput = True

env.workspace = "S:\Travail\Zone_Support_mutuel_test\Test_temp\ServiceAreaV2"

j = 1
outIntersectGlobal = "in_memory" + "\\" + "IntersectGlobal"
outIntersect = outputPolygons + "\\" + "Intersect"
IntersectResult = "in_memory" + "\\" + "Intersect" + j

fcGlobal = arcpy.ListFeatureClasses("Global*","Polygons")
fclist = arcpy.ListFeatureClasses("C*","Polygons")
print fcGlobal[0]
print fclist[1]

#for fc in arcpy.ListFeatureClasses("C*","Polygons"):
    #print fcGlobal
    #print fc.Nom
   
# Intersect Network analysis polygon( All fire Station coverage) with (All fire station minus 1)
arcpy.Intersect_analysis([fcGlobal,fclist[0]],"outIntersectGlobal")

codeblock = "def isSame(x, y):\n\n    fieldA = str(x)\n\n    fieldB = str(y)\n\n    if fieldA == fieldB:\n\n        return 1\n\n    else:\n\n        return 0\n"
isSameQuery = "isSame" = '1'

arcpy.AddField_management(outIntersectGlobal,"isSame","SHORT", "5", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(outIntersectGlobal,"isSame","isSame( !Name!, !Name_1! )","Python_9.3",codeblock)
#arcpy.CopyFeatures_management(outIntersectGlobal, outputPolygons + "\\" + "Intersect")
arcpy.MakeFeatureLayer_management(outIntersectGlobal,"Intersect1",isSameQuery)

j+= 1

# Second intersection 
arcpy.Intersect_analysis([outIntersectGlobal,fclist[1]],IntersectResult)
arcpy.AddField_management(IntersectResult,"isSame","SHORT", "5", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(IntersectResult,"isSame","isSame( !Name!, !Name_1! )","Python_9.3",codeblock)
arcpy.MakeFeatureLayer_management(outIntersectGlobal,IntersectResult,isSameQuery)

j+= 1

#########################
### Blocked with the ouput of the intersect result and still incrementing it to have it as input for the next loop ###

for i in range(fclist):
    print i
    arcpy.Intersect_analysis(["IntersectResult","i"],
    arcpy.AddField_management(IntersectResult,"isSame","SHORT", "5", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.CalculateField_management(IntersectResult,"isSame","isSame( !Name!, !Name_1! )","Python_9.3",codeblock)
    arcpy.MakeFeatureLayer_management(IntersectResult,"IntersectResult" + j,isSameQuery)

j+= 1


Thanks you for your help

Outcomes