Tuesday, January 10, 2012

Tip - Faster Update Cursors

There are many ways to limit the number of fields an update cursor can update.  The cursor object itself has a where clause option.  Another way, which seems at first glance to be faster than just a where clause on the cursor, is to use a Layer object with a definition query defined on the Layer object, then reference the object instead of the feature class or feature layer itself.

import arcpy
from arcpy import mapping
fclayer = "fclayer"
arcpy.MakeFeatureLayer_management(source, fclayer)
layer = mapping.Layer(fclayer)
layer.definitionQuery = "UIDField = 'unique'"
uRows = arcpy.UpdateCursor(layer)
row = None
for row in uRows:
    row.URL = url
    uRows.updateRow(row)
del row
del uRows
del layer
del fclayer

For more information check out the help page here.

Enjoy

Tuesday, January 3, 2012

I heart my Nook Color and Nook Tablet

For over a year now, I have had the Nook Color, and I am very pleased with the e-reader, so this year I upped the game and bought the tablet version of Nook.  The OS and feel is exactly the same except the hardware inside is vastly improved.  It's quicker and more responsive than the Nook Color and can do more with the applications that can be put on it.  The biggest improvement I see with the tablet version of the Nook is the anti-glare screen.  I know this can be achieved by buying an anti-glare screen guard, but who wants that on the Nook.  I sure don't.

Anyway this wasn't for me, it was for my wife and she loves it.  All the Nook needs is a better selection of apps.  As an e-reader, this is number 1, and Barnes and Noble seems to have more and more e-books daily.

In addition to using the bn website to buy books, you can go to site like ManyBooks, or if you are a sci-fi junkie try the Baen free library.  The thing I really like about my Nook, is that I can use it at my local library as well!  I can just check out my e-book from anywhere as long as I have my library card.

The issue I really do not like about any tablet/reader is the virtual keyboard, but that's even an issue for the sacred iPad.

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)
try:
    arcpy.AddMessage(value)
    arcpy.AddMessage("VERY INTERESTING VALUE....")
    arcpy.AddMessage("FORCED EXIT")
    sys.exit(0)
except SystemExit:
    arcpy.AddMessage("WHEN I EXIT I GO HERE: SystemExit exception")
    pass
except:
    arcpy.AddError("ERROR ERROR" )
    arcpy.AddError(arcpy.GetMessages(2))


Enjoy!

*You can also use:
import os
os_exit(0)
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
sr.create()
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:

http://ideas.arcgis.com/ideaView?id=08730000000bsT8AAI

Thanks

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: http://www.npr.org/2011/12/12/143587133/as-global-population-grows-water-matters-more

Enjoy

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 + " "
    else:
        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
arcpy.RefreshActiveView()

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!