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

Monday, July 16, 2012

Constructing Polygons at 10.1

Polygon geometries are closes paths forms from a series of X,Y coordinates.  A polygon geometry object can be constructed from using an Array object containing the arcpy.Point(X,Y) values.

In this example, I will construct the extent of a feature class as a polygon to clip another datasource:
import arcpy
dsParcel = r"c:\temp\sample.gdb\Parcels"
dsLandUse = r"c:\temp\sample.gdb\LULC"


pnt = arcpy.Point()
array = arcpy.Array()
extentParcel = arcpy.Describe(dsParcel).extent
coords = [[extentParcel.XMin,extentParcel.YMin],[extentParcel.XMax,extentParcel.YMin],
                [extentParcel.XMax,extentParcel.YMax],[extentParcel.XMin,extentParcel.YMax]]
outLU = r"c:\temp\sample.gdb\clipLULC"
#  Create Spatial Reference
sr = arcpy.SpatialReference()
sr.factoryCode = 4326
sr.create()
# Load the Location
for coord in coords:
    pnt.X = coord[0]
    pnt.Y = coord[1]
    array.add(pnt)
polygon = arcpy.Polygon(array,sr)

arcpy.Clip_analysis(dsLandUse,polygon,outLU)


Notice how we didn't have the close the polygon!  The object does it for us.

Enjoy

Friday, July 13, 2012

Create a List of Geometries (10.1)

At 10.1, you can use geometries directly in geoprocessing functions like Clip or reading geometry properties.  Here is a tip to copy all the geometries into memory and use it as a list:

Example:
import arcpy
ds = r"c:\temp\sample.gdb\Parcels"
g = arcpy.Geometry()
geomList = arcpy.CopyFeatures_management(ds,g)
# Get the Area (Sq Ft)
area = 0.0
for geom in geomList:
    area += geom.area
print "Total Parcel Area: {0}".format(area)


The CopyFeatures() copies the results as a geometry object to a list and the area property is a standard Geometry object property.

Enjoy

Thursday, July 12, 2012

ArcGIS Python Window Color Scheme

If you are like me, I prefer dark backgrounds and white or bright text verse the standard white on black.

The python window color scheme can be changed by doing the following:

  1. Right click on the python window 
  2. Select Format
  3. Select 'Set Black Theme'
Now your background is black


Wednesday, July 11, 2012

ArcGIS 10 SP5 Released

Service Pack 5 has been released.

Get it while it's hot from here.

List of issues fixed here.

Enjoy

Tuesday, July 10, 2012

Insert Cursor (arcpy.da) at 10.1

The insert cursor, just like the old insert cursor object, is used to write a new row to an existing table.  When using the InserCursor object you must tell the cursor object what type of geometry you will be using.  So if your feature class is a point feature class, use "SHAPE@XY" but if it's a polygon or polyline, use "SHAPE@".  It should be noted that "SHAPE@" is valid for point features as well.

The InsertCursor has one function called insertRow(), which is a list object with field values in it.

Table Examples:

from arcpy import da
with da.InsertCursor(fc, ["name"]) as cursor:
   cursor.insertRow(["BOB SMITH"])

Feature Class Example (Point):

from arcpy import da
with da.InsertCursor(fc, ["name", "SHAPE@"]) as cursor:
   cursor.insertRow(["A Point",(-77, 34)])



Feature Class Example (Line):

from arcpy import da

array = arcpy.Array([arcpy.Point(-77, 34),
                     arcpy.Point(-77.1, 34.1),
                     arcpy.Point(-77.2, 34.2)])
polyline = arcpy.Polyline(array)

with da.InsertCursor(fc, ["name", "SHAPE@"]) as cursor:
   cursor.insertRow(["A Line", polyline])
Feature Class Example (Polygon):

from arcpy import da

array = arcpy.Array([arcpy.Point(-77, 34),
                     arcpy.Point(-77, 34.1),
                     arcpy.Point(-77.2, 34.1),
                     arcpy.Point(-77, 34)])
pg = arcpy.Polygon(array)

with da.InsertCursor(fc, ["name", "SHAPE@"]) as cursor:
   cursor.insertRow(["Polygon", pg])

Enjoy

Monday, July 9, 2012

Setup WING IDE for ArcPy 10.1

I've changed python IDE to WING.  It's a very nice IDE, but it took sometime getting use to from pydev's eclipse add on.

Here is the basic setup of WING IDE with PyLint:

1. Download easy_install: http://pypi.python.org/pypi/setuptools/#windows
2. Install easy_install
3. Go to the install location of Python 2.7.x and run 'easy_install pylint'
4. Install WING IDE
5. Start WING IDE
6. Select Tools -> PyLint
7. Right click on the PyLint window and select 'configure'
8. Edit the 'command =' to be 'command = C:\Python27\ArcGIS10.1\Scripts\pylint.bat'
9. Run pylint on a .py file

* if you get an error, try adding the full path to the python.exe in the pylint.bat file.

Enjoy