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:

        fsock = open(r"c:\temp\test2.sde", "r")
    except IOError:
    print 'its all good'
    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.


Friday, August 19, 2011

Note on DataDrivenPages (Arcpy) Object

The property pageRow in the DataDrivenPages object for Arcpy states that it is read/write object, but it is only read only.  This mean if you need to update a record on your reference grid, you have to use an update cursor.

Thursday, August 18, 2011

Tips for Map Book Automation

The mapping module for Arcpy is awesome!  I can't wait to see what happens at 10.1.  Everything should be automated, and here is some tips that might help everyone develop map books a little better:
  1. Manage unneeded objects, delete them from memory if they are not needed anymore using the del statement.  (you can always extend some object to use using statements as well)
  2. If the area of interest is large and the grid area is small, it's going to take a long time to complete.  Get a cup of coffee!
  3. Eliminate the need to reference multiple Map Documents.  If you need a left and right page, use one Map Document and turn the layers on/off as needed.
  4. Use cached local map services if possible for basemap data.  This should speed production up.
  5. Modules like shutil.copy() can be quicker than some arcpy functions, so use them.
  6. Always check to see if the PDF file name exists (os.path.isfile()) and use arcpy.CreateUniqueName() frequently
  7. If the map book is going to be large (2000+ pages), consider splitting the book into multiple books
  8. Subprocessing module can be your friend and enemy, use it wisely or it will destroy you.
  9. Reference local data, not data over the network when possible
Now go automate your map production!

Please add your comments/tips to improve map automation, I'd love see what you have encountered.

Monday, August 8, 2011

Great post on Arcpy

If you want a standard template for working with python, Esri provided a great blog post on just that!

I recommend you use it to make life easier.