EPSG Codes & CRS IDs

This commit is contained in:
ClemensFischer 2024-07-12 13:57:27 +02:00
parent b74e5aaf7a
commit ce6f190bf1
18 changed files with 151 additions and 195 deletions

View file

@ -18,10 +18,10 @@ namespace MapControl
{
public const string DefaultCrsId = "AUTO2:42004";
public AutoEquirectangularProjection()
public AutoEquirectangularProjection(string crsId = DefaultCrsId)
{
Type = MapProjectionType.NormalCylindrical;
CrsId = DefaultCrsId;
CrsId = crsId;
}
public override Point? LocationToMap(Location location)

View file

@ -15,7 +15,9 @@ namespace MapControl
/// </summary>
public class AzimuthalEquidistantProjection : AzimuthalProjection
{
public AzimuthalEquidistantProjection(string crsId)
public const string DefaultCrsId = "AUTO2:97003"; // proprietary CRS ID
public AzimuthalEquidistantProjection(string crsId = DefaultCrsId)
{
CrsId = crsId;
}

View file

@ -17,13 +17,12 @@ namespace MapControl
/// </summary>
public class EquirectangularProjection : MapProjection
{
public const int DefaultEpsgCode = 4326;
public static readonly string DefaultCrsId = $"EPSG:{DefaultEpsgCode}";
public const int EpsgCode = 4326;
public EquirectangularProjection()
public EquirectangularProjection(string crsId = "EPSG:4326")
{
Type = MapProjectionType.NormalCylindrical;
CrsId = DefaultCrsId;
CrsId = crsId;
}
public override Point GetRelativeScale(Location location)

View file

@ -26,7 +26,7 @@ namespace MapControl
}
Zone = zone;
CrsId = $"EPSG:{Zone - FirstZone + FirstZoneEpsgCode}";
CrsId = $"EPSG:{25800 + Zone}";
// GRS 1980
EquatorialRadius = 6378137d;

View file

@ -17,9 +17,9 @@ namespace MapControl
{
public const string DefaultCrsId = "AUTO2:97001"; // GeoServer non-standard CRS ID
public GnomonicProjection()
public GnomonicProjection(string crsId = DefaultCrsId)
{
CrsId = DefaultCrsId;
CrsId = crsId;
}
public override Point? LocationToMap(Location location)

View file

@ -42,7 +42,7 @@ namespace MapControl
/// <summary>
/// Gets or sets an optional projection center.
/// </summary>
public virtual Location Center { get; set; } = new Location();
public virtual Location Center { get; protected internal set; } = new Location();
/// <summary>
/// Gets the relative map scale at the specified Location.
@ -118,7 +118,7 @@ namespace MapControl
/// </summary>
public virtual string GetCrsValue()
{
return CrsId.StartsWith("AUTO:") || CrsId.StartsWith("AUTO2:")
return CrsId.StartsWith("AUTO2:") || CrsId.StartsWith("AUTO:")
? string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", CrsId, Center.Longitude, Center.Latitude)
: CrsId;
}

View file

@ -16,93 +16,67 @@ namespace MapControl
public virtual MapProjection GetProjection(int epsgCode)
{
MapProjection projection = null;
switch (epsgCode)
{
case WorldMercatorProjection.DefaultEpsgCode:
projection = new WorldMercatorProjection();
break;
case WorldMercatorProjection.EpsgCode:
return new WorldMercatorProjection();
case WebMercatorProjection.DefaultEpsgCode:
projection = new WebMercatorProjection();
break;
case WebMercatorProjection.EpsgCode:
return new WebMercatorProjection();
case EquirectangularProjection.DefaultEpsgCode:
projection = new EquirectangularProjection();
break;
case EquirectangularProjection.EpsgCode:
return new EquirectangularProjection();
case UpsNorthProjection.DefaultEpsgCode:
projection = new UpsNorthProjection();
break;
case UpsNorthProjection.EpsgCode:
return new UpsNorthProjection();
case UpsSouthProjection.DefaultEpsgCode:
projection = new UpsSouthProjection();
break;
case UpsSouthProjection.EpsgCode:
return new UpsSouthProjection();
case var c when c >= Etrs89UtmProjection.FirstZoneEpsgCode && c <= Etrs89UtmProjection.LastZoneEpsgCode:
projection = new Etrs89UtmProjection(epsgCode % 100);
break;
return new Etrs89UtmProjection(epsgCode % 100);
case var c when c >= Nad83UtmProjection.FirstZoneEpsgCode && c <= Nad83UtmProjection.LastZoneEpsgCode:
projection = new Nad83UtmProjection(epsgCode % 100);
break;
return new Nad83UtmProjection(epsgCode % 100);
case var c when c >= Wgs84UtmProjection.FirstZoneNorthEpsgCode && c <= Wgs84UtmProjection.LastZoneNorthEpsgCode:
projection = new Wgs84UtmProjection(epsgCode % 100, true);
break;
return new Wgs84UtmProjection(epsgCode % 100, true);
case var c when c >= Wgs84UtmProjection.FirstZoneSouthEpsgCode && c <= Wgs84UtmProjection.LastZoneSouthEpsgCode:
projection = new Wgs84UtmProjection(epsgCode % 100, false);
break;
return new Wgs84UtmProjection(epsgCode % 100, false);
default:
break;
return null;
}
return projection;
}
public virtual MapProjection GetProjection(string crsId)
{
if (crsId.StartsWith("EPSG:") && int.TryParse(crsId.Substring(5), out int epsgCode))
{
return GetProjection(epsgCode);
}
MapProjection projection = null;
switch (crsId)
{
case Wgs84AutoUtmProjection.DefaultCrsId:
projection = new Wgs84AutoUtmProjection();
break;
return new Wgs84AutoUtmProjection();
case OrthographicProjection.DefaultCrsId:
projection = new OrthographicProjection();
break;
return new OrthographicProjection();
case AutoEquirectangularProjection.DefaultCrsId:
projection = new AutoEquirectangularProjection();
break;
return new AutoEquirectangularProjection();
case GnomonicProjection.DefaultCrsId:
projection = new GnomonicProjection();
break;
return new GnomonicProjection();
case StereographicProjection.DefaultCrsId:
projection = new StereographicProjection();
break;
return new StereographicProjection();
case "AUTO2:97003": // proprietary CRS ID
projection = new AzimuthalEquidistantProjection(crsId);
break;
case AzimuthalEquidistantProjection.DefaultCrsId:
return new AzimuthalEquidistantProjection();
default:
break;
return crsId.StartsWith("EPSG:") && int.TryParse(crsId.Substring(5), out int epsgCode)
? GetProjection(epsgCode)
: null;
}
return projection;
}
}
}

View file

@ -26,7 +26,7 @@ namespace MapControl
}
Zone = zone;
CrsId = $"EPSG:{Zone - FirstZone + FirstZoneEpsgCode}";
CrsId = $"EPSG:{26900 + Zone}";
// GRS 1980
EquatorialRadius = 6378137d;

View file

@ -17,9 +17,9 @@ namespace MapControl
{
public const string DefaultCrsId = "AUTO2:42003";
public OrthographicProjection()
public OrthographicProjection(string crsId = DefaultCrsId)
{
CrsId = DefaultCrsId;
CrsId = crsId;
}
public override Point? LocationToMap(Location location)

View file

@ -117,12 +117,11 @@ namespace MapControl
/// </summary>
public class UpsNorthProjection : PolarStereographicProjection
{
public const int DefaultEpsgCode = 32661;
public static readonly string DefaultCrsId = $"EPSG:{DefaultEpsgCode}";
public const int EpsgCode = 32661;
public UpsNorthProjection()
public UpsNorthProjection(string crsId = "EPSG:32661")
{
CrsId = DefaultCrsId;
CrsId = crsId;
IsNorth = true;
}
}
@ -132,12 +131,11 @@ namespace MapControl
/// </summary>
public class UpsSouthProjection : PolarStereographicProjection
{
public const int DefaultEpsgCode = 32761;
public static readonly string DefaultCrsId = $"EPSG:{DefaultEpsgCode}";
public const int EpsgCode = 32761;
public UpsSouthProjection()
public UpsSouthProjection(string crsId = "EPSG:32761")
{
CrsId = DefaultCrsId;
CrsId = crsId;
IsNorth = false;
}
}

View file

@ -17,9 +17,9 @@ namespace MapControl
{
public const string DefaultCrsId = "AUTO2:97002"; // GeoServer non-standard CRS ID
public StereographicProjection()
public StereographicProjection(string crsId = DefaultCrsId)
{
CrsId = DefaultCrsId;
CrsId = crsId;
}
public override Point? LocationToMap(Location location)

View file

@ -15,13 +15,12 @@ namespace MapControl
/// </summary>
public class WebMercatorProjection : MapProjection
{
public const int DefaultEpsgCode = 3857;
public static readonly string DefaultCrsId = $"EPSG:{DefaultEpsgCode}";
public const int EpsgCode = 3857;
public WebMercatorProjection()
public WebMercatorProjection(string crsId = "EPSG:3857")
{
Type = MapProjectionType.WebMercator;
CrsId = DefaultCrsId;
CrsId = crsId;
}
public override Point GetRelativeScale(Location location)

View file

@ -21,27 +21,21 @@ namespace MapControl
public int Zone { get; private set; }
public bool IsNorth { get; private set; }
protected Wgs84UtmProjection()
{
}
public Wgs84UtmProjection(int zone, bool north)
{
SetZone(zone, north);
}
protected void SetZone(int zone, bool north, string crsId = null)
protected void SetZone(int zone, bool north)
{
if (zone < FirstZone || zone > LastZone)
{
throw new ArgumentException($"Invalid WGS84 UTM zone {zone}.", nameof(zone));
}
var epsgCode = zone - FirstZone + (north ? FirstZoneNorthEpsgCode : FirstZoneSouthEpsgCode);
Zone = zone;
IsNorth = north;
CrsId = crsId ?? $"EPSG:{epsgCode}";
CrsId = $"EPSG:{(north ? 32600 : 32700) + zone}";
EquatorialRadius = Wgs84EquatorialRadius;
Flattening = Wgs84Flattening;
ScaleFactor = DefaultScaleFactor;
@ -58,38 +52,43 @@ namespace MapControl
{
public const string DefaultCrsId = "AUTO2:42001";
public Wgs84AutoUtmProjection(bool useZoneCrsId = false)
{
UseZoneCrsId = useZoneCrsId;
UpdateZone();
}
private readonly string autoCrsId;
public bool UseZoneCrsId { get; }
public Wgs84AutoUtmProjection(string crsId = DefaultCrsId)
: base(31, true)
{
autoCrsId = crsId;
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = autoCrsId;
}
}
public override Location Center
{
get => base.Center;
set
protected internal set
{
if (!Equals(base.Center, value))
{
base.Center = value;
UpdateZone();
}
}
}
private void UpdateZone()
{
var lon = Location.NormalizeLongitude(Center.Longitude);
var lon = Location.NormalizeLongitude(value.Longitude);
var zone = (int)Math.Floor(lon / 6d) + 31;
var north = Center.Latitude >= 0d;
var north = value.Latitude >= 0d;
if (Zone != zone || IsNorth != north || string.IsNullOrEmpty(CrsId))
if (Zone != zone || IsNorth != north)
{
SetZone(zone, north, UseZoneCrsId ? null : DefaultCrsId);
}
}
SetZone(zone, north);
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = autoCrsId;
}
}
}
}
}
}
}

View file

@ -15,13 +15,12 @@ namespace MapControl
/// </summary>
public class WorldMercatorProjection : MapProjection
{
public const int DefaultEpsgCode = 3395;
public static readonly string DefaultCrsId = $"EPSG:{DefaultEpsgCode}";
public const int EpsgCode = 3395;
public WorldMercatorProjection()
public WorldMercatorProjection(string crsId = "EPSG:3395")
{
Type = MapProjectionType.NormalCylindrical;
CrsId = DefaultCrsId;
CrsId = crsId;
}
public override Point GetRelativeScale(Location location)

View file

@ -8,75 +8,64 @@ namespace MapControl.Projections
{
public class GeoApiProjectionFactory : MapProjectionFactory
{
public static GeoApiProjectionFactory GetInstance()
{
if (!(Instance is GeoApiProjectionFactory factory))
{
factory = new GeoApiProjectionFactory();
Instance = factory;
}
return factory;
}
public Dictionary<int, string> CoordinateSystemWkts { get; } = new Dictionary<int, string>();
public override MapProjection GetProjection(int epsgCode)
{
MapProjection projection = null;
if (CoordinateSystemWkts.TryGetValue(epsgCode, out string wkt))
{
projection = new GeoApiProjection(wkt);
}
else
{
switch (epsgCode)
{
case WorldMercatorProjection.DefaultEpsgCode:
projection = new WorldMercatorProjection();
break;
case WorldMercatorProjection.EpsgCode:
return new WorldMercatorProjection();
case WebMercatorProjection.DefaultEpsgCode:
projection = new WebMercatorProjection();
break;
case WebMercatorProjection.EpsgCode:
return new WebMercatorProjection();
case int c when c >= Ed50UtmProjection.FirstZoneEpsgCode && c <= Ed50UtmProjection.LastZoneEpsgCode:
projection = new Ed50UtmProjection(epsgCode % 100);
break;
return new Ed50UtmProjection(epsgCode % 100);
case var c when c >= Etrs89UtmProjection.FirstZoneEpsgCode && c <= Etrs89UtmProjection.LastZoneEpsgCode:
projection = new Etrs89UtmProjection(epsgCode % 100);
break;
return new Etrs89UtmProjection(epsgCode % 100);
case var c when c >= Nad27UtmProjection.FirstZoneEpsgCode && c <= Nad27UtmProjection.LastZoneEpsgCode:
projection = new Nad27UtmProjection(epsgCode % 100);
break;
return new Nad27UtmProjection(epsgCode % 100);
case var c when c >= Nad83UtmProjection.FirstZoneEpsgCode && c <= Nad83UtmProjection.LastZoneEpsgCode:
projection = new Nad83UtmProjection(epsgCode % 100);
break;
return new Nad83UtmProjection(epsgCode % 100);
case var c when c >= Wgs84UtmProjection.FirstZoneNorthEpsgCode && c <= Wgs84UtmProjection.LastZoneNorthEpsgCode:
projection = new Wgs84UtmProjection(epsgCode % 100, true);
break;
return new Wgs84UtmProjection(epsgCode % 100, true);
case var c when c >= Wgs84UtmProjection.FirstZoneSouthEpsgCode && c <= Wgs84UtmProjection.LastZoneSouthEpsgCode:
projection = new Wgs84UtmProjection(epsgCode % 100, false);
break;
return new Wgs84UtmProjection(epsgCode % 100, false);
default:
break;
return CoordinateSystemWkts.TryGetValue(epsgCode, out string wkt)
? new GeoApiProjection(wkt)
: base.GetProjection(epsgCode);
}
}
return projection ?? base.GetProjection(epsgCode);
}
public override MapProjection GetProjection(string crsId)
{
MapProjection projection = null;
switch (crsId)
{
case Wgs84AutoUtmProjection.DefaultCrsId:
projection = new Wgs84AutoUtmProjection();
break;
return new Wgs84AutoUtmProjection();
default:
break;
return base.GetProjection(crsId);
}
return projection ?? base.GetProjection(crsId);
}
}
}

View file

@ -18,7 +18,7 @@ namespace MapControl.Projections
/// </summary>
public class WebMercatorProjection : GeoApiProjection
{
public const int DefaultEpsgCode = 3857;
public const int EpsgCode = 3857;
public WebMercatorProjection()
{

View file

@ -22,10 +22,6 @@ namespace MapControl.Projections
public int Zone { get; private set; }
public bool IsNorth { get; private set; }
protected Wgs84UtmProjection()
{
}
public Wgs84UtmProjection(int zone, bool north)
{
SetZone(zone, north);
@ -51,40 +47,41 @@ namespace MapControl.Projections
{
public const string DefaultCrsId = "AUTO2:42001";
public Wgs84AutoUtmProjection(bool useZoneCrsId = false)
{
UseZoneCrsId = useZoneCrsId;
UpdateZone();
}
private readonly string autoCrsId;
public bool UseZoneCrsId { get; }
public Wgs84AutoUtmProjection(string crsId = DefaultCrsId)
: base(31, true)
{
autoCrsId = crsId;
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = autoCrsId;
}
}
public override Location Center
{
get => base.Center;
set
protected set
{
if (!Equals(base.Center, value))
{
base.Center = value;
UpdateZone();
}
}
}
private void UpdateZone()
{
var lon = Location.NormalizeLongitude(Center.Longitude);
var lon = Location.NormalizeLongitude(value.Longitude);
var zone = (int)Math.Floor(lon / 6d) + 31;
var north = Center.Latitude >= 0d;
var north = value.Latitude >= 0d;
if (Zone != zone || IsNorth != north || string.IsNullOrEmpty(CrsId))
if (Zone != zone || IsNorth != north)
{
SetZone(zone, north);
if (!UseZoneCrsId)
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = DefaultCrsId;
CrsId = autoCrsId;
}
}
}
}
}

View file

@ -17,7 +17,7 @@ namespace MapControl.Projections
/// </summary>
public class WorldMercatorProjection : GeoApiProjection
{
public const int DefaultEpsgCode = 3395;
public const int EpsgCode = 3395;
public WorldMercatorProjection()
{
@ -43,7 +43,7 @@ namespace MapControl.Projections
+ "AUTHORITY[\"EPSG\",\"9001\"]],"
+ "AXIS[\"Easting\",EAST],"
+ "AXIS[\"Northing\",NORTH],"
+ "AUTHORITY[\"EPSG\",\"3395\"]]";
+ $"AUTHORITY[\"EPSG\",\"{EpsgCode}\"]]";
}
public override Point GetRelativeScale(Location location)