Friday, June 15, 2012

Introducing the Data Access Module (arcpy.da) at 10.1

The data access module also known as arcpy.da is used to manipulate and search data.  It allows control of an edit session, faster and better cursor support, function to convert to and from numpy arrays, and supports versioning, replicas, domains, and subtypes work flows.

The data access module has the following functions:

  • ExtendTable - Joins the content of a numpy array based on a common attribute field.  This is like a table join but more flexible.
  • FeatureClassToNumPyArray - this convert a feature class to a numpy structured array
  • ListDomains - Lists the attribute domains in a geodatabase
  • ListReplicas - Lists replicas in a work space
  • ListSubtypes - returns a dictionary of subtypes for a table or feature class
  • ListVersions - list the versions in the work space
  • NumPyToFeatureClass - converts a numpy array to a point feature class
  • NumPyToTable - converts a numpy array to a table
  • TableToNumPyArray - converts a table to numpy array

The data access classes:

  • Domain - the domain object properties that describes an attribute domain
  • Editor - editor class allows use of edit sessions and operations to manage database transactions.  This means rollbacks, and better error handling!
  • InsertCursor - adds new rows to a table or feature class
  • Replica - contains properties of a replica
  • SearchCursor - read only access to a table or feature class
  • UpdateCursor - allows read/write access to a table or feature class.  This is what you use to update or delete a row.
  • Version - contains properties that describe a version.

I plan on detailing these items in future posts.

Thursday, June 14, 2012

Random Number Generator (arcpy.env)

This is a tool that creates a random number based on a seed number and an algorithm selection.
Inputs are as follows:
  1. Seed—The seed is an integer value and is used to initiate the random number generator. The default value is 0.
  2. Random Generator Type—The random generator algorithm.


    • ACM599—ACM collected algorithm 599. This is the default.
    • MERSENNE_TWISTER—Mersenne Twister mt19937.
    • STANDARD_C—Standard C Rand

So to create a random number generation environmental variable you can do it two ways:

Option 1:

from arcpy import env
env.randomGenerator = "4 MERSENNE_TWISTER"


Option 2:

from arcpy import env
env.randomGenerator = arcpy.CreateRandomValueGenerator(4, "MERSENNE_TWISTER")



To produce a random number, the easiest way is to use the arcpy.CalculateValue() and get the results from that tool.
Example:

import arcpy
arcpy.env.randomGenerator = "4 MERSENNE_TWISTER"
result = arcpy.CalculateValue_management("arcgis.rand('normal 0.0 10.0')")
randomValue = float(result.getOutput(0))
print randomValue


The code should print a random number.

It should be noted that this environmental variable will effect any tool that uses random values.

Enjoy

Wednesday, June 13, 2012

env.ScratchGDB (ArcPy 10.1)

A new environmental parameter as 10.1 is env.scratchGDB.  This environmental parameter is guarantees to have a temporary scratch file geodatabase for your writing pleasure.  This means no more checking to see if a user created a file geodatabase for doing some work.
I personally think this is great.  It takes a common task and puts it into core.  Bravo ArcPy Development Team!
From this help:
Its primary purpose is for use by scripts and models as geoprocessing services, with the added focus of only pointing to a known geodatabase. When ArcGIS for Server executes a geoprocessing task, the scratch GDB is always available to write output to. This environment is also useful in authoring scripts and models for use on the desktop. Writing output to the scratch GDB will make your tool portable, because this location will always be available or created at execution time.

Some notes on the env.scratchGDB:

  1. Scratch GDB will exist if called and user will have write access to the file geodatabase
  2. Scratch GDB is read-only.  You can only change the location of the scratch GDB by changing the env.scratchWorkspace 
  3. If the env.scratchWorkspace is not set, then it defaults to the user's temporary directory

Example:
>>> import arcpy

>>> from arcpy import env
>>> env.scratchWorkspace = r"c:\temp"
>>> print env.scratchGDB
c:\\temp\\scratch.gdb


Enjoy

Tuesday, June 12, 2012

Introducing env.scratchFolder (ArcGIS 10.1)


The scratch folder is a new environmental variable that is used for the development of both published and non-published scripts.  It will point to a known folder that exists, which is very nice because at 10.0, env.scratchWorkspace or env.workspace could always be set to None or even worse set to a file geodatabase.
From the online help:
"The Scratch Folder is the location of a folder you can use to write file-based data, such as shapefiles, text files, and layer files. It is a read-only environment that is managed by ArcGIS. 
The Scratch Folder environment complements the scratch workspace environment. Its primary purpose is for use by scripts and models as geoprocessing services, with the added focus of pointing only to a known folder. When ArcGIS for Server executes a geoprocessing task, the Scratch Folder is always available to write output to. This environment is also useful in authoring scripts and models for use on the desktop. Writing output to the Scratch Folder will make your tool portable, because this location will always be available or created at execution time."
 Some important notes about env.scratchFolder:

  1. It will always exists
  2. It's read only property
  3. It will always be folder even if your scratch workspace is set to a file geodatabase
  4. If there is no Scratch Workspace set in your map document, or you're working in an environment where the Scratch Workspace has not been explicitly set, the Scratch Folder defaults to the current user's temporary files directory. This directory is typically at C:\Users\\AppData\Local\Temp on Windows 7 or C:\Documents and Settings\\Localsystem\Temp on Windows XP.

Example:
>>> import arcpy
>>> from arcpy import env
>>> env.scratchWorkspace = r"c:\temp\myfgdb.gdb"
>>> print env.scratchFolder
u'c:\\temp'


Enjoy

Monday, June 11, 2012

Using SQL to Get n number of Rows

When you query spatial data in ArcMap/Catalog/Python, etc.. you cannot limit the row search by default and you can only specify the where part of the sql statement: "SELECT * from < DATASET > WHERE" and then you can specify TYPE = '1'.

Let's assume you want to just get back n number of rows.  If you are using SDE, you could use ArcSDESQLExecute's methods to perform your sql, but you could also use a sub-query. 

Take your default statement that ArcMap allows you to perform on a layer: 

     "Select * from wgs.temp where "

Since we want to just get back n number of rows, use the OBJECTID field as a reference and the 'in' function:

    "Select * from wgs.temp where OBJECT in (select OBJECTID  from wgs.temp where rownum <= 5)"

This will return 5 rows in the data set.  If you want 10, 15, of n number of rows, replace the 5 with whatever value you want.  You can apply this to update and search cursors on SDE data only.

Enjoy



Thursday, June 7, 2012

Monday, June 4, 2012

Creating a Single Directory or Multiple Directories

When automating processes, you should be very aware of system directory structures and the directory structure that your processes need to function properly.

The OS module has a collection of directory tools to make or remove directories as well as checking if they exist or not.

The standard make a directory function is the os.mkdir()

>>> import os
>>> directories = r"c:\temp3\level1"
>>> os.mkdir(directories)

The other folder creation tool I find very helpful is the os.makedirs(), which will generate a several folders at once.

>>> import os
>>> directories = r"c:\temp3\level1\level2\level3\level4"
>>> os.makedirs(directories)

Enjoy