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
> Jen
>
> On May 17, 9:50 pm, Dylan Hettinger <[email protected]> wrote:
> > sys.argv is a Python list of arguments. The first value, sys.argv[0], is
> > always the current script. You're probably running this script from the
> > command line (or an editor like IDLE) without giving it a shapefile as an
> > argument. 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:
> >
> > > C:\python26\python.exe D:\MA_resevior\LU_PL_Exportcoef.py
> >
> > C:\path\to\your\shapefile.shp
> >
> > You could also change the line:
> > inputFC = sys.argv[1]
> > to:
> > inputFC = "C:\\path\\to\\your\\shapefile.shp"
> >
> > This way it won't expect an argument, but you'll have to change that line
> > every time you want to use a different shapefile.
> >
> > dylan
> >
> > On Mon, May 17, 2010 at 6:57 PM, Jen <[email protected]> wrote:
> > > There is some existing code for calculating shapefile area with
> > > python, as I listed below.
> >
> > > import arcgisscripting
> > > import sys
> >
> > > gp = arcgisscripting.create()
> >
> > > # Set a default workspace
> > > gp.workspace = "d:/MA_resevior"
> >
> > > # An input polygon feature class
> > > inputFC = sys.argv[1]
> >
> > > gp.AddField_management(inputFC, "new_area", "DOUBLE")
> > > gp.CalculateField_management(inputFC, "new_area", "float(!
> > > SHAPE.AREA!)", "PYTHON")
> >
> > > However, python shows error message:
> >
> > > Traceback (most recent call last):
> > > File "D:\MA_resevior\LU_PL_Exportcoef.py", line 16, in <module>
> > > inputFC = sys.argv[1]
> > > IndexError: list index out of range
> >
> > > If I change sys.argv[1] to sys.argv[0], and print sys.argv[0], it
> > > shows D:\MA_resevior\LU_PL_Exportcoef.py, which is my python code
> > > file. It shows error message:
> >
> > > Traceback (most recent call last):
> > > File "D:\MA_resevior\subbasin_LU\LU_PL_Exportcoef.py", line 24, in
> > > <module>
> > > gp.AddField_management(inputFC, "new_area", "DOUBLE")
> > > ExecuteError: Failed to execute. Parameters are not valid.
> > > ERROR 000732: Input Table: Dataset D:\MA_resevoir\LU_PL_Exportcoef.py
> > > does not exist or is not supported
> > > Failed to execute (AddField).
> >
> > > I'd appreciate any help you might give me.
> >
> > > Jen
>
--
Anita