From 45b5d037bb569a732ced7b6ddb429bea682758ab Mon Sep 17 00:00:00 2001 From: Clemens Date: Sat, 22 Jan 2022 20:32:17 +0100 Subject: [PATCH] Updated GeoApiProjectionFactory --- MapControl/Shared/MapProjectionFactory.cs | 5 ++- .../Shared/GeoApiProjectionFactory.cs | 40 +++---------------- MapUiTools/Shared/MapProjectionsMenuButton.cs | 4 +- 3 files changed, 11 insertions(+), 38 deletions(-) diff --git a/MapControl/Shared/MapProjectionFactory.cs b/MapControl/Shared/MapProjectionFactory.cs index a312e719..e4f292a0 100644 --- a/MapControl/Shared/MapProjectionFactory.cs +++ b/MapControl/Shared/MapProjectionFactory.cs @@ -6,7 +6,7 @@ namespace MapControl { public class MapProjectionFactory { - public virtual MapProjection CreateProjection(string crsId) + public virtual MapProjection GetProjection(string crsId) { MapProjection projection = null; @@ -43,6 +43,9 @@ namespace MapControl case "EPSG:97003": // proprietary CRS ID projection = new AzimuthalEquidistantProjection { CrsId = crsId }; break; + + default: + break; } return projection; diff --git a/MapProjections/Shared/GeoApiProjectionFactory.cs b/MapProjections/Shared/GeoApiProjectionFactory.cs index d419a45f..efdbdfa2 100644 --- a/MapProjections/Shared/GeoApiProjectionFactory.cs +++ b/MapProjections/Shared/GeoApiProjectionFactory.cs @@ -2,11 +2,6 @@ // © 2022 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Net.Http; - namespace MapControl.Projections { public class GeoApiProjectionFactory : MapProjectionFactory @@ -22,10 +17,7 @@ namespace MapControl.Projections public const int Wgs84UtmSouthLast = 32760; public const int Wgs84UpsSouth = 32761; - private readonly Dictionary wkts = new Dictionary(); - private readonly HttpClient httpClient = new HttpClient(); - - public override MapProjection CreateProjection(string crsId) + public override MapProjection GetProjection(string crsId) { MapProjection projection = null; @@ -62,17 +54,16 @@ namespace MapControl.Projections break; default: - projection = new GeoApiProjection(GetWkt(epsgCode)); break; } } - return projection ?? base.CreateProjection(crsId); + return projection ?? base.GetProjection(crsId); } private static string GetEtrs89UtmWkt(int epsgCode) { - const string etrs89UtmWktFormat + const string wktFormat = "PROJCS[\"ETRS89 / UTM zone {1}N\"," + "GEOGCS[\"ETRS89\"," + "DATUM[\"European_Terrestrial_Reference_System_1989\"," @@ -97,29 +88,10 @@ namespace MapControl.Projections + "AXIS[\"Northing\",NORTH]," + "AUTHORITY[\"EPSG\",\"{0}\"]]"; - int centralMeridian = 6 * (epsgCode - Etrs89UtmNorthFirst) - 15; + var zone = epsgCode % 100; + var centralMeridian = 6 * (epsgCode - Etrs89UtmNorthFirst) - 15; - return string.Format(etrs89UtmWktFormat, epsgCode, epsgCode - 25800, centralMeridian); - } - - private string GetWkt(int epsgCode) - { - if (!wkts.TryGetValue(epsgCode, out string wkt)) - { - var url = string.Format("https://epsg.io/{0}.wkt", epsgCode); - - try - { - wkt = httpClient.GetStringAsync(url).Result; // potential deadlock? - wkts[epsgCode] = wkt; - } - catch (Exception ex) - { - Debug.WriteLine($"GeoApiProjectionFactory.GetWkt({epsgCode}): {url}: {ex.Message}"); - } - } - - return wkt; + return string.Format(wktFormat, epsgCode, zone, centralMeridian); } } } diff --git a/MapUiTools/Shared/MapProjectionsMenuButton.cs b/MapUiTools/Shared/MapProjectionsMenuButton.cs index f506c907..9dddb870 100644 --- a/MapUiTools/Shared/MapProjectionsMenuButton.cs +++ b/MapUiTools/Shared/MapProjectionsMenuButton.cs @@ -4,7 +4,6 @@ using System.Collections.ObjectModel; using System.Collections.Specialized; -using System.Diagnostics; using System.Linq; #if WINUI using Microsoft.UI.Xaml; @@ -14,7 +13,6 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Markup; #else using System.Windows; -using System.Windows.Controls; using System.Windows.Markup; #endif @@ -91,7 +89,7 @@ namespace MapControl.UiTools if (selectedProjection != projection) { selectedProjection = projection; - Map.MapProjection = MapProjection.Factory.CreateProjection(selectedProjection); + Map.MapProjection = MapProjection.Factory.GetProjection(selectedProjection); } UpdateCheckedStates();