XAML-Map-Control/MapProjections/Shared/GeoApiProjectionFactory.cs

98 lines
3.8 KiB
C#
Raw Normal View History

2022-01-19 23:40:05 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// <20> 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
namespace MapControl.Projections
{
public class GeoApiProjectionFactory : MapProjectionFactory
{
2022-01-21 00:17:01 +01:00
public const int WorldMercator = 3395;
public const int WebMercator = 3857;
public const int Etrs89UtmNorthFirst = 25828;
public const int Etrs89UtmNorthLast = 25838;
public const int Wgs84UtmNorthFirst = 32601;
public const int Wgs84UtmNorthLast = 32660;
public const int Wgs84UpsNorth = 32661;
public const int Wgs84UtmSouthFirst = 32701;
public const int Wgs84UtmSouthLast = 32760;
public const int Wgs84UpsSouth = 32761;
2022-01-22 20:32:17 +01:00
public override MapProjection GetProjection(string crsId)
2022-01-19 23:40:05 +01:00
{
2022-01-21 00:17:01 +01:00
MapProjection projection = null;
2022-01-19 23:40:05 +01:00
2022-01-21 00:17:01 +01:00
if (crsId.StartsWith("EPSG:") && int.TryParse(crsId.Substring(5), out int epsgCode))
2022-01-19 23:40:05 +01:00
{
2022-01-21 00:17:01 +01:00
switch (epsgCode)
2022-01-19 23:40:05 +01:00
{
2022-01-21 00:17:01 +01:00
case WorldMercator:
projection = new WorldMercatorProjection();
break;
case WebMercator:
projection = new WebMercatorProjection();
break;
case int c when c >= Etrs89UtmNorthFirst && c <= Etrs89UtmNorthLast:
projection = new GeoApiProjection(GetEtrs89UtmWkt(epsgCode));
break;
case int c when c >= Wgs84UtmNorthFirst && c <= Wgs84UtmNorthLast:
projection = new UtmProjection(epsgCode - Wgs84UtmNorthFirst + 1, true);
break;
case int c when c >= Wgs84UtmSouthFirst && c <= Wgs84UtmSouthLast:
projection = new UtmProjection(epsgCode - Wgs84UtmSouthFirst + 1, false);
break;
case Wgs84UpsNorth:
projection = new UpsNorthProjection();
break;
case Wgs84UpsSouth:
projection = new UpsSouthProjection();
break;
default:
break;
2022-01-19 23:40:05 +01:00
}
}
2022-01-22 20:32:17 +01:00
return projection ?? base.GetProjection(crsId);
2022-01-21 00:17:01 +01:00
}
private static string GetEtrs89UtmWkt(int epsgCode)
{
2022-01-22 20:32:17 +01:00
const string wktFormat
2022-01-21 00:17:01 +01:00
= "PROJCS[\"ETRS89 / UTM zone {1}N\","
+ "GEOGCS[\"ETRS89\","
+ "DATUM[\"European_Terrestrial_Reference_System_1989\","
2022-01-21 23:26:21 +01:00
+ "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
+ "AUTHORITY[\"EPSG\",\"7019\"]],"
+ "TOWGS84[0,0,0,0,0,0,0],"
+ "AUTHORITY[\"EPSG\",\"6258\"]],"
+ "PRIMEM[\"Greenwich\",0,"
+ "AUTHORITY[\"EPSG\",\"8901\"]],"
+ "UNIT[\"degree\",0.0174532925199433,"
+ "AUTHORITY[\"EPSG\",\"9122\"]],"
2022-01-21 00:17:01 +01:00
+ "AUTHORITY[\"EPSG\",\"4258\"]],"
+ "PROJECTION[\"Transverse_Mercator\"],"
+ "PARAMETER[\"latitude_of_origin\",0],"
+ "PARAMETER[\"central_meridian\",{2}],"
+ "PARAMETER[\"scale_factor\",0.9996],"
+ "PARAMETER[\"false_easting\",500000],"
+ "PARAMETER[\"false_northing\",0],"
2022-01-21 23:26:21 +01:00
+ "UNIT[\"metre\",1,"
+ "AUTHORITY[\"EPSG\",\"9001\"]],"
2022-01-21 00:17:01 +01:00
+ "AXIS[\"Easting\",EAST],"
+ "AXIS[\"Northing\",NORTH],"
+ "AUTHORITY[\"EPSG\",\"{0}\"]]";
2022-01-22 20:32:17 +01:00
var zone = epsgCode % 100;
var centralMeridian = 6 * (epsgCode - Etrs89UtmNorthFirst) - 15;
2022-01-19 23:40:05 +01:00
2022-01-22 20:32:17 +01:00
return string.Format(wktFormat, epsgCode, zone, centralMeridian);
2022-01-19 23:40:05 +01:00
}
}
}