Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The code thus far works to create multiple output files, but I'd like to set it up so that the data is all exported onto a single table across multiple rows. The issue is I believe with the part in bold below:

#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

    # 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")
            f = open(logfile, "w")    


        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, "Overlap", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        f.write("\n" + "Adding \"Overlap\" field to " + int_output)
        print "Adding \"Overlap\" field to " + int_output

        # process: calculate field, "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, "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", ""), 'orangutan1', 'TEXT'), 'isopleth1', 'DOUBLE'), 'orangutan2', 'TEXT'), '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)

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


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

So, is there a method (perhaps similar to f = open(logfile, "a" ?), whereby I could append to rather than overwrite a table?

share|improve this question

Here's one idea: arcpy.Append_management will append things but they have to be similar. So feature classes append to feature classes, tables to tables. I recently wrote a script where I had to export my FC to a scratch (temporary) table using CopyRows and then append that table to a big "collector" table. You could do something similar in a geodatabase and export the final table to excel.

share|improve this answer

You have used both a SearchCursor and an UpdateCursor in your code but to write additional rows to a table the type of cursor to use is an InsertCursor.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.