Access to ArcMap map document grid using Python and ArcObjects

When automating mapping production and exporting your map document layouts, you may need to manage map grids. They are grids or graticules which show the network of latitude and longitude lines in the layout.

For the very least, you would like to hide/show map grids in a map document’s data frame before exporting the map. The reason for this is that if your grid cell is of 1×1 km size, when exporting the map layout being in a small extent (for instance, 1: 1,000,000 you will get a pretty much no map except a black image because the grid will take all the place.

Being able to access map grids can also be handy when you have multiple grids with different grid cell sizes and you want to be able to control at what map scales each of the map grids should be visible.

Unfortunately, you cannot use arcpy to access grids defined for a data frame. However, the grids are exposed via an ArcGIS extension called Production Mapping which according to the Esri Help page

streamlines your GIS data and map production by providing tools that facilitate data creation, maintenance, and validation, as well as tools for producing high-quality cartographic products.

You might be surprised to learn this, but Production Mapping extension has its own arcpy-like site-package called arcpyproduction. Within this package, there is a class Grid class which provides access to grid properties for a grids and graticules layer. However, if you don’t have Production Mapping extension (it might be hard to justify buying this extension if you are not involved in heavy map production), as map grids are not exposed via arcpy, you have to use ArcObjects.

Since many would like to automate showing/hiding map grids when exporting map layouts, I’ve written a tiny function which can do that using Python. I have blogged earlier about how to start using ArcObjects from Python, so make sure to read this post first. Using this code, you will be able to choose which map grid you would like to use (if you have multiple grids with different cell size defined) when exporting a map layout.

8 thoughts on “Access to ArcMap map document grid using Python and ArcObjects

  1. Great article! just to add you can add the following line of code to change the intervals, saves you creating multiple references

    grids = [mapGrids.MapGrid(i) for i in xrange(mapGrids.MapGridCount)]
    for g in grids:
    properties = g.QueryInterface(esriCarto.IMeasuredGrid)
    properties.XIntervalSize = XIntervalSize

    1. Hey Ryan, thanks for sharing this. Very handy. However, I think many users would still rather design multiple graticules in the data frame tuning all the properties. However, just changing the interval would definitely work for someone who doesn’t need custom properties set beforehand. Cheers.

  2. Hi Alex,
    Very interesting! I’m just starting out with using Python to automate map production. Scales and grids/graticules are a big issue for me, so I was very excited to find your articles. However ArcGIS 10.6 won’t play the game! Do you have any updates on comtypes and snippets for 10.6?

    1. Hey Chris! 10.6 worked fine for me with comtypes and snippets. Just replace `10.x` with the `10.6` in module. If you stuck – please post a question on GIS.SE – I’d be pleased to help!

      1. Thanks Alex. I keep getting ‘AttributeError’, so I’ll put together a post on the problem. I don’t have a programming background so the stuff you and others put on forums is so helpful, I wouldn’t have got this far without it. Much appreciated.

  3. Update! Have got it working! Missed a couple of important points in the comments in This is very exciting. Thanks Alex.

Leave a Reply to Chris Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s