コード例 #1
0
        public static void TextElementLabel(IMapControl2 mapControl, IFeatureLayer featureLayer, string fieldName)
        {
            IMap pMap = mapControl.Map;
            // 获取图层所有要素
            IFeatureCursor pFeatureCursor = featureLayer.FeatureClass.Search(null, false);
            IFeature       pFeature       = pFeatureCursor.NextFeature();

            while (pFeature != null)
            {
                IFields pFields = pFeature.Fields;
                // 找出标注字段的索引号
                int index = pFields.FindField(fieldName);
                // 得到要素的Envelope对象
                IEnvelope pEnv   = pFeature.Extent;
                IPoint    pPoint = new PointClass()
                {
                    X = pEnv.XMin + pEnv.Width / 2,
                    Y = pEnv.YMin + pEnv.Height / 2
                };
                // 新建字体对象
                stdole.IFontDisp pFont = new stdole.StdFontClass() as stdole.IFontDisp;
                pFont.Name = "arial";
                // 设置文本符号
                ITextSymbol pTextSymbol = new TextSymbolClass()
                {
                    Size  = 20,
                    Font  = pFont,
                    Color = AeUtils.GetRgbColor(255, 0, 0)
                };
                // 添加文本对象
                IElement pTextElement = new TextElementClass()
                {
                    Text      = pFeature.get_Value(index).ToString(),
                    ScaleText = true,
                    Symbol    = pTextSymbol
                };
                pTextElement.Geometry = pPoint;
                (pMap as IGraphicsContainer).AddElement(pTextElement, 0);
                mapControl.Refresh(esriViewDrawPhase.esriViewGraphics);
                pFeature = pFeatureCursor.NextFeature();
            }
        }
コード例 #2
0
        // 将检测站点导入列表,并设置选择触发事件
        private void setListBox()
        {
            IFeatureLayer  pFeatureLayer  = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "监测站");
            IFields        pFields        = pFeatureLayer.FeatureClass.Fields;
            IFeatureCursor pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
            IFeature       pFeature       = pFeatureCursor.NextFeature();

            while (pFeature != null)
            {
                string name = pFeature.get_Value(pFields.FindField("Name")).ToString();
                listBox_pts.Items.Add(name);
                pFeature = pFeatureCursor.NextFeature();
            }
            // 列表选择项变化触发事件
            listBox_pts.SelectedIndexChanged += (object sender, EventArgs e) => {
                m_pMapC2.Map.ClearSelection();
                string name = listBox_pts.SelectedItem.ToString();
                pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
                pFeature       = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    string value = pFeature.get_Value(pFields.FindField("Name")).ToString();
                    if (value == name)
                    {
                        break;
                    }
                    pFeature = pFeatureCursor.NextFeature();
                }
                m_pMapC2.Map.SelectFeature(pFeatureLayer, pFeature);
                m_pMapC2.Refresh(esriViewDrawPhase.esriViewGeoSelection);
                IEnvelope pEnv = pFeature.Extent;
                pEnv.XMin      -= 0.5; pEnv.YMin -= 0.5;
                pEnv.XMax      += 0.5; pEnv.YMax += 0.5;
                m_pMapC2.Extent = pEnv;
                showInfo(pFeature);
            };
        }
コード例 #3
0
        void Button_Click(object sender, EventArgs e)
        {
            #region 识别
            if ((Button)sender == btn_Identify)
            {
                m_isIdentify = !m_isIdentify;
                if (m_isIdentify)
                {
                    m_isDraw              = false;
                    m_isAddpoint          = false;
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerIdentify;
                }
                else
                {
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerArrow;
                }
                return;
            }
            #endregion
            #region  择
            if ((Button)sender == btn_select)
            {
                m_isDraw = !m_isDraw;
                if (m_isDraw)
                {
                    m_isIdentify          = false;
                    m_isAddpoint          = false;
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
                }
                else
                {
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerArrow;
                }
                return;
            }
            #endregion
            #region 链接
            if ((Button)sender == btn_link)
            {
                ;

                Geoprocessor gp = new Geoprocessor()
                {
                    OverwriteOutput = true
                };
                string table = debugDir + "\\AirQuality\\kqzlzk.dbf";
                ESRI.ArcGIS.ConversionTools.ExcelToTable pExcelToTavle = new ESRI.ArcGIS.ConversionTools.ExcelToTable()
                {
                    Input_Excel_File = debugDir + "\\AirQuality\\空气质量状况.xls",
                    Output_Table     = table,
                    Sheet            = "Sheet1"
                };
                gp.Execute(pExcelToTavle, null);

                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "监测站");

                ESRI.ArcGIS.DataManagementTools.JoinField pJoinField = new ESRI.ArcGIS.DataManagementTools.JoinField()
                {
                    in_data    = pFeatureLayer,
                    in_field   = "Name",
                    join_table = table,
                    join_field = "NAME",
                    fields     = "StationID;PM2_5;SO2;NO2"
                };
                gp.Execute(pJoinField, null);
            }
            #endregion
            #region 注记
            if ((Button)sender == btn_anno)
            {
                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "北京区县界");
                AeUtils.TextElementLabel(m_pMapC2, pFeatureLayer, "NAME");
            }
            #endregion
            #region 符号化
            if ((Button)sender == btn_symbol)
            {
                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "北京区县界");
                AeUtils.UniqueValueRenderer(pFeatureLayer, "NAME", m_pMapC2, axTOCControl_main);
            }
            #endregion
            #region 保存图层
            if ((Button)sender == btn_outlyr)
            {
                IFeatureLayer  pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "北京区县界");
                SaveFileDialog sfg           = new SaveFileDialog()
                {
                    Title  = "保存图层",
                    Filter = "图层文件 (*.lyr)|*.lyr"
                };
                if (sfg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ILayerFile layerfile = new LayerFileClass();
                    layerfile.New(sfg.FileName);
                    layerfile.ReplaceContents(pFeatureLayer);
                    layerfile.Save();
                }
            }
            #endregion
            if ((Button)sender == btn_addpt)
            {
                m_isAddpoint = !m_isAddpoint;
                if (m_isAddpoint)
                {
                    m_isDraw              = false;
                    m_isIdentify          = false;
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
                }
                else
                {
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerArrow;
                }
            }
        }
コード例 #4
0
        void axMapControl_main_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            // c. 在地图上点击,选择一个监测站点,并显示该监测站点的属性信息 第②步
            #region ...
            if (e.button == 1 && m_isIdentify)
            {
                IEnvelope pClickEnv = new EnvelopeClass()
                {
                    XMin = e.mapX - 0.01,
                    YMin = e.mapY - 0.01,
                    XMax = e.mapX + 0.01,
                    YMax = e.mapY + 0.01
                };
                IFeatureLayer  pFeatureLayer  = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "监测站");
                IFeatureCursor pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
                IFeature       pFeature       = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    IEnvelope pTargetEnv = pFeature.Extent;
                    if (pTargetEnv.XMax < pClickEnv.XMax && pTargetEnv.XMin > pClickEnv.XMin &&
                        pTargetEnv.YMax < pClickEnv.YMax && pTargetEnv.YMin > pClickEnv.YMin)
                    {
                        break;
                    }
                    pFeature = pFeatureCursor.NextFeature();
                }
                m_pMapC2.Map.ClearSelection();
                m_pMapC2.Map.SelectFeature(pFeatureLayer, pFeature);
                m_pMapC2.Refresh();
                if (pFeature != null)
                {
                    showInfo(pFeature);
                }
            }
            #endregion
            // d. 在监测站点列表中选择一个监测站点后,在地图上高亮显示,缩放至该监测站点,并显示该监测站点的属性信息 第②步
            #region ...
            if (e.button == 1 && m_isDraw)
            {
                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "监测站");

                IGeometry pGeometry = m_pMapC2.TrackPolygon();
                IElement  pElement  = new PolygonElementClass()
                {
                    Geometry = pGeometry
                };
                IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
                pFillShapeElement.Symbol = new SimpleFillSymbolClass()
                {
                    Color   = AeUtils.GetRgbColor(0, 0, 0, 0),
                    Outline = new SimpleLineSymbolClass()
                    {
                        Color = AeUtils.GetRgbColor(255, 0, 0),
                        Width = 1
                    }
                };
                IGraphicsContainer pGraphicsContainer = m_pMapC2.Map as IGraphicsContainer;
                pGraphicsContainer.DeleteAllElements();
                pGraphicsContainer.AddElement(pFillShapeElement as IElement, 0);
                m_pMapC2.Refresh(esriViewDrawPhase.esriViewGraphics);


                AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "北京区县界").Selectable = false;

                m_pMapC2.Map.SelectByShape(pGeometry, null, false);
                m_pMapC2.Refresh(esriViewDrawPhase.esriViewGeoSelection);
                MessageBox.Show(String.Format("当前已选中{0}个监测站", m_pMapC2.Map.SelectionCount));
            }
            #endregion
            // j. 在监测站点图层添加一个新站点
            #region ...
            if (e.button == 1 && m_isAddpoint)
            {
                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "监测站");
                IPoint        pPoint        = new PointClass()
                {
                    X = e.mapX,
                    Y = e.mapY,
                    SpatialReference = m_pMapC2.SpatialReference
                };
                IFeatureClassWrite pWrite = pFeatureLayer.FeatureClass as IFeatureClassWrite;
                IWorkspaceEdit     pEdit  = (pFeatureLayer.FeatureClass as IDataset).Workspace as IWorkspaceEdit;
                pEdit.StartEditing(true);
                pEdit.StartEditOperation();
                IFeature pFeature = pFeatureLayer.FeatureClass.CreateFeature();
                pFeature.Shape = pPoint;
                FormSetName formSetName = new FormSetName();
                formSetName.ShowDialog();
                pFeature.set_Value(pFeatureLayer.FeatureClass.Fields.FindField("Name"), formSetName.name);
                pFeature.Store();
                pWrite.WriteFeature(pFeature);
                pEdit.StopEditOperation();
                pEdit.StopEditing(true);
                m_pMapC2.Refresh();
            }
            #endregion
            if (e.button == 4)
            {
                m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerPanning;
                m_pMapC2.Pan();
                m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerPan;
            }
        }