Arcgis engine开发

来源:互联网 发布:有关狐狸的网络电视剧 编辑:程序博客网 时间:2024/04/29 07:58
0颜色方法

        public IRgbColor GetRgbColor(intintR, int intG, int intB)

        {

            IRgbColor pRgbColor =null;

            if (intR < 0 || intR > 255 || intG < 0 ||intG > 255 || intB < 0 || intB > 255)

            {

               return pRgbColor;

            }

           pRgbColor = new RgbColorClass();

           pRgbColor.Red = intR;

           pRgbColor.Green = intG;

           pRgbColor.Blue = intB;

            return pRgbColor;

        }

1IMapdocument加载mxd

if (openFileDialog.ShowDialog() ==DialogResult.OK)

                {

                    string sFileName =openFileDialog.FileName;

                    if(string.IsNullOrEmpty(sFileName))

                    {

                        return;

                    }

 

                    if(axMapControl.CheckMxFile(sFileName))

                    {

                        ClearAllData();

                        IMapDocument pMapDoc =new MapDocumentClass();

                        pMapDoc.Open(sFileName,"");

 

                        axMapControl.Map =pMapDoc.ActiveView.FocusMap;

                       axMapControl.ActiveView.Refresh();

                    }

                    else

                   {

                        MessageBox.Show("选择了无效的地图文档【"+ sFileName + "】!");

                    }

2加载mdb文件

private void AddMdbFile(string sAccessFilePath)

        {

            if (string.IsNullOrEmpty(sAccessFilePath))

return;

ry

            {

               IWorkspaceFactory pWksFac =new AccessWorkspaceFactory();

               IWorkspace pWks =pWksFac.OpenFromFile(sAccessFilePath, 0);

IEnumDataset pEnumDs = pWks.get_Datasets(esriDatasetType.esriDTAny);

               pEnumDs.Reset();

 

                IDatasetpDs = pEnumDs.Next();

               while (pDs != null)

               {

                   if (pDs isIFeatureDataset)

                   {

                        IEnumDataset pEnumDs2 = pDs.Subsets;

                        pEnumDs2.Reset();

                        IDataset pDs2 = pEnumDs2.Next();

                        while (pDs2 !=null)

                        {

                            IFeatureClass pFeaCls = pDs2as IFeatureClass;

                            IFeatureLayerpFeaLyr = new FeatureLayerClass();

                           pFeaLyr.FeatureClass = pFeaCls;

                            pFeaLyr.Name =pFeaCls.AliasName;

 

                           axMapControl.Map.AddLayer(pFeaLyr);

                            pDs2 =pEnumDs2.Next();

                        }

                   }

                   else if(pDs is IFeatureClass)

                   {

                        IFeatureClass pFeaCls = pDsas IFeatureClass;

                        IFeatureLayer pFeaLyr =new FeatureLayerClass();

                        pFeaLyr.FeatureClass =pFeaCls;

                        pFeaLyr.Name =pFeaCls.AliasName;

 

                       axMapControl.Map.AddLayer(pFeaLyr);

                    }

 

                   pDs = pEnumDs.Next();

               }

 

               axMapControl.ActiveView.Refresh();

 

            }

            catch (Exceptionex)

            {

            }

       

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3加载shapfile

private void AddShapeFile(stringsShpFilePath)

        {

            if (string.IsNullOrEmpty(sShpFilePath))

            {

               return;

            }

 

            try

            {

string sFolderPath =sShpFilePath.Substring(0, sShpFilePath.LastIndexOf("\\"));

tring sFeaClsName =sShpFilePath.Substring(sShpFilePath.LastIndexOf("\\")+ 1);

 

               IWorkspaceFactory pWksFac =new ShapefileWorkspaceFactory();

               IFeatureWorkspace pFeaWks = (IFeatureWorkspace)pWksFac.OpenFromFile(sFolderPath,0);

               IFeatureClass pFeaCls =pFeaWks.OpenFeatureClass(sFeaClsName);

 

               IFeatureLayer pFeaLyr = newFeatureLayerClass();

               pFeaLyr.FeatureClass = pFeaCls;

               pFeaLyr.Name = pFeaCls.AliasName;

 

               axMapControl.Map.AddLayer(pFeaLyr);

               axMapControl.ActiveView.Refresh();

 

            }

            catch (Exceptionex)

            {

            }

       

 

 

 

 

 

 

 

3右击Toc

private IFeatureLayerm_SelectFeatureLayer =null;

 

privatevoid axTOCControl_OnMouseDown(objectsender,toCControlEvents_OnMouseDownEvent e)

        {

            try

            {

               if (e.button == 2)

               {

                   esriTOCControlItem pItem = esriTOCControlItem.esriTOCControlItemNone;

                   IBasicMap pMap = null;

                   ILayer pLayer = null;

                   object unk = null;

                   object data = null;

 

                   axTOCControl.HitTest(e.x, e.y, refpItem, ref pMap, refpLayer, ref unk, refdata);

                   if (pItem ==esriTOCControlItem.esriTOCControlItemLayer && pLayer !=null)

                   {

                        m_SelectFeatureLayer =pLayer as IFeatureLayer;

                        btnCanSelect.Enabled =!m_SelectFeatureLayer.Selectable;

                        btnNotCanSelect.Enabled= m_SelectFeatureLayer.Selectable;

                        contextMenuStrip.Show(Control.MousePosition);

                   }

               }

            }

            catch (Exceptionex)

            {

               MessageBox.Show("TOCControl MouseDown Errorêo" + ex.Message);

            }

        }

 

private voidbtnAttributeTable_Click(object sender, EventArgs e)

       {

           if (m_SelectFeatureLayer == null)

           {

                return;

           }

 

           frmAttributeTable frmAttribute = new frmAttributeTable();

           frmAttribute.FromLoad(m_SelectFeatureLayer);

           frmAttribute.ShowDialog();

       }

 

       private void btnCanSelect_Click(objectsender, EventArgs e)

       {

           if (m_SelectFeatureLayer != null)

           {

                m_SelectFeatureLayer.Selectable= true;

                btnCanSelect.Enabled = false;

                btnNotCanSelect.Enabled = true;

           }

       }

 

       private void btnNotCanSelect_Click(objectsender, EventArgs e)

       {

           if (m_SelectFeatureLayer != null)

           {

                m_SelectFeatureLayer.Selectable= false;

                btnCanSelect.Enabled = true;

                btnNotCanSelect.Enabled =false;

           }

       }

 

       private void btnRemoveLayer_Click(objectsender, EventArgs e)

       {

           try

           {

                if (m_SelectFeatureLayer !=null)

                {

                    if (MessageBox.Show("是否删除【" +m_SelectFeatureLayer.Name + "】图层", "移除图层",  MessageBoxButtons.OKCancel,MessageBoxIcon.Information) == DialogResult.OK)

                    {

                       axMapControl.Map.DeleteLayer(m_SelectFeatureLayer as ILayer);

                    }

 

                   axMapControl.ActiveView.Refresh();

                    m_SelectFeatureLayer =null;

 

                    ////鹰眼同步

                    SynchronizeEagleEye();

                }

           }

           catch(Exception ex)

           {

                MessageBox.Show("移除图层出现异常:"+ ex.Message);

           }

       }

 

       private void btnPanToLayer_Click(objectsender, EventArgs e)

       {

           if (m_SelectFeatureLayer != null)

           {

                axMapControl.ActiveView.Extent= m_SelectFeatureLayer.AreaOfInterest;

                axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null, null);

           }

       }

4符号化选择图层

public partialclass frmFeatureLayerSelect : DevExpress.XtraEditors.XtraForm

    {

        ///<summary>

        ///??Ì?ª??

        ///</summary>

        private IFeatureLayer m_FeatureLayer =null;

        public IFeatureLayer SelectFeatureLayer

        {

            get

            {

               return m_FeatureLayer;

            }

        }

 

        public frmFeatureLayerSelect()

        {

           InitializeComponent();

        }

        ///ä¡ã¬??º?¡¥

        ///</summary>

        ///<param name="Map">Ì?ª?ä¡ã¨²</param>

        ///<param name="featureType">°a?¤¨¤¨ª</param>

        ///<param name="geometryType">ª??¤¨¤¨ª</param>

publicvoid FormLoad(IMap Map, esriFeatureType featureType, esriGeometryTypegeometryType)

        {

            if (Map == null ||Map.LayerCount == 0)

            {

               return;

            }

 

            this.cboLayer.Properties.Items.Clear();

 

            for (int i = 0; i< Map.LayerCount; i++)

            {

               IFeatureLayer pFeaLyr = Map.get_Layer(i) asIFeatureLayer;

               if (pFeaLyr == null|| pFeaLyr.FeatureClass == null)

               {

                   continue;

               }

 

               if (featureType ==esriFeatureType.esriFTAnnotation &&pFeaLyr.FeatureClass.FeatureType ==esriFeatureType.esriFTAnnotation)

               {

                   LayerFun pLyrFun =new LayerFun();

                   pLyrFun.FeaClsName = (pFeaLyr.FeatureClass asIDataset).Name;

                   pLyrFun.AliasName = pFeaLyr.FeatureClass.AliasName;

                   pLyrFun.FeatureLayer = pFeaLyr;

 

                   this.cboLayer.Properties.Items.Add(pLyrFun);

               }

               else if(featureType== esriFeatureType.esriFTSimple)

               {

                   if (pFeaLyr.FeatureClass.ShapeType ==geometryType)

                   {

                       LayerFunpLyrFun =new LayerFun();

                        pLyrFun.FeaClsName =(pFeaLyr.FeatureClassas IDataset).Name;

                        pLyrFun.AliasName =pFeaLyr.FeatureClass.AliasName;

                        pLyrFun.FeatureLayer =pFeaLyr;

 

                        this.cboLayer.Properties.Items.Add(pLyrFun);

                   }

               }

            }

 

            if (this.cboLayer.Properties.Items.Count> 0)

            {

               this.cboLayer.SelectedIndex = 0;

            }

        }

 

        private voidfrmFeatureLayerSelect_FormClosing(object sender, FormClosingEventArgs e)

        {

            if (this.DialogResult==DialogResult.OK)

            {

               object obj = this.cboLayer.SelectedItem;

                if(obj == null)

               {

                   MessageBox.Show("???¨¨°a¤??¡¥Ì?ª??¡ê");

                   e.Cancel = true;

                   return;

               }

 

               m_FeatureLayer = (obj asLayerFun).FeatureLayer;

            }

        }

    }

}

5多图层符号化

privatevoid btnMultiLayerPoint_ItemClick(object sender, ItemClickEventArgs e)

        {

            frmFeatureLayerSelect frm =new frmFeatureLayerSelect();

           frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple,esriGeometryType.esriGeometryPoint);

            if (frm.ShowDialog() ==DialogResult.OK)

            {

               IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;

               SymbolMultiLayerPoint(pFeaLyr);

            }

        }

 

        private voidSymbolMultiLayerPoint(IFeatureLayer pFeaLyr)

        {

            if (pFeaLyr == null|| pFeaLyr.FeatureClass == null)

            {

               return;

            }

 

            try

            {

               IGeoFeatureLayer pGeoFeaLyr = pFeaLyr asIGeoFeatureLayer;

 

               ////ä¡ä¡§Á?¤?¤??

               ICharacterMarkerSymbol pCharacterMarkerSymbol = newCharacterMarkerSymbolClass();

               IRgbColor pRgbColor = GetRgbColor(0, 0, 0);

               IFontDisp pFontDisp = (IFontDisp)(newStdFontClass());

               pFontDisp.Name = "arial";

               pFontDisp.Italic = true;

               pFontDisp.Size = 12;

               pCharacterMarkerSymbol.Angle = 0;

               pCharacterMarkerSymbol.CharacterIndex = 97;

               pCharacterMarkerSymbol.Color = pRgbColor;

               pCharacterMarkerSymbol.Font = pFontDisp;

               pCharacterMarkerSymbol.Size = 24;

 

               ////ä¡ä¡§ª??¤??

               IPictureMarkerSymbol pPictureMarkerSymbol = newPictureMarkerSymbolClass();

               string sFilePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

               sFilePath = sFilePath + @"..\..\res\bmp\city.bmp";

               if (!File.Exists(sFilePath))

               {

                   return;

               }              pPictureMarkerSymbol.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap,sFilePath);

               pPictureMarkerSymbol.Angle = 0;

               pPictureMarkerSymbol.BitmapTransparencyColor = pRgbColor;

               pPictureMarkerSymbol.Size = 10;

 

               ////ä¡ä¡§Ìt¨®¤??

               IMultiLayerMarkerSymbol pMultiLayerMarkerSymbol = new MultiLayerMarkerSymbolClass();

               pMultiLayerMarkerSymbol.AddLayer(pCharacterMarkerSymbol);

               pMultiLayerMarkerSymbol.AddLayer(pPictureMarkerSymbol);

               pMultiLayerMarkerSymbol.Angle = 0;

               pMultiLayerMarkerSymbol.Size = 20;

               pMultiLayerMarkerSymbol.XOffset = 5;

               pMultiLayerMarkerSymbol.YOffset = 5;

 

               ////¨¹?¤??¨´º?

               ISymbol pSymbol = (ISymbol)pMultiLayerMarkerSymbol;

               ISimpleRenderer pSimpleRender = newSimpleRendererClass();

               pSimpleRender.Symbol = pSymbol;

               pGeoFeaLyr.Renderer = pSimpleRender asIFeatureRenderer;

 

               axMapControl.Refresh();

               axTOCControl.Update();

            }

            catch (Exceptionex)

            {

               MessageBox.Show(符号化失败+ ex.Message);

            }

        }

6多图层线符号化

private voidSymbolMultiLayerLine(IFeatureLayer pFeaLyr)

        {

            if (pFeaLyr == null|| pFeaLyr.FeatureClass == null)

            {

               return;

            }

               IGeoFeatureLayer pGeoFeaLyr = pFeaLyr asIGeoFeatureLayer;

 

               ////ä¡ä¡§¨°Ì£¤¤??

              

               ////ä¡ä¡§?ª?¤??

               ICartographicLineSymbol pCartographicLineSymbol = new CartographicLineSymbolClass();

               pCartographicLineSymbol.Cap = esriLineCapStyle.esriLCSRound;

               pCartographicLineSymbol.Join = esriLineJoinStyle.esriLJSRound;

               pCartographicLineSymbol.Width = 2;

               pRgbColor = GetRgbColor(0, 255, 0);

               pCartographicLineSymbol.Color = pRgbColor;

 

               ////¦¨¨?线?°a?¤??¡êã?

               ILineProperties pLineProperties = pCartographicLineSymbolas ILineProperties;

               pLineProperties.Offset = 0;

               double[] dob = newdouble[6];

               dob[0] = 0;

               dob[1] = 1;

               dob[2] = 2;

               dob[3] = 3;

               dob[4] = 4;

               dob[5] = 5;

               ITemplate pTemplate = newTemplateClass();

               pTemplate.Interval = 1;

               for (inti = 0; i < dob.Length; i = i + 2)

               {

                    pTemplate.AddPatternElement(dob[i], dob[i+ 1]);

               }

               pLineProperties.Template = pTemplate;

 

               ////ä¡ä¡§¨¤ª??¤??

               IMultiLayerLineSymbol pMultiLayerLineSymbol = newMultiLayerLineSymbolClass();

               pMultiLayerLineSymbol.AddLayer(pSimpleLineSymbol);

               pMultiLayerLineSymbol.AddLayer(pCartographicLineSymbol);

 

               ////¨¹?¤??¨´º?

               ISymbol pSymbol = (ISymbol)pMultiLayerLineSymbol;

               ISimpleRenderer pSimpleRender = newSimpleRendererClass();

               pSimpleRender.Symbol = pSymbol;

               pGeoFeaLyr.Renderer = pSimpleRender asIFeatureRenderer;

 

               ////¡é?

                axMapControl.Refresh();

               axTOCControl.Update();

            }

           

7唯一值渲染

  private voidbtnSimpleRender_ItemClick(object sender, ItemClickEventArgs e)

        {

            frmFeatureLayerSelect frm =new frmFeatureLayerSelect();

            frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple,esriGeometryType.esriGeometryPolygon);

            if (frm.ShowDialog() ==DialogResult.OK)

            {

               IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;

                UniqueValueRender(pFeaLyr,pFeaLyr.FeatureClass.FeatureCount(null),pFeaLyr.FeatureClass.OIDFieldName);

            }

        }

 

        private void UniqueValueRender(IFeatureLayerpFtLayer, int pCount, string pFieldName)

        {

           IGeoFeatureLayer pGeoFeaturelayer = pFtLayer asIGeoFeatureLayer;

           IUniqueValueRenderer pUnique = newUniqueValueRendererClass();

           pUnique.FieldCount = 1;

           pUnique.set_Field(0, pFieldName);

           ISimpleFillSymbol pSimFill = newSimpleFillSymbolClass();

            //??¦?¨²??̦Ìê??eªaä?

            IFeatureCursor pFtCursor =pFtLayer.FeatureClass.Search(null,false);

            IFeature pFt = pFtCursor.NextFeature();

           IFillSymbol pFillSymbol1;

           IRandomColorRamp pColorRamp = newRandomColorRampClass();

           pColorRamp.StartHue = 0;

           pColorRamp.MinValue = 20;

           pColorRamp.MinSaturation = 15;

           pColorRamp.EndHue = 360;

           pColorRamp.MaxValue = 100;

           pColorRamp.MaxSaturation = 30;

           pColorRamp.Size = pCount;

            bool ok = true;

           pColorRamp.CreateRamp(out ok);

           IEnumColors pEnumRamp = pColorRamp.Colors;

            intpIndex = pFt.Fields.FindField(pFieldName);

            while (pFt != null)

            {

               IColor pColor = pEnumRamp.Next();

               if (pColor == null)

               {

                   pEnumRamp.Reset();

                   pColor = pEnumRamp.Next();

               }

 

               pFillSymbol1 = newSimpleFillSymbolClass();

               pFillSymbol1.Color = pColor;

               pUnique.AddValue(Convert.ToString(pFt.get_Value(pIndex)),pFieldName, pFillSymbol1as ISymbol);

               pFt = pFtCursor.NextFeature();

            }

 

           pGeoFeaturelayer.Renderer = pUnique asIFeatureRenderer;

           axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null,null);

           axTOCControl.Update();

        }

 

9另存为数据

privatevoid btnSaveAsMap_ItemClick(object sender, ItemClickEventArgs e)

        {

            try

            {

               SaveFileDialog saveFileDialog =new SaveFileDialog();

               saveFileDialog.Title = "¢¨ªä?a";

               saveFileDialog.OverwritePrompt = true;

               saveFileDialog.Filter = "ArcMap?̦Ì(*.mxd)|*.mxd|ArcMap¡êã?(*.mxt)|*.mxt";

               saveFileDialog.RestoreDirectory = true;

               if (saveFileDialog.ShowDialog() ==DialogResult.OK)

               {

                   string sFilePath =saveFileDialog.FileName;

 

                   IMapDocument pMapDoc = newMapDocumentClass();

                   pMapDoc.New(sFilePath);

                   pMapDoc.ReplaceContents(axMapControl.Map asIMxdContents);

                   pMapDoc.Save(true,true);

                   pMapDoc.Close();

                   ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pMapDoc);

                }

            }

            catch (Exceptionex)

            {

               MessageBox.Show("¢¨ªä?aº¡ì㨹êo" + ex.Message);

            }

        }

10保存数据

privatevoid btnSaveMxd_ItemClick(object sender, ItemClickEventArgs e)保存mxd文档

        {

            try

            {

               string sFilePath =axMapControl.DocumentFilename;

               if (string.IsNullOrEmpty(sFilePath))

               {

                   return;

               }

 

               IMapDocument pMapDoc = newMapDocumentClass();

               if (axMapControl.CheckMxFile(sFilePath))

               {

                   if (pMapDoc.get_IsReadOnly(sFilePath))

                   {

                        MessageBox.Show("À??̦̺??¨¢Ì?ê??¨¹À¡êä?¡ê");

                        return;

                   }

               }

 

               pMapDoc.New(sFilePath);

               pMapDoc.ReplaceContents(axMapControl.Map asIMxdContents);

               pMapDoc.Save(pMapDoc.UsesRelativePaths, true);

                pMapDoc.Close();

               ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pMapDoc);

            }

             

        }

 

11 选择图层

using System.Windows.Forms;

 

usingESRI.ArcGIS.Carto;

usingESRI.ArcGIS.Geodatabase;

usingESRI.ArcGIS.Geometry;

 

namespace 实º¦Ì验¨¦课?

{

    public partial class frmFeatureLayerSelect: DevExpress.XtraEditors.XtraForm

    {

        ///<summary>

        ///??Ì?ª??

        ///</summary>

        private IFeatureLayer m_FeatureLayer =null;

        public IFeatureLayer SelectFeatureLayer

        {

            get

            {

               return m_FeatureLayer;

            }

        }

 

        public frmFeatureLayerSelect()

        {

           InitializeComponent();

        }

 

        ///<summary>

        ///ä¡ã¬??º?¡¥

        ///</summary>

        ///<param name="Map">Ì?ª?ä¡ã¨²</param>

        ///<param name="featureType">°a?¤¨¤¨ª</param>

        ///<param name="geometryType">ª??¤¨¤¨ª</param>

        public voidFormLoad(IMap Map, esriFeatureTypefeatureType, esriGeometryType geometryType)

        {

            if (Map == null ||Map.LayerCount == 0)

            {

               return;

            }

 

            this.cboLayer.Properties.Items.Clear();

 

            for (int i = 0; i < Map.LayerCount; i++)

            {

               IFeatureLayer pFeaLyr = Map.get_Layer(i) asIFeatureLayer;

               if (pFeaLyr == null|| pFeaLyr.FeatureClass == null)

               {

                   continue;

               }

 

               if (featureType ==esriFeatureType.esriFTAnnotation &&pFeaLyr.FeatureClass.FeatureType ==esriFeatureType.esriFTAnnotation)

               {

                   LayerFun pLyrFun =new LayerFun();

                   pLyrFun.FeaClsName = (pFeaLyr.FeatureClass asIDataset).Name;

                   pLyrFun.AliasName = pFeaLyr.FeatureClass.AliasName;

                   pLyrFun.FeatureLayer = pFeaLyr;

 

                   this.cboLayer.Properties.Items.Add(pLyrFun);

               }

               else if(featureType== esriFeatureType.esriFTSimple)

               {

                   if (pFeaLyr.FeatureClass.ShapeType ==geometryType)

                   {

                        LayerFun pLyrFun =new LayerFun();

                        pLyrFun.FeaClsName =(pFeaLyr.FeatureClassas IDataset).Name;

                        pLyrFun.AliasName =pFeaLyr.FeatureClass.AliasName;

                        pLyrFun.FeatureLayer =pFeaLyr;

 

                        this.cboLayer.Properties.Items.Add(pLyrFun);

                   }

               }

            }

 

            if (this.cboLayer.Properties.Items.Count> 0)

            {

               this.cboLayer.SelectedIndex = 0;

            }

        }

 

        private void frmFeatureLayerSelect_FormClosing(object sender,FormClosingEventArgse)

        {

            if (this.DialogResult==DialogResult.OK)

            {

               object obj = this.cboLayer.SelectedItem;

               if (obj == null)

               {

                   MessageBox.Show("???¨¨°a¤??¡¥Ì?ª??¡ê");

                   e.Cancel = true;

                   return;

               }

 

               m_FeatureLayer = (obj asLayerFun).FeatureLayer;

            }

        }

    }

}

 

 

 

0 0
原创粉丝点击