Select to view content in your preferred language

Batch processing animal home range overlap data

1219
2
04-23-2013 10:48 PM
daviddellatore
Deactivated User
Greetings,

Am new to python, but even with rudimentary knowledge am already finding it incredibly useful.

Have put together the following script to calculate home range polygon overlap, at this point just between two user-set individuals.There's a lot more am planning on workout on, with for example the log/output system being wonky at the moment, but what I'd really like to do is set this up to iterate through various polygon shapefiles in a folder.

So if anyone could help me set it up so that ou1 (orangutan!) remains static, but then have the script iterate the ou2 variable across a set of other individuals (from polygon shapefiles all in one location), I'd really appreciate it. 

It'd be fantastic to have all of the results from ou1 output onto a single table, showing the overlap he/she has with each other ou2 - but at this point I'd be ecstatic with just having the loop figured out so that it outputs each pairing to its own table, and I can compile them manually later.

#script to calculate home range overlap (hectares) between two individuals

# import modules
import arcpy
import time
import os
from arcpy import env

env.overwriteOutput = True


# local variables
ou1 = arcpy.GetParameterAsText(0)
ou2 = arcpy.GetParameterAsText(1)
int_output = arcpy.GetParameterAsText(2)
int_output_Statistics = arcpy.GetParameterAsText(3)
logfile = arcpy.GetParameterAsText(4)


try:
    f = open(logfile, "a")
    f.write(time.strftime('%c'))
    f.write("\n")
    f.write("------------------------------")
    f.write("\n")

    # process: intersect
    #intersect_analysis (in_features, out_feature_class, {join_attributes}, {cluster_tolerance}, {output_type})
    arcpy.analysis.Intersect([ou1, ou2], int_output, "ALL", "", "INPUT")
    f.write("\n" + "Calculating home range overlap between " + ou1 + " & " + ou2)
    print "Calculating home range overlap between " + ou1 + " & " + ou2

    # process: add field
    arcpy.management.AddField(int_output, "intersect", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    f.write("\n" + "Adding \"Intersect\" field to " + int_output)
    print "Adding \"intersect\" field to " + int_output

    # process: calculate field
    arcpy.management.CalculateField(int_output, "intersect", "!shape.area@HECTARES!", "PYTHON_9.3", "")
    f.write("\n" + "Calculating geometry of overlap between " + ou1 + " " + ou2)
    print "Calculating geometry of overlap between " + ou1 + " " + ou2

    # process: delete identical
    arcpy.management.DeleteIdentical(int_output, "intersect", "", "0")
    f.write("\n" + "Deleting identical values from \"Intersect\" field")
    print "Deleting identical values from \"Intersect\" field"

    # process: summary statistics
    arcpy.analysis.Statistics(int_output, int_output_Statistics, "intersect SUM", "")
    f.write("\n" + "Determine overlap value in hectares")
    print "Determine overlap value in hectares"
    f.write("\n" + int_output_Statistics)
    f.write("\n" + "------------------------------")

    f.close



except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message


Thanks very much for your time,

David
Tags (2)
0 Kudos
2 Replies
JimCousins
MVP Alum
David,
I like to create functions, and then use them inside of models. You could do this and let the model take care of the iterations by setting the input for ou2 as a parameter, and setting its properties to "A list of values". Set an input parameter for ou1, drop the home range polygons of interest into the "ou2" parameter, and execute. It is efficient and flexible. I have a simple script to set the symbology of a raster dataset from a layer file, and use it this way often. Image of model attached.
Regards,
Jim
0 Kudos
daviddellatore
Deactivated User
Thanks very much for the response.

However, I have managed to put together a loop in the script that will cycle through each shapefile and compare them all individually with one animal:

#script to calculate home range overlap (hectares) between two individuals

# import modules
import arcpy
import time
import os
#import tabletoexcel
arcpy.ImportToolbox("xxx\Additional Conversion - Generic Tools.tbx")

from arcpy import env

env.overwriteOutput = True


# local variables

#hardcode test
ou1 = r"xxx\bbmmAnsPoly90.shp"
arcpy.env.workspace = r"xxx\ninety"
outputfolder = r"xxx\ninety\test"
#logfile = r"xxx\ansoverlap.txt"
#ou1 = arcpy.GetParameterAsText(0)
#arcpy.env.workspace = arcpy.GetParameterAsText(1)
#outputfolder = arcpy.GetParameterAsText(2)
#int_output_Statistics = arcpy.GetParameterAsText(3)
logfile = outputfolder + "\\" + ou1 + "_" + "overlaps.txt"
print logfile

try:
    # process: intersect
    #intersect_analysis (in_features, out_feature_class, {join_attributes}, {cluster_tolerance}, {output_type})
    ou2s = arcpy.ListFeatureClasses()
    
    for ou2 in ou2s:  
        fcname1 = arcpy.Describe(ou1).basename
        fcname2 = arcpy.Describe(ou2).basename
        int_output = os.path.join(outputfolder, str(fcname1 + fcname2) + ".shp")
        #int_output = os.path.join(outputfolder, str(fcname1) + "_overlaps.shp")
        int_output_Statistics = r"xxx\Default.gdb" + "\\" + str(fcname1 + fcname2)
        #int_output_Statistics = r"xxx\Default.gdb" + "\\" + str(fcname1) + "_overlaps"
        excel_output = outputfolder + "\\" + str(fcname1 + fcname2)  
        #excel_output = outputfolder + "\\" + str(fcname1) + "_overlaps"

        logfile = outputfolder + "\\" + fcname1 + "_" + "overlaps.txt"
        if arcpy.Exists(logfile):
            f = open(logfile, "a")
        else:
            f = open(logfile, "w")    

        f.write(time.strftime('%c'))
        f.write("\n")
        f.write("------------------------------")
        f.write("\n")

        arcpy.analysis.Intersect([ou1, ou2], int_output)
        f.write("\n" + "Calculating home range overlap between " + fcname1 + " & " + fcname2)
        print "Calculating home range overlap between " + fcname1 + " & " + fcname2

        # process: add field
        arcpy.management.AddField(int_output, "Overlap", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        f.write("\n" + "Adding \"Overlap\" field to " + int_output)
        print "Adding \"Overlap\" field to " + int_output

        # process: calculate field
        arcpy.management.CalculateField(int_output, "Overlap", "!shape.area@HECTARES!", "PYTHON_9.3", "")
        f.write("\n" + "Calculating geometry of overlap between " + fcname1 + " " + fcname2)
        print "Calculating geometry of overlap between " + fcname1 + " " + fcname2

        # process: delete identical
        arcpy.management.DeleteIdentical(int_output, "Overlap", "", "0")
        f.write("\n" + "Deleting identical values from \"Overlap\" field")
        print "Deleting identical values from \"Overlap\" field"

        # process: summary statistics
        f.write("\n" + "Determine overlap value in hectares")
        print "Determine overlap value in hectares"
        
        arcpy.analysis.Statistics(int_output, int_output_Statistics, "Overlap SUM", "")
        arcpy.management.AddField(int_output_Statistics, 'orangutan1', 'TEXT')
        arcpy.management.AddField(int_output_Statistics, 'isopleth1', 'DOUBLE')
        arcpy.management.AddField(int_output_Statistics, 'orangutan2', 'TEXT')
        arcpy.management.AddField(int_output_Statistics, 'isopleth2', 'DOUBLE')

        search1 = arcpy.SearchCursor(ou1)
        for scrap in search1:
            ou1_name = scrap.getValue("ou")
            ou1_iso = scrap.getValue("iso")

        search2 = arcpy.SearchCursor(ou2)
        for scrap in search2:
            ou2_name = scrap.getValue("ou")
            ou2_iso = scrap.getValue("iso")
            #row_values = [(scrap.getValue("ou"), scrap.getValue("iso"))]

        cursor = arcpy.UpdateCursor(int_output_Statistics)

        for row in cursor:
            row.setValue("orangutan1", ou1_name)
            row.setValue("orangutan2", ou2_name)
            row.setValue("isopleth1", ou1_iso)
            row.setValue("isopleth2", ou2_iso)
            cursor.updateRow(row)


        f.write("\n" + "Writing to:" + " " + int_output_Statistics)

        #export_to_xls(dataset, output):
        arcpy.TableToExcel_conversion2(int_output_Statistics, excel_output, "XLS")
        print "Writing to excel file"
        f.write("\n" + "Writing to excel file:" + " " + excel_output)
        f.write("\n" + "------------------------------")
        f.write ("\n")




    f.close
    


except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message




It's not perfect yet, but think it's getting there!

Thanks again
0 Kudos