Automating Maps: Using ArcObjects without ArcMap
Posted by Dave Bouwman | Posted in .NET, ArcMap, ESRI | Posted on 31-01-2007
0
[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 Id cook up another simple example application that
shows how you can streamline the creation of repetitive maps. In this sample
Im 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 its based on ArcGIS 9.2.
Scenario
You work for a
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 its a
pain to close your current map, open the countys 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. Its
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 Ill 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.
Have fun!



One challenge to software developers is to keep software simple. As

