Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
It was suggested to me to add a dictionary to assign values to the temp_polys. example: {'temp_poly0':'original_filename1', 'temp_poly1':'original_filename2', etc} This solution sounds intriguing but I am unsure as to how to implement it. Can anyone help? –  Visceral Jun 6 '12 at 19:38
add comment

1 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.

share|improve this answer
    
Yes, I would like to keep any intermediate intersects, but I don't want the names to change. I think your solution would be suitable for my needs, i.e., perhaps a new subfolder in each applicable tree. But I'm not sure how I would write this code-wise. –  Visceral Jun 5 '12 at 20:51
add comment

Your Answer

 
discard

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.