Tuesday, January 30, 2007
Posted on Tuesday, January 30, 2007 9:40:56 PM (Mountain Standard Time, UTC-07:00)  Comments [2] | 
Categories: .NET | ArcMap | ESRI
[UPDATE: 1/31/07 I got a note from a member of the ArcMap team who indicated that when using the IActiveView interface on a MapDocument object, you need to call IActiveView.Activate. The sample has been updated to reflect this, and you can read more about this in the ArcObjects documentation for IActiveView.Activate]

Thought I’d cook up another simple example application that shows how you can streamline the creation of repetitive maps. In this sample I’m going to create either a PDF of the map layout, or a JPEG of the map. This sample also shows another use of the configuration section handlers I posted recently. It's written in VB.NET, and it’s based on ArcGIS 9.2.

Scenario

You work for a county GIS department and every day people come in, and ask for maps of their property. These are very simple maps – basically just the parcel outline with other base data. Since you are an industrious person, you always have ArcMap open doing some other work, and it’s a pain to close your current map, open the county’s standard Parcel Map mxd, zoom to the parcel and export the map. You want a simpler way to do this.

IMapDocument & MapDocument Class

This is the central point for getting things started. The MapDocument Class can be instantiated and used to load a map document. It’s worth noting that this happens much faster than opening ArcMap – the whole thing – open map, select feature, zoom to it, and export to PDF takes ~ 1 second. Time savings of not watching ArcMap spin up could more than pay for the effort to customize this sample!

'Load up a map into an IMapDocument

 Dim mapDoc As IMapDocument = New MapDocument

 mapDoc.Open(myMxdFile)

Once you have the document, the ArcObjects code needed to zoom to a particular area is very simple. The export to an image is somewhat complex, but the sample wraps that up in the ExportMapToImage and ExportPDF methods.

Sample Solution

The sample solution has two projects – the MapMaker class library that contains the configuration handler, and the code that actually exports the maps, and a simple WinForms app that can drive the class library, Sample data and a MXD file is included in the zip file. All you need to change to get this to run is set the path to the mxd file in the app.config of the MapMakerForm project.

 

MapMaker Class Library

The MapMaker Class library really only has one class of signifigance - the MapMaker itself. Since I like to make things flexible, it also has a configuration section handler, which is used to define the various map files that the class can work with. These are referred to as “MapMakerServices”. The class model is below

A “service” has a name, an mxd file, the layer the query will be run on, and the field that the "where clause" in the query will be applied. The following shows the app.config section for the sample.

  <mapmakerservices tempfolder="G:\temp">

    <service name="ParcelMap" mxdfile="G:\SVN\Research\Automation\Data\parcelmap.mxd" querylayer="Parcels" queryfield="pidn"/>

  </mapmakerservices>

By having a config section, you can cook up a simple application that can access a wide array of map files, without having to change the actual implementation at all. Although there is only one service defined in the sample, the list of MapMakerServices is bound to a combo box.

Functionality

Basically, you pass in a whereclause, the height and width of the map you want, the type of output (JPEG or PDF) and an output file. The MapMaker will then locate the layer specified in the service, query it for a matching feature, zoom to it, and create the map.

 

The data that comes with the sample is of parcels. So the usage in the sample is that the user passed in a PIN, the MapMaker locates and zooms to the parcel, and then exports a map image. The layout in the sample is plain as can be, but I’ll leave the cartography up to you.

 

Anyhow – this is a super simple example of how to automate ArcMap map creation without actually firing up ArcMap.

Download the source code (2.25Mb includes sample data)

Have fun!

Wednesday, January 31, 2007 9:04:08 AM (Mountain Standard Time, UTC-07:00)
What a great topic for a talk at the Fort Collins ArcDeveloper's group. :-)
Rich Ruh
Tuesday, February 06, 2007 1:22:08 PM (Mountain Standard Time, UTC-07:00)
Hello Dave,

This is very interesting as we have been working on automating just such a system over the past few months. Some bugs have got in our way, such as trying to set the scale correctly after zooming to the location.

Keep up the good work, your block is very informative.

Bryan
Bryan Waller
Comments are closed.