public void CRSFactoryTest() { CoordinateReferenceSystemFactory crsFactory = new CoordinateReferenceSystemFactory(); CoordinateReferenceSystem crsSource = crsFactory.CreateFromName("EPSG:4326"); Assert.IsNotNull(crsSource); Assert.AreEqual("EPSG:4326", crsSource.Name); CoordinateReferenceSystem crsTarget = crsFactory.CreateFromParameters("EPSG:3875", "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"); Assert.IsNotNull(crsTarget); Assert.AreEqual("EPSG:3875", crsTarget.Name); BasicCoordinateTransform t = new BasicCoordinateTransform(crsSource, crsTarget); ProjCoordinate prjSrc = new ProjCoordinate(0, 0); ProjCoordinate prjTgt = new ProjCoordinate(); t.Transform(prjSrc, prjTgt); BasicCoordinateTransform t2 = new BasicCoordinateTransform(crsTarget, crsSource); ProjCoordinate prjTgt2 = new ProjCoordinate(); t2.Transform(prjTgt, prjTgt2); Assert.AreEqual(0d, prjSrc.Distance(prjTgt2)); }
public void SetSpatialReferences(ISpatialReference from, ISpatialReference to) { if (from == null) { this.FromSpatialReference = from; } if (to == null) { this.ToSpatialReference = to; } if ((from != null && from.Datum == null) && (to != null && to.Datum != null)) { ISpatialReference toSRef = (ISpatialReference)to.Clone(); toSRef.Datum = null; this.ToSpatialReference = toSRef; this.FromSpatialReference = from; } else if ((from != null && from.Datum != null) && (to != null && to.Datum == null)) { this.ToSpatialReference = to; ISpatialReference fromSRef = (ISpatialReference)from.Clone(); fromSRef.Datum = null; this.FromSpatialReference = fromSRef; } else { this.ToSpatialReference = to; this.FromSpatialReference = from; } _projectionPipeline = ProjectionPipeline(_fromSrs, _toSrs); _basicCoordinateTransformations = new BasicCoordinateTransform[_projectionPipeline.Length - 1]; _basicCoordinateTransformationsInverse = new BasicCoordinateTransform[_projectionPipeline.Length - 1]; for (int p = 0, p_to = _projectionPipeline.Length; p < p_to - 1; p++) { BasicCoordinateTransform t = new BasicCoordinateTransform(_projectionPipeline[p], _projectionPipeline[p + 1]); _basicCoordinateTransformations[p] = t; } for (int p = _projectionPipeline.Length; p > 1; p--) { BasicCoordinateTransform t = new BasicCoordinateTransform(_projectionPipeline[p - 1], _projectionPipeline[p - 2]); _basicCoordinateTransformationsInverse[_projectionPipeline.Length - p] = t; } }
private object Transform2D_(object geometry, bool inverse) { if (_projectionPipeline == null) { return(geometry); } CoordinateReferenceSystem from = _fromSrs, to = _toSrs; bool fromProjective = _fromProjective, toProjektive = _toProjective; if (geometry == null) { return(null); } if (from == null || to == null) { return(geometry); } if (geometry is PointCollection) { IPointCollection pColl = (IPointCollection)geometry; int pointCount = pColl.PointCount; if (pointCount == 0) { return(geometry); } IPointCollection target = null; var basicTransformations = BasicTransformations(inverse); for (int b = 0, b_to = basicTransformations.Length; b < b_to; b++) { BasicCoordinateTransform t = basicTransformations[b]; if (pColl is IRing) { target = new Ring(); } else if (pColl is IPath) { target = new Path(); } else if (pColl is IMultiPoint) { target = new MultiPoint(); } else { target = new PointCollection(); } for (int i = 0; i < pointCount; i++) { ProjCoordinate cFrom = new ProjCoordinate(pColl[i].X, pColl[i].Y); var cTo = new ProjCoordinate(); t.Transform(cFrom, cTo); target.AddPoint(new Point(cTo.X, cTo.Y)); } pColl = target; } return(target); } if (geometry is IPoint) { IPoint target = null; var basicTransformations = BasicTransformations(inverse); for (int b = 0, b_to = basicTransformations.Length; b < b_to; b++) { BasicCoordinateTransform t = basicTransformations[b]; ProjCoordinate cFrom = new ProjCoordinate(((IPoint)geometry).X, ((IPoint)geometry).Y), cTo = new ProjCoordinate(); t.Transform(cFrom, cTo); target = new Point(cTo.X, cTo.Y); geometry = target; } return(target); } if (geometry is IEnvelope) { return(Transform2D_(((IEnvelope)geometry).ToPolygon(10), inverse)); } if (geometry is IPolyline) { int count = ((IPolyline)geometry).PathCount; IPolyline polyline = new Polyline(); for (int i = 0; i < count; i++) { polyline.AddPath((IPath)Transform2D_(((IPolyline)geometry)[i], inverse)); } return(polyline); } if (geometry is IPolygon) { int count = ((IPolygon)geometry).RingCount; IPolygon polygon = new Polygon(); for (int i = 0; i < count; i++) { polygon.AddRing((IRing)Transform2D_(((IPolygon)geometry)[i], inverse)); } return(polygon); } if (geometry is IAggregateGeometry) { int count = ((IAggregateGeometry)geometry).GeometryCount; IAggregateGeometry aGeom = new AggregateGeometry(); for (int i = 0; i < count; i++) { aGeom.AddGeometry((IGeometry)Transform2D_(((IAggregateGeometry)geometry)[i], inverse)); } return(aGeom); } return(null); }