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.

WorkSpaceType = __enum(ACCESS_WORKSPACE="ACCESS_WORKSPACE",
                       ARCINFO_WORKSPACE="ARCINFO_WORKSPACE",
                       CAD_WORKSPACE="CAD_WORKSPACE",
                       EXCEL_WORKSPACE="EXCEL_WORKSPACE",
                       FILEGDB_WORKSPACE="FILEGDB_WORKSPACE",
                       NONE="NONE",
                       OLEDB_WORKSPACE="OLEDB_WORKSPACE",
                       PCCOVERAGE_WORKSPACE="PCCOVERAGE_WORKSPACE",
                       RASTER_WORKSPACE="RASTER_WORKSPACE",
                       SDE_WORKSPACE="SDE_WORKSPACE",
                       SHAPEFILE_WORKSPACE="SHAPEFILE_WORKSPACE",                     
                       TEXT_WORKSPACE="TEXT_WORKSPACE",
                       TIN_WORKSPACE="TIN_WORKSPACE",
                       VPF_WORKSPACE="VPF_WORKSPACE")

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

Enjoy

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:
   arcpy.AddSpatialIndex_management(fc)
print 'fin'
Enjoy

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)
myFile.flush()
myFile.close()
del myFile
del data


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

Enjoy

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
Enjoy