3.2 地圖布局
3.2.1 PageLayout對象
PageLayout用於顯示地圖數據,並通過對地圖數據進行整飾以便對地圖打印輸出滿足不同行業對GIS出圖功能的需求。
PageLayout和Map這兩個對象看起來非常相似,他們都是視圖對象,可以顯示地圖;也都是圖形元素的容器,可以容納圖形元素(Graphics Element)
區別:PageLayout除了保存圖形元素外,還可以保存諸如MapFrame的框架元素(Frame Element)。
PageLayout控件上的Map對象被PageLayout的MapFrame對象所管理
PageLayout 類主要實現了IPageLayout接口,它定義了用於修改頁面板式(layout)的方法和屬性
IPageLayout的ZoomToWhole方法可以讓PageLayout以最大尺寸顯示;
IPageLayout 的ZoomToPercent方法可以按照輸入的比例顯示;
IPageLayout的ZoomToWidth方法可以讓視圖顯示的范圍匹配控件對象的寬度
IPageLayout的Page屬性用以獲取Page對象
IPageLayout的RulerSettings屬性用以獲取RulerSettings對象
IPageLayout的HorizontalSnapGuides和VerticalSnapGuides屬性可以獲取SnapGuides對象
3.2.2 Page對象
主要用來管理PageLayout對象中的頁面,它用來加載地理數據,但不提供分析和查詢功能。
Page類的主要接口是IPage,它除了用於管理Page的顏色、尺寸和方向,還可以管理邊框類型和打印區域。FromID可以設置紙張大小類型
3.2.3 SnapGrid對象
是PageLayout上用於擺放元素而設置的輔助點。
以下代碼片段演示 如何 設置PageLayout控件上的ISnapGrid

3.2.4 SnapGuides對象
SnapGuides對象,是為了更好的放置地圖,而在PageLayout上設置的輔助線;同樣分為 水平和垂直兩種。
ISnapGuides 的AreVisible設定SnapGuides是否可見。
ISnapGuides 的GuideCount屬性返回一個SnapGuides對象中的Guide的個數。
而使用ISnapGuides的Guide屬性可以按索引值獲得某個具體的Guide對象。
ISnapGuides的AdGuide方法將一個Guide放在指定位置上。
ISnapGuides的RemoveAllGuides和RemoveGuide方法分別可以清除所有的Guide和按索引值清除Guide。
以下代碼片段演示如何為PageLayout對象添加輔助線:

3.2.5 RulerSettings 對象
標尺對象,它是為了輔助圖形元素的放置位置而出現在Pagelayout對象上方和左方的輔助尺;通過IPageLayout的RulerSetting屬性可以獲得PageLayout上的RulerSetting對象;IRulerSetting接口的SmallestDivision屬性用於設置標尺對象的最小刻度值屬性。
3.2.6 Element 對象
在Map對象和Pagelayout對象顯示的數據,除了地理數據外,還有 元素數據Element
Element 是一個非常龐大復雜的對象集合:a 圖形元素(GraphicElement)和框架元素(FrameElement)
圖形元素包括:LineElement,MarkerElement,TextElemet,GroupElement,FillshapElement,PictureElement,MultiPatchElement等
這些元素都是以圖形的形式存在,在地圖視圖或者PageLayout視圖上是可見的。
框架元素包括:MapFrameElement(地圖框架元素),MapSurroundElement(地圖環繞元素) 等,它們是作為不可見的容器存在的
Map對象或者Pagelayout對象可以通過IGraphicsContainer 接口來管理這些元素,
使用IGraphicsContainer接口可以添加、刪除和更新位於Map或Pagelayout上的元素
使用GroupElement對象還可以將多個元素編組為單個實體來給用戶使用
IElement是所有圖形元素和框架元素都實現的接口,通過IElement接口可以確定Element對象的Geometry屬性;
同時IElement接口也提供了用於查找和繪制元素的方法。
注意:IElement 與 ILineElement 以及 ITextElement等 不是父子關系,后者沒有Geometry屬性
3.2.7 MapGrid 對象
它是布局視圖中的一系列參考線和參考點,用來幫助地圖使用者快速的確定地圖要素的位置。
公里格網,MapGridBorder,MapGridLabel,MapGrid等
MapGrid由MapGrids來管理,一個 MapGrids可以包含多個MapGrid
MapGrid是一個抽象類,它的子類有MeasuredGrid,IndexGrid,MgrsGrid,Graticule和CustomOverlayGrid五種;
這些子類的對象由MapGridFactory對象創建。

1 /// <summary> 2 /// 為Pagelayout對象添加格網對象 3 /// </summary> 4 /// <param name="pPageLayout"></param> 5 public void AddMeasuredGridToPageLayout(IPageLayout pPageLayout) 6 { 7 try 8 { 9 // 獲取MapFrame對象 10 IActiveView pActiveView = pPageLayout as IActiveView; 11 IMap pMap = pActiveView.FocusMap; 12 // QI為IGraphicsContainer對象,來管理Pagelayout上的元素 13 IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer; 14 // 從IGraphicsContainer對象中找到指定地圖的框架元素 15 IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame; 16 // 將框架元素QI為IMapGrids 17 IMapGrids pMapGrids = pMapFrame as IMapGrids; 18 19 // 自此,我們得到了Pagelayout對象的 參考(參考線和參考點),接下來就開始整它 20 21 // 創建一個MeasureGrid對象 22 IMeasuredGrid pMeasureGrid = new MeasuredGridClass(); 23 IMapGrid pMapGrid = pMeasureGrid as IMapGrid; 24 pMeasureGrid.FixedOrigin = true;// 固定源 25 pMeasureGrid.Units = pMap.MapUnits;// 設置測量格網的單位為地圖的單位 26 // 設置間隔尺寸 27 pMeasureGrid.XIntervalSize = 100; 28 pMeasureGrid.YIntervalSize = 100; 29 // 設置原點位置? 30 pMeasureGrid.XOrigin = -180; 31 pMeasureGrid.YOrigin = -90; 32 // 設置MeasuredGride投影屬性 33 IProjectedGrid pProGrid = pMeasureGrid as IProjectedGrid; 34 pProGrid.SpatialReference = pMap.SpatialReference; 35 pMapGrid.Name = "Measured Grid"; 36 // 創建一個CalibratedMapGridBorder對象(參考邊界校准)並設置為pMapGrid的Border屬性 37 ICalibratedMapGridBorder pCalibratedBorder = new CalibratedMapGridBorderClass(); 38 pCalibratedBorder.BackgroundColor = GetRgbColor(255,255,255); 39 pCalibratedBorder.ForegroundColor = GetRgbColor(0, 0, 0); 40 pCalibratedBorder.BorderWidth = 0.1; 41 pCalibratedBorder.Interval = 72; 42 pCalibratedBorder.Alternating = true; 43 pMapGrid.Border = pCalibratedBorder as IMapGridBorder; 44 45 // 創建一個FormattedGridLabel對象(格式化參考標簽) 46 IFormattedGridLabel pFormattedGridLabel = new FormattedGridLabelClass(); 47 IGridLabel pGridLabel = pFormattedGridLabel as IGridLabel; 48 49 // 設置標簽字體,需要引用類庫 50 //stdole.StdFont pFont = new stdole.StdFont(); 51 //pFont.Name = "Arial"; 52 //pFont.Size = 6; 53 //pGridLabel.Font = pFont as stdole.IFontDisp; 54 55 pGridLabel.Color = GetRgbColor(0, 0, 0); 56 pGridLabel.LabelOffset = 4; 57 pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisLeft, false); 58 pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisRight, false); 59 60 INumericFormat pNumericFormat = new NumericFormatClass();// 數字格式 61 pNumericFormat.AlignmentOption = esriNumericAlignmentEnum.esriAlignRight; 62 pNumericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfSignificantDigits; 63 pNumericFormat.RoundingValue = 0; 64 pNumericFormat.ShowPlusSign = false; 65 pNumericFormat.ZeroPad = true; 66 pFormattedGridLabel.Format = pNumericFormat as INumberFormat; 67 68 // 設置pMapGrid的LabelFormat屬性 69 pMapGrid.LabelFormat = pGridLabel; 70 // 添加格網 71 pMapGrids.AddMapGrid(pMapGrid); 72 73 } 74 catch (Exception) 75 { 76 77 throw; 78 } 79 }
3.2.8 MapSurround對象
MapSurround 對象是與一個地圖對象關聯的用於修飾地圖的輔助圖形元素對象。 比例尺,比例尺文本,圖例,指北針。。
MapSurround對象由MapSurroundFrame對象管理,所有的MapSurround對象添加在布局視圖上,每一個MapSurround對象可以
通過IMap接口的MapSurrounds屬性的索引值所獲取;也可以通過IMap接口的MapSurroundCount來遍歷布局視圖上的所有MapSurround對象
所有的MapSurround對象都實現了IMapSurround接口,使用該接口的Name屬性可以獲得MapSurround對象的名稱,
通過IMapSurround的FitToBound方法可以設置一個MapSurround對象的大小。
同時,MapSurround類也實現了IMapSurroundEvents接口,可以用來觸發MapSurround相關事件,如:AfterDraw,BeforeDraw,ContensChange
如下代碼演示如何為PageLayout添加Legend:

1 /// <summary> 2 /// 為PageLayout對象添加圖例對象 3 /// </summary> 4 /// <param name="pPageLayout"></param> 5 /// <param name="pEnvelop">圖例添加的位置</param> 6 private void AddLegendToPageLayout(IPageLayout pPageLayout, IEnvelope pEnvelop) 7 { 8 try 9 { 10 IActiveView pActiveView = pPageLayout as IActiveView; 11 IMap pMap = pActiveView.FocusMap; 12 IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer; 13 IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame; 14 UID pUID = new UID(); 15 pUID.Value = "{7A3F81E34-B9E3-11D1-8756-000F8751720}"; 16 ISymbolBackground pSymbolBackground = new SymbolBackgroundClass(); 17 IFillSymbol pFillSymbol = new SimpleFillSymbolClass(); 18 ILineSymbol pLineSymbol = new SimpleLineSymbolClass(); 19 pFillSymbol.Color = GetRgbColor(255, 255, 255); 20 pLineSymbol.Color = GetRgbColor(255, 255, 255); 21 pFillSymbol.Outline = pLineSymbol; 22 pSymbolBackground.FillSymbol = pFillSymbol; 23 IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, null); 24 pMapSurroundFrame.Background = pSymbolBackground; 25 26 IElement pElement = pMapSurroundFrame as IElement; 27 pElement.Geometry = pEnvelop; 28 IMapSurround pMapSurround = pMapSurroundFrame.MapSurround; 29 ILegend pLegend = pMapSurround as ILegend; 30 pLegend.ClearItems(); 31 pLegend.Title = "圖 ☆例"; 32 ITextSymbol pTextSymbol = new TextSymbolClass(); 33 pTextSymbol.Size = 10; 34 pTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft; 35 ILegendItem pLegendItem = null; 36 37 for (int i = 0; i < pActiveView.FocusMap.LayerCount; i++) 38 { 39 ILayer pLayer = pActiveView.FocusMap.get_Layer(i); 40 if (pLayer is IFeatureLayer) 41 { 42 IFeatureLayer pFlayer = pLayer as IFeatureLayer; 43 IFeatureClass pFeatureClass = pFlayer.FeatureClass; 44 if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation) 45 { 46 continue; 47 } 48 else 49 { 50 pLegendItem = new HorizontalBarLegendItemClass(); 51 pLegendItem.Layer = pLayer; 52 pLegendItem.Columns = 1; 53 pLegendItem.ShowDescriptions = false; 54 pLegendItem.ShowHeading = false; 55 pLegendItem.ShowLabels = true; 56 pLegendItem.LayerNameSymbol = pTextSymbol; 57 pLegend.AddItem(pLegendItem); 58 } 59 } 60 } 61 } 62 catch (Exception) 63 { 64 65 throw; 66 } 67 }
程序結果如下圖所示:
第三章到此結束。這章的內容是地圖和地圖布局,所圍繞的大都是圖形布局對象的操作。