Wow, so many good ideas.
Thank you very much!

My arcGIS is 9.3.1 version, python is 2.5.1 version.

I tried two methods above. But, both show the same error message, I
really can't figure out what went wrong.

Traceback (most recent call last):
  File "D:/MA_resevior/subbasin_LU/get_area.py", line 17, in <module>
    gp.AddField_management(fc, "n_area", "double")
ExecuteError: ERROR 000464: Cannot get exclusive schema lock.  Either
being edited or in use by another application.
Failed to execute (AddField).

The code is actually short. I attached below, I'd appreciate any
advice you might give.

Code 1:

import arcgisscripting
import random
#import pylab
import sys
import glob

gp = arcgisscripting.create()

# Set a default workspace
gp.workspace = "D:\MA_resevior\subbasin_LU"

for fc in glob.glob("*.shp"):
    gp.AddField_management(fc, "n_area", "double")
    gp.CalculateField_management(fc, "n_area", "float(!shape.area!)",
"PYTHON")


Code 2:

import arcgisscripting
import sys
import glob

gp = arcgisscripting.create(9.3)

# Set a default workspace
gp.workspace = "D:\MA_resevior\subbasin_LU"

for fc in gp.listfeatureclasses():
    gp.AddField_management(fc, "n_area", "double")
    gp.CalculateField_management(fc, "n_area", "float(!shape.area!)",
"PYTHON")


Jen



On May 18, 8:13 am, Jeff Konnen <[email protected]> wrote:
> you can have it easier:
>
> this should do:
>
> #for 9.1, the instantiation of the gp object is different (win32com.dispatch
> ...blabla)
>
> gp=arcgisscripting.create()
>
> #change this to your dir or use sys.argv[1] to set it from the commandline
> gp.workspace = "d:/MA_resevior"
>
> fcs=gp.listfeatureclasses()
> fc=fcs.next()
> while fc:
>     gp.AddField_management(inputFC, "new_area", "DOUBLE")
>     gp.CalculateField_management(inputFC, "new_area",
> "float(!SHAPE.AREA!)","PYTHON")
>     fc=fcs.next()
>
>
>
> On Tue, May 18, 2010 at 11:40 AM, Félix <[email protected]> wrote:
> > For 9.1 version
>
> > # Para v9.1
> >            area = 0.0
> >            rows = gp.UpdateCursor(f5 + ".shp")
> >            row = rows.Next() # get the first one
> >            while row :
> >                area = float(row.Shape.Area)
> >                row.m2 = area
> >                row.Ha = area / 10000
> >                rows.UpdateRow(row)
> >                row = rows.Next()
> >            del rows,row
>
> > 2010/5/18 Jeff Konnen <[email protected]>:
> > > 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
>
> --
> Jeff Konnen

Reply via email to