Created
April 14, 2017 16:11
-
-
Save AlexArcPy/d88802c3c788692796263544e33c47c4 to your computer and use it in GitHub Desktop.
Show or hide ArcMap map document (.mxd) map grids using comtypes and ArcObjects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| This code shows how to hide/show map grids in a map document before exporting the map. | |
| This can be handy when you have multiple grids with different grid cell size and you | |
| want to be able to control at what map scales each of the map grids should be visible. | |
| As this is not exposed via arcpy, you have to use ArcObjects. | |
| This code assumes there are two grids for the data frame with the name `small` and `large` | |
| ''' | |
| import sys | |
| from comtypes.client import GetModule, CreateObject | |
| import arcpy | |
| import arcpy.mapping as mp | |
| from snippets102 import GetStandaloneModules, InitStandalone | |
| esriCarto = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriCarto.olb") | |
| # Running this code for the first time, you would need to import | |
| #the “StandaloneModules”. Can comment out later. | |
| GetStandaloneModules() | |
| InitStandalone() | |
| mxd_path = r'C:\GIS\arcobjects\MapGridMXD.mxd' | |
| #---------------------------------------------------------------------- | |
| def export_map(show_scale): | |
| handle_grids(show_scale) | |
| mxd = mp.MapDocument(mxd_path) | |
| df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0] | |
| lyr = arcpy.mapping.ListLayers(df,'states')[0] | |
| if show_scale == 'large': | |
| arcpy.SelectLayerByAttribute_management(lyr,'NEW_SELECTION', | |
| '''STATE_NAME = 'California' ''') | |
| df.zoomToSelectedFeatures() | |
| arcpy.mapping.ExportToPNG(mxd,r'C:\GIS\out_{}.png'.format(show_scale),"PAGE_LAYOUT") | |
| #---------------------------------------------------------------------- | |
| def handle_grids(show_scale): | |
| #creating a map document object | |
| mxdObject = CreateObject(esriCarto.MapDocument, interface=esriCarto.IMapDocument) | |
| mxdObject.Open(mxd_path) | |
| #accessing the map | |
| IMap = mxdObject.ActiveView.FocusMap | |
| activeView = mxdObject.ActiveView | |
| pageLayout = activeView.QueryInterface(esriCarto.IPageLayout) | |
| graphicsContainer = pageLayout.QueryInterface(esriCarto.IGraphicsContainer) | |
| #IFrameElement | |
| frameElement = graphicsContainer.FindFrame(IMap) | |
| #IMapFrame | |
| mapFrame = frameElement.QueryInterface(esriCarto.IMapFrame) | |
| #IMapGrids | |
| mapGrids = mapFrame.QueryInterface(esriCarto.IMapGrids) | |
| [mapGrids.MapGrid(i) for i in xrange(mapGrids.MapGridCount)] | |
| grids = [mapGrids.MapGrid(i) for i in xrange(mapGrids.MapGridCount)] | |
| for g in grids: | |
| print g.Name | |
| if show_scale.lower() in g.Name.lower(): | |
| g.Visible = True | |
| else: | |
| g.Visible = False | |
| for g in grids: | |
| print g.Name, g.Visible | |
| #or perhaps better to create copies | |
| mxdObject.Save() | |
| export_map('large') | |
| export_map('small') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment