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.
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.
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.
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.
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!
I'm Dave and this is my blog. I'm usually writing about .NET Software Development, ArcGIS, or Agile Practices, but other stuff does creep in from time to time. I hope you find something of use, and feel free to contact me if you have any questions. You can also check out my profile on LinkedIn
dojo.DTSAgile.com is our technology preview / demo site. As I and my team cook up cool things we post them here.
ArcDeveloper.net is a site that hosts a set of open source projects related to ArcGIS. This includes Tile Cache for .NET (TC4N) and Feature Server for .NET (FS4N). Come over and check it out!
Assembla is a free service that provides Subversion source control, wikis and work Tracking. The ArcDeveloper project is run from here. It rocks. Check them out today.
Agilistas is a LinkedIn group focused on discussing and promoting Agile practices. Everyone is welcome to join in the conversation as we evolve the process of creating software to make it more enjoyable for all involved.