Wednesday, July 27, 2011

Select the correct geographic (datum) transformation when projecting between datums

I recently had to change lots of data from one datum to another, and I found this help article from Esri very useful:  http://support.esri.com/en/knowledgebase/techarticles/detail/21327

Happy Projecting!

Monday, July 25, 2011

Printing Using Arcpy.mapping

The ArcGIS Server Blog has a great article covering gp printing from server.  I highly recommend that anyone who wants to export maps to PDFs or automate printing go to this site: http://blogs.esri.com/Dev/blogs/arcgisserver/archive/2011/04/12/An-introduction-to-arcpy.mapping-for-ArcGIS-Server-developers.aspx

Enjoy

Thursday, July 21, 2011

2011 UC Geoprocessing Presentation

Did you miss the 2011 Esri User Conference, but you wanted to see the geoprocessing/python presentation?

Check out this link: http://resources.arcgis.com/content/geoprocessing/10.0/presentations

Enjoy

Wednesday, July 20, 2011

Extract Package

Extracts the contents of a layer or map package to a specified folder. The contents of the output folder is updated with the contents of the input package.


Example: Extract a Layer Package

import arcpy
import os
package = arcpy.GetParameterAsText(0)
destinationFolder = arcpy.GetParameterAsText(1)

extracted = destinationFolder + os.sep + 'unpackedLayers'

arcpy.ExtractPackage_management(package, extracted)

arcpy.SetParameterAsText(2,extracted)

Tuesday, July 19, 2011

Create a Layer Package

The layer package allows users to consolidate all your data resources into a single folder or compressed file.  The tools help users organize data that can be spread across the network.  The layer package also allows users to Share data by making it easy to publish the package to ArcGIS.com. 


Some interesting notes:

  • Layer packages are backwards compatible with ArcGIS 9.3.1.

  • A warning is issued when this tool encounters an unsupported layer type (a schematics or tool layer).

  • For layers that contain a join or participate in a relationship class, all joined or related data sources will be consolidated into the output folder.
  • The Schema Only parameter, if checked, will only consolidate the schema of the input data source(s).



Example of Consolidating all Layers:

import arcpy
import os

env.workspace = r"c:\temp"
listLayers = arcpy.ListFiles("*.lyr")
arcpy.PackageLayer_management(listLayers, 'packagelayers.lpk', "PRESERVE", "CONVERT_ARCSDE", "#", "ALL", "ALL", "CURRENT")

Monday, July 18, 2011

Testing for Schema Locks

TestingSchemaLock tests if a feature class or table has a schema lock.  Any tool that alters schema will require a schema lock to be placed on the input data.  If a tool requires a schema lock and is unable to acquire one an error message is provided.


import arcpy
data = arcpy.GetParameterAsText(0)
isLocked = arcpy.TextSchemaLock(data)
if lockTest:
   arcpy.AddField_management(data,"Flag","long")
else:
   print "Unable to acquire the necessary schema lock to add the new field"

Thursday, July 14, 2011

Removing Illegal Characters and Preventing Unicode Errors

I hate unicode errors like the one below, and I kept getting them intermediately on a table I was writing some scripts against.  Here is an example of an error I received:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4:
ordinal not in range(128)


After much internal debate, I decided to just remove the illegal characters when exporting or reading values in that table.  First I check to see if the value returned is a unicode type, then I apply my operation.

userValue = row.getValue(field)
if type(userValue) is unicode:
   val = ''.join([x for x in userValue if ord(x) < 128]) 

   # do something with val #
Now, the illegal characters are gone!

Here is a simpler example using IDLE:

>>> userValue = "abcdéf"
>>> val = ''.join([x for x in userValue if ord(x) < 128])
>>> print val
abcdf

Notice that the function just removed the é value and produced 'abcdf'.

Hope this helps!