MapProjectionFactory throws NotSupportedException

This commit is contained in:
ClemensFischer 2024-09-12 15:48:20 +02:00
parent 85cf0483ec
commit 45b9fb2e89
5 changed files with 48 additions and 23 deletions

View file

@ -58,7 +58,7 @@ namespace MapControl
if (values == null || values.Length != 4 && values.Length != 5)
{
throw new FormatException("BoundingBox string must contain a comma-separated sequence of four or five floating point numbers.");
throw new FormatException($"{nameof(BoundingBox)} string must contain a comma-separated sequence of four or five floating point numbers.");
}
var rotation = values.Length == 5

View file

@ -165,8 +165,7 @@ namespace MapControl
{
var epsgCode = geoKeyDirectory[i + 3];
mapProjection = MapProjectionFactory.Instance.GetProjection($"EPSG:{epsgCode}") ??
throw new ArgumentException($"Can not create MapProjection \"EPSG:{epsgCode}\".");
mapProjection = MapProjectionFactory.Instance.GetProjection($"EPSG:{epsgCode}");
}
}
}

View file

@ -66,7 +66,7 @@ namespace MapControl
if (values?.Length != 2)
{
throw new FormatException("Location string must contain a comma-separated pair of floating point numbers.");
throw new FormatException($"{nameof(Location)} string must contain a comma-separated pair of floating point numbers.");
}
return new Location(

View file

@ -2,6 +2,8 @@
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
namespace MapControl
{
public class MapProjectionFactory
@ -16,48 +18,65 @@ namespace MapControl
public virtual MapProjection GetProjection(string crsId)
{
MapProjection projection = null;
switch (crsId)
{
case WebMercatorProjection.DefaultCrsId:
return new WebMercatorProjection();
projection = new WebMercatorProjection();
break;
case WorldMercatorProjection.DefaultCrsId:
return new WorldMercatorProjection();
projection = new WorldMercatorProjection();
break;
case EquirectangularProjection.DefaultCrsId:
case "CRS:84":
case "EPSG:4087":
return new EquirectangularProjection(crsId);
projection = new EquirectangularProjection(crsId);
break;
case UpsNorthProjection.DefaultCrsId:
return new UpsNorthProjection();
projection = new UpsNorthProjection();
break;
case UpsSouthProjection.DefaultCrsId:
return new UpsSouthProjection();
projection = new UpsSouthProjection();
break;
case Wgs84AutoUtmProjection.DefaultCrsId:
return new Wgs84AutoUtmProjection();
projection = new Wgs84AutoUtmProjection();
break;
case OrthographicProjection.DefaultCrsId:
return new OrthographicProjection();
projection = new OrthographicProjection();
break;
case AutoEquirectangularProjection.DefaultCrsId:
return new AutoEquirectangularProjection();
projection = new AutoEquirectangularProjection();
break;
case GnomonicProjection.DefaultCrsId:
return new GnomonicProjection();
projection = new GnomonicProjection();
break;
case StereographicProjection.DefaultCrsId:
return new StereographicProjection();
projection = new StereographicProjection();
break;
case AzimuthalEquidistantProjection.DefaultCrsId:
return new AzimuthalEquidistantProjection();
projection = new AzimuthalEquidistantProjection();
break;
default:
return crsId.StartsWith("EPSG:") && int.TryParse(crsId.Substring(5), out int epsgCode)
? GetProjection(epsgCode)
: null;
if (crsId.StartsWith("EPSG:") && int.TryParse(crsId.Substring(5), out int epsgCode))
{
projection = GetProjection(epsgCode);
}
break;
}
return projection ?? throw new NotSupportedException($"MapProjection \"{crsId}\" is not supported.");
}
public virtual MapProjection GetProjection(int epsgCode)
@ -67,8 +86,8 @@ namespace MapControl
case var c when c >= Etrs89UtmProjection.FirstZoneEpsgCode && c <= Etrs89UtmProjection.LastZoneEpsgCode:
return new Etrs89UtmProjection(epsgCode % 100);
case var c when c >= Nad27UtmProjection.FirstZoneEpsgCode && c <= Nad27UtmProjection.LastZoneEpsgCode:
return new Nad27UtmProjection(epsgCode % 100);
//case var c when c >= Nad27UtmProjection.FirstZoneEpsgCode && c <= Nad27UtmProjection.LastZoneEpsgCode:
// return new Nad27UtmProjection(epsgCode % 100);
case var c when c >= Nad83UtmProjection.FirstZoneEpsgCode && c <= Nad83UtmProjection.LastZoneEpsgCode:
return new Nad83UtmProjection(epsgCode % 100);

View file

@ -5,6 +5,7 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
#if WPF
using System.Windows;
@ -101,9 +102,15 @@ namespace MapControl.UiTools
{
if (selectedProjection != projection)
{
try
{
Map.MapProjection = MapProjectionFactory.Instance.GetProjection(projection);
selectedProjection = projection;
Map.MapProjection = MapProjectionFactory.Instance.GetProjection(selectedProjection) ??
throw new ArgumentException($"Can not create MapProjection \"{selectedProjection}\".");
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(MapProjectionFactory)}: {ex.Message}");
}
}
UpdateCheckedStates();