示例#1
0
        public void TestNamedMeridians()
        {
            foreach (NamedMeridian nm in Enum.GetValues(typeof(NamedMeridian)))
            {
                if (nm == NamedMeridian.Unknown || nm == NamedMeridian.Undefined)
                {
                    continue;
                }

                var m = Meridian.CreateByNamedMeridian(nm);
                Assert.AreEqual((int)nm, m.Code);
                Assert.AreEqual(nm, m.Name);
                Assert.AreEqual(string.Format(" +pm={0}", nm.ToString().ToLower()), m.Proj4Description);
                var c = new GeoAPI.Geometries.Coordinate(0, 0);
                m.InverseAdjust(c);
                Assert.AreEqual(m.Longitude, c.X, 1e-7);
                m.Adjust(c);
                Assert.AreEqual(0, c.X, 1e-7);

                var m2 = Meridian.CreateByName(nm.ToString().ToLower());
                Assert.AreEqual(m, m2);

                var m3 = Meridian.CreateByDegree(Utility.ProjectionMath.ToDegrees(m.Longitude));
                Assert.AreEqual(m, m3);
            }
        }
示例#2
0
        ///<summary>
        /// Creates a <see cref="Projection"/> initialized from a PROJ.4 argument list.
        ///</summary>
        private Projection.Projection ParseProjection(IDictionary <String, String> parameters, Ellipsoid ellipsoid)
        {
            Projection.Projection projection = null;

            String s;

            if (parameters.TryGetValue(Proj4Keyword.proj, out s))
            {
                projection = registry.GetProjection(s, parameters);
            }

            if (projection == null)
            {
                throw new ArgumentException("Unknown projection: " + s);
            }

            projection.Ellipsoid = ellipsoid;

            // not sure what CSes use this??

            /*
             * s = (String)parameters.TryGetValue( "init" );
             * if ( s != null ) {
             * projection = CreateFromName( s ).getProjection();
             * if ( projection == null )
             *  throw new ProjectionException( "Unknown projection: "+s );
             *      a = projection.getEquatorRadius();
             *      es = projection.getEllipsoid().getEccentricitySquared();
             * }
             */


            //TODO: better error handling for things like bad number syntax.
            // Should be able to report the original param string in the error message
            // Also should the exception be lib specific?  (Say ParseException)

            // Other parameters
            //   projection.ProjectionLatitudeDegrees = 0;
            //   projection.ProjectionLatitude1Degrees = 0;
            //   projection.ProjectionLatitude2Degrees = 0;
            if (parameters.TryGetValue(Proj4Keyword.alpha, out s))
            {
                projection.AlphaDegrees = Double.Parse(s, CultureInfo.InvariantCulture);
            }

            if (parameters.TryGetValue(Proj4Keyword.lonc, out s))
            {
                projection.LonCDegrees = Double.Parse(s, CultureInfo.InvariantCulture);
            }

            if (parameters.TryGetValue(Proj4Keyword.lat_0, out s))
            {
                projection.ProjectionLatitudeDegrees = ParseAngle(s);
            }

            if (parameters.TryGetValue(Proj4Keyword.lon_0, out s))
            {
                projection.ProjectionLongitudeDegrees = ParseAngle(s);
            }

            if (parameters.TryGetValue(Proj4Keyword.lat_1, out s))
            {
                projection.ProjectionLatitude1Degrees = ParseAngle(s);
            }

            if (parameters.TryGetValue(Proj4Keyword.lat_2, out s))
            {
                projection.ProjectionLatitude2Degrees = ParseAngle(s);
            }

            if (parameters.TryGetValue(Proj4Keyword.lat_ts, out s))
            {
                projection.TrueScaleLatitudeDegrees = ParseAngle(s);
            }

            if (parameters.TryGetValue(Proj4Keyword.x_0, out s))
            {
                projection.FalseEasting = Double.Parse(s, CultureInfo.InvariantCulture);
            }

            if (parameters.TryGetValue(Proj4Keyword.y_0, out s))
            {
                projection.FalseNorthing = Double.Parse(s, CultureInfo.InvariantCulture);
            }

            if (!parameters.TryGetValue(Proj4Keyword.k_0, out s))
            {
                if (!parameters.TryGetValue(Proj4Keyword.k, out s))
                {
                    s = null;
                }
            }
            if (s != null)
            {
                projection.ScaleFactor = Double.Parse(s, CultureInfo.InvariantCulture);
            }

            if (parameters.TryGetValue(Proj4Keyword.units, out s))
            {
                Unit unit = Units.Units.FindUnit(s);
                // TODO: report unknown units name as error
                if (unit != null)
                {
                    projection.FromMetres = (1.0 / unit.Value);
                    projection.Unit       = unit;
                }
            }

            if (parameters.TryGetValue(Proj4Keyword.to_meter, out s))
            {
                projection.FromMetres = (1.0 / Double.Parse(s, CultureInfo.InvariantCulture));
            }

            if (parameters.ContainsKey(Proj4Keyword.south))
            {
                projection.SouthernHemisphere = true;
            }

            if (parameters.TryGetValue(Proj4Keyword.pm, out s))
            {
                double pm;
                projection.PrimeMeridian = double.TryParse(s, out pm)
                    ? Meridian.CreateByDegree(pm)
                    : Meridian.CreateByName(s);
            }

            //TODO: implement some of these parameters ?

            // this must be done last, since behaviour depends on other parameters being set (eg +south)
            if (projection is TransverseMercatorProjection)
            {
                if (parameters.TryGetValue("zone", out s))
                {
                    ((TransverseMercatorProjection)projection).UTMZone = int.Parse(s);
                }
            }

            projection.Initialize();

            return(projection);
        }