GIS的积累
It is never to late to learn

导航

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.AnalysisTools; //使用GP工具
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;



namespace SearchAndDisplay
{
    public partial class Form1 : Form
    {
        //传递数据之用
        private IFeatureClass pCommonPolygonClass;
        private IFeatureClass pCommonPolylineClass;
        private IFeatureClass pCommonPointClass;
        private IFeatureWorkspace pPublicFeatureWorkspace=null;

        private IArray pArray;
        private bool isFileExist;
        private bool isGDBExist;

        //传递数据库中的数据
        private string XY;
        private string Globle;
        private double falseEasting;
        private double falseNorthing;
        private double centralMeridian;
        private double scaleFactor;
        private double latitudeOfOrigion;

        private string[] XandY;
        private IFeatureClass pPublicInMemoryClass;
        public Form1()
        {
            InitializeComponent();
        }

        //Create GCS:Beijing54
        private ISpatialReference GetGCSBeijing54()
        {
            ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
            ISpatialReference pSR = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);
            pSR.SetDomain(-400,-400,400,400);
            return pSR;
 
        }


        //Create GCS:Xi'an80
        private ISpatialReference GetGCSXian80()
        {
            ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
            //pSRF.CreateProjectedCoordinateSystem(esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_19);
            //pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980);
            ISpatialReference pSR = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980);
            pSR.SetDomain(-400, -400, 400, 400);
            return pSR;
        }


        //Create GCS:WGS1984
        private ISpatialReference GetGCSWGS1984()
        {
            ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
            ISpatialReference pSR = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
            pSR.SetDomain(-400, -400, 400, 400);
            return pSR;
        }
        //Create PCS:Xian80 zone19
        private ISpatialReference GetPCSXian80()
        {
            ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
            ISpatialReference pSR = pSRF.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_19);
            pSR.SetDomain(13876800, -10002100, 900709923374.099, 900733802274.099);
            return pSR;
        }


       //Create PCS:Beijng54 zone19
       private ISpatialReference GetPCSBeijing54()
       {
           ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
           ISpatialReference pSR = pSRF.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_Beijing1954GK_19);
           pSR.SetDomain(14876700, -10002300, 900734802174.099, 900709923174.099);
           return pSR;

        }
        
       //Create PCS:WGS1984 zong49
       private ISpatialReference    GetPCSWGS1984()
       {
           ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
           ISpatialReference pSR = pSRF.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_49N);
           pSR.SetDomain(-5120900, -9998100, 900714804574.099, 900709927374.099);
           return pSR;

        }
       
        //Load 3 featureClasses with different spatial reference
        private void Form1_Load(object sender, EventArgs e)
        {
            axMapControl1.Map.SpatialReference = GetPCSBeijing54();
            axMapControl2.Map.SpatialReference = GetPCSXian80();
            axMapControl3.Map.SpatialReference = GetPCSWGS1984();

        }
        private void TabsChanged()
        {
          
        }
        
        //add 3 layers to 3 mapcontrols with the different spatial reference
        private void button8_Click(object sender, EventArgs e)
        {
            IPoint pPoint=null ;
            IPolyline pL = null;
            object _missing = Type.Missing;


            IWorkspaceFactory pSdeWorkspaceFactory = new SdeWorkspaceFactory();
            IPropertySet pPropertySet= new PropertySetClass();
            pPropertySet.SetProperty("Server","siwei-gr7bzmd3q");
            pPropertySet.SetProperty("Instance","5008");
            pPropertySet.SetProperty("Database","sdeTest");
            pPropertySet.SetProperty("user","sa");
            pPropertySet.SetProperty("password","zhangjun1130");
            pPropertySet.SetProperty("version","sde.DEFAULT");
            IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pSdeWorkspaceFactory.Open(pPropertySet, 0);
            pPublicFeatureWorkspace =pFeatureWorkspace;
            
            IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pFeatureWorkspace;
            IFeatureClass pFeatureClass;
            IFeatureLayer pFeatureLayer;

            pFeatureClass = pFeatureWorkspace.OpenFeatureClass("sdeTest.DBO.DomBeijing54");
            pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            axMapControl1.AddLayer(pFeatureLayer);

            pFeatureClass = pFeatureWorkspace.OpenFeatureClass("sdeTest.DBO.DomXian80");
            pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            axMapControl2.AddLayer(pFeatureLayer);

            pFeatureClass = pFeatureWorkspace.OpenFeatureClass("sdeTest.DBO.DomWGS84");
            pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            axMapControl3.AddLayer(pFeatureLayer);




            #region 暂时不用
            //// 摄区
            //#region 搞摄区
            //pWorkspaceEdit.StartEditing(true);
            //pWorkspaceEdit.StartEditOperation();

            //    IFeatureClass pPolygonsClassOld = pFeatureWorkspace.OpenFeatureClass("polygons");
            //    ITable pTable = (ITable)pPolygonsClassOld;
            //    if (pTable != null)
            //    {
            //        pTable.DeleteSearchedRows(null);

            //    }

            //IFeatureClass pPolygonsClass = pFeatureWorkspace.OpenFeatureClass("polygons");
            //isGDBExist = true;

            //IFeature pPolygonsFeaure;
            //pPolygonsFeaure= pPolygonsClass.CreateFeature();

            //IPolygon pPolygon = new PolygonClass();
            //IPointCollection pPointCollection = (IPointCollection)pPolygon;
            //pPoint = new PointClass();
            //pPoint.PutCoords(10, 10);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(15, 10);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(15, 8);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(18, 8);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(18, 4);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(10, 4);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPolygonsFeaure.Shape = pPolygon;
            //pPolygon.Close();
            //pPolygonsFeaure.Store();



            //pPolygonsFeaure = pPolygonsClass.CreateFeature();
            //pPolygon = new PolygonClass();
            //pPointCollection = (IPointCollection)pPolygon;
            //pPoint = new PointClass();
            //pPoint.PutCoords(20, 8);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(25, 8);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(25, 4);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(20, 4);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPolygonsFeaure.Shape = pPolygon;
            //pPolygon.Close();
            //pPolygonsFeaure.Store();


            //#endregion

            //// 航线
            //#region 搞航线
            //IFeatureClass pPolylineClass = pFeatureWorkspace.OpenFeatureClass("polylines");
            //IFeature pPolylinesFeature = null;

            //pPolylinesFeature = pPolylineClass.CreateFeature();
            //pL = new PolylineClass();
            //pPointCollection = (IPointCollection)pL;
            //pPoint = new PointClass();
            //pPoint.PutCoords(9.5, 9.5);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(15.5, 9.5);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPolylinesFeature.Shape = pL;
            //pPolylinesFeature.Store();

            //pPolylinesFeature = pPolylineClass.CreateFeature();
            //pL = new PolylineClass();
            //pPointCollection = (IPointCollection)pL;
            //pPoint = new PointClass();
            //pPoint.PutCoords(9.5, 6.5);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(15.5, 6.5);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPolylinesFeature.Shape = pL;
            //pPolylinesFeature.Store();

            //pPolylinesFeature = pPolylineClass.CreateFeature();
            //pL = new PolylineClass();
            //pPointCollection = (IPointCollection)pL;
            //pPoint = new PointClass();
            //pPoint.PutCoords(9.5, 4.5);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPoint.PutCoords(15.5, 4.5);
            //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            //pPolylinesFeature.Shape = pL;
            //pPolylinesFeature.Store();
            //#endregion

            //// 像点
            //#region 搞像点
            //IFeatureClass pPointClass = pFeatureWorkspace.OpenFeatureClass("points");
            //IFeature pPointFeature = null;

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(9.7, 9.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(12.7, 9.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(14.7, 9.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(9.7, 6.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(12.7, 6.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(14.7, 6.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(9.7, 4.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(12.7, 4.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //pPointFeature = pPointClass.CreateFeature();
            //pPoint = new PointClass();
            //pPoint.PutCoords(14.7, 4.5);
            //pPointFeature.Shape = pPoint;
            //pPointFeature.Store();

            //#endregion

            //pWorkspaceEdit.StopEditOperation();
            //pWorkspaceEdit.StopEditing(true);

            //pCommonPolygonClass = pPolygonsClass;
            //pCommonPolylineClass = pPolylineClass;
            //pCommonPointClass = pPointClass;
            #endregion

        }

        private void button9_Click(object sender, EventArgs e)
        {


            IWorkspaceFactory pFeatureWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pFeatureWorkspaceFactory.OpenFromFile("D:/Work", 0);
            IFields pFields = pCommonPolygonClass.Fields;
            if (isFileExist == true)
            {
                IFeatureClass pPolygonsFeatureClassOld = pFeatureWorkspace.OpenFeatureClass("Newpolygons");
                ITable pTable = (ITable)pPolygonsFeatureClassOld;
                if (pTable != null)
                {
                    pTable.DeleteSearchedRows(null);
                }
            }
            #region 摄区出库
    
            IFeatureClass pPolygonsFeatureClass = pFeatureWorkspace.CreateFeatureClass("Newpolygons", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
            isFileExist = true;
            IFeature pPolygonFeaure;
            IFeature pConvertFeature;
            //IFeatureCursor pPolygonCursor = pCommonPolygonClass.Search(null, true);
            //IFeature pFeaure = pPolygonCursor.NextFeature();

            for (int i = 0; i < pArray.Count;i++ )
            {
                pPolygonFeaure = pPolygonsFeatureClass.CreateFeature();
                pConvertFeature = (IFeature)pArray.get_Element(i);
                pPolygonFeaure.Shape = pConvertFeature.Shape;
                pPolygonFeaure.Store();

            }
            #endregion

            #region 航线出库
            //pFields = pCommonPolylineClass.Fields;
            //IFeatureClass pPolylineFeatureClass = pFeatureWorkspace.CreateFeatureClass("Newpolines", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
            //IFeature pPloylineFeature;

            //IFeatureCursor pPolylineCursor = pCommonPolylineClass.Search(null, true);
            //IFeature pFeature1 = pPolylineCursor.NextFeature();
            //while (pFeature1!= null)
            //{
            //    pPloylineFeature = pPolylineFeatureClass.CreateFeature();
            //    pPloylineFeature.Shape = pFeature1.Shape;
            //    pPloylineFeature.Store();
            //    pFeature1 = pPolylineCursor.NextFeature();

            //}

            #endregion

            #region 像点
            //pFields = pCommonPointClass.Fields;
            //IFeatureClass pPointsFeatureClass = pFeatureWorkspace.CreateFeatureClass("Newpoints", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
            //IFeature pPointFeaure;

            //IFeatureCursor pPointCursor = pCommonPointClass.Search(null, true);
            //IFeature pFeature2 = pPointCursor.NextFeature();
            
            //while(pFeature2!=null)
            //{
            //    pPointFeaure=pPointsFeatureClass.CreateFeature();
            //    pPointFeaure.Shape = pFeature2.Shape;
            //    pPointFeaure.Store();
            //    pFeature2 = pPointCursor.NextFeature();



            //}


            #endregion


        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            IFeatureLayer pPolygonLayer=new FeatureLayerClass();
            IFeatureLayer pPolylineLayer= new FeatureLayerClass();
            IFeatureLayer pPointLayer= new FeatureLayerClass();
            pPolygonLayer.FeatureClass=pCommonPolygonClass;
            pPolylineLayer.FeatureClass=pCommonPolylineClass;
            pPointLayer.FeatureClass=pCommonPointClass;

            axMapControl1.AddLayer(pPolygonLayer);
            axMapControl1.AddLayer(pPolylineLayer);
            axMapControl1.AddLayer(pPointLayer);





        }

        private void axMapControl1_OnMouseDown_1(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                ISpatialReference pSR = axMapControl1.Map.SpatialReference;
                ILayer pLayer = axMapControl1.Map.get_Layer(0);
                IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
                int count = pFeatureLayer.FeatureClass.FeatureCount(null);
                MessageBox.Show("投影名字是:" + pSR.Name + "  图层Feature个数是:" + count);
            }
        }

        private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                ISpatialReference pSR = axMapControl2.Map.SpatialReference;
                ILayer pLayer = axMapControl2.Map.get_Layer(0);
                IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
                int count = pFeatureLayer.FeatureClass.FeatureCount(null);
                MessageBox.Show("投影名字是:" + pSR.Name + "  图层Feature个数是:" + count);
            }
        }

        private void axMapControl3_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                ISpatialReference pSR = axMapControl3.Map.SpatialReference;
                ILayer pLayer = axMapControl3.Map.get_Layer(0);
                IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
                int count = pFeatureLayer.FeatureClass.FeatureCount(null);
                MessageBox.Show("投影名字是:" + pSR.Name + "  图层Feature个数是:" + count);
            }
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {


        }

        private void tabControl1_Click(object sender, EventArgs e)
        {
           

        }

        private void axMapControl3_TabIndexChanged(object sender, EventArgs e)
        {



        }

        //切换Tabs使用的方法
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 0:
                    axToolbarControl1.SetBuddyControl(axMapControl1);
                    Console.WriteLine("0");
                    break;
                case 1:

                    axToolbarControl1.SetBuddyControl(axMapControl2);
                    Console.WriteLine("1");
                    break;
                case 2:
                    axToolbarControl1.SetBuddyControl(axMapControl3);
                    Console.WriteLine("2");
                    break;
                default:
                    break;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            IEnvelope pEnvelop = new EnvelopeClass();
            ILayer pLayer=axMapControl1.Map.get_Layer(0);
            IFeatureLayer pFeatureLayer=(IFeatureLayer)pLayer;
            IFeatureClass pFeatureClass=pFeatureLayer.FeatureClass;
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                pEnvelop.Union(pFeature.Shape.Envelope);
                pFeature = pFeatureCursor.NextFeature();
            }
            pEnvelop.Expand(1.2, 1.2, true);
            axMapControl1.ActiveView.Extent = pEnvelop.Envelope;
            axMapControl1.Refresh();
        }

        private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            label2.Text = e.mapX.ToString();
            label3.Text = e.mapY.ToString();
        }

        private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            label2.Text = e.mapX.ToString();
            label3.Text = e.mapY.ToString();
        }

        private void axMapControl3_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            label2.Text = e.mapX.ToString();
            label3.Text = e.mapY.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ICommand command = new ControlsAddDataCommandClass();
            command.OnCreate(axMapControl1.Object);
            command.OnClick();
        }


        //新建类似西安80PCS
        private IProjectedCoordinateSystem CreatePCSBasedOnXianan80(IGeographicCoordinateSystem pGCS, double falseEasting, double fasleNorthing,
           double centralMeridian, double scaleFactor, double latitudeOfOrigion)//
        {       
             ISpatialReferenceFactory2 pSpatialReferenceFactory2=new SpatialReferenceEnvironmentClass();
             IProjectionGEN pProjectionGEN=(IProjectionGEN)pSpatialReferenceFactory2.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger);
             IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory2.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980);
             (pGeographicCoordinateSystem as ISpatialReference).SetDomain(-400, -400, 400, 400);
             ILinearUnit pLinearUnit=(ILinearUnit)pSpatialReferenceFactory2.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter);
             IParameter[] pParameters=pProjectionGEN.GetDefaultParameters();
             for( int i=0;i< pParameters.Length;i++)
             {
                  IParameter currentParameter=pParameters[i];
              }
                //修改parameters
            IParameter falseEastingParameter = pParameters[0];
            falseEastingParameter.Value = falseEasting;
            IParameter fasleNorthingParameter = pParameters[1];
            fasleNorthingParameter.Value = fasleNorthing;
            IParameter centralMeridianParameter = pParameters[2];
            centralMeridianParameter.Value =centralMeridian;
            IParameter scaleFactorParameter = pParameters[3];
            scaleFactorParameter.Value =scaleFactor;
            IParameter latitudeOfOrigionParameter = pParameters[4];
            latitudeOfOrigionParameter.Value =latitudeOfOrigion;
            IProjectedCoordinateSystemEdit pPCSE = new ProjectedCoordinateSystemClass();
            object name = "new Dom Project cooridinate system";
            object alias="nDPos";
            object abbreviation="NDPCS";
            object remarks = "most eastern province in China";
            object usage="when making maps of new Dom";
            object gcsobject = pGeographicCoordinateSystem as object;
            object unitobject = pLinearUnit as object;
            object projection = pProjectionGEN as object;
            object parameters = pParameters as object;

            pPCSE.Define(ref name,ref alias,ref abbreviation,ref remarks,ref usage,ref gcsobject,ref unitobject,ref projection,ref parameters);
            IProjectedCoordinateSystem pPCS = (IProjectedCoordinateSystem)pPCSE;
            {
                (pPCS as ISpatialReferenceTolerance).XYTolerance = 0.000001;
                pPCS.SetDomain(-10002100, -10002100, 900709923374.099, 900733802274.099);
       
            }
            return pPCS;
            


         }

        //新建类似北京54PCS
        private IProjectedCoordinateSystem CreatePCSBasedOnBeijing54(IGeographicCoordinateSystem pGCS, double falseEasting, double fasleNorthing,
          double centralMeridian, double scaleFactor, double latitudeOfOrigion)//
        {
            ISpatialReferenceFactory2 pSpatialReferenceFactory2 = new SpatialReferenceEnvironmentClass();
            IProjectionGEN pProjectionGEN = (IProjectionGEN)pSpatialReferenceFactory2.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger);
            IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory2.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);
            (pGeographicCoordinateSystem as ISpatialReference).SetDomain(-400, -400, 400, 400);
            ILinearUnit pLinearUnit = (ILinearUnit)pSpatialReferenceFactory2.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter);
            IParameter[] pParameters = pProjectionGEN.GetDefaultParameters();
            for (int i = 0; i < pParameters.Length; i++)
            {
                IParameter currentParameter = pParameters[i];
            }
            //修改parameters
            IParameter falseEastingParameter = pParameters[0];
            falseEastingParameter.Value = falseEasting;
            IParameter fasleNorthingParameter = pParameters[1];
            fasleNorthingParameter.Value = fasleNorthing;
            IParameter centralMeridianParameter = pParameters[2];
            centralMeridianParameter.Value = centralMeridian;
            IParameter scaleFactorParameter = pParameters[3];
            scaleFactorParameter.Value = scaleFactor;
            IParameter latitudeOfOrigionParameter = pParameters[4];
            latitudeOfOrigionParameter.Value = latitudeOfOrigion;
            IProjectedCoordinateSystemEdit pPCSE = new ProjectedCoordinateSystemClass();
            object name = "new Dom Project cooridinate system";
            object alias = "nDPos";
            object abbreviation = "NDPCS";
            object remarks = "most eastern province in China";
            object usage = "when making maps of new Dom";
            object gcsobject = pGeographicCoordinateSystem as object;
            object unitobject = pLinearUnit as object;
            object projection = pProjectionGEN as object;
            object parameters = pParameters as object;

            pPCSE.Define(ref name, ref alias, ref abbreviation, ref remarks, ref usage, ref gcsobject, ref unitobject, ref projection, ref parameters);
            IProjectedCoordinateSystem pPCS = (IProjectedCoordinateSystem)pPCSE;
            {
                (pPCS as ISpatialReferenceTolerance).XYTolerance = 0.000001;
                pPCS.SetDomain(-10002300, -10002300, 900734802174.099, 900709923174.099);
            }
            return pPCS;



        }


        //新建类似WGS1984PCS
        private IProjectedCoordinateSystem CreatePCSBasedOnWGS1984(IGeographicCoordinateSystem pGCS, double falseEasting, double fasleNorthing,
          double centralMeridian, double scaleFactor, double latitudeOfOrigion)//
        {
            ISpatialReferenceFactory2 pSpatialReferenceFactory2 = new SpatialReferenceEnvironmentClass();
            IProjectionGEN pProjectionGEN = (IProjectionGEN)pSpatialReferenceFactory2.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger);
            IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory2.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
            (pGeographicCoordinateSystem as ISpatialReference).SetDomain(-400, -400, 400, 400);
            ILinearUnit pLinearUnit = (ILinearUnit)pSpatialReferenceFactory2.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter);
            IParameter[] pParameters = pProjectionGEN.GetDefaultParameters();
            for (int i = 0; i < pParameters.Length; i++)
            {
                IParameter currentParameter = pParameters[i];
            }
            //修改parameters
            IParameter falseEastingParameter = pParameters[0];
            falseEastingParameter.Value = falseEasting;
            IParameter fasleNorthingParameter = pParameters[1];
            fasleNorthingParameter.Value = fasleNorthing;
            IParameter centralMeridianParameter = pParameters[2];
            centralMeridianParameter.Value = centralMeridian;
            IParameter scaleFactorParameter = pParameters[3];
            scaleFactorParameter.Value = scaleFactor;
            IParameter latitudeOfOrigionParameter = pParameters[4];
            latitudeOfOrigionParameter.Value = latitudeOfOrigion;
            IProjectedCoordinateSystemEdit pPCSE = new ProjectedCoordinateSystemClass();
            object name = "new Dom Project cooridinate system";
            object alias = "ndpos";
            object abbreviation = "NDPCS";
            object remarks = "most eastern province in China";
            object usage = "when making maps of new Dom";
            object gcsobject = pGeographicCoordinateSystem as object;
            object unitobject = pLinearUnit as object;
            object projection = pProjectionGEN as object;
            object parameters = pParameters as object;

            pPCSE.Define(ref name, ref alias, ref abbreviation, ref remarks, ref usage, ref gcsobject, ref unitobject, ref projection, ref parameters);
            IProjectedCoordinateSystem pPCS = (IProjectedCoordinateSystem)pPCSE;
            {
                (pPCS as ISpatialReferenceTolerance).XYTolerance = 0.000001;
                pPCS.SetDomain(-9998100, -9998100, 900714804574.099, 900709927374.099);
                
            }
            return pPCS;
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            ITable pTable=pPublicFeatureWorkspace.OpenTable("DomDataTest");
            int indexofArea=pTable.Fields.FindField("AreaDefinition2");
            int indexofGCS = pTable.Fields.FindField("Globle");
            int indexoffalseEasting = pTable.Fields.FindField("falseEasting");
            int indexoffalseNorthing = pTable.Fields.FindField("falseNorthing");
            int indexofcentralMeridian = pTable.Fields.FindField("centralMeridian");
            int indexofscaleFactor = pTable.Fields.FindField("scaleFactor");
            int indexoflatitudeOfOrigion = pTable.Fields.FindField("LatitudeOfOrigion");
            ICursor pCursor=pTable.Search(null,false);
            IRow pRow=pCursor.NextRow();
            string StrXY=string.Empty;
            string StrGCS = string.Empty;
            while(pRow!=null)
             {
                 StrGCS = (string)pRow.get_Value(indexofGCS);
                 //如果pRow的“数据坐标系”是北京54  
                switch ( StrGCS )
                {
                    case "北京54":

                        Globle = "北京54";
                        StrXY = (string)pRow.get_Value(indexofArea);
                        XY = StrXY;
                        falseEasting = (double)pRow.get_Value(indexoffalseEasting);
                        falseNorthing = (double)pRow.get_Value(indexoffalseNorthing);
                        centralMeridian=(double) pRow.get_Value(indexofcentralMeridian);
                        scaleFactor = (double)pRow.get_Value(indexofscaleFactor);
                        latitudeOfOrigion = (double)pRow.get_Value(indexoflatitudeOfOrigion);

                        break;
                    case  "西安80":
                        StrXY = (string)pRow.get_Value(indexofArea);
                        XY = StrXY;

                        break;
                    case  "WGS84":
                        StrXY = (string)pRow.get_Value(indexofArea);
                        XY=StrXY;

                        break;
                    default:
                        break;

                }           
                 //如果pRow的“数据坐标系”是西安80                                   




                 //如果pRow的“数据坐标系”是WGS84
                pRow = pCursor.NextRow();

              }

 


        }

        //解析字符串
        private string[] ParseAreaDefinition2(string AreaStr)
        {
            int begin;
            int end;
            string[] split2=null;
            string[] split1;
            string sub = string.Empty;
                
            split1 = AreaStr.Split(new char[] { '+' });
            foreach (string a in split1)
            {
                if (a.Trim() != "")
                {
                    begin = a.IndexOf('(');
                    end = a.IndexOf(')');
                    sub = a.Substring(begin + 1, end - begin - 1);
                    split2 = sub.Split(new char[] { ';' });
                }

            }
            return split2;

        }



        //Create inMemoryWorkspace
        private IWorkspace CreateInMemoryWorkspace()
        {
            // Create an InMemory workspace factory.
            IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass();
            // Create an InMemory geodatabase.
            IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace",
              null, 0);
            // Cast for IName.
            IName name = (IName)workspaceName;
            //Open a reference to the InMemory workspace through the name object.
            IWorkspace workspace = (IWorkspace)name.Open();
            return workspace;
        }


        private void button2_Click_1(object sender, EventArgs e)
        {

            IWorkspace pInMemoryWorkspace = CreateInMemoryWorkspace();
            IFeatureWorkspace pInMemoryFeatureWorkspace = (IFeatureWorkspace)pInMemoryWorkspace;
            //-----------------------------------------------------------------------------

            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

            IGeometryDef pGometryDef = new GeometryDefClass();
            IGeometryDefEdit pGeometryDefEdit = (IGeometryDefEdit)pGometryDef;
            {
                pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                pGeometryDefEdit.GridCount_2 = 1;
                pGeometryDefEdit.AvgNumPoints_2 = 2;
                pGeometryDefEdit.HasM_2 = false;
                pGeometryDefEdit.HasZ_2 = false;
                pGeometryDefEdit.set_GridSize(0, 10);
                pGeometryDefEdit.SpatialReference_2 = (ISpatialReference)CreatePCSBasedOnBeijing54((IGeographicCoordinateSystem)GetGCSBeijing54(), falseEasting, falseNorthing,
                     centralMeridian, scaleFactor, latitudeOfOrigion);
            }
            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;

            pFieldEdit.Name_2 = "Globle";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldEdit.Length_2 = 50;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            pFieldEdit.GeometryDef_2 = pGometryDef;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Name_2 = "OBJECTID";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
            pFieldsEdit.AddField(pField);

            IFeatureClass pInMemoryFeatureClass = pInMemoryFeatureWorkspace.CreateFeatureClass("InMemory", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");

            //--------------------------------------------------------------------------------

            IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pInMemoryFeatureWorkspace;

            pWorkspaceEdit.StartEditing(true);
            pWorkspaceEdit.StartEditOperation();


            //设置几何字段

            string[] strXY = ParseAreaDefinition2(XY);

            IPolygon pPolygon = new PolygonClass();
            IPointCollection pPointCollection = (IPointCollection)pPolygon;
            IPoint pPoint;
            object _missing = Type.Missing;
            double x, y;
            for (int i = 0; i < strXY.Length; i++)
            {
                XandY = strXY[i].Split(new char[] { ',', ',' });
                x = double.Parse(XandY[0]);
                y = double.Parse(XandY[1]);
                pPoint = new PointClass();
                pPoint.PutCoords(x, y);
                pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            }
            pPolygon.Close();

            //------------------------------------------------------------------------

            IFeatureBuffer pFeatureBuffer = pInMemoryFeatureClass.CreateFeatureBuffer();
            IFeatureCursor pFeatureCursor = pInMemoryFeatureClass.Insert(true);

            int index;
            {
                try
                {
                    pFeatureBuffer.Shape = pPolygon;
                    index = pInMemoryFeatureClass.Fields.FindField("Globle");
                    pFeatureBuffer.set_Value(index, Globle);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            pFeatureCursor.InsertFeature(pFeatureBuffer);
            pFeatureCursor.Flush();
            //-----------------------------------------------------------------------
            pPublicInMemoryClass = pInMemoryFeatureClass;
            IFeatureLayer pFL = new FeatureLayerClass();
            pFL.FeatureClass = pInMemoryFeatureClass;
            axMapControl1.AddLayer(pFL);

            //------------------------------------------------------------------------

        }


        //复制字段,包括几何字段、非几何字段和空间参考的设置
        private IFields CloneFeatureClassFields(IFeatureClass pFeatureClass, ISpatialReference pSpatialReference)
        {
            IFields pInputFields = pFeatureClass.Fields;
            IFields pOutputFields = null;
            IField pShapeField = null;
            IGeometryDefEdit pSFGeoDefEdit = null;
            IFieldChecker pFieldChecker = new FieldCheckerClass();
            IEnumFieldError obj = null;
            pFieldChecker.Validate(pInputFields, out obj, out pOutputFields);
            pShapeField = pOutputFields.get_Field(pOutputFields.FindField(pFeatureClass.ShapeFieldName));
            pSFGeoDefEdit = (IGeometryDefEdit)pShapeField.GeometryDef;
            pSFGeoDefEdit.SpatialReference_2 = pSpatialReference;
            return pOutputFields;

        }
        //创建内存FeatureClass
        private IFeatureClass CreateInMemoryFeatureClass(string name, IFields pInputFields)
        {
            IWorkspaceFactory pInMemoryWorkspaceFactory = new InMemoryWorkspaceFactoryClass();
            IWorkspaceName pWorkspaceName = pInMemoryWorkspaceFactory.Create("", "InMemoryWorkspace", null, 0);
            IName pName = (IName)pWorkspaceName;
            IWorkspace pInMemoryWorkspace = (IWorkspace)pName.Open();
            IFeatureWorkspace pInMemoryFeatureWorkspace = (IFeatureWorkspace)pInMemoryWorkspace;
            IFeatureClass pInMemoryFeatureClass = pInMemoryFeatureWorkspace.CreateFeatureClass(name, pInputFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
            return pInMemoryFeatureClass;

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            IFeatureClass pFC = pPublicInMemoryClass;

            IFeatureClass pInsertFC = pPublicFeatureWorkspace.OpenFeatureClass("InsertDomBeijing54");

            { }



        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        //非常重要
        private void button4_Click_1(object sender, EventArgs e)
        {

            string[] strXY = ParseAreaDefinition2(XY);

            IPolygon pPolygon = new PolygonClass();
            pPolygon.SpatialReference = (ISpatialReference)CreatePCSBasedOnBeijing54((IGeographicCoordinateSystem)GetGCSBeijing54(), falseEasting, falseNorthing,
         centralMeridian, scaleFactor, latitudeOfOrigion);
            IPointCollection pPointCollection = (IPointCollection)pPolygon;
            IPoint pPoint;
            object _missing = Type.Missing;
            double x, y;
            for (int i = 0; i < strXY.Length; i++)
            {
                XandY = strXY[i].Split(new char[] { ',', ',' });
                x = double.Parse(XandY[0]);
                y = double.Parse(XandY[1]);
                pPoint = new PointClass();
                pPoint.PutCoords(x, y);
                pPointCollection.AddPoint(pPoint, ref _missing, ref _missing);
            }
            pPolygon.Close();
            pPolygon.Project(GetPCSBeijing54());


            IFeatureClass pInsertFC = pPublicFeatureWorkspace.OpenFeatureClass("InsertDomBeijing54");
            IFeatureBuffer pInsertFB = pInsertFC.CreateFeatureBuffer();
            IFeatureCursor pInsertFCursor = pInsertFC.Insert(true);
            {
                pInsertFB.Shape = pPolygon;

            }
            pInsertFCursor.InsertFeature(pInsertFB);
            pInsertFCursor.Flush();


        }
        //解析字符串






    }
}
posted on 2009-05-15 15:02  GIS的学习  阅读(4736)  评论(2编辑  收藏  举报