3.16.2011

PYTHON CODE THAT ADDS A NEW FIELD TO A SHAPEFILE AND SPECIFIES ITS CONTENT

Arcpy class is new to ArcGIS 10.0. ArcPy is a site-package that builds on (and is a successor to) the successful arcgisscripting module. Its goal is to create the cornerstone for a useful and productive way to perform geographic data analysis, data conversion, data management, and map automation with Python.

Here's a code that will add a new field to a shapefile and then specify the contents of the added field. In this case, all areas greater than 1000 square meters will be labelled "YES" meaning it is subdividable otherwise it will be "NOO".

**********************************************************************************
import arcpy
def cursor():
    inFeatures = GetParameterAsText(0)
    subdvdable = GetParameterAsText(1)
    #inFeatures = "C:\\Users\\RS Lab\\Desktop\\222\\vector\\parcel_area.shp"
    arcpy.AddField_management(inFeatures, subdvdable, "TEXT")
    rows = arcpy.UpdateCursor(inFeatures)
    for row in rows:
        if row.Shape_Area > 1000:
            row.subdvdable = "YES"
        else: row.subdvdable = "NOO"
     rows.updateRow(row)

    del row,rows

**********************************************************************************

To implement this, a new toolbox should be created then the parameters will be set using a GUI. For the inFeatures parameter, it can be specified as Feature Class. The next parameter subdvdable will be of String type which will specify the name of the field to be added.

When run, the specified shapefile will already contain the added field and the specified content based on the logic set.

No comments:

Post a Comment