XAML-Map-Control/MapProjections/Shared/AutoUtmProjection.cs
2022-02-19 18:32:27 +01:00

49 lines
1.3 KiB
C#

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using ProjNet.CoordinateSystems;
using System.Windows;
namespace MapControl.Projections
{
public class AutoUtmProjection : GeoApiProjection
{
private int zoneNumber;
private bool zoneIsNorth;
public AutoUtmProjection()
{
UpdateZone();
}
public override Point LocationToMap(Location location)
{
UpdateZone();
return base.LocationToMap(location);
}
public override Location MapToLocation(Point point)
{
UpdateZone();
return base.MapToLocation(point);
}
private void UpdateZone()
{
var north = Center.Latitude >= 0d;
var lon = Location.NormalizeLongitude(Center.Longitude);
var zone = (int)(lon + 180d) / 6 + 1;
if (zoneNumber != zone || zoneIsNorth != north)
{
zoneNumber = zone;
zoneIsNorth = north;
CoordinateSystem = ProjectedCoordinateSystem.WGS84_UTM(zoneNumber, zoneIsNorth);
CrsId = "AUTO2:42001";
}
}
}
}