Batch intersect from a text file using Python (ArcGIS)

661
3
06-05-2012 12:14 PM
FullMetalzinc
New Contributor
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.
0 Kudos
3 Replies
FabianBlau
Occasional Contributor II
You could save the results to tmp-Files and copy the results to your input-filenames after the computations in a loop.
To find the corresponding filenames you could use a dictionary {'tmp1':'filename1', 'tmp2':'filename2', ...}
0 Kudos
FullMetalzinc
New Contributor
You could save the results to tmp-Files and copy the results to your input-filenames after the computations in a loop.
To find the corresponding filenames you could use a dictionary {'tmp1':'filename1', 'tmp2':'filename2', ...}

I had not thought of this before.  I have never used a dictionary of this type before.  Could you point me the way on how to implement this?

Thanks
0 Kudos
FabianBlau
Occasional Contributor II
fileNames = {}                                                     # Defines an empty dictionary
# Loop for the file processing each line
for line in f: 
    outFC = outDir + "\\temp_poly" + str(i)
    fileNames[outFC] = line                                     # A dictionary stores key/value-pairs, take a look to the python-help
    arcpy.AddMessage(fileNames)                            # Look, how the dictionary increases

...
for key in fileNames:
    arcpy.CopyFeatures_management(key, fileNames[key])  # Copies the tmp-files to the origin files (an overrides them)

Maybe there is an easier way, but try this out.
0 Kudos