Monday, May 9, 2011

Using fields and indexes

Feature classes and tables have fields properties that returns a list of fields objects when described.  Each field or index object has a number of properties that can be used to explore the object.  Alternately, the ListFields() and ListIndexes() can be used to also create the same lists. 

Example:

# Describe feature class
#
desc = arcpy.Describe(fc)
# Get Fields
#
fields = desc.fields
for field in fields:
   # do something with the field
   print field. name


The properties of the field and index object are below:
Enjoy

Friday, May 6, 2011

Setting paths to data in Python ie the '\'

Python treat the backslash (\) as an escape character.  For example, \n represents a new line, \t is a tab, etc...  When specifying a path, a forward slash (/) can be used in place of a backslash.  Two backslashes can can also be used instead of one to avoid syntax error.  A string literal can also be used by placing the letter r before a string.

Example: Correct way
 

>>> import arcpy
>>> arcpy.GetCount_management("c:/temp/streams.shp")
>>> arcpy.GetCount_management("c:\\temp\\streams.shp")
>>> arcpy.GetCount_management(r"c:\temp\streams.shp")

Example: Incorrect Way

>>> arcpy.GetCount_management("c:\temp\streams.shp")
# ExecuteError: Failed to execute. Parameters are not valid.
# ERROR 000732: Input Rows: Dataset c: em\streams.shp does not exist or is not supported
# Failed to execute (GetCount)


Enjoy

Thursday, May 5, 2011

Listing Data with ArcPy

ArcPy has a number of functions built into the package that perform list operations.  From the web help here is the complete list at ArcGIS 10.
The results of each of the above function is a python list containing the data you want.  A list in scripting can contain any data type.  To access the data, use looping to iterate through the list and get the values.

Example: Listing Feature Classes in a Workspace

import arcpy
from arcpy import env

# Set the workspace. List all of the feature classes that start with 'G'
#
env.workspace = r"c:\temp\data.gdb"
fcs = arcpy.ListFeatureClasses("G*")

The results of this is a list of feature classes in the env.workspace path. The variable fcs is an array of paths to each feature class begining with the letter G.

Example: Using List Results

# Set the workspace. List all of the TIFF files
#
env.workspace= r"c:\temp\images"

# Gets a list of raster data (TIF)
#
for tiff in arcpy.ListRasters("*", "TIF"):
print tiff

Here we can a simple process that takes the list data and prints the results back to the user. The only returned results are of type Tagged Image File Format. If no images of that file type are found, then an empty array is returned.

List Function Type Keywords
The keywords for the list functions return what is returned to the user in the list.  Here is the list of key words as found in the ArcGIS 10 help documentation:


Enjoy

Wednesday, May 4, 2011

Arcpy Envrionmental Variables in Python

Esri's arcpy library has a common set of parameters that it uses among all it's tools.  These parameters are originally obtained from the geoprocessing environment that all tools use during execution.  When a tool is launched, the script/model/tool goes out and grab the general environmental settings. 

From the help:
"When a script is run inside a tool from an ArcGIS application or from another geoprocessing script, the environment settings used by the calling application or script are passed to it. These settings become the default settings used by the tool's script when it is executed. The called script may alter the settings passed to it, but those changes are only used within that script or by any other tool it may call."
The environmental settings are exposed as properties on the env class.  These properties can be used to retrieve the default values and to set custom values. 

Setting Workspace Example:

>>> import arcpy
>>> from arcpy import env
>>> env.workspace = "c:/data"

To get a python list of all envrionmental values do the following:

>>> import arcpy
>>> print arcpy.ListEnvironments()

You can load and save envrionmental settings to a file using the

>>> # Load settings
>>> arcpy.LoadSettings(file)
>>> # Save Settings
>>> arcpy.SaveSettings(saveFile)


Enjoy

Tuesday, May 3, 2011

Calling Tools and Models from Python

In general, Python is only aware of the out of the box geoprocessing functionality and not custom tools created by the end user. These tools can by accessed via Python much like the 'import' method used in the last post.  To access a custom made toolbox and tool from Python use the ImportToolbox().  After importing the toolbox, the custom tools can be accessed as:

>>> arcpy.ImportToolbox(".tbx")
# Run the imported tool
#
>>> arcpy.toolname_alias(parameters)


When accessing tools using the arcpy package, the toolbox alias is extremely important. If a unique name is not used, then the system will not know what tool to run.

When working with server, published geoprocessing tasks can be imported as well:

>>> arcpy.ImportToolbox("http://server/arcgis/services;BufferByVal")


Enjoy

Monday, May 2, 2011

Importing: The Basics

Everyday, when I look at the forums, there are always many posts asking for help because they are new at programming in python.  To start, you need to load the ArcGIS Python library into the python script.  To access the library, use the import function to access the 'arcpy' library.

Simple Examples:

import arcpy


Now you can begin using all the modules, libraries and classes contained within the ArcGIS Python Library.

Let's say we just need the Spatial Analyst libraries. Instead of loading the whole library, just load part of the arcpy library.


from arcpy import sa


The Geo-statistical analyst module

from arcpy import ga

The mapping module

from arcpy import mapping

This can be done for all the python modules, and it allows users to load just what they want.

Friday, April 22, 2011

Fun With ArcGIS Online

I've been playing around with ArcGIS Online, it's very interesting way of sharing complex geo-spatial information. 

Here is a simple example:

View Larger Map