fullmetalzinc

Batch intersect from a text file using Python (ArcGIS)

Discussion created by fullmetalzinc on Jun 5, 2012
Latest reply on Jun 8, 2012 by fabl
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)
# 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 succeeds in recreating the folder structure, but no files are output.

Any help is much appreciated.

Outcomes