import arcpy, sys, traceback, os workspaceDir = "C:/tmp/Test_Species.gdb" arcpy.env.workspace = workspaceDir speciesFC = "C:/tmp/Test_Species.gdb/Bats_EqAr_Dis" try: #create unique ID for one species (make sure you have "ID" field): uCur = arcpy.UpdateCursor(speciesFC) idCount = 0 for uRow in uCur: idCount += 1 uRow.setValue("ID", idCount) uCur.updateRow(uRow) #check each species geometry relation # create matrices to store output spArrayInt = [ [ 0 for x in range(idCount) ] for y in range(idCount) ] spArrayIntArea = [ [ 0 for x in range(idCount) ] for y in range(idCount) ] #loops for first species for i in range(1, idCount+1): sCurI = arcpy.SearchCursor(speciesFC, '"ID" = ' + str(i)) for rowI in sCurI: geomI = rowI.Shape #first geometry #loops for second species for j in range(1, idCount+1): if i > j: sCurJ = arcpy.SearchCursor(speciesFC, '"ID" = ' + str(j)) for rowJ in sCurJ: geomJ = rowJ.Shape #second geometry #check if gemetries intersects if geomI.overlaps(geomJ): #if true, calculate intersection geometry (stored in memory) and get geometry area arcpy.Intersect_analysis([geomI,geomJ], "in_memory/Intersection") sCurInt = arcpy.SearchCursor("in_memory/Intersection") for rowInt in sCurInt: geomInt = rowInt.Shape geomIntArea = int((geomInt.area)/1000000) arcpy.Delete_management("in_memory/Intersection") #fill matrices: 1 - geometries intersect, area - intersection area in km2 spArrayInt[i-1][j-1] = 1 spArrayInt[j-1][i-1] = 1 spArrayIntArea[i-1][j-1] = geomIntArea spArrayIntArea[j-1][i-1] = geomIntArea #write output matrices to files outputFileInt = open("C:/tmp/Species_int.txt","w") for line in spArrayInt: lineToWrite = (", ").join(str(v) for v in line) outputFileInt.write(lineToWrite + "\n") outputFileInt.close() outputFileIntArea = open("C:/tmp/Species_int_area.txt","w") for line in spArrayIntArea: lineToWrite = (", ").join(str(v) for v in line) outputFileIntArea.write(lineToWrite + "\n") outputFileIntArea.close() except: print arcpy.GetMessages() # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a # message string pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value) # Return python error messages for use with a script tool arcpy.AddError(pymsg) # Print Python error messages for use in Python/PythonWin print pymsg del sCurI, sCurJ, sCurInt
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.