Python

the post relates to Python Programming

Scratch Workspace

A short script with a few inputs and outputs is one thing but when you have a script that performs a series of operations, some outputs are bound to be temporary intermediate outputs. For this reason, I wanted to explore using the scratch workspace in the general environment when formulating a large python script for ArcMap.
I finally found some useful information showing that you can output temporary feature layers like this ...

TMP_OUTPUT = "%SCRATCHWORKSPACE%\\OUTPUT1"

This works just fine for me and when I'm done I'll typically do this sort of thing to clean up the mess.

if arcpy.Exists(TMP_OUTPUT):
     arcpy.Delete_management(TMP_OUTPUT)

Feature Datasets and Spatial Reference

Like most things on my GIS blog, this serves to remind me of something. I'm a little late getting on board with the Geodatabase but more and more I'm adopting it for everything that I do (especially when working with ArcGIS 10.1). Not only have I finally started using the File Geodatabase but I recently started having output put into different Feature Datasets within the Geodatabase.

One thing that I need to remember is that you can't name two feature classes in two different feature dataset with the same name. It would be convenient if you could but it will scream at you that “it already exists”. You can argue that it does not but in the mind of ArcGIS ... it does.

Field Mappings - Multiple merge rules with a Spatial Join

I googled the title of this blog entry several ways and couldn't find any useful information so I wanted to post it here. I had the need to do a spatial join (in a script) but the tool in the toolbox doesn't give you the ability to get all the useful statistics (mean, max, min, sum, standard deviation) that you get by checking check-boxes doing a spatial join by right-clicking in the ArcMap table of contents.

MakeFeatureLayer and SelectByLocation_management()

I need to always remember this when I see the error that says “The value cannot be a feature class”. The difference between a feature layer and a feature class seems sort of nebulous to me. As long as I remember to convert a feature class to a feature layer first before doing the selection, it will work fine. For example ...


gp.MakeFeatureLayer_management("some_fc", "some_fc_Make")
gp.SelectLayerByAttribute_management("some_fc_Make", "NEW_SELECTION", "GRIDCODE = 2")

Oh and this is necessary for Select By Attribute AND Select By Location.

This is also important when using CopyFeatures. The help says this about CopyFeatures.

execfile - saving me time

I'm continuing to test python scripts in version 10 with arcpy . When a long script encounters an error, I have been re-running it from the beginning. I came across the execfile command (knowing there must be a way to restart the script) and it has been very useful for me in picking up where I left off. I can look at the spot where things stop, and run this command in the Python Shell and I'm back in business.

The syntax is simply this … execfile(“path_and_filename.py”)

Unable to Merge Rasters programmatically with ArcGIS 10

Ok to add to my disdain over the way the spatial analyst toolbar was stripped down ... I know cannot figure out how to do a simple merge (rasters) with spatial analyst and the Raster Calculator command (in the Map Algebra Toolbox). This is something I do all the time when using 9.3. I am re-writing a script and gave up on this so now I'm trying their recommended way of doing this. This recommendation is the "improved" (their words not mine) "Mosaic to New Raster" tool.

Now I'm getting a new error (000157: Input and target dataset should have the same number of bands). My problem with this error is that the input and target datasets do have the same number of bands. To explain this problem, I found this.

MakeTableView_management

I just posted a note to the ESRI Web Team that their documentation on the old 9.3 product (I'm still using 9.3.1) had an error. Lets see if they update it. I've been using MakeTableView_management in Python which is necessary for doing a join (long story; see below) and is also apparently handy for renaming fields which is another purpose that I have for the tool. The documentation in the software and online is incorrect with respect to the field_info argument. The script example says ...

gp.maketableview "nf_roads", "TCH", "[ROAD_CLASS] = 'TCH'", "LEVEL ROAD_CODE VISIBLE, NAME NAME HIDDEN"

The Power of the Calculate Tool

Just a little curiosity I discovered today. I've been working with a Python script that has some problems calculating a field using an expression. I figured out that the problem was the syntax used in the expression is treating a variable as if it were a field in the feature class (but it is not a field). This was obvious to me because the variable in the expression is surrounded by Python decode field characters (the exclamation mark).

Anyway my search landed me on this page.

Syndicate content