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've tried to develop some workflows with the ArcPy module for splitting a shapefile based on the unique values in a specified field. The code works as expected except one thing I am not able to figure out.

import arcpy, os
from arcpy import env

#getting user inputs
in_fc = arcpy.GetParameterAsText(0) #'roads.shp'
outfolder = arcpy.GetParameterAsText(1) #r'C:\GIS\Temp\OutSHP'
fieldname = arcpy.GetParameterAsText(2) #"FEATURE"
createfolder = arcpy.GetParameterAsText(3) #true/false
env.workspace = outfolder

#user chosen NOT to create a folder with the name of feature class
#
if createfolder == "false":

    with arcpy.da.SearchCursor(in_fc, ["{0}".format(fieldname)]) as cursor: #defining a search cursor
        for row in cursor:

            if not arcpy.Exists(os.path.join(outfolder,row[0])): #checking if shapefile already exists
                #Analysis toolbox > Extract > Select GP tool to select features
                arcpy.Select_analysis(in_fc, os.path.join(outfolder,row[0]), ''' "FEATURE" = '{0}' '''.format(row[0]))
            else:
                    print "The shapefile with this name already exists"


#user chosen to create a folder with the name of feature class
#
else:

   with arcpy.da.SearchCursor(in_fc, ["{0}".format(fieldname)]) as cursor: #defining a search cursor
        for row in cursor:
            directory = os.path.join(outfolder,row[0]) #create a path for directory that will be created
            if not os.path.exists(directory): #checking if directory exists
                os.makedirs(directory)

                if not arcpy.Exists(os.path.join(directory,row[0])): #checking if shapefile already exists
                #Analysis toolbox > Extract > Select GP tool to select features
                    arcpy.Select_analysis(in_fc, os.path.join(directory, row[0]), ''' "FEATURE" = '{0}' '''.format(row[0]))
                else:
                    print "The shapefile with this name already exists"

I've created a script tool in ArcGIS which takes an input shapefile, an output folder, a field name and whether user wants to create a folder for each unique value in the specified field before creating a shapefile there (the folder and the shapefile will have identical name except .shp in the end for shapefiles).

The issue is that the tool gets the work done for 5 seconds when user chooses to create folder with the unique name for each feature class and for around 3 minutes if the shapefiles are created directly in the output folder. I am having hard times trying to find out anything that could stipulate this behaviour in the code.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Compare your script to this one...it should work in 10.1 although not tested. http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9920F808

share|improve this answer
    
Thanks, Dan! Have already seen this one. Your script's logic is fairly different from mine - I'm only learning Python now and thus trying to build the code starting really simple :) do you have any idea why the tool works so slower that are applicable specifically to the code I posted? It feels as it does some unneccessary work yet I can't figure out what is wrong. –  Alex Tereshenkov Feb 28 '13 at 14:29
3  
Alex to begin with directory should be made a variable since it doesn't have to use the os.path repeated calls ie arcpy.Exists(os.path.join(directory,row[0])): it calls os.path, then does a join etc etc. If something can be made a variable, then it doesn't need to be repeated. –  Dan Patterson Feb 28 '13 at 15:14
    
Thanks, Dan. Good point, there is always room for improvement :). However, since the os.path.join is being performed in both scenarios (when the folders get created and not), I don't think it is this part that makes the code run 200 times slower when the folders do not get created. I believe it is some logical error that forces the script runs slower when writing shapefiles directly to the output without creating individual folders. –  Alex Tereshenkov Feb 28 '13 at 15:43
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.