Beispiel #1
0
        public void TestCopenhagen()
        {
            var ctx = new ProjContext();
            var src = CoordinateReferenceSystem.Create("EPSG:4326", ctx);
            var dst = CoordinateReferenceSystem.Create(/*"+proj=utm +zone=32 +datum=WGS84" or */ "EPSG:32632", ctx);

            Assert.AreEqual("WGS 84", src.Name);
            Assert.AreEqual("WGS 84 / UTM zone 32N", dst.Name);

            var t = CoordinateTransform.Create(src, dst, ctx);

            var t2 = CoordinateTransform.Create(src.WithAxisNormalized(), dst.WithAxisNormalized(), ctx);


            var p = t2.ApplyReversed(new double[] { 12, 55 });

            Trace.WriteLine($"Easting: {p[0]}, Northing: {p[1]}");

            var r = t2.ApplyReversed(p);

            Trace.WriteLine($"Longitude: {r[0]}, Latitude: {r[1]}");


            var tt = CoordinateTransform.Create(src, src, null);

            Assert.AreEqual("Null geographic offset from WGS 84 to WGS 84", tt.Name);

            var ss = ctx.Create("+proj=utm +zone=32 +datum=WGS84 +ellps=clrk66");
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TGeometry"></typeparam>
        /// <param name="geometry"></param>
        /// <param name="toSrid"></param>
        /// <returns></returns>
        public static TGeometry Reproject <TGeometry>(this TGeometry geometry, SridItem toSrid)
            where TGeometry : Geometry
        {
            if (geometry == null)
            {
                return(null);
            }

            if (toSrid == null)
            {
                throw new ArgumentNullException(nameof(toSrid));
            }

            int srcSRID = geometry.SRID;

            if (srcSRID == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(geometry), "Geometry doesn't have valid srid");
            }

            SridItem srcItem = SridRegister.GetByValue(srcSRID);

            using (ProjContext pc = toSrid.CRS.Context.Clone()) // Make thread-safe. Use settings from crs
                using (CoordinateTransform ct = CoordinateTransform.Create(srcItem, toSrid, pc))
                {
                    return(Reproject(geometry, ct, toSrid.Factory));
                }
        }
Beispiel #3
0
        public void CreateBasicTransform()
        {
            using (var pc = new ProjContext())
            {
                using (var crs1 = CoordinateReferenceSystem.Create("EPSG:25832", pc))
                    using (var crs2 = CoordinateReferenceSystem.Create("EPSG:25833", pc))
                    {
                        Assert.AreEqual(ProjType.ProjectedCrs, crs1.Type);
                        Assert.AreEqual(ProjType.ProjectedCrs, crs2.Type);

                        using (var t = CoordinateTransform.Create(crs1, crs2))
                        {
                            CoordinateTransformList steps = t as CoordinateTransformList;
                            Assert.IsNotNull(steps);

                            Assert.AreEqual(2, steps.Count);
                            Assert.AreEqual("Inverse of Transverse Mercator", steps[0].MethodName);
                            Assert.AreEqual("Transverse Mercator", steps[1].MethodName);

                            Assert.AreEqual("Inverse of UTM zone 32N + UTM zone 33N", t.Name);

                            using (var tr = t.CreateInverse())
                            {
                                Assert.AreEqual("Inverse of UTM zone 33N + UTM zone 32N", tr.Name);
                            }
                        }

                        using (var t = CoordinateTransform.Create(crs2, crs1))
                        {
                            Assert.AreEqual("Inverse of UTM zone 33N + UTM zone 32N", t.Name);
                        }
                    }
            }
        }
Beispiel #4
0
        public void DistanceInNL()
        {
            using (ProjContext pc = new ProjContext())
            {
                pc.AllowNetworkConnections = true;

                Coordinate dom, servaas;

                using (var nl = CoordinateReferenceSystem.Create("EPSG:28992", pc))
                {
                    using (var wgs84 = CoordinateReferenceSystem.Create("EPSG:4326", pc))
                    {
                        using (var t = CoordinateTransform.Create(wgs84, nl))
                        {
                            dom     = t.Apply(DomUtrechtWGS84);
                            servaas = t.Apply(StServaasMaastrichtWGS84);
                        }

                        Assert.AreEqual(143.562, Math.Round(wgs84.DistanceTransform.GeoDistance(DomUtrechtWGS84, StServaasMaastrichtWGS84)) / 1000.0, "Distance WGS84");
                    }

                    Assert.AreEqual(143.562, Math.Round(nl.DistanceTransform.GeoDistance(dom, servaas), 0) / 1000.0, "Distance RD");

                    double dx = (dom.X - servaas.X);
                    double dy = (dom.Y - servaas.Y);

                    Assert.AreEqual(143.556, Math.Round(Math.Sqrt(dx * dx + dy * dy)) / 1000.0, "Distance pythagoras");
                }

                using (var nlNAP = CoordinateReferenceSystem.Create("EPSG:7415", pc))
                {
                    Assert.IsNotNull(nlNAP.GeodeticCRS);

                    if (nlNAP is CoordinateReferenceSystemList l)
                    {
                        var cs0 = l[0];
                        var cs1 = l[1];

                        GC.KeepAlive(cs0);
                        GC.KeepAlive(cs1);
                    }

                    using (var WGS84 = CoordinateReferenceSystem.Create("EPSG:4329", pc))
                    {
                        GC.KeepAlive(WGS84);
                    }
                }
            }
        }
Beispiel #5
0
        public void EpsgEnumTests()
        {
            var r       = SridRegister.GetById(Epsg.Netherlands);
            var factory = r.Factory;

            //SharpProj.
            Assert.AreEqual((int)Epsg.Netherlands, r.SRID);
            Assert.AreEqual((int)Epsg.Netherlands, factory.SRID);

            Assert.AreEqual((int)Epsg.BelgiumLambert, SridRegister.GetById(Epsg.BelgiumLambert).SRID);

            Assert.AreEqual(2, r.CRS.CoordinateSystem.Axis.Count);
            Point p = factory.CreatePoint(new Coordinate(155000, 463000));

            Point pp = p.Reproject(SridRegister.GetById(Epsg.BelgiumLambert));

            Assert.IsNotNull(pp);

            Assert.IsNotNull(p.Reproject <Geometry>(SridRegister.GetById(Epsg.BelgiumLambert)));

            Assert.AreEqual((int)Epsg.BelgiumLambert, pp.SRID);
            Assert.AreEqual(new Point(719706, 816781), new Point(pp.Coordinate.RoundAll(0)));

            using (CoordinateTransform t = CoordinateTransform.Create(SridRegister.GetByValue(p.SRID), SridRegister.GetById(Epsg.BelgiumLambert), new CoordinateTransformOptions {
                NoBallparkConversions = true
            }))
            {
                if (t is CoordinateTransformList mc)
                {
                    Assert.AreEqual(3, mc.Count);
                    Assert.AreEqual(5, mc[0].Parameters.Count);
                    Assert.AreEqual(7, mc[1].Parameters.Count);
                    Assert.AreEqual(6, mc[2].Parameters.Count);
                }
                else
                {
                    Assert.Fail();
                }

                var rr = t.Apply(new PPoint(155000, 463000));
            }
        }
Beispiel #6
0
        public void TestSpain()
        {
            using (var c = new ProjContext())
            {
                using (var wgs84 = CoordinateReferenceSystem.Create("EPSG:4326", c))
                    using (var google = CoordinateReferenceSystem.Create("EPSG:3857", c))
                        using (var q1 = CoordinateReferenceSystem.Create("EPSG:23030"))
                            using (var q2 = CoordinateReferenceSystem.Create("EPSG:2062"))
                            {
                                Assert.AreEqual("Engineering survey, topographic mapping.", q1.Scope);
                                Assert.AreEqual("Engineering survey, topographic mapping.", q2.Scope);

                                using (var t = CoordinateTransform.Create(google, wgs84))
                                {
                                    var r = t.Apply(-333958.47, 4865942.28);
                                    Assert.AreEqual(0, t.GridUsageCount);
                                    Assert.AreEqual(40.0, Math.Round(r[0], 3));
                                    Assert.AreEqual(-3, Math.Round(r[1], 3));
                                }

                                using (var t = CoordinateTransform.Create(google, q1))
                                {
                                    var r = t.Apply(-333958.47, 4865942.28);
                                    Assert.AreEqual(0, t.GridUsageCount);

                                    Assert.AreEqual(500110.0, Math.Round(r[0], 0));
                                    Assert.AreEqual(4427965.0, Math.Round(r[1], 0));
                                }

                                using (var t = CoordinateTransform.Create(google, q2))
                                {
                                    var r = t.Apply(-333958.47, 4865942.28);
                                    Assert.AreEqual(0, t.GridUsageCount);

                                    Assert.AreEqual(658629.5, Math.Round(r[0], 1));
                                    Assert.AreEqual(600226.1, Math.Round(r[1], 1));
                                }
                            }
            }
        }
Beispiel #7
0
        public void TestAmersfoort()
        {
            using (var c = new ProjContext())
            {
                using (var rd = CoordinateReferenceSystem.Create("EPSG:28992", c))
                    using (var wgs84 = CoordinateReferenceSystem.Create("EPSG:4326", c))
                        using (var google = CoordinateReferenceSystem.Create("EPSG:3857", c))
                        {
                            var area = rd.UsageArea;

                            Assert.IsNotNull(area);
                            Assert.AreEqual("Netherlands - onshore, including Waddenzee, Dutch Wadden Islands and 12-mile offshore coastal zone.", area.Name);
                            Assert.AreEqual(3.2, area.WestLongitude);
                            Assert.AreEqual(7.22, area.EastLongitude);


                            using (var t = CoordinateTransform.Create(rd, wgs84))
                            {
                                var r = t.Apply(155000, 463000);
                                Assert.AreEqual(52.155, Math.Round(r[0], 3));
                                Assert.AreEqual(5.387, Math.Round(r[1], 3));

                                Assert.AreEqual(1, t.Accuraracy);
                            }

                            using (var t = CoordinateTransform.Create(rd, google))
                            {
                                var r = t.Apply(155000, 463000);

                                Assert.AreEqual(599701.0, Math.Round(r[0], 0));
                                Assert.AreEqual(6828231.0, Math.Round(r[1], 0));

                                Assert.AreEqual(1, t.Accuraracy);
                            }
                        }
            }
        }
Beispiel #8
0
        public void HeightConversionNLBE()
        {
            using (ProjContext pc = new ProjContext())
                using (var nlNAP = CoordinateReferenceSystem.Create("EPSG:7415", pc))
                    using (var beOstend = CoordinateReferenceSystem.Create("EPSG:8370", pc))
                    {
                        pc.AllowNetworkConnections = true;
                        Assert.AreEqual(3, nlNAP.AxisCount);
                        Assert.AreEqual(3, beOstend.AxisCount);

                        Coordinate domNL, servaasNL;
                        using (var wgs84 = CoordinateReferenceSystem.Create("EPSG:4326", pc))
                            using (var wgs84D3 = CoordinateReferenceSystem.Create("EPSG:4329", pc))
                            {
                                Assert.AreEqual(2, wgs84.AxisCount);
                                Assert.AreEqual(3, wgs84D3.AxisCount);

                                using (var n = wgs84D3.WithAxisNormalized())
                                {
                                    Assert.IsFalse(wgs84D3.IsEquivalentTo(n));
                                    Assert.IsTrue(wgs84D3.IsEquivalentToRelaxed(n));
                                }

                                Coordinate domWGS84       = new Coordinate(DomUtrechtWGS84);
                                Coordinate stServaasWGS84 = new Coordinate(StServaasMaastrichtWGS84);

                                using (var t = CoordinateTransform.Create(wgs84, nlNAP))
                                {
                                    domNL = t.Apply(domWGS84);

                                    Assert.AreEqual(new CoordinateZ(136898.7, 455851.9, -43.4), domNL.RoundAll(1));

                                    servaasNL = t.Apply(stServaasWGS84);

                                    Assert.AreEqual(new CoordinateZ(176164.5, 317770.5, -45.7), servaasNL.RoundAll(1));
                                }


                                using (var t = CoordinateTransform.Create(nlNAP, beOstend))
                                {
                                    var servaasBE = t.Apply(servaasNL);

                                    Assert.AreEqual(new CoordinateZ(742877.4, 671835, -45.7), servaasBE.RoundAll(1));

                                    servaasNL.Z = 15;
                                    servaasBE   = t.Apply(servaasNL);

                                    Assert.IsTrue(new CoordinateZ(742877.4, 671835, 15).Equals3D(servaasBE.RoundAll(1)), $"Unexpected coordinate {servaasBE.RoundAll(1)}");

                                    servaasNL.Z = 0; //revert to original value
                                }


                                using (var t = CoordinateTransform.Create(nlNAP, wgs84D3))
                                {
                                    var domGPS = t.Apply(domNL);
                                    Assert.AreEqual(new CoordinateZ(52.09063, 5.123078, 0), domGPS.RoundAll(7));

                                    var servaasGPS = t.Apply(servaasNL);
                                    Assert.AreEqual(new CoordinateZ(50.84938, 5.687712, 45.7353891), servaasGPS.RoundAll(7));

                                    servaasNL.Z = 15;

                                    servaasGPS = t.Apply(servaasNL);

                                    Assert.IsTrue(new CoordinateZ(50.84938, 5.68771, 60.73539).Equals3D(servaasGPS.RoundAll(5)), $"Unexpected coordinate {servaasGPS.RoundAll(5)}");
                                }
                            }
                    }
        }
Beispiel #9
0
        public void WithGrid()
        {
            using (var pc = new ProjContext())
            {
                // Don't use old cache
                pc.SetGridCache(true, Path.Combine(TestContext.TestResultsDirectory, "proj.cache"), 300, 3600 * 24);
                pc.LogLevel = ProjLogLevel.Trace;

                using (var crsAmersfoort = CoordinateReferenceSystem.Create(@"EPSG:4289", pc)) // Amersfoort
                    using (var crsETRS89 = CoordinateReferenceSystem.Create(@"EPSG:4258", pc))
                    {
                        // Do it the dumb way
                        using (var t = CoordinateTransform.Create(crsAmersfoort, crsETRS89))
                        {
                            Assert.IsFalse(t is ChooseCoordinateTransform);
                            var r = t.Apply(51, 4, 0);

                            Assert.AreEqual(50.999, Math.Round(r[0], 3));
                            Assert.AreEqual(4.0, Math.Round(r[1], 3));
                        }

                        // Now, let's enable gridshifts
                        Assert.IsFalse(pc.AllowNetworkConnections);
                        pc.AllowNetworkConnections = true;
                        pc.EndpointUrl             = ProjContext.DefaultEndpointUrl;// "https://cdn.proj.org";
                        bool usedHttp = false;
                        pc.Log += (_, x) => { if (x.Contains("https://"))
                                              {
                                                  usedHttp = true;
                                              }
                        };

                        using (var t = CoordinateTransform.Create(crsAmersfoort, crsETRS89))
                        {
                            ChooseCoordinateTransform cl = t as ChooseCoordinateTransform;
                            Assert.IsNotNull(cl);
                            Assert.AreEqual(2, cl.Count);

                            Assert.IsTrue(cl[0].GridUsageCount > 0);
                            Assert.IsTrue(cl[1].GridUsageCount == 0);

                            Assert.AreEqual(new PPoint(50.999, 4.0), t.Apply(new PPoint(51, 4)).RoundXY(3));
                            var r = t.Apply(51, 4, 0);
                            Assert.IsTrue(usedHttp, "Now http");


                            var r0 = cl[0].Apply(51, 4, 0);
                            usedHttp = false;
                            var r1 = cl[1].Apply(51, 4, 0);
                            Assert.IsFalse(usedHttp, "No http");
                            Assert.IsNotNull(r0);
                            Assert.IsNotNull(r1);

                            Assert.AreEqual(50.999, Math.Round(r0[0], 3));
                            Assert.AreEqual(4.0, Math.Round(r0[1], 3));

                            Assert.AreEqual(50.999, Math.Round(r1[0], 3));
                            Assert.AreEqual(4.0, Math.Round(r1[1], 3));

                            Assert.IsNotNull(cl[0].MethodName);
                        }
                    }
            }
        }
Beispiel #10
0
        public void FewEpsg()
        {
            bool hasDeprecated = false;

            using (var wgs84 = CoordinateReferenceSystem.Create("EPSG:4326"))
            {
                for (int i = 2000; i < 2200; i++)
                {
                    CoordinateReferenceSystem crs;
                    try
                    {
                        crs = CoordinateReferenceSystem.Create($"EPSG:{i}");
                    }
                    catch (ProjException)
                    {
                        Trace.WriteLine($"Not supported: {i}");
                        //Assert.IsTrue(new int[] { 0, 1, 2,3 }.Contains(i), $"EPSG {i} not supported");
                        continue;
                    }

                    using (crs)
                    {
                        if (crs.IsDeprecated)
                        {
                            hasDeprecated = true;
                            continue;
                        }

                        CoordinateTransform t;
                        try
                        {
                            t = CoordinateTransform.Create(wgs84, crs, new CoordinateTransformOptions {
                                NoBallparkConversions = false
                            });
                        }
                        catch (ProjException)
                        {
                            Trace.WriteLine($"Not convertible: {i}");
                            //Assert.IsTrue(new int[] { 0, 1, 2,3 }.Contains(i), $"EPSG {i} not supported");
                            continue;
                        }


                        using (t)
                        {
                            var a = crs.UsageArea;

                            double[] center;
                            try
                            {
                                center = t.Apply((a.EastLongitude + a.WestLongitude) / 2, (a.NorthLatitude + a.SouthLatitude) / 2);
                            }
                            catch (ProjException)
                            {
                                center = null;
                            }


                            if (center != null && t.HasInverse && !(t is ChooseCoordinateTransform))
                            {
                                double[] ret = t.ApplyReversed(center);
                            }
                        }
                    }
                }
            }
            Assert.IsTrue(hasDeprecated, "Found deprecated");
        }
Beispiel #11
0
        public void TransformNL()
        {
            using (var pc = new ProjContext())
            {
                pc.LogLevel = ProjLogLevel.Error;
                using (var crs1 = CoordinateReferenceSystem.Create("EPSG:3857", pc))
                    using (var crs2 = CoordinateReferenceSystem.Create("EPSG:23095", pc))
                        using (var crs3 = CoordinateReferenceSystem.Create("EPSG:28992", pc))
                        {
                            Assert.AreEqual("WGS 84 / Pseudo-Mercator", crs1.Name);
                            Assert.AreEqual("ED50 / TM 5 NE", crs2.Name);
                            Assert.AreEqual("Amersfoort / RD New", crs3.Name);

                            Assert.AreEqual(ProjType.ProjectedCrs, crs1.Type);
                            Assert.AreEqual(ProjType.ProjectedCrs, crs2.Type);
                            Assert.AreEqual(ProjType.ProjectedCrs, crs3.Type);

                            Assert.IsNotNull(crs3.GeodeticCRS);
                            Assert.AreEqual("Amersfoort", crs3.GeodeticCRS.Name);
                            Assert.AreEqual(ProjType.Geographic2DCrs, crs3.GeodeticCRS.Type);

                            Assert.IsNotNull(crs3.Datum);
                            Assert.AreEqual("Amersfoort", crs3.Datum.Name);
                            Assert.AreEqual(ProjType.GeodeticReferenceFrame, crs3.Datum.Type);
                            Assert.IsFalse(crs3.Datum is Proj.DatumList);
                            Assert.AreEqual(null, crs3.CoordinateSystem.Name);
                            Assert.AreEqual(ProjType.CoordinateSystem, crs3.CoordinateSystem.Type);
                            Assert.AreEqual(CoordinateSystemType.Cartesian, crs3.CoordinateSystem.CoordinateSystemType);
                            Assert.AreEqual(2, crs3.CoordinateSystem.Axis.Count);

                            Assert.AreEqual("Easting", crs3.CoordinateSystem.Axis[0].Name);
                            Assert.AreEqual("Northing", crs3.CoordinateSystem.Axis[1].Name);
                            Assert.AreEqual("X", crs3.CoordinateSystem.Axis[0].Abbreviation);
                            Assert.AreEqual("Y", crs3.CoordinateSystem.Axis[1].Abbreviation);
                            Assert.AreEqual("metre", crs3.CoordinateSystem.Axis[0].UnitName);
                            Assert.AreEqual("metre", crs3.CoordinateSystem.Axis[1].UnitName);
                            Assert.AreEqual("EPSG", crs3.CoordinateSystem.Axis[0].UnitAuthName);
                            Assert.AreEqual("EPSG", crs3.CoordinateSystem.Axis[1].UnitAuthName);
                            Assert.AreEqual(1.0, crs3.CoordinateSystem.Axis[0].UnitConversionFactor);
                            Assert.AreEqual(1.0, crs3.CoordinateSystem.Axis[1].UnitConversionFactor);
                            Assert.AreEqual("9001", crs3.CoordinateSystem.Axis[0].UnitCode);
                            Assert.AreEqual("9001", crs3.CoordinateSystem.Axis[1].UnitCode);
                            Assert.AreEqual("east", crs3.CoordinateSystem.Axis[0].Direction);
                            Assert.AreEqual("north", crs3.CoordinateSystem.Axis[1].Direction);

                            Assert.AreEqual("Bessel 1841", crs3.Ellipsoid.Name);
                            Assert.AreEqual(ProjType.Ellipsoid, crs3.Ellipsoid.Type);

                            Assert.AreEqual(6377397.0, Math.Round(crs3.Ellipsoid.SemiMajorMetre, 0));
                            Assert.AreEqual(6356079.0, Math.Round(crs3.Ellipsoid.SemiMinorMetre, 0));
                            Assert.AreEqual(true, crs3.Ellipsoid.IsSemiMinorComputed);
                            Assert.AreEqual(299.0, Math.Round(crs3.Ellipsoid.InverseFlattening, 0));

                            Assert.AreEqual("Greenwich", crs3.PrimeMeridian.Name);
                            Assert.AreEqual(0.0, crs3.PrimeMeridian.Longitude);
                            Assert.AreEqual(0.0175, Math.Round(crs3.PrimeMeridian.UnitConversionFactor, 4));
                            Assert.AreEqual("degree", crs3.PrimeMeridian.UnitName);

                            using (var t = CoordinateTransform.Create(crs1, crs2))
                            {
                                Assert.IsTrue(t is ChooseCoordinateTransform);

                                {
                                    var c2 = crs1.GeodeticCRS;
                                    Assert.AreEqual("WGS 84", c2.Name);
                                    Assert.AreEqual(ProjType.Geographic2DCrs, c2.Type);
                                    using (var t2 = CoordinateTransform.Create(crs1, c2))
                                    {
                                        Assert.AreEqual("Inverse of Popular Visualisation Pseudo-Mercator", t2.Name);
                                    }
                                }
                            }

                            Assert.AreEqual(ProjType.ProjectedCrs, crs1.Type);
                            Assert.AreEqual(ProjType.ProjectedCrs, crs2.Type);
                        }
            }
        }