Hi Sean is right about the gp-creation overhead.
arcgisscripting provides lists that you should use (i'll use the syntax for arcgis 9.3, if you have an earlier version, the syntax is a bit different, as native python lists were not supported and you'd have to use iterators instead) *import arcgisscripting,sys #this only work in arcgis 9.3 gp=arcgisscripting.create(9.3) # change this to your dir or use sys.argv[1] to set it from the commandline gp.workspace = "d:/MA_resevior" for inputFC in gp.listfeatureclasses(): gp.AddField_management(inputFC, "new_area", "DOUBLE") gp.CalculateField_management(inputFC, "new_area", "float(!SHAPE.AREA!)", "PYTHON")* hope this helps jeff On Tue, May 18, 2010 at 10:28 AM, Sean Gillies <[email protected]> wrote: > On Tue, May 18, 2010 at 7:58 AM, anita kean <[email protected]> wrote: >> On Mon, May 17, 2010 at 09:29:51PM -0700, Jen wrote: >>> Thank you for the comments, that's very helpful. >>> >>> I have 56 shapefiles, and I need to calculate area for each one. >>> Therefore, could you explain the first method clearer: "to use the >>> script as is, run it from the command line with the full path to the >>> shapefile you want to calculate the area, e.g. from the command >>> line:" ? >>> >>> I'd appreciate any help you might give me. >> >> If you've got 56 shapefiles (say they're called shape1,shp, ... shape56.shp), >> then you are either going to have to call the program >> 56 times with 56 different shapefile names: >> >> python LU_PL_Exportcoef.py C:\path\to\shapefile\folder\shape1.shp >> python LU_PL_Exportcoef.py C:\path\to\shapefile\folder\shape2.shp >> python LU_PL_Exportcoef.py C:\path\to\shapefile\folder\shape3.shp >> python LU_PL_Exportcoef.py C:\path\to\shapefile\folder\shape4.shp >> python LU_PL_Exportcoef.py C:\path\to\shapefile\folder\shape5.shp >> ... >> python LU_PL_Exportcoef.py C:\path\to\shapefile\f older\shape56.shp >> >> (that looks like too much work!) >> >> or you could get python to do the work for you: >> >> If it were me, to make it easy for myself, I'd >> put all the shapefiles in one folder, >> copy the python program to the same folder, >> and in that folder, write a little python file like : >> >> ============================ >> import os >> import subprocess >> >> rootdir = os.getcwd() >> files = os.listdir(rootdir) >> for f in files: >> if os.path.splitext(f)[1]=='.shp': >> print 'shapefile', f >> shapefile_area = subprocess.call(['python','LU_PL_Exportcoef.py',f]) >> print 'area of %s is %s' % (f, shapefile_area) >> ============================ >> >> Then if you call this file get_area.py, >> all you have to do is type, in that same folder, >> >> python get_area.py >> >> and you should see the areas associated with all 56 shapefiles. >> - assuming your LU_... program prints out what you need. >> >> Hope that helps. >> -- >> Anita >> > > This is a good start, but I don't think subprocess is the way to go > here. If your geoprocessing program was a C program or a Perl script, > you would well make a system call to it. If it's Python code, and > arcgisscripting in particular, it's better to stay in one Python > process and call a Python function instead. This approach avoids the > overhead of starting a new Python interpreter (not inconsiderable) or > ArcGIS geoprocesser (very considerable overhead, I am told) for each > shapefile. > > Cheers, > > -- > Sean > -- Jeff Konnen
