How to create map grids


Summary
This article demonstrates working with map grids. Examples include creating a custom grid by code, modifying its properties and labeling, and adding it the map frame, as well creating a measured grid with a different projection and draw map grid borders.

In this topic:

Get a Map Grid programmatically

To get to a map grid programmatically, navigate to the PageLayout coclass, then use its IGraphicsContainer interface’s findFrame method to get to the Map’s MapFrame. The MapFrame coclass has an IMapGrids interface from which you can get to all the map grids for that data frame.
[Java]
public void findMapGrid(IActiveView activeView, IPageLayout pageLayout)throws
    IOException{
    IMap map = activeView.getFocusMap();
    IGraphicsContainer graphicsContainer = (IGraphicsContainer)pageLayout;
    IFrameElement frameElement = graphicsContainer.findFrame(map);
    IMapFrame mapFrame = (IMapFrame)frameElement;
    IMapGrids mapGrids = (IMapGrids)mapFrame;
    IMapGrid mapGrid = null;
    if (mapGrids.getMapGridCount() > 0){
        mapGrid = mapGrids.getMapGrid(0);
        JOptionPane.showMessageDialog(null, "Grid found.");
    }
    else{
        JOptionPane.showMessageDialog(null, "No grid found.");
    }
}

Create and Edit a Custom Grid

The following example shows how to create a custom grid by code, modify its properties and labeling, and add it the map frame. It is best to use cartographic line symbol so that the grids lines have square butts.
[Java]
public void createGrid(IActiveView activeView, IPageLayout pageLayout)throws
    IOException{
    //Create the grid 
    IMapGrid mapGrid = new Graticule();
    mapGrid.setName("Map Grid");
    //Create a color 
    IColor color = new RgbColor();
    color.setRGB(0XBBBBBB); // -> Gray

    //Set the line symbol used to draw the grid
    ICartographicLineSymbol cartographicLineSymbol = new CartographicLineSymbol();
    cartographicLineSymbol.setCap(esriLineCapStyle.esriLCSButt);
    cartographicLineSymbol.setWidth(2);
    cartographicLineSymbol.setColor(color);
    mapGrid.setLineSymbol((ILineSymbol)cartographicLineSymbol);
    mapGrid.setBorder(null); // clear the default frame border

    //Set the Tick Properties 
    mapGrid.setTickLength(15);
    cartographicLineSymbol = new CartographicLineSymbol();
    cartographicLineSymbol.setCap(esriLineCapStyle.esriLCSButt);
    cartographicLineSymbol.setWidth(1);
    cartographicLineSymbol.setColor(color);
    mapGrid.setTickLineSymbol((ILineSymbol)cartographicLineSymbol);
    mapGrid.setTickMarkSymbol(null);

    //Set the Sub Tick Properties 
    mapGrid.setSubTickCount((Short)5);
    mapGrid.setSubTickLength(10);
    cartographicLineSymbol = new CartographicLineSymbol();
    cartographicLineSymbol.setCap(esriLineCapStyle.esriLCSButt);
    cartographicLineSymbol.setWidth(0.2);
    cartographicLineSymbol.setColor(color);
    mapGrid.setSubTickLineSymbol((ILineSymbol)cartographicLineSymbol);

    // Set the Grid labels properties 
    IGridLabel gridLabel = mapGrid.getLabelFormat();
    gridLabel.setLabelOffset(15);

    //Set the Tick, SubTick, Label Visibility along the 4 sides of the grid 
    mapGrid.setTickVisibility(true, true, true, true);
    mapGrid.setSubTickVisibility(true, true, true, true);
    mapGrid.setLabelVisibility(true, true, true, true);

    //Make map grid visible, so it gets drawn when Active View is updated 
    mapGrid.setVisible(true);

    //Set the IMeasuredGrid properties 
    IMeasuredGrid measuredGrid = (IMeasuredGrid)mapGrid;
    measuredGrid.setFixedOrigin(true);
    measuredGrid.setXIntervalSize(10); //meridian interval
    measuredGrid.setXOrigin(5); //shift grid 5°
    measuredGrid.setYIntervalSize(10); //parallel interval
    measuredGrid.setYOrigin(5); //shift grid 5°

    // Add the grid to the MapFrame 
    IMap map = activeView.getFocusMap();
    IGraphicsContainer graphicsContainer = (IGraphicsContainer)pageLayout;
    IFrameElement frameElement = graphicsContainer.findFrame(map);
    IMapFrame mapFrame = (IMapFrame)frameElement;
    IMapGrids mapGrids = null;
    mapGrids = (IMapGrids)mapFrame;
    mapGrids.addMapGrid(mapGrid);

    //Refresh the view
    activeView.partialRefresh(esriViewDrawPhase.esriViewBackground, null, null);
}

Create an Index Grid

IIndexGrid gives you access to the functionality common to all index grids. Using the setXLabel and the setYLabel methods, you can set the label for each column and index in the grid. You can create an index grid as illustrated in the sample below:
[Java]
public void createIndexGrid()throws IOException{

    IIndexGrid indexGrid = new IndexGrid();

    //Set the IIndexGrid properties 
    indexGrid.setColumnCount(5);
    indexGrid.setRowCount(5);

    //Set grid label strings for the x and y axes int i = 0;
    for (int i = 0; i <= (indexGrid.getColumnCount() - 1); i++){
        indexGrid.setXLabel(i, ((Integer)i).toString());
    }
    for (int i = 0; i <= (indexGrid.getRowCount() - 1); i++){
        indexGrid.setYLabel(i, ((Integer)i).toString());
    }
}

Create a Measured Grid

To create a measured grid with a different projection, you should first create an instance of a coclass that inherits from SpatialReference. You can then set the IProjectedGrid.setSpatialReferenceByRef() method of the grid with the ISpatialReference interface of this object. The following example shows how to create a measured grid and set its properties exposed through its specific methods.
[Java]
public void createMeasuredGrid(IActiveView activeview)throws IOException{
    IMap map = activeview.getFocusMap();
    IMeasuredGrid measuredGrid = new MeasuredGrid();
    IMapGrid mapGrid = (IMapGrid)measuredGrid;

    //Set the IMeasuredGrid properties 
    //Origin coordinates and interval sizes are in map units
    measuredGrid.setFixedOrigin(true);
    measuredGrid.setUnits(map.getMapUnits());
    measuredGrid.setXIntervalSize(1000000);

    //meridian interval 
    measuredGrid.setXOrigin( - 3000000);
    measuredGrid.setYIntervalSize(1000000);

    //parallel interval 
    measuredGrid.setYOrigin( - 3000000);

    //Set the IProjectedGrid properties 
    IProjectedGrid projectedGrid = (IProjectedGrid)measuredGrid;
    projectedGrid.setSpatialReferenceByRef(map.getSpatialReference());
}

Create a Simple Map Grid Border

The ISimpleMapGridBorder interface provides access to the line symbol used to draw the grid border through the LineSymbol property. The code below illustrates how you can create a simple map grid border.
[Java]
public void createSimpleMapGridBorder()throws IOException{

    //Create the grid 
    IMapGrid mapGrid = new Graticule();
    mapGrid.setName("Map Grid");

    //Create a simple map grid border 
    ISimpleMapGridBorder simpleMapGridBorder = new SimpleMapGridBorder();

    //Set the ISimpleMapGridBorder properties
    ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbol();
    simpleLineSymbol.setStyle(esriSimpleLineStyle.esriSLSSolid);
    simpleLineSymbol.setColor(buildRGB(255, 255, 128));
    simpleLineSymbol.setWidth(2);
    simpleMapGridBorder.setLineSymbol((ILineSymbol)simpleLineSymbol);

    //Assign this border to the map grid 
    mapGrid.setBorder((IMapGridBorder)simpleMapGridBorder);
}

//method to create required RGBColor ArcObject
public IColor buildRGB(int red, int green, int blue)throws IOException{
    IRgbColor rgbColor = new RgbColor();
    rgbColor.setRed(red);
    rgbColor.setGreen(green);
    rgbColor.setBlue(blue);
    rgbColor.setUseWindowsDithering(true);
    return rgbColor;
}

Create a Calibrated Map Grid Border

You can use the ICalibratedMapGridBorder interface to set or retrieve the properties of a calibrated map grid border, such as the foreground and background color of the pattern, the interval of the pattern, the background color of the band, and the width of the border. If you want the pattern to alternate in two bands across the width of the border, set the Alternating property to True. Setting this property to False will produce a border with a single band of the pattern.
[Java]
public void createCalibratedMapGridBorder()throws IOException{
    //Create the grid 
    IMapGrid mapGrid = new Graticule();
    mapGrid.setName("Map Grid");
    //Create a calibrated map grid border
    ICalibratedMapGridBorder calibratedMapGridBorder = new CalibratedMapGridBorder();
    //Set ICalibratedMapGridBorder properties
    calibratedMapGridBorder.setBackgroundColor(buildRGB(255, 255, 255));
    calibratedMapGridBorder.setForegroundColor(buildRGB(0, 0, 0));
    calibratedMapGridBorder.setBorderWidth(10);
    calibratedMapGridBorder.setInterval(72);
    calibratedMapGridBorder.setAlternating(true); //Double alternating border
    //Assign this border to the map grid 
    mapGrid.setBorder((IMapGridBorder)calibratedMapGridBorder);
}

//method to create required RGBColor ArcObject
public IColor buildRGB(int red, int green, int blue)throws IOException{
    IRgbColor rgbColor = new RgbColor();
    rgbColor.setRed(red);
    rgbColor.setGreen(green);
    rgbColor.setBlue(blue);
    rgbColor.setUseWindowsDithering(true);
    return rgbColor;
}


See Also:

IMapGrid interface
About map grids
How to create Map Grid labels




Development licensingDeployment licensing
Engine Developer KitArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced
Engine