Thursday, September 27, 2012

Using Map() or Apply_async() For Multiprocessing


With the release of this new python article on the ArcGIS blogs.  I find it increasingly harder to find concrete/simple explanations of important functions that help make life easier for processing data in ArcGIS.
Though the crux of this article is to give guidance to when or when not to use multiprocessing with ArcGIS, it does use a variation on a method of multiprocessing different than I prefer.  So I decided to investigate the differences between map() and apply_async() on the Pool object.
Pool.apply_async() is also like Python's built-in apply(), except that the call returns immediately instead of waiting for the result. An ApplyResult object is returned. You call its get() method to retrieve the result of the function call. The get() method blocks until the function is completed. Apply_async() does not preserve the order of the results meaning that the codes return results will not be a 1:1 correlation between when it was called and when it finished.  In addition, apply_async() allows coders to call multiple functions instead of a single function.
Pool.map() is like the Python's built-in map() function.  Pool.map() applies the same function to many arguments. The results are returned in an order corresponding to the order of the arguments.

When it comes right down to it, you basically need to find the right tool for the right job.

Here are some resources to help you along.

Enjoy

Monday, September 24, 2012

Delete Rows Using the UpdateCursor - arcpy.da

The arcpy.da.UpdateCursor() provides the method to alter and update field values.  This quick post will show you how to delete a row.

All you need to do is specify the fields, here I take the default of * and the feature class.  You can reduce the number of rows by passing a query as well.

Code Sample Run From Python Window:

with da.UpdateCursor(r'C:\temp\scratch.gdb\floodsub', "*") as urows:
...     for row in urows:
...         urows.deleteRow()
...         break


This little sample just erases the first row in the feature class.  The 'with' statement automatically closes the cursor object, so it does not have to be manually deleted.  This ensures the locks are removed on success or on failure.

Enjoy

Friday, September 21, 2012

Uninstalling Time!

The funny things you see when upgrading versions of software:

Installers always were lousy estimators of time, but this one was way off!!!! I guess I need two cups of coffee for this one.

Enjoy

Friday, September 7, 2012

Article on 3rd Party Modules

The Esri python team wrote another good article on 3rd party modules, which is a good read for anyone who is new to python.

The article can be found here.

Enjoy

Tuesday, August 28, 2012

Tutorial - How to add built-in commands to your custom toolbar or menu

Esri posted a great blog post on building an add-in using python.  You can find the information here.

Enjoy


Friday, July 20, 2012

The Tile Package

What is it?
New at 10.1, tile packages are compressed files that contain a map document's cached data.  The tile package or .tpk is ideal for disconnected use and for sharing information to ArcGIS Online.
Why Should I Care?
It makes sharing cache easy, and you can create custom caches on the fly.  From a geoprocessing/application view of things, this means you can get your data out to mobile users without needing an air card.  It also produces an easy way to share cache from server to server or AGOL.

Other reasons include:
  • Improved rendering performance
  • Improved quality
  • Follows industry standards (AGOL, Google, Bing, etc..)

You can create a package using geoprocessing as follows:
import os
import arcpy
from arcpy import env
# Set environment settings
env.overwriteOutput = True
env.workspace = "C:/Tilepackages/"
# Loop through the workspace, find all the mxds and create a tile package using the same name as the mxd
for mxd in arcpy.ListFiles("*.mxd"):
    print "Packaging " + mxd
    arcpy.CreateMapTilePackage_management(mxd, "ONLINE",
                                       os.path.splitext(mxd)[0] + '.tpk',   "PNG8", "10")
Pretty easy to create.  It should be noted that your ArcMap Document's extent determines the area to be processed.  You can embed web service layers as well as local data.  The cached is fused together and it means you cannot query the base data as well.

Have packaging


Tuesday, July 17, 2012

Create a Map Service Layer (10.1)

At 10.1, you can use JSON to create maps and add map services to that layer.  This means you can create map service layer indirectly.  There is no direct way to create a map service layer in 10.1, but by using the ConvertWebMapToMapDocument() you can.  Information about the function can be found here.


ConvertWebMapToMapDocument() described in the help is as follows:


Converts a web map (in JSON format) that you intend to print or export to a map document. The map document can be further modified before finally being printed or exported.


You notice that the help does not directly mention the creation of the map service layer, but when you drill down into the supported JSON syntax, you can see how you can use this method to generate your map service layer.


A JSON map consists of the following:
{ 
   "mapOptions": {}, 
   "operationalLayers": [], 
   "baseMap": [], 
   "exportOptions": {}, 
   "layoutOptions": {} 
}

Using the JSON library and dictionaries, you can easily match this structure. As shown below:
import json
from arcpy import mapping
mapService = {} 
operationalLayer = [] 
layer = {} 
layer["id"] = str(webmapID) 
layer["url"] = str(url) 
layer["title"] = str(title) 
layer["opacity"] = str(opacity) 
layer["visibility"] = str(visibility) 
layer["minScale"] = str(minScale) 
layer["maxScale"]= str(maxScale) 
operationalLayer.append(layer) 
mapService["operationalLayers"] = operationalLayer 
jsonDump = json.dumps(mapService) 
result = mapping.ConvertWebMapToMapDocument(jsonDump)

Now we have a MapDocument object that has our map service layer in it.  Using the other rmapping module functions, loop through the TOC and save the layer to disk.

mxd = result.mapDocument
layers = mapping.ListLayers(mxd)
for layer in layers:
    layer.saveACopy(saveLayer)
    break

That's it, now we have create a map service layer saved to disk.  If you have to deal with security, you can add a token to your operational layer.


Enjoy