1 //如何构建Point 2 private IPoint ConstructPoint(double x, double y) 3 { 4 IPoint pPoint = new ESRI.ArcGIS.Geometry.Point(); 5 pPoint.PutCoords(x,y); 6 return pPoint; 7 } 8 9 10 //如何构建MultiPoint 11 private IGeometry GetMutipointGeometry() 12 { 13 const double multiPointCount = 25; 14 IPointCollection pointCollection = new Multipoint(); 15 for (int i = 0; i < multiPointCount; i++) 16 { 17 pointCollection.AddPoint(GetPoint(),Type.Missing,Type.Missing); 18 } 19 return pointCollection as IGeometry; 20 } 21 private IPoint GetPoint() 22 { 23 const double min=-10; 24 const double max=10; 25 Random random=new Random(); 26 double x=min+(max-min)*random.NextDouble(); 27 double y = min + (max - min) * random.NextDouble(); 28 return ConstructPoint(x,y); 29 } 30 31 //Polyline的构成 32 private IGeometry GetPolylineGeometry() 33 { 34 const double pathCount = 3; 35 const double pathVetexCount = 3; 36 IGeometryCollection pGeometryCollection = new Polyline() as IGeometryCollection; 37 for (int i = 0; i < pathCount; i++) 38 { 39 IPointCollection pointCollection = new Path(); 40 for (int j = 0; j < pathVetexCount; j++) 41 { 42 pointCollection.AddPoint(GetPoint(),Type.Missing,Type.Missing); 43 } 44 pGeometryCollection.AddGeometry(pointCollection as IGeometry,Type.Missing,Type.Missing); 45 } 46 return pGeometryCollection as IGeometry; 47 } 48 49 //构造Polygon 50 public IPolygon CreatePolygonByPoints(IPointCollection pointCollection) 51 { 52 IGeometryBridge2 pGeometryBridge2 = new GeometryEnvironment() as IGeometryBridge2; 53 IPointCollection4 polygon = new Polygon() as IPointCollection4; 54 WKSPoint[] wksPoint = new WKSPoint[pointCollection.PointCount]; 55 for (int i = 0; i < pointCollection.PointCount; i++) 56 { 57 wksPoint[i].X = pointCollection.get_Point(i).X; 58 wksPoint[i].Y = pointCollection.get_Point(i).Y; 59 } 60 pGeometryBridge2.SetWKSPoints(polygon as IPointCollection4, wksPoint); 61 IPolygon pPoly = polygon as IPolygon; 62 pPoly.Close(); 63 return pPoly; 64 } 65 66 //通过IGeometryCollection构造Polygon对象 67 private IPolygon ConstructorPolygon(List<IRing> ringList) 68 { 69 try 70 { 71 IGeometryCollection pGCollection = new Polygon() as IGeometryCollection; 72 object o = Type.Missing; 73 for (int i = 0; i < ringList.Count; i++) 74 { 75 //通过IGeometryCollection的AddGeometry方法像polygon里添加Ring对象 76 pGCollection.AddGeometry(ringList[i]); 77 } 78 //QI至ITopologicalOperator 79 ITopologicalOperator pTopoloical = pGCollection as ITopologicalOperator; 80 //执行Simplify操作 81 pTopoloical.Simplify(); 82 IPolygon polygon = pTopoloical as IPolygon; 83 return polygon; 84 85 } 86 catch(Exception ex) 87 { 88 return null; 89 } 90 } 91 92 private IPolygon MergePoilygons(IPolygon firstPolygon, IPolygon secondPolygon) 93 { 94 try 95 { 96 //创建一个Polygon对象 97 IGeometryCollection pGCollection = new Polygon() as IGeometryCollection; 98 IGeometryCollection pGCollection1 = firstPolygon as IGeometryCollection; 99 IGeometryCollection pGCollection2 = secondPolygon as IGeometryCollection; 100 pGCollection.AddGeometryCollection(pGCollection1); 101 pGCollection.AddGeometryCollection(pGCollection2); 102 ITopologicalOperator pTopological = pGCollection as ITopologicalOperator; 103 pTopological.Simplify(); 104 IPolygon polygon = pTopological as IPolygon; 105 return polygon; 106 } 107 catch (Exception ex) 108 { 109 return null; 110 } 111 } 112 113 //等距离打断线 114 private IEnumGeometry MakeMutiPoints(IPolyline pGeometry,int intPoints) 115 { 116 IConstructGeometryCollection pConGeoCollection = new GeometryBag() as IConstructGeometryCollection; 117 pConGeoCollection.ConstructDivideEqual(pGeometry, intPoints, esriConstructDivideEnum.esriDivideIntoPolylines); 118 IEnumGeometry pEnumGeo = pConGeoCollection as IEnumGeometry; 119 return pEnumGeo; 120 } 121 //同一基准面的坐标转换 122 private IPoint GetpProjectedPoint(IPoint pPoint, bool pBool) 123 { 124 ISpatialReferenceFactory pSpaceReferenceFactory = new SpatialReferenceEnvironment(); 125 ISpatialReference pFromSpatialReference = pSpaceReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980); 126 ISpatialReference pToSpatialReference = pSpaceReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_34); 127 if (pBool == true) 128 { 129 IGeometry pGeo = pPoint as IGeometry; 130 pGeo.SpatialReference = pFromSpatialReference; 131 pGeo.Project(pToSpatialReference); 132 return pPoint; 133 } 134 else 135 { 136 IGeometry pGeo = pPoint as IPoint; 137 pGeo.SpatialReference = pToSpatialReference; 138 pGeo.Project(pFromSpatialReference); 139 return pPoint; 140 } 141 } 142 //不同基准面子之间的坐标转换 143 public void ProjectExExample() 144 { 145 ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment(); 146 // ISpatialReference pFromCustom = pSpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(@"E:1.prj"); 147 148 IPoint pFromPoint = new ESRI.ArcGIS.Geometry.Point(); 149 pFromPoint.X = 518950.788; 150 pFromPoint.Y = 4335923.97; 151 IZAware pZAware = pFromPoint as IZAware; 152 pZAware.ZAware = true; 153 pFromPoint.Z = 958.4971; 154 //pFromPoint.SpatialReference = pFromCustom; 155 156 //自定义下定的北京6度19带 157 pFromPoint.SpatialReference = CreateCustomProjectedSystem(); 158 159 //目标投影 160 IProjectedCoordinateSystem projectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_19); 161 162 //因为目标基准面和原始基准面不在同一个基准面上,所以牵涉参数的设置,我们用七参数转换 163 ICoordinateFrameTransformation pCoordinateFrameTransformation = new CoordinateFrameTransformation() as ICoordinateFrameTransformation; 164 pCoordinateFrameTransformation.PutParameters(-112.117,4.530,21.89,-0.00058702,-0.00476421,0.00009358,0.99998006411); 165 pCoordinateFrameTransformation.PutSpatialReferences(CreateCustomProjectedSystem(),projectedCoordinateSystem as ISpatialReference); 166 167 168 169 //投影转换 170 IGeometry2 pGeometry = pFromPoint as IGeometry2; 171 pGeometry.ProjectEx(projectedCoordinateSystem ,esriTransformDirection.esriTransformForward,pCoordinateFrameTransformation,false,0,0); 172 173 174 } 175 176 private IProjectedCoordinateSystem CreateCustomProjectedSystem() 177 { 178 ISpatialReferenceFactory2 pSpatialReferenceFactory = new SpatialReferenceEnvironment() as ISpatialReferenceFactory2; 179 IProjectionGEN pProjection = pSpatialReferenceFactory.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger) as IProjectionGEN; 180 181 IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); 182 183 ILinearUnit pUnit = pSpatialReferenceFactory.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter) as ILinearUnit; 184 185 IParameter[] pParameter = pProjection.GetDefaultParameters(); 186 IProjectedCoordinateSystemEdit pProjectedCoordinateSystemEdit = new ProjectedCoordinateSystem() as IProjectedCoordinateSystemEdit; 187 188 object pName = "WGS-Beijing1954"; 189 object pAlias = "WGS-Beijing1954"; 190 object pAbbreviation = "WGS-Beijing1954"; 191 object pRemarks = "WGS-Beijing1954"; 192 object pUsage = "Caculate meter from lat and lon"; 193 object ppGeographicCoordinateSystemObject = pGeographicCoordinateSystem ; 194 object pUnitObject = pUnit; 195 object pProjectionObject = pProjection; 196 object pParameterObject = pParameter; 197 198 pProjectedCoordinateSystemEdit.Define(ref pName, ref pAlias, ref pAbbreviation, ref pRemarks, ref pUsage, ref ppGeographicCoordinateSystemObject, ref pUnitObject, ref pProjectionObject, ref pParameterObject); 199 200 IProjectedCoordinateSystem5 pProjectedCoordinateSystem = pProjectedCoordinateSystemEdit as IProjectedCoordinateSystem5; 201 pProjectedCoordinateSystem.FalseEasting = 5000000; 202 pProjectedCoordinateSystem.LatitudeOfOrigin = 0; 203 pProjectedCoordinateSystem.set_CentralMeridian(true,111); 204 pProjectedCoordinateSystem.ScaleFactor = 1; 205 pProjectedCoordinateSystem.FalseNorthing = 0; 206 return pProjectedCoordinateSystem; 207 }