Thursday, November 29, 2012

Walk, new arcpy.da function as 10.1 SP1

Walk is a useful function that walks directories.  Though this is an easy function to create using recursion, it is now including as a core function at 10.1 SP1 for arcpy.

The help describes the function as such:
Generate data names in a catalog tree by walking the tree top-down or bottom-up. Each directory/workspace in the tree yields a tuple of three: dirpath, dirnames, and filenames. Python's os module includes an os.walk function that can be used to walk through a directory tree and find data. os.walk is file based and does not recognize database contents such as geodatabase feature classes, tables, or rasters. arcpy.da.Walk can be used to catalog data.

import arcpy
import os
workspace = "c:/data"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))


Wednesday, November 21, 2012

Shift Those Polygons at 10.1

The arcpy team at Esri has a great blog post that shows how to shift polygons using the SHAPE@XY tag.
Here is the sample:

with arcpy.da.UpdateCursor(in_features, ['SHAPE@XY']) as cursor:
        for row in cursor:
            cursor.updateRow([[row[0][0] + (x_shift or 0),
                               row[0][1] + (y_shift or 0)]])

Here we have the shape column (column 0) and to access the X and Y property, you just edit the first and second objects in the X,Y pair.  The x_shift and y_shift are float values that will move the code in one direction or another.  

It's very simple sample, but it shows how you can easily move polygons.  In the past you had to adjust each vertex, but now this is done internally thanks to the new arcpy.da module at 10.1.

Tuesday, November 20, 2012

New Geometry Tags for Cursors

At 10.1 SP1, there are 3 new tags that can be used to represent geometry objects.  Those tags are:
The well-known binary (WKB) representation for OGC geometry. It provides a portable representation of a geometry value as a contiguous stream of bytes.  The well-known text (WKT) representation for OGC geometry. It provides a portable representation of a geometry value as a text string.  The esri JSON string representing the geometry.

Here is a simple usage example from the python window:

from arcpy import da
with da.SearchCursor("Counties", ["SHAPE@JSON", "SHAPE@WKT", "SHAPE@WKB"]) as rows:
     for r in rows:
         print r[0]
         print r[1]
         print r[2]
You'll see all three types print out.  The print out doesn't really mean anything except to show the various string formats that a coder can now export the geometry object into. It will also make exporting geometries to something like a TAB or Excel table a bit more practical.  I would stay away from csv, since many of these formats use the ',' (comma) to separate the points.


Monday, November 19, 2012

Using the Oracle Instant Client with ArcGIS Server

At 10.1 you can use the Oracle instant clients to connect to your oracle SDE instances.  It's easier than it seems.

  1. Download the appropriated instant client (for ArcGIS Server use x64 version)
  2. Create a folder on the local hard drive (write down the path)
  3. Unzip the instant client in the folder in step 2
  4. Assuming windows, edit the 'PATH' variable and add Step #2's path to the variable
  5. Restart ArcGIS service
  6. Create your direct connect connection:
  7. Register the connection with server
Now you can use the sde connection for any map document, geoprocessing service, etc... 

This workflow will be valid for desktop instant client setup, except it should be noted that the 64-bit version should be before the 32-bit reference path.


Thursday, November 15, 2012

Using the Constant PI in C++

In visual c++, you need to do the following in order to use the defined PI constant.

#include < math.h > 

double EuclidFunctions::AreaOfCircle(double radius){
return (pow(radius,2)) * M_PI;

where 'M_PI' is the constant value.


Tuesday, November 6, 2012

Go Vote!

If you live in the United States, please go vote.   You have no excuse.

Thank you