1. 程式人生 > >ArcGis Engine程式設計之子集、選擇集和版面檢視

ArcGis Engine程式設計之子集、選擇集和版面檢視

實驗:子集和選擇集的使用

1.應用QueryFilter,查詢記錄的子集迴圈,並以訊息框顯示統計結果。

這裡寫圖片描述

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string strPath = Application.StartupPath + "\\實驗六資料\\實驗六.mxd"
; if (axMapControl1.CheckMxFile(strPath)) { axMapControl1.LoadMxFile(strPath); axTOCControl1.SetBuddyControl(axMapControl1); } } private void button1_Click(object sender, EventArgs e) { IFeatureLayer pFLayer; IFeatureClass pFClass; pFLayer = axMapControl1.Map.get_Layer(2
) as IFeatureLayer; pFClass = pFLayer.FeatureClass; IQueryFilter pQFilter=new QueryFilterClass(); IFeatureCursor pFCursor ; pQFilter.WhereClause = "LANDLOCKED='Y'"; pFCursor = pFClass.Search(pQFilter, true); double intPop = 0.0; double
intArea = 0.0; int intCounties = 0; IFeature pFeature=pFCursor.NextFeature(); while( pFeature!=null){ intPop = intPop + double.Parse(pFeature.get_Value(6).ToString());//對應Population欄位 intArea = intArea + double.Parse(pFeature.get_Value(7).ToString());//對應Area欄位 intCounties = intCounties + 1; pFeature = pFCursor.NextFeature(); } double dblPopDensity; dblPopDensity = intPop/intArea; MessageBox.Show("內陸國家資訊是:\n" + "數量是:" + intCounties.ToString() + "\n" + "平均面積是:" + (intArea / intCounties).ToString() + "\n" + "人口密度是:" + dblPopDensity.ToString()); } }

2.基於空間查詢過濾器(SpatialFilter)建立一個子集,並以訊息框顯示結果。

這裡寫圖片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace 實驗6_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string strPath = Application.StartupPath + "\\實驗六資料\\實驗六.mxd";
            if (axMapControl1.CheckMxFile(strPath))
            {
                axMapControl1.LoadMxFile(strPath);
                axTOCControl1.SetBuddyControl(axMapControl1);
                axToolbarControl1.SetBuddyControl(axMapControl1);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFLayer=axMapControl1.Map.get_Layer(2) as IFeatureLayer;
            IFeatureSelection pCountryLayer = pFLayer as IFeatureSelection;
            ISelectionSet pCountrySelection;
            ICursor pCursor;
            IFeatureCursor pCountryCursor;
            pCountrySelection = pCountryLayer.SelectionSet;
            pCountrySelection.Search(null, true, out pCursor);
            pCountryCursor = pCursor as IFeatureCursor;
            IFeature pCountry;
            pCountry = pCountryCursor.NextFeature();
            if (pCountry == null)
            {
                MessageBox.Show("請選擇一個Country");
                return ;
            }
            ISpatialFilter pSpatialFilter = new SpatialFilterClass();
            pSpatialFilter.WhereClause = "Population>2000000";
            pSpatialFilter.Geometry = pCountry.Shape;
            pSpatialFilter.SpatialRel= esriSpatialRelEnum.esriSpatialRelContains;
            IFeatureLayer pCityLayer;
            IFeatureClass pCityFClass;
            pCityLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
            pCityFClass = pCityLayer.FeatureClass;
            IFeatureCursor pCityCursor=pCityFClass.Search(pSpatialFilter,true);
            IFeature pCity=pCityCursor.NextFeature();
            int intCount = 0;
            while(pCity !=null)
            {
                intCount = intCount + 1;
                pCity=pCityCursor.NextFeature();
            }
            MessageBox.Show("這個國家有:" + intCount + "個城市人口超過200萬");
        }

    }
}

實驗:使用版面檢視元素

在Form1_Load中,執行窗體,呼叫實驗七資料資料夾中的tahe.mxd文件,並且顯示圖例和格網。

這裡寫圖片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GeoSurvey;
using ESRI.ArcGIS.Display;

namespace shiyan7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Application.StartupPath + "\\實驗七資料\\塔河.mxd";
            if (axPageLayoutControl1.CheckMxFile(path))
            {
                axPageLayoutControl1.LoadMxFile(path);
                axTOCControl1.SetBuddyControl(axPageLayoutControl1);
            }
            IGraphicsContainer graphicsContainer = axPageLayoutControl1.ActiveView.GraphicsContainer;
            //得到MapFrame物件
            IMapFrame mapFrame = (IMapFrame)graphicsContainer.FindFrame(axPageLayoutControl1.ActiveView.FocusMap);
            if (mapFrame == null) return;
            //生成一個圖例
            UID uID = new UIDClass();
            uID.Value = "esriCarto.Legend";
            //從MapFrame中生成一個MapSurroundFrame
            IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uID, null);
            if (mapSurroundFrame == null) return;
            //MapSurroundFrame名稱
            mapSurroundFrame.MapSurround.Name = "圖例";
            ILegend pleg;
            pleg = new Legend();
            pleg = mapSurroundFrame.MapSurround as ILegend;
            pleg.Title = "圖例";
            //設定圖例的實現範圍
            IEnvelope envelope = new EnvelopeClass();
            envelope.PutCoords(2, 2, 6, 6);
            IElement element = (IElement)mapSurroundFrame;
            element.Geometry = envelope;
            //新增圖例元素
            axPageLayoutControl1.ActiveView.GraphicsContainer.AddElement(element, 0);
            axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

            try {
                IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;
                IActiveView pActiveView = pPageLayout as IActiveView;
                IMap pMap = pActiveView.FocusMap;
                IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer;
                IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
                IMapGrids pMapGrids = pMapFrame as IMapGrids;

                IMeasuredGrid pMeasuredGrid = new MeasuredGridClass();
                IMapGrid pMapGrid = pMeasuredGrid as IMapGrid;
                pMeasuredGrid.FixedOrigin = true;
                pMeasuredGrid.Units = pMap.MapUnits;
                pMeasuredGrid.XIntervalSize = 1000;
                pMeasuredGrid.YIntervalSize = 1000;
                pMeasuredGrid.XOrigin = -180;
                pMeasuredGrid.YOrigin = -90;
                IProjectedGrid pProGrid = pMeasuredGrid as IProjectedGrid;
                pProGrid.SpatialReference = pMap.SpatialReference;
                pMapGrid.Name = "Measured Grid";

                ICalibratedMapGridBorder pCalibratedMapGridBorder = new CalibratedMapGridBorderClass();

                pCalibratedMapGridBorder.BackgroundColor = GetRGB(255,255,255);
                pCalibratedMapGridBorder.ForegroundColor = GetRGB(255,0,0);
                pCalibratedMapGridBorder.BorderWidth = 0.1;
                pCalibratedMapGridBorder.Interval = 72;
                pCalibratedMapGridBorder.Alternating = true;
                pMapGrid.Border = pCalibratedMapGridBorder as IMapGridBorder;

                IFormattedGridLabel pFormattedGridLabel = new FormattedGridLabelClass();
                IGridLabel pGridLabel = pFormattedGridLabel as IGridLabel;
                stdole.StdFont pFont = new stdole.StdFont();
                pFont.Name = "Arial";
                pFont.Size = 6;
                pGridLabel.Font = pFont as stdole.IFontDisp;
                pGridLabel.Color = GetRGB(0,0,250);
                pGridLabel.LabelOffset = 4;
                pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisLeft,false);
                pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisRight, false);
                INumericFormat pNumericFormat = new NumericFormatClass();
                pNumericFormat.AlignmentOption = esriNumericAlignmentEnum.esriAlignRight;
                pNumericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfSignificantDigits;
                pNumericFormat.RoundingValue = 0;
                pNumericFormat.ShowPlusSign = false;
                pNumericFormat.ZeroPad = true;
                pFormattedGridLabel.Format = pNumericFormat as INumberFormat;
                pMapGrid.LabelFormat = pGridLabel;
                pMapGrids.AddMapGrid(pMapGrid);
                axPageLayoutControl1.Refresh();
            }
            catch(Exception Err){
                MessageBox.Show(Err.Message,"資訊提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }


        }
        private IRgbColor GetRGB(int r,int g,int b)
        {
            IRgbColor pColor=new RgbColorClass();
            pColor.Red = r;
            pColor.Green = g;
            pColor.Blue = b;
            return pColor;
        }
    }
}