public static SWEREF99Position ToSweRef99(this WebMercatorPosition pos)
        {
            var calc     = new WebMercatorCalculator();
            var wgs84Pos = new WGS84Position(calc.YToLatitude(pos.Latitude), calc.XToLongitude(pos.Longitude));

            return(new SWEREF99Position(wgs84Pos, SWEREF99Position.SWEREFProjection.sweref_99_tm));
        }
        public static RT90Position ToRt90(this WebMercatorPosition pos)
        {
            var calc     = new WebMercatorCalculator();
            var wgs84Pos = new WGS84Position(calc.YToLatitude(pos.Latitude), calc.XToLongitude(pos.Longitude));

            return(new RT90Position(wgs84Pos, RT90Position.RT90Projection.rt90_2_5_gon_v));
        }
        public string XYZoomToBBox(int x, int y, int zoom)
        {
            var tileSize = 256;

            NumberFormatInfo nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            // From the grid position and zoom, work out the min and max Latitude / Longitude values of this tile
            double w = ((float)(x * tileSize) * 360 / (float)(tileSize * Math.Pow(2, zoom)) - 180);
            double n = ((float)Math.Asin((Math.Exp((0.5 - (y * tileSize) / (tileSize) / Math.Pow(2, zoom)) * 4 * Math.PI) - 1) / (Math.Exp((0.5 - (y * tileSize) / 256 / Math.Pow(2, zoom)) * 4 * Math.PI) + 1)) * 180 / (float)Math.PI);
            double e = ((float)((x + 1) * tileSize) * 360 / (float)(tileSize * Math.Pow(2, zoom)) - 180);
            double s = ((float)Math.Asin((Math.Exp((0.5 - ((y + 1) * tileSize) / (tileSize) / Math.Pow(2, zoom)) * 4 * Math.PI) - 1) / (Math.Exp((0.5 - ((y + 1) * tileSize) / 256 / Math.Pow(2, zoom)) * 4 * Math.PI) + 1)) * 180 / (float)Math.PI);

            if (UseSweref99)
            {
                WGS84Position wgstopLeft     = new WGS84Position(n, w);
                WGS84Position wgsBottomRight = new WGS84Position(s, e);

                SWEREF99Position swerefTopLeft     = new SWEREF99Position(wgstopLeft, SWEREF99Position.SWEREFProjection.sweref_99_tm);
                SWEREF99Position swerefBottomRight = new SWEREF99Position(wgsBottomRight, SWEREF99Position.SWEREFProjection.sweref_99_tm);

                string[] bounds = new string[] { swerefTopLeft.Longitude.ToString(nfi), swerefBottomRight.Latitude.ToString(nfi), swerefBottomRight.Longitude.ToString(nfi), swerefTopLeft.Latitude.ToString(nfi) };

                return(string.Join(",", bounds));
            }
            else
            {
                string[] bounds = new string[] { w.ToString(nfi), s.ToString(nfi), e.ToString(nfi), n.ToString(nfi) };

                return(string.Join(",", bounds));
            }
        }
Esempio n. 4
0
        public void ToSweRef99_FromWgs84(double lat, double lng, double expectedLat, double expectedLng, int decimals)
        {
            var pos       = new WGS84Position(lat, lng);
            var converted = PositionConverter.ToSweRef99(pos);

            Assert.AreEqual(Math.Round(expectedLat, decimals, MidpointRounding.AwayFromZero), Math.Round(converted.Latitude, decimals, MidpointRounding.AwayFromZero));
            Assert.AreEqual(Math.Round(expectedLng, decimals, MidpointRounding.AwayFromZero), Math.Round(converted.Longitude, decimals, MidpointRounding.AwayFromZero));
        }
Esempio n. 5
0
        [TestCase(57.633573, 11.909510, 7890733, 1325761, 0)]            // Välen, Göteborg
        public void ToWebMercator_FromPosition(double lat, double lng, double expectedLat, double expectedLng, int decimals)
        {
            Position pos       = new WGS84Position(lat, lng);
            var      converted = pos.ToWebMercator();

            Assert.AreEqual(Math.Round(expectedLat, decimals, MidpointRounding.AwayFromZero), Math.Round(converted.Latitude, decimals, MidpointRounding.AwayFromZero));
            Assert.AreEqual(Math.Round(expectedLng, decimals, MidpointRounding.AwayFromZero), Math.Round(converted.Longitude, decimals, MidpointRounding.AwayFromZero));
        }
Esempio n. 6
0
        public void LatitudeToStringTest2()
        {
            SWEREF99Position swePos = new SWEREF99Position(6652797.165, 658185.201);
            WGS84Position    wgsPos = swePos.ToWGS84();

            string latDegStringFromLM = "59.982008334868";
            string lonDegStringFromLM = "17.8350333249246";

            Assert.AreEqual(latDegStringFromLM, wgsPos.LatitudeToString(WGS84Position.WGS84Format.Degrees));
            Assert.AreEqual(lonDegStringFromLM, wgsPos.LongitudeToString(WGS84Position.WGS84Format.Degrees));
        }
Esempio n. 7
0
        public void WGS84ParseString()
        {
            // Values from Eniro.se
            WGS84Position wgsPosDM  = new WGS84Position("N 62º 10.560' E 015º 54.180'", WGS84Position.WGS84Format.DegreesMinutes);
            WGS84Position wgsPosDMs = new WGS84Position("N 62º 10' 33.60\" E 015º 54' 10.80\"", WGS84Position.WGS84Format.DegreesMinutesSeconds);

            Assert.AreEqual(62.176, Math.Round(wgsPosDM.Latitude, 3));
            Assert.AreEqual(15.903, Math.Round(wgsPosDM.Longitude, 3));

            Assert.AreEqual(62.176, Math.Round(wgsPosDMs.Latitude, 3));
            Assert.AreEqual(15.903, Math.Round(wgsPosDMs.Longitude, 3));
        }
Esempio n. 8
0
        public void SwerefToWGS84()
        {
            SWEREF99Position swePos = new SWEREF99Position(6652797.165, 658185.201);
            WGS84Position    wgsPos = swePos.ToWGS84();

            // String values from Lantmateriet.se, they convert DMS only.
            // Reference: http://www.lantmateriet.se/templates/LMV_Enkelkoordinattransformation.aspx?id=11500
            string latDmsStringFromLM = "N 59º 58' 55.23001\"";
            string lonDmsStringFromLM = "E 17º 50' 6.11997\"";

            Assert.AreEqual(latDmsStringFromLM, wgsPos.LatitudeToString(WGS84Position.WGS84Format.DegreesMinutesSeconds));
            Assert.AreEqual(lonDmsStringFromLM, wgsPos.LongitudeToString(WGS84Position.WGS84Format.DegreesMinutesSeconds));
        }
Esempio n. 9
0
        public void WGS84ToRT90()
        {
            WGS84Position wgsPos = new WGS84Position("N 59º 58' 55.23\" E 017º 50' 06.12\"", WGS84Position.WGS84Format.DegreesMinutesSeconds);
            RT90Position  rtPos  = new RT90Position(wgsPos, RT90Position.RT90Projection.rt90_2_5_gon_v);

            // Conversion values from Lantmateriet.se, they convert from DMS only.
            // Reference: http://www.lantmateriet.se/templates/LMV_Enkelkoordinattransformation.aspx?id=11500
            double xPosFromLM = 6653174.343;
            double yPosFromLM = 1613318.742;

            Assert.AreEqual(Math.Round(rtPos.Latitude, 3), xPosFromLM);
            Assert.AreEqual(Math.Round(rtPos.Longitude, 3), yPosFromLM);
        }
Esempio n. 10
0
        public static Point GetPointFromWgs(Geometry geo)
        {
            if (!string.IsNullOrWhiteSpace(geo.Wgs84))
            {
                var point  = PointFromString(geo.Wgs84);
                var wgsPos = new WGS84Position(point.Latitude, point.Longitude);
                var rtPos  = new SWEREF99Position(wgsPos, SWEREF99Position.SWEREFProjection.sweref_99_tm);

                return(new Point(rtPos.Longitude, rtPos.Latitude));
            }

            return(null);
        }
Esempio n. 11
0
        public void Load(string path)
        {
            var networkPath  = Path.Combine(path, "network-plain.xml");
            var schedulePath = Path.Combine(path, "transitSchedule.xml");
            var vehiclePath  = Path.Combine(path, "vehicles.xml");

            var matSimNetwork     = MatSimNetwork.Load(networkPath);
            var matSimSchedule    = MatSimSchedule.Load(schedulePath);
            var vehicleDefinition = VehicleDefinitions.Load(vehiclePath);

            var networkMinMax = matSimNetwork.GetMinMaxXY();

            SWEREF99Position minSwe = new SWEREF99Position(networkMinMax[1], networkMinMax[0]);
            SWEREF99Position maxSwe = new SWEREF99Position(networkMinMax[3], networkMinMax[2]);

            WGS84Position netMinWgs = minSwe.ToWGS84();
            WGS84Position netMaxWgs = maxSwe.ToWGS84();

            Console.WriteLine($"Min Lat: {netMinWgs.LatitudeToString(WGS84Position.WGS84Format.Degrees)}");
            Console.WriteLine($"Min Lon: {netMinWgs.LongitudeToString(WGS84Position.WGS84Format.Degrees)}");
            Console.WriteLine($"Max Lat: {netMaxWgs.LatitudeToString(WGS84Position.WGS84Format.Degrees)}");
            Console.WriteLine($"Max Lon: {netMaxWgs.LongitudeToString(WGS84Position.WGS84Format.Degrees)}");

            var scheduleMinMax = matSimSchedule.GetMinMaxXY();

            minSwe = new SWEREF99Position(scheduleMinMax[1], scheduleMinMax[0]);
            maxSwe = new SWEREF99Position(scheduleMinMax[3], scheduleMinMax[2]);

            WGS84Position schMinWgs = minSwe.ToWGS84();
            WGS84Position schMaxWgs = maxSwe.ToWGS84();

            Console.WriteLine($"Min Lat: {schMinWgs.LatitudeToString(WGS84Position.WGS84Format.Degrees)}, {scheduleMinMax[1]}");
            Console.WriteLine($"Min Lat: {schMinWgs.Latitude}");

            Console.WriteLine($"Min Lon: {schMinWgs.LongitudeToString(WGS84Position.WGS84Format.Degrees)}, {scheduleMinMax[0]}");
            Console.WriteLine($"Max Lat: {schMaxWgs.LatitudeToString(WGS84Position.WGS84Format.Degrees)}, {scheduleMinMax[3]}");
            Console.WriteLine($"Max Lon: {schMaxWgs.LongitudeToString(WGS84Position.WGS84Format.Degrees)}, {scheduleMinMax[2]}");


            //Console.WriteLine(matSimSchedule.GetStopString("99999"));
            //Console.WriteLine(matSimSchedule.GetLineString("line1_r"));
            //Console.WriteLine(matSimSchedule.GetLineString("line10_r"));

            //Console.WriteLine(matSimNetwork.GetLinkString("100004_AB"));
            //Console.WriteLine(matSimNetwork.GetNodeString("tr_65905"));

            //Console.WriteLine(vehicleDefinition.GetVehicleTypeString("BUS"));
            //Console.WriteLine(vehicleDefinition.GetVehicleString("Veh144931"));
            //Console.WriteLine($"vehicle list count: {vehicleDefinition.vehicles.Count}");
        }
Esempio n. 12
0
        public void WGS84ToSweref()
        {
            WGS84Position wgsPos = new WGS84Position();

            wgsPos.SetLatitudeFromString("N 59º 58' 55.23\"", WGS84Position.WGS84Format.DegreesMinutesSeconds);
            wgsPos.SetLongitudeFromString("E 017º 50' 06.12\"", WGS84Position.WGS84Format.DegreesMinutesSeconds);

            SWEREF99Position rtPos = new SWEREF99Position(wgsPos, SWEREF99Position.SWEREFProjection.sweref_99_tm);

            // Conversion values from Lantmateriet.se, they convert from DMS only.
            // Reference: http://www.lantmateriet.se/templates/LMV_Enkelkoordinattransformation.aspx?id=11500
            double xPosFromLM = 6652797.165;
            double yPosFromLM = 658185.201;

            Assert.AreEqual(Math.Round(rtPos.Latitude, 3), xPosFromLM);
            Assert.AreEqual(Math.Round(rtPos.Longitude, 3), yPosFromLM);
        }
Esempio n. 13
0
        public async Task <IList <Site> > GetNearbySites(double latitude, double longitude, double distanceRadians)
        {
            var distance = distanceRadians * 7500000;

            if (distance == 0)
            {
                distance = 5000;
            }
            var pos = new WGS84Position(latitude, longitude).ToWebMercator();
            var sw  = new WebMercatorPosition(pos.Latitude - distance, pos.Longitude - distance);
            var ne  = new WebMercatorPosition(pos.Latitude + distance, pos.Longitude + distance);

            var sites = await _ap2WebClient.GetSitesWithinBoundsAsync(sw, ne);

            _artportalenAccountStorage.SaveCookies();

            // Run this in background for non blocking of UI
            Task.Run(async() =>
            {
                await UpdateSites(sites).ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        t.Exception.Handle(ex => true);
                    }

                    return(t.Result);
                });
            });

            var webMercCalc = new WebMercatorCalculator();

            return(sites.Select(s =>
                                new Site
            {
                SiteId = s.SiteId,
                SiteName = s.SiteName,
                SiteYCoord = s.SiteYCoord,
                SiteXCoord = s.SiteXCoord,
                Latitude = webMercCalc.YToLatitude(s.SiteYCoord),
                Longitude = webMercCalc.XToLongitude(s.SiteXCoord),
                Kommun = s.Kommun,
                DistanceKm = GetDistanceKm(pos, new WebMercatorPosition(s.SiteYCoord, s.SiteXCoord)),
            }
                                ).OrderBy(x => x.DistanceKm).ToList());
        }
Esempio n. 14
0
        public async Task<IList<Site>> GetNearbySites(double latitude, double longitude, double distanceRadians)
        {
            var distance = distanceRadians*7500000;
            if (distance == 0) distance = 5000;
            var pos = new WGS84Position(latitude, longitude).ToWebMercator();
            var sw = new WebMercatorPosition(pos.Latitude - distance, pos.Longitude - distance);
            var ne = new WebMercatorPosition(pos.Latitude + distance, pos.Longitude + distance);

            var sites = await _ap2WebClient.GetSitesWithinBoundsAsync(sw, ne);
            _artportalenAccountStorage.SaveCookies();

            // Run this in background for non blocking of UI
            Task.Run(async () =>
            {
                await UpdateSites(sites).ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        t.Exception.Handle(ex => true);
                    }

                    return t.Result;
                });
            });

            var webMercCalc = new WebMercatorCalculator();

            return sites.Select(s =>
                new Site
                {
                    SiteId = s.SiteId,
                    SiteName = s.SiteName,
                    SiteYCoord = s.SiteYCoord,
                    SiteXCoord = s.SiteXCoord,
                    Latitude = webMercCalc.YToLatitude(s.SiteYCoord),
                    Longitude = webMercCalc.XToLongitude(s.SiteXCoord),
                    Kommun = s.Kommun,
                    DistanceKm = GetDistanceKm(pos, new WebMercatorPosition(s.SiteYCoord, s.SiteXCoord)),
                }
            ).OrderBy(x => x.DistanceKm).ToList();
        }
Esempio n. 15
0
        public void RT90ToWGS84()
        {
            RT90Position  position = new RT90Position(6583052, 1627548);
            WGS84Position wgsPos   = position.ToWGS84();

            // Values from Hitta.se for the conversion
            double latFromHitta = 59.3489;
            double lonFromHitta = 18.0473;

            double lat = Math.Round(wgsPos.Latitude, 4);
            double lon = Math.Round(wgsPos.Longitude, 4);

            Assert.AreEqual(latFromHitta, lat);
            Assert.AreEqual(lonFromHitta, lon);

            // String values from Lantmateriet.se, they convert DMS only.
            // Reference: http://www.lantmateriet.se/templates/LMV_Enkelkoordinattransformation.aspx?id=11500
            string latDmsStringFromLM = "N 59º 20' 56.09287\"";
            string lonDmsStringFromLM = "E 18º 2' 50.34806\"";

            Assert.AreEqual(latDmsStringFromLM, wgsPos.LatitudeToString(WGS84Position.WGS84Format.DegreesMinutesSeconds));
            Assert.AreEqual(lonDmsStringFromLM, wgsPos.LongitudeToString(WGS84Position.WGS84Format.DegreesMinutesSeconds));
        }
 public static RT90Position ToRt90(this WGS84Position pos)
 {
     return(new RT90Position(pos, RT90Position.RT90Projection.rt90_2_5_gon_v));
 }
        public static WebMercatorPosition ToWebMercator(this WGS84Position pos)
        {
            var calc = new WebMercatorCalculator();

            return(new WebMercatorPosition(calc.LatitudeToY(pos.Latitude), calc.LongitudeToX(pos.Longitude)));
        }
 public static SWEREF99Position ToSweRef99(this WGS84Position pos)
 {
     return(new SWEREF99Position(pos, SWEREF99Position.SWEREFProjection.sweref_99_tm));
 }
Esempio n. 19
0
 public static SWEREF99Position SwerefFromWgs(WGS84Position position)
 {
     return(new SWEREF99Position(position, SWEREF99Position.SWEREFProjection.sweref_99_tm));
 }