Wednesday, November 16, 2011

Using ESRI arcpy GetParameter, instead of python sys.argv

There are two options for passing parameters to your python script in ArcMap.
1) Using the python sys library.
2) Using ESRI arcpy library.
grasslandSelect = sys.argv[1]
grasslandSelect = arcpy.GetParameter(0)

There are some obvious differences. Python uses the argv[0] to pass the argument of the actual python script, so the first argument entered in the command line is argv[1].
ESRI GetParameter(0) is the first parameter passed when calling the script from a Toolbox.

For the case of Boolean arguments, it is worth mentioning that sys.argv will return ,with a lower case, 'true' or 'false' while arcpy.GetParameter will return, with a capital, 'True' or 'False'. Though I had not problems complementing either(not True, not true), I did have some unidentified issues using the lower case 'true/false'

Display ArcMap Dialog Messages During Script

Easy to-do! You must, however, cast numbers with str().
arcpy.AddMessage("Total number of features: " + str(count))

Monday, November 14, 2011

Update: Arcmap Toolbox with Python Script

An initial step to publishing an ArcServer geoprocessing tool is adding the script to an ArcMap toolbox, setting the parameters for the script, and then publishing the tool.
The tool I'm developing allows for non-technical users to perform technical queries by the push of a button.
Currently, I have the tool added to a Toolbox in ArcMap. The tool takes 12 arguments from the user. 9 arguments are check-boxes, 2 arguments are values between 0 and 1, and the final argument is a feature-set(a polygon). The check-boxes allow the user to select attributes from the data-set, in a 'on/off' sort of way. To select a range of juniper density, the two arguments that take decimal numbers specify the low and high values of density. And the final argument,the feature set, allows the user to first draw a polygon around a specific area; from this specific area, all the other arguments are then used to refine the query.

For example, the screenshot below is building a query that will select: Low and High producing grassland and savanna, that are in private land, that have juniper tree density less than 25%--all within the specified polygon.

The resulting feature set selection for this query is below.


Another example, here is the result of the query (High and Low Producing Grassland, that is State Trust, with 0 to 25% juniper density--in the specified polygon).


The most unique and easily repeatable part of this tool is finding a 'range' of data. This tool could easily be applied to the whole state with respect to answering questions about slope, distance to water catchments, and possibly other density questions--like animal population density.

Wednesday, November 9, 2011

Rendering Feature Sets in ArcMap from Python

If your creating a python script that will run inside ArcMap, and the feature sets that are generated from the script must be then displayed, the basic script code is below.
Read further to see how to setup your script in an ArcToolbox.


import arcpy
from arcpy import env
import sys

#The set that I will query, it has two polygons. I will select one and then render it.
queryFset = r'C:\Development\2Test_QueryPolygon.shp'

arcpy.env.overwriteOutput = True # So the script can be repeated
arcpy.env.workspace = "in_memory" # Faster have your environment in_memory
result = "in_memory/featureSetOut" # Faster to have your result in_memory

try:
selectPoly = sys.argv[1] # Argument 1, not 0, 0 is the program itself
except:
print "\nNo arguments" # Incase your testing in the command line
selectPoly = 1

arcpy.MakeFeatureLayer_management(queryFset, "lyr") # Feature layer to do work on
queryStringAll = '\"Id\" =' + str(selectPoly) # Query String with dynamic variable
print queryStringAll
selectionOption = "NEW_SELECTION" # Select by attribute option
arcpy.SelectLayerByAttribute_management("lyr", selectionOption,queryStringAll) # The actual select by attribute method

arcpy.CopyFeatures_management("lyr", result) # Put the layer in memory (see above declaration)
featSet = arcpy.FeatureSet ("featureSet") # Geoprocessing record set object arcobject.Featureset
featSet.load(result) # Put whats it memory into the feature set

arcpy.SetParameter(1,featSet) # This is confusing, its works with '1'
# Even though 1 is the sys.argv[1]
print "exited"
print arcpy.GetMessages()



Monday, November 7, 2011

ArcGIS Viewer for Flex: Creating a Custom Widget

I tried to dive right in to making my own widget for ArcGIS Viewer for Flex, but I couldn't understand all the working parts of a larger pre-built widget--So I started with a basic tutorial.
http://resources.arcgis.com/gallery/video/arcgis-api-for-flex/details?entryID=870F152C-1422-2418-A010-7C82711FE22F