Wednesday, September 28, 2011

Turning on labels through ArcPy

The Layer object in the ArcPy module allows python developers to turn on labels of a feature class on and off at run time.  This is especially helpful if you are exporting maps and you want to control what Layers are labeled.
Example: Turning on Labels
import arcpy
from arcpy import mapping
fc = r"c:\temp\fc.shp"
tempLayer = "tempLayer"
arcpy.MakeFeatureLayer_management(fc, tempLayer)
#   Make the Layer Object
#
layer = mapping.Layer(tempLayer)
layer.showLabels = True
#   Show the changes in ArcMap
#
arcpy.RefreshActiveView()
del layer 


Enjoy

Tuesday, September 27, 2011

Usage() - Arcpy

Usage() is a helpful tool that will return the syntax for a specific tool.  It's a great thing to use in IDLE when writing a python script and you forget what variables go where.

Example:
>>> import arcpy
>>> print arcpy.Usage("Buffer_analysis")
Buffer_analysis(in_features, out_feature_class, buffer_distance_or_field, {FULL | LEFT | RIGHT | OUTSIDE_ONLY}, {ROUND | FLAT}, {NONE | ALL | LIST}, {dissolve_field;dissolve_field...})
Buffer Features
>>> print arcpy.Usage("MakeFeatureLayer_management")
MakeFeatureLayer_management(in_features, out_layer, {where_clause}, {workspace}, {field_info}) Create a Feature Layer

Pretty help for those who don't have to refer to the help document all the time.
Enjoy!

Monday, September 19, 2011

A Quick Note on Multiprocessing

If you create a multiprocessing script and you want to use the python script in ArcToolbox, ensure the following settings are set on your script:
1. Show command window when executing is unchecked
2. Run python script in process is unchecked

Your python script should now multi-process if you wrote it correctly.

Enjoy


Tuesday, September 6, 2011

Finding Disk Size

Ever need to know how much space is left on a disk drive, well I did when creating map books for large areas. import os 
import platform
import ctypes 
def get_free_space(folder):
   """ Return folder/drive free space (in bytes) """ 
   if platform.system() == 'Windows': 
      free_bytes = ctypes.c_ulonglong(0)
      ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes)) return free_bytes.value 
   else: 
      return os.statvfs(folder).f_bfree 
if __name__ == "__main__":
   path = (r"network drive") 
   print get_free_space(path) 

This prints the free space as bytes. It is cross platform and will work on both Linux,MaxOS and Windows. Enjoy

Wednesday, August 24, 2011

Sorting Dictionary Values

Let's say you want to find the largest feature in your feature class.  There are many ways to do this, but here is one simple way using dictionaries.


rows = arcpy.SearchCursor(infc)
featDict = {}
for row in rows:
   feat = row.getValue(shapefieldname)
   featDict[row.getValue("ObjectID")] = feat.area
del row, rows
for w in sorted(featDict,key=featDict.get,reverse=True):
   print w, featDict[w] # prints the largest area first and gives the OID value to get the feature
del featDict


The first entry in the dictionary will be the highest shape area. If you wanted the smallest area, you would set reverse=False.

The key here is the sorted(), which more information can be found here.

Tuesday, August 23, 2011

Ignoring Errors, Sometimes You Need To...

Say you need to write a multiprocessing script to perform a long running process and it's going to run on Windows XP or 7.  Sometime it can throw random IO errors, like data does not exists with ArcPy functions.

To compensate for that, you can embed try/except statements to ignore specific errors within your program.

For example:

try:
    try:
        fsock = open(r"c:\temp\test2.sde", "r")
    except IOError:
        pass
    print 'its all good'
except:
    print 'OH NO SOME ERROR HAS HAPPENED!'
finally:
    print 'see no errors, my script is awesome!'


The code will just ignore and IOErrors that occur during that section of code. I do not recommend using this all the time, but there are times were odd OS issues can prevent a multiprocessing task from completing correctly, at least in my experience.

Enjoy