[GIS] Batch intersect from a text file using Python (ArcGIS)

arcgis-10.0batchintersectionpython

I am attempting to modify my code below which reads from a text file of shapefiles and executes an intersect and outputs them to a user specified location:

import arcpy, sys, os
from arcpy import env

# Set overwrite option
arcpy.env.overwriteOutput = True

# Get the intersectFeatures layer, intersectFeature, and intersectFeature name field
intersectFeature = sys.argv[1]
i = 0
outDir = sys.argv[2]
f = open(sys.argv[3])
arcpy.AddMessage("Program Initiated")

# Loop for the file processing each line
for line in f: 
    outFC = outDir + "\\temp_poly" + str(i)
    #split the drive and path using os.path.splitdrive
    (drive, pathname) = os.path.splitdrive(line)
    #split the path and fliename using os.path.split
    (pathname, filename) = os.path.split(pathname)
    # Put in error trapping in case an error occurs when running tool
    print line.strip
    arcpy.AddMessage(line.strip)
    i += 1
    try:

        # Make a feature layer with all the intersectFeatures
        arcpy.MakeFeatureLayer_management(line, "inFeatureLayer")
        #line.strip
        # Select all features that intersect features
        arcpy.SelectLayerByLocation_management("inFeatureLayer", "INTERSECT", intersectFeature, "", "ADD_TO_SELECTION")
        #intersectFeature
        # Within the selection select a subset
        arcpy.SelectLayerByAttribute_management("inFeatureLayer", "SUBSET_SELECTION", "")

        # Write the selected features to a new featureclass
#        arcpy.CopyFeatures_management("inFeatureLayer", outFC)
        result = arcpy.GetCount_management("inFeatureLayer")
        arcpy.AddMessage("Converting " + "inFeatureLayer")
        if int(result.getOutput(0)) > 0:
            #<---LAST CHANGED 5/23/2012 4:00 PM --->
            #Process: Clipping!
            arcpy.Clip_analysis("inFeatureLayer", intersectFeature, "outFL", "")
            #arcpy.FeatureToPolygon_management("inFeatureLayer", outFC)
            arcpy.CopyFeatures_management("outFL", outFC)
        else:
            print arcpy.GetMessages()
#            arcpy.AddMessage(line + " made to a Feature Layer")

            print outFC + " has no features."
#line.strip

    except:
                # By default any other errors will be caught here
                #
        print arcpy.GetMessages(2)
# Process: Intersect
for line in f:
        try:

            arcpy.Intersect_analysis(line, outFC, "ALL", "", "INPUT")
            print arcpy.Intersect_analysis(line, outFC, "ALL", "", "INPUT")

            arcpy.AddMessage("Intersecting " + outFC + " with " + intersectFeature)
                #line.strip
            print "Completed: " + str(line)
            line.strip

        except:
                # By default any other errors will be caught here
                #
            print arcpy.GetMessages(2)    
#

This code executes but the I want the files to retain their original filenames rather than temp_poly0, temp_poly1, etc. I would also like the original file path to be appended to where ever I choose it to go.

I have attempted to use this code:

import arcpy, sys, os
from arcpy import env

# Set overwrite option
arcpy.env.overwriteOutput = True
inputs = [item.replace('\n', '') for item in open(sys.argv[1]).readlines()]
# Get the intersectFeatures layer, intersectFeature, and intersectFeature name field
intersectFeature = sys.argv[2]
outDir = sys.argv[3]
#Set the input datasets

arcpy.AddMessage("Program Initiated")

# Loop for the file processing each line
for line in inputs:



    #split the drive and path using os.path.splitdrive
    (drive, pathname) = os.path.splitdrive(line)
    #split the path and filename using os.path.split
    (pathname, filename) = os.path.split(pathname)
    # Put in error trapping in case an error occurs when running tool
    print line.strip
    outFC = os.path.splitdrive(line)
    arcpy.AddMessage(line.strip)

    try:

        # Make a feature layer with all the intersectFeatures
        arcpy.MakeFeatureLayer_management(line, "inFeatureLayer")
        #line.strip
        # Select all features that intersect features
        arcpy.SelectLayerByLocation_management("inFeatureLayer", "INTERSECT", intersectFeature, "", "ADD_TO_SELECTION")
        #intersectFeature
        # Within the selection select a subset
        arcpy.SelectLayerByAttribute_management("inFeatureLayer", "SUBSET_SELECTION", "")

        # Write the selected features to a new feature class
#        arcpy.CopyFeatures_management("inFeatureLayer", outFC)
        result = arcpy.GetCount_management("inFeatureLayer")
        arcpy.AddMessage("Converting " + "inFeatureLayer")
        if int(result.getOutput(0)) > 0:
            #<---LAST CHANGED 5/23/2012 4:00 PM --->
            #Process: Clipping!
            arcpy.Clip_analysis("inFeatureLayer", intersectFeature, "outFL", "")
            #arcpy.FeatureToPolygon_management("inFeatureLayer", outFC)
            arcpy.CopyFeatures_management("outFL", outFC)
        else:
            print arcpy.GetMessages()
#            arcpy.AddMessage(line + " made to a Feature Layer")

            print outFC + " has no features."
#line.strip

    except:
                # By default any other errors will be caught here
                #
        print arcpy.GetMessages(2)
for line in inputs:
    line = line.replace("\\\\", "\\")#changing slashes to single slashes
    #print the stripped line
    (drive, path) = os.path.splitdrive(line)

    print "path before the join is " + path

    outdata=os.path.normpath(sys.argv[3] + path)
    # Process: Intersect      
    try:
        (path2,fileName) = os.path.split(outdata)
        if(os.path.exists(path2)==False):
            os.makedirs(path2)
        arcpy.Intersect_analysis(line, outFC, "ALL", "", "INPUT")
        print arcpy.Intersect_analysis(line, outFC, "ALL", "", "INPUT")

        arcpy.AddMessage("Intersecting " + outFC + " with " + intersectFeature)
                #line.strip
        print "Completed: " + str(line)
        line.strip
    except:
                # By default any other errors will be caught here
                #
        print arcpy.GetMessages(2)  
#

This code succeeds in recreating the folder structure, but no files are output.

Any help is much appreciated.

Best Answer

Do you need to keep all of your intermediate intersects (ie- temp_poly0, temp_poly1)?

If not, you will find that sending results to "in_memory" workspace will not only be easier to manage (all the data is cleaned up from memory on exit of the program, or you could just delete it from memory with arcpy.Delete to clear the memory space if you need to), you may also see a significant speed increase by having the processes avoid the write to disk bottleneck.

Your final step in the process could be then to write out the results to a new name in the original folder location.

Related Question