Example #1
0
        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));
        }
        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);
        }