小菜的ArcObjects学习之路------Map Grids的使用

来源:互联网 发布:电子商务数据分析前景 编辑:程序博客网 时间:2024/05/17 23:23

How to create map grids

Getting a map grid programmatically
1:To get to a map grid programmatically, navigate to the PageLayout coclass.
2:Use its IGraphicsContainer interface's FindFrame method to get to the map's MapFrame. 
The MapFrame coclass has an IMapGrids interface where you can get to all map grids for that data frame. See the following code example:
[C#]
public void FindMapGrid(IActiveView activeView, IPageLayout pageLayout)
{
    IMap map = activeView.FocusMap;
    IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer;
    IFrameElement frameElement = graphicsContainer.FindFrame(map);
    IMapFrame mapFrame = frameElement as IMapFrame;
    IMapGrids mapGrids = mapFrame as IMapGrids;

    IMapGrid mapGrid = null;
    if (mapGrids.MapGridCount > 0)
    {
        mapGrid = mapGrids.get_MapGrid(0);
    }
    else
    {
        MessageBox.Show("No grid found.");
    }
}

Creating and editing a custom grid
The following code example shows how to create a custom grid by code, modify its properties and labeling, and add it to the map frame. Use 
cartographic line symbols so the grids lines have square butts.
[C#]
public void CreateGrid(IActiveView activeView, IPageLayout pageLayout)
{
    //Create the grid.
    IMapGrid mapGrid = new GraticuleClass();
    mapGrid.Name = "Map Grid";

    //Create a color.
    IColor color = new RgbColorClass();
    color.RGB = 0XBBBBBB; // -> Gray.

    //Set the line symbol used to draw the grid.
    ICartographicLineSymbol cartographicLineSymbol = new CartographicLineSymbolClass
        ();
    cartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
    cartographicLineSymbol.Width = 2;
    cartographicLineSymbol.Color = color;
    mapGrid.LineSymbol = cartographicLineSymbol as ILineSymbol;
    mapGrid.Border = null; // Clear the default frame border.

    //Set the Tick properties.
    mapGrid.TickLength = 15;
    cartographicLineSymbol = new CartographicLineSymbolClass();
    cartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
    cartographicLineSymbol.Width = 1;
    cartographicLineSymbol.Color = color;
    mapGrid.TickLineSymbol = cartographicLineSymbol as ILineSymbol;
    mapGrid.TickMarkSymbol = null;

    //Set the SubTick properties.
    mapGrid.SubTickCount = 5;
    mapGrid.SubTickLength = 10;
    cartographicLineSymbol = new CartographicLineSymbolClass();
    cartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
    cartographicLineSymbol.Width = 0.2;
    cartographicLineSymbol.Color = color;
    mapGrid.SubTickLineSymbol = cartographicLineSymbol as ILineSymbol;

    // Set the Grid label properties.
    IGridLabel gridLabel = mapGrid.LabelFormat;
    gridLabel.LabelOffset = 15;

    //Set the Tick, SubTick, and Label Visibility along the four sides of the grid.
    mapGrid.SetTickVisibility(true, true, true, true);
    mapGrid.SetSubTickVisibility(true, true, true, true);
    mapGrid.SetLabelVisibility(true, true, true, true);

    //Make the map grid visible so it gets drawn when Active View is updated.
    mapGrid.Visible = true;

    //Set the IMeasuredGrid properties.
    IMeasuredGrid measuredGrid = mapGrid as IMeasuredGrid;
    measuredGrid.FixedOrigin = true;
    measuredGrid.XIntervalSize = 10; 
    measuredGrid.XOrigin = 5; //Shift grid 5°.
    measuredGrid.YIntervalSize = 10; 
    measuredGrid.YOrigin = 5; //Shift grid 5°.

    // Add the grid to the MapFrame.
    IMap map = activeView.FocusMap;
    IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer;
    IFrameElement frameElement = graphicsContainer.FindFrame(map);
    IMapFrame mapFrame = frameElement as IMapFrame;
    IMapGrids mapGrids = null;
    mapGrids = mapFrame as IMapGrids;
    mapGrids.AddMapGrid(mapGrid);

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

Creating an index grid

IIndexGrid gives you access to the functionality common to all index grids. Using the XLabel and the YLabel properties, set or retrieve the label for each column and index in the grid. You can create an index grid as shown in the following code example: 

[C#]
publicvoid CreateIndexGrid()
{
    IIndexGrid indexGrid = new IndexGridClass();

    //Set the IIndexGrid properties.
    indexGrid.ColumnCount = 5;
    indexGrid.RowCount = 5;

    //Set grid label strings for the x,y axes.int i = 0;
    for (i = 0; i <= (indexGrid.ColumnCount - 1); i++)
    {
        indexGrid.set_XLabel(i, i.ToString());
    }

    for (i = 0; i <= (indexGrid.RowCount - 1); i++)
    {
        indexGrid.set_YLabel(i, i.ToString());
    }
}



通过编程获取地图格网
MapFrame组件类实现了IMapGrids接口,那样你可以通过它获取该数据框架的全部地图格网,实例代码如下:
[C#]
public void FindMapGrid(IActiveView activeView, IPageLayout pageLayout)
{
    IMap map = activeView.FocusMap;
    IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer;
    IFrameElement frameElement = graphicsContainer.FindFrame(map);
    IMapFrame mapFrame = frameElement as IMapFrame;
    IMapGrids mapGrids = mapFrame as IMapGrids;

    IMapGrid mapGrid = null;
    if (mapGrids.MapGridCount > 0)
    {
        mapGrid = mapGrids.get_MapGrid(0);
    }
    else
    {
        MessageBox.Show("No grid found.");
    }
}

创建和编辑普通地图格网
以下代码将演示如何以编程方式创建地图格网,修改他的属性和标签。添加到地图框架,使用制图线符号所以地图格网有方头
public void CreateGrid(IActiveView activeView, IPageLayout pageLayout)
{
    // 创建格网
    IMapGrid mapGrid = new GraticuleClass();
    mapGrid.Name = "Map Grid";

    // 创建颜色
    IColor color = new RgbColorClass();
    color.RGB = 0XBBBBBB; 

    //  设置线符号,用于绘制格网
    ICartographicLineSymbol cartographicLineSymbol = new CartographicLineSymbolClass();
    cartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
    cartographicLineSymbol.Width = 2;
    cartographicLineSymbol.Color = color;
    mapGrid.LineSymbol = cartographicLineSymbol as ILineSymbol;
    mapGrid.Border = null; // 去除格网边界

    //Set the Tick properties.
    mapGrid.TickLength = 15;
    cartographicLineSymbol = new CartographicLineSymbolClass();
    cartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
    cartographicLineSymbol.Width = 1;
    cartographicLineSymbol.Color = color;
    mapGrid.TickLineSymbol = cartographicLineSymbol as ILineSymbol;
    mapGrid.TickMarkSymbol = null;

    //Set the SubTick properties.
    mapGrid.SubTickCount = 5;
    mapGrid.SubTickLength = 10;
    cartographicLineSymbol = new CartographicLineSymbolClass();
    cartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
    cartographicLineSymbol.Width = 0.2;
    cartographicLineSymbol.Color = color;
    mapGrid.SubTickLineSymbol = cartographicLineSymbol as ILineSymbol;

    // Set the Grid label properties.
    IGridLabel gridLabel = mapGrid.LabelFormat;
    gridLabel.LabelOffset = 15;

    //Set the Tick, SubTick, and Label Visibility along the four sides of the grid.
    mapGrid.SetTickVisibility(true, true, true, true);
    mapGrid.SetSubTickVisibility(true, true, true, true);
    mapGrid.SetLabelVisibility(true, true, true, true);

    //Make the map grid visible so it gets drawn when Active View is updated.
    mapGrid.Visible = true;

    //Set the IMeasuredGrid properties.
    IMeasuredGrid measuredGrid = mapGrid as IMeasuredGrid;
    // FixedOrigin属性设置为true,指示原点坐标从XOrigin和YOrigin获取
    measuredGrid.FixedOrigin = true;
    measuredGrid.XIntervalSize = 10; //Meridian interval.
    measuredGrid.XOrigin = 5; //Shift grid 5°.
    measuredGrid.YIntervalSize = 10; 
    measuredGrid.YOrigin = 5; //Shift grid 5°.

    // 将地图格网添加到数据框架
    IMap map = activeView.FocusMap;
    IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer;
    IFrameElement frameElement = graphicsContainer.FindFrame(map);
    IMapFrame mapFrame = frameElement as IMapFrame;
    IMapGrids mapGrids = null;
    mapGrids = mapFrame as IMapGrids;
    mapGrids.AddMapGrid(mapGrid);

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