Thursday, May 19, 2011

Search Cursors - Using Expressions

In the last post, search cursor basics were discussed.  When creating search cursors, you can specify many properties, one way to limit the number of row object returned to the end user is the use SQL expressions.


import arcpy
sqlExpression = "\"STATE_NAME\" = 'California'"
fc = r"c:\temp\counties.shp"
rows = arcpy.SearchCursor("C:/Data/Counties.shp", sqlExpression)
# Do stuff with rows


Here only the California counties will be returned.

Enjoy

Wednesday, May 18, 2011

Search Cursors

I couldn't say it better, so from the ArcGIS Help:
"The SearchCursor function establishes a read-only cursor on a feature class or table. The SearchCursor can be used to iterate through row objects and extract field values. The search can optionally be limited by a where clause or by field, and optionally sorted."

Looking Object Model of SearchCursor() we see the following:

Let do a example:

import arcpy
fc = arcpy.GetParameter(0)
rows = arcpy.SearchCursor(fc)
for row in rows:
   print 'hello rows!'
del rows

This is a very simple example where the end user would provide a feature class as an input and the output would be displayed for each row in the rows object.

Not only is it import to understand cursors, but it's also important to understand what a cursor object will give you. It's a collection of Row objects. Row Objects have collection of methods that include getValue(), setValue(), inNull(), and setNull(). The names pretty much explain what each function does, but here is a graphic to explain it better:
If you use the web help like I often do to look up examples, how-to, etc.. for geoprocessing with python, you'll notice that many times the examples show a field value from a row being returned as such:

value = row.FieldName

Though there is nothing wrong with this, I do not prefer this method. I prefer calling the row method getValue().

value = row.getValue("FieldName")


Also remember, you cannot set values with a SearchCursor(), if you need to alter or add rows/features use the Update or Insert cursor objects.

Enjoy

Tuesday, May 17, 2011

Checking the Existence of Data

To check if data exists using the arcpy module, use the Exists().  This function tests for the existence of feature classes, tables, data sets, shapefiles, workspaces, layers, and other files in the current workspace at the time of execution.  The function will return a Boolean value (true/false) to let the end user know if the file exists.

Example: Check for Existences

import arcpy
from arcpy import env

env.workspace = r"c:\temp"
fc = sample.shp
if arcpy.Exists(fc):
   print "The sample.shp does exist"
else:
   print "The sample.shp does not exist"


Note
By default, when scripting, the results of any script or existing data is not to overwrite the data. To change that behavior, set the env.overwriteOutput to True. If data exists and the overwrite property is not set, it could cause an error to the thrown.

Monday, May 16, 2011

Spatial Reference Class

The other day, I was using the new data driven page tools found in the cartography section of ArcToolbox, and I noticed that the 'Grid Index Feature' tool will generate the incorrect sized grid cells if the spatial reference is not set when run outside of ArcMap. To fix this problem, use the spatial reference object and set the output spatial reference parameters to whatever coordinate system you are working with.

Before starting, you need to know what geographic or projected coordinate system needed to use for the tool.  Let's assume that WGS-1984 will be used which has a WKID of 4326.  It's a very common geographic coordinate system, so I won't go into any details about WGS-1984.

Usings the geographic coordinate systems references from the above provided links, create a spatial reference object based on the WKID.
Example: Using Factory Codes

import arcpy
from arcpy import env
WKID = 4326 # WGS-1984
sr = arcpy.SpatialReference()
sr.factoryCode = WKID
sr.create()
env.outputCoordinateSystem = sr


Now the default output coordinate system in WGS-1984.

There are many other ways to create a spatial reference obeject and they can be found here.

Enjoy

Friday, May 13, 2011

A Tip: Writing Queries Expression for Feature Class in Python

I see this question posted many times and it goes something like this: "Help, I've fallen and my SQL expression won't work!"  Well not exactly, but here is my tip.  Always use AddFieldDelimiters() if you don't know how to the field name should be formatted for a SQL query.

Example:

import arcpy

fc = "c:\temp\data.mdb/Stores"
fieldname = "ShopType"

# Create field name with the proper delimiters
#
field = arcpy.AddFieldDelimiters(fc, fieldname)
# Create a search cursor using an SQL expression
#
rows = arcpy.SearchCursor(fc, field + " = 2")
for row in rows:
     #do something
del rows


Enjoy

Thursday, May 12, 2011

Describing Data

Since you can work with all types of data geoprocessing, sometimes you want to know some additional information about the dataset than just its path.  The describe function is the tool you should use to examine dataset's properties.  For example, say you want to know what type of geometry a shapefile is:

Example: Determine Shape Type

fc = r"c:\temp\myshape.shp
desc = arcpy.Describe(fc)
print desc.shapeType

This block of code will output a string telling the end user the type of geometry used for this shapefile.

In general, the Describe function returns a Describe object. The output of Describe contains various properties, such as data type, fields, indexes, etc... The objects properties are dynamic which means that the data type will determine what properties are returned.

All Describe object will have the same base properties:

Common Describe Properties
 After that, the properties vary depending on data type and it is recommended that developers read up on the various data type properties found here.

Enjoy

Wednesday, May 11, 2011

Streamlining Geo-enabled Lists for SharePoint 2010

If you use ArcGIS for SharePoint you will be creating Geo-enabled Lists multiple times.  To save time and effort, create a list template so you can reuse the same basic list structure over and over again.

1. Create a new blank and custom list

2. Name the list something memorable, like geo-enabled list
3. Add your fields to geocode against
  • Example: Address,City,State,Zip_Code

4. Add a 'ShapeX' and 'ShapeY' column as numbers
5. Add a Location Column name 'Location'
  • When Creating the location column, fill out all the geocoding information
6. Attach a geocoding workflow to the list (optional)
7. Once all your columns are added, click on list settings on the ribbon

8. Click on 'save as template'

9. Enter in a template name and save the list template

The list should now appear in the list template gallery.

Enjoy