helenak

batch a python code for features to point

Discussion created by helenak on Apr 29, 2012
Latest reply on May 3, 2012 by helenak
Hello

I have a python code for features to point but for singular shape files only.
(ArcGIS 9.3 and python 2.5)

Please kindly help with the two questions.
1. how to modify it for multiple shape files in a folder
2. for the name of outfeatureclass, how to make  the name of outfeatureclass the same as the input shapefile.
   (For instance, the input is CXXXX.shp, and the output will become CXXXX_shp.shp. How to make the output CXXXX.shp)

Thanks a lot.

Code
##Script Name: Multi centroid
##Description: Get centroid of shapefiles
##Created By: Elaine Kuo
##Date: 29/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_stage_2_dissolve_by_B_NB/test1"

#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/Sylviidae_86"


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")
            fc = fcs.Next()
           
           

except:
    GP.AddMessage(GP.GetMessages(2))
    print GP.GetMessages(2)

Outcomes