Monday, December 19, 2011

Exit that Script, and exit it now!

Help, I need to kill my script!  Use the sys.exit() gives me an error!!  What to do?

Use the try/except method to just move beyond those errors my friend.  Very simple.

import arcpy
from arcpy import env
import sys
value = arcpy.GetParameterAsText(0)
    arcpy.AddMessage("VERY INTERESTING VALUE....")
    arcpy.AddMessage("FORCED EXIT")
except SystemExit:
    arcpy.AddMessage("WHEN I EXIT I GO HERE: SystemExit exception")
    arcpy.AddError("ERROR ERROR" )


*You can also use:
import os
I haven't tested this though.

Saturday, December 17, 2011

Displaying Messages for a Specific Tool

Robust messaging is very important to help developers debug issues and it helps end users understand what the tool is doing.

ArcGIS Desktop geoprocessing tools have robust messaging built in, but when you run a script, those messages get lost.  To access individual tool messages, just do the following:

import arcpy
from arcpy import env
import sys
env.overwriteOutput = True
sr = arcpy.SpatialReference()
sr.factoryCode = 4326
pt = arcpy.Point(-78,38)
pointGeom = arcpy.PointGeometry(pt,sr)
copiedPt = arcpy.CopyFeatures_management(pointGeom)
print copiedPt.getMessages()

This produces:

Executing: CopyFeatures in_memory\f331817DB_DC61_4523_88F8_1B0329E5F568 c:\TEMP\copiedPT.shp # 0 0 0
Start Time: Fri Dec 16 13:10:14 2011
Succeeded at Fri Dec 16 13:10:14 2011 (Elapsed Time: 0.00 seconds

Very cool and enjoy

Friday, December 16, 2011

Support HTML Tages in Page Layout

Please support the idea of allowing HTML tags in labels:


Tuesday, December 13, 2011

Population and Water Tackled on NPR's Talk of the Nation

An interesting segment was played on Talk of the Nation yesterday about population growth, water, and food.  As a geographer and someone who is interested in have a sustainable environment, I feel it's worth taking a look at:


Monday, December 5, 2011

Fitting Text in Page Elements Based on Sentence Length

When working with Page Layout and Map Automation, sometimes you want to let the user pass some random text into a text element, but you need to format the text so it doesn't look like one big line of mush.

For this example assume you have a page layout and the maximum length a text item can be is 90 characters.  To format this correctly, you need to check if the values provided are less than 90 characters, if it is, write it, if not, start a new line.

sentence = "No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise please start a new thread in this forum instead."
lineLength = 90
count = 0
currentLine =""
lines = []
for word in str(sentence).split(" "):
    wordLength = len(word + " ")
    if (count + 1) < lineLength:
        currentLine = currentLine + word + " "
        lines.append(currentLine.strip() + "\n")
        currentLine = word + " "
        count = 0
    count += wordLength
lines.append(currentLine.strip() + "\n") 

So I took some test from a random forums I was reading and used that as my dummy text.  At the end of each sentence when I reach the 90 character limit or when the next word will push the line over the 90 character limit, I add a '\n' which will signal for a newline.
Now, put the text in a text element:

mxd = arcpy.mapping.MapDocument("CURRENT")
elem = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","txtTestLength")[0]
elem.text = ""
for line in lines:
    elem.text += line

The arcpy.RefreshActiveView() is essential to use in order to see the new text.  If there is a better way, post it in the comments.

Enjoy and Happy Mapping!