Friday, April 27, 2012

Simple enum implementation in python

When using Arcpy, sometimes I always felts that the parameters that require specific entries, like workspace type for layer.replaceDataSource() would be best served if they were an enumerated value.
Python has rejected the enum type, but it does not mean you cannot create your own enum type. Let's get started:

 def __enum(**enums): 
    return type('Enum', (), enums)
Now we can define the enum values as needed.


To use the newly created type, just call it like a property on any object.
>>> print WorkSpaceType.CAD_WORKSPACE


Friday, April 6, 2012

Snippet: Add a Spatial Index To All Feature Classes

Ever need to run a batch spatial index in a work space? Yes, well here is a snippet to help you out. import arcpy
from arcpy import env

env.workspace = arcpy.GetParameterAsText(0) # workspace parameter
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
print 'fin'

Thursday, April 5, 2012

Python Snippet, Writing Text To A File

Let's say you have a program that runs a long process, and you want to write information to a text file. Using the file IO functions, you can easily do this:

data = ["a","b","c","d"]
myFile = open("info.dat", 'w',0)
for item in data:
    myFile.write("%s\n" % item)
del myFile
del data

Now you have a text file where each item in the list is on a new line.


Wednesday, April 4, 2012

Adding Python Extensions to ArcCatalog File Types

When writing complex python processes, sometimes you do not want to put all the logic in a single python file.  This means if you need to make an edit during testing from ArcCatalog, you will have to have file explorer open and switch between screens.

As a programmer, this is unacceptable, so make life easier and add the PY extensions as a viewable file extension in ArcCatalog.
  1. To do this, bring up ArcCatalog and select Customize from the menu bar.
  2. Next, select 'ArcCatalog Options'
  3. Click on the 'File Types' tab
  4. For Standard Installations:
    • If you did a standard installation, the computer should already have python installed, so click on 'Import File Type From Registry'
    • Select the PY extension in the list
  5. For Non-Standard Installations, or Step 4 does not work:
    • For 'File Extension' type PY
    • 'Description Type' - Python File
    • Set the icon to: C:\Python26\ArcGIS10.0\DLLs\py.ic
  6. Press OK to save your changes
  7. Press Apply then OK one more time
  8. Now you can see your python files in ArcCatalog