C# Arcgis 中 Point Polyline Polygon 等的操作

//如何构建Point
        private IPoint ConstructPoint(double x, double y)
        {
            IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
            pPoint.PutCoords(x,y);
            return pPoint;
        }


        //如何构建MultiPoint
        private IGeometry GetMutipointGeometry()
        {
            const double multiPointCount = 25;
            IPointCollection pointCollection = new Multipoint();
            for (int i = 0; i < multiPointCount; i++)
            {
                pointCollection.AddPoint(GetPoint(),Type.Missing,Type.Missing);
            }
            return pointCollection as IGeometry;
        }
        private IPoint GetPoint()
        { 
            const double min=-10;
            const double max=10;
            Random random=new Random();
            double x=min+(max-min)*random.NextDouble();
            double y = min + (max - min) * random.NextDouble();
            return ConstructPoint(x,y);
        }

        //Polyline的构成
        private IGeometry GetPolylineGeometry()
        {
            const double pathCount = 3;
            const double pathVetexCount = 3;
            IGeometryCollection pGeometryCollection = new Polyline() as IGeometryCollection;
            for (int i = 0; i < pathCount; i++)
            {
                IPointCollection pointCollection = new Path();
                for (int j = 0; j < pathVetexCount; j++)
                {
                    pointCollection.AddPoint(GetPoint(),Type.Missing,Type.Missing);
                }
                pGeometryCollection.AddGeometry(pointCollection as IGeometry,Type.Missing,Type.Missing);
            }
            return pGeometryCollection as IGeometry;
        }

        //构造Polygon
        public IPolygon CreatePolygonByPoints(IPointCollection pointCollection)
        {
            IGeometryBridge2 pGeometryBridge2 = new GeometryEnvironment() as IGeometryBridge2;
            IPointCollection4 polygon = new Polygon() as IPointCollection4;
            WKSPoint[] wksPoint = new WKSPoint[pointCollection.PointCount];
            for (int i = 0; i < pointCollection.PointCount; i++)
            {
                wksPoint[i].X = pointCollection.get_Point(i).X;
                wksPoint[i].Y = pointCollection.get_Point(i).Y;
            }
            pGeometryBridge2.SetWKSPoints(polygon as IPointCollection4, wksPoint);
            IPolygon pPoly = polygon as IPolygon;
            pPoly.Close();
            return pPoly;
        }

        //通过IGeometryCollection构造Polygon对象
        private IPolygon ConstructorPolygon(List ringList)
        {
            try
            {
                IGeometryCollection pGCollection = new Polygon() as IGeometryCollection;
                object o = Type.Missing;
                for (int i = 0; i < ringList.Count; i++)
                { 
                  //通过IGeometryCollection的AddGeometry方法像polygon里添加Ring对象
                    pGCollection.AddGeometry(ringList[i]);
                }
                //QI至ITopologicalOperator
                ITopologicalOperator pTopoloical = pGCollection as ITopologicalOperator;
                //执行Simplify操作
                pTopoloical.Simplify();
                IPolygon polygon = pTopoloical as IPolygon;
                return polygon;

            }
            catch(Exception ex)
            {
                return null;
            }            
        }

        private IPolygon MergePoilygons(IPolygon firstPolygon, IPolygon secondPolygon)
        {
            try
            {
                //创建一个Polygon对象
                IGeometryCollection pGCollection = new Polygon() as IGeometryCollection;
                IGeometryCollection pGCollection1 = firstPolygon as IGeometryCollection;
                IGeometryCollection pGCollection2 = secondPolygon as IGeometryCollection;
                pGCollection.AddGeometryCollection(pGCollection1);
                pGCollection.AddGeometryCollection(pGCollection2);
                ITopologicalOperator pTopological = pGCollection as ITopologicalOperator;
                pTopological.Simplify();
                IPolygon polygon = pTopological as IPolygon;
                return polygon;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

       //等距离打断线
        private IEnumGeometry MakeMutiPoints(IPolyline pGeometry,int intPoints)
        {
            IConstructGeometryCollection pConGeoCollection = new GeometryBag() as IConstructGeometryCollection;
            pConGeoCollection.ConstructDivideEqual(pGeometry, intPoints, esriConstructDivideEnum.esriDivideIntoPolylines);
            IEnumGeometry pEnumGeo = pConGeoCollection as IEnumGeometry;
            return pEnumGeo;
        }
        //同一基准面的坐标转换
        private IPoint GetpProjectedPoint(IPoint pPoint, bool pBool)
        {
            ISpatialReferenceFactory pSpaceReferenceFactory = new SpatialReferenceEnvironment();
            ISpatialReference pFromSpatialReference = pSpaceReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980);
            ISpatialReference pToSpatialReference = pSpaceReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_34);
            if (pBool == true)
            {
                IGeometry pGeo = pPoint as IGeometry;
                pGeo.SpatialReference = pFromSpatialReference;
                pGeo.Project(pToSpatialReference);
                return pPoint;
            }
            else 
            {
                IGeometry pGeo = pPoint as IPoint;
                pGeo.SpatialReference = pToSpatialReference;
                pGeo.Project(pFromSpatialReference);
                return pPoint;
            }        
        }
        //不同基准面子之间的坐标转换
        public void ProjectExExample()
        {
            ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
           // ISpatialReference pFromCustom = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(@"E:\1.prj");

            IPoint pFromPoint = new ESRI.ArcGIS.Geometry.Point();
            pFromPoint.X = 518950.788;
            pFromPoint.Y = 4335923.97;
            IZAware pZAware = pFromPoint as IZAware;
            pZAware.ZAware = true;
            pFromPoint.Z = 958.4971;
            //pFromPoint.SpatialReference = pFromCustom;

            //自定义下定的北京6度19带
            pFromPoint.SpatialReference = CreateCustomProjectedSystem();

            //目标投影
            IProjectedCoordinateSystem projectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_19);
            
            //因为目标基准面和原始基准面不在同一个基准面上,所以牵涉参数的设置,我们用七参数转换
            ICoordinateFrameTransformation pCoordinateFrameTransformation = new CoordinateFrameTransformation() as ICoordinateFrameTransformation;
            pCoordinateFrameTransformation.PutParameters(-112.117,4.530,21.89,-0.00058702,-0.00476421,0.00009358,0.99998006411);
            pCoordinateFrameTransformation.PutSpatialReferences(CreateCustomProjectedSystem(),projectedCoordinateSystem as ISpatialReference);



            //投影转换
            IGeometry2 pGeometry = pFromPoint as IGeometry2;
            pGeometry.ProjectEx(projectedCoordinateSystem ,esriTransformDirection.esriTransformForward,pCoordinateFrameTransformation,false,0,0);


        }

        private IProjectedCoordinateSystem CreateCustomProjectedSystem()
        {
            ISpatialReferenceFactory2 pSpatialReferenceFactory = new SpatialReferenceEnvironment() as ISpatialReferenceFactory2;
            IProjectionGEN pProjection = pSpatialReferenceFactory.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger) as IProjectionGEN;

            IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

            ILinearUnit pUnit = pSpatialReferenceFactory.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter) as ILinearUnit;

            IParameter[] pParameter = pProjection.GetDefaultParameters();
            IProjectedCoordinateSystemEdit pProjectedCoordinateSystemEdit = new ProjectedCoordinateSystem() as IProjectedCoordinateSystemEdit;

            object pName = "WGS-Beijing1954";
            object pAlias = "WGS-Beijing1954";
            object pAbbreviation = "WGS-Beijing1954";
            object pRemarks = "WGS-Beijing1954";
            object pUsage = "Caculate meter from lat and lon";
            object ppGeographicCoordinateSystemObject = pGeographicCoordinateSystem ;
            object pUnitObject = pUnit;
            object pProjectionObject = pProjection;
            object pParameterObject = pParameter;

            pProjectedCoordinateSystemEdit.Define(ref pName, ref pAlias, ref pAbbreviation, ref pRemarks, ref pUsage, ref ppGeographicCoordinateSystemObject, ref pUnitObject, ref pProjectionObject, ref pParameterObject);

            IProjectedCoordinateSystem5 pProjectedCoordinateSystem = pProjectedCoordinateSystemEdit as IProjectedCoordinateSystem5;
            pProjectedCoordinateSystem.FalseEasting = 5000000;
            pProjectedCoordinateSystem.LatitudeOfOrigin = 0;
            pProjectedCoordinateSystem.set_CentralMeridian(true,111);
            pProjectedCoordinateSystem.ScaleFactor = 1;
            pProjectedCoordinateSystem.FalseNorthing = 0;
            return pProjectedCoordinateSystem;
        }

摘自:https://www.cnblogs.com/rockman/p/3405094.html

你可能感兴趣的:(ArcGIS,C#)