helenak

a multi_centroid code (long time to run)

Discussion created by helenak on Apr 28, 2012
Hello

I have a python code to produce centroid for many shapefiles.
However, it ran slowly for 72 files. (ArcGIS 9.3, PythonWin 2.5.1)

Please kindly provide any refinement or correction and thanks.

Code
##Script Name: Multi centroid
##Description: Get centroid of shapefiles
##Created By: Elaine Kuo
##Date: 28/04/2012

#Import standard library modules
import win32com.client, sys, os

#Create the Geoprocessor object
GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

#Set the input workspace
#GP.workspace = sys.argv[1]
#Set the workspace.
GP.Workspace= "G:/temp_GIS_range_size_mig_586_centroid/stage_2_dissolve_by_B_NB/S"

#Set the output workspace
#outWorkspace = sys.argv[2]
#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "G:/temp_stage_3_centroid_of_B_NB/S"


try:
    #Get a list of the featureclasses in the input folder
    fcs = GP.ListFeatureClasses()

    # Loop through the above list
    fcs.reset
    fc = fcs.next()


    while fc:
    #Validate the new feature class name for the output workspace.
        outFeatureClass = outWorkspace + "/" + GP.ValidateTableName(fc,
                      outWorkspace)
   
# Get centroids of the feature classes
# a single feature class
    GP.Toolbox = "Data Management"
    GP.FeaturetoPoint(fc, outFeatureClass, "INSIDE")


except:
    GP.AddMessage(GP.GetMessages(2))
    print "Error occured"
    print GP.GetMessages(2)

Outcomes