Tuesday, December 18, 2012

Using env.addOutputsToMap

The environmental variable addOutputsToMap prevents geoprocessing task results from being displayed in the TOC.  This environmental property by default is set to true, so every tool's result will display in the table of contents in ArcMap.

When developing python add-ins, you should utilize this tool to prevent sub-processes results from being added to the map.  A good example of this, is if your python add-in creates a table if it doesn't exist for logging purposes.  Your end user does not want to see this table, or know it exists, but if addOutputsToMap is set to true, the value will display in the table of contents.  Changing it to false would prevent the data from showing.

Example:
from arcpy import env
env.addOutputsToMap = false
#... perform GP task...
env.addOutputsToMap = true

Enjoy

Monday, December 17, 2012

Finding Distance Along A Line

The arcpy polyline geometry object has a very helpful method called positionAlongLine (value, {use_percentage}).  This function finds either a distance (in the feature's coordinate system) or the percentage from the line's starting location.  It then returns a PointGeometry object that can be used for further analysis, or that can be saved out as a feature class using the CopyFeatures tool.  If you use the optional use_percentages, false means it's a distance, true means it is a percentage with values between 1-100.  If a value is great than the length or over 100 % then the last point is used.  If values are less than 0 or less than 0% then the first point is used as well.

Assuming you have a line feature class, just create a search cursor and begin accessing your locations.  

fc = r"c:\temp\lines.shp" 
with da.SearchCursor(fc, ["SHAPE@"] as cursor:
    for row in cursor:
       print row[0].positionAlongLine(10, True)    
       print row[0].positionAlongLine(10, False)
Enjoy

Wednesday, December 12, 2012

Finding and Changing Max Value in Numpy Array

Today, I answered a forum question about finding and changing a numpy array.  You can use the max() and the where() in the numpy to search for values in your array and change them using loops.

You can download a very simple example here.

Here is the simple code:

import numpy
myarray = numpy.array([[  5.4,   7.5,   2.2,   8.5,   8.6,   7.5],
                       [  7.7,   3.5,   1.4,   9.6,   8.5,   5.5],
                       [  5.2,   6.1,   8.6,   6.7,   4.3 ,  6.8],
                       [  9.6,   4.5,   2.7,   3.6,   6.7,   4.5],
                       [  1.2,   2.3,   7.2,   6.3,   2.2,   2.0 ],
                       [  1.3,   2.0,  -99.0,    9.6,  -99.0,    1.2]
                       ]
                      )
print myarray
maxValue = myarray.max()

itemindex = numpy.where(myarray >= maxValue)

for i in xrange(0, len(itemindex[0])):
    array1 = itemindex[0]
    array2 = itemindex[1]
    myarray[array1[i]][array2[i]] = 0

print myarray




Here we have the numpy array and find the array's maximum value.  Then using the where(), the location in the array are given back.  From there, the code then changes the values in the 2-D array from the maximum value back to zero.

Enjoy

Thursday, December 6, 2012

ArcPy Sample: Zipping a Shapefile

I've decided to post full code samples on github.  Here is my first sample to zip shapefiles.

Enjoy

Tuesday, December 4, 2012

AGS 10.1 Quirk with Windows Domain Security

Today I configured a test machine to use windows domain accounts at the GIS tier for security.  When I went to connect to the site using catalog, I noticed that if I included the '\' (backslash) in the user name it could not connect.  After removing the backslash, the connection worked.

This means that if your user is 'domainname\name' you would use just the 'name' in the AGS connection string.


Monday, December 3, 2012

Truncate Table, A New Way To Erase Rows

Truncate table is the 10.1 way to erase ALL the rows in a table or feature class.  It will ignore any sql query, so use this tool carefully.

Some Notes:

  • Supported data types are simple, meaning no terrains, topologies, etc..
  • Data must be unversioned to execute
  • After rows are removed, the data is unrecoverable, so make sure you know what you are doing before you go and use this.

This is a good tool for workflows where you need to clear out rows on some regular basis.

Usage Example:
import arcpy
from arcpy import env
wrksp = r"c:\temp\data.gdb"
env.workspace = wrksp
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
   arcpy.TruncateTable_management(fc)
del fcs


Enjoy