2018-08-29 20:54:42 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2018-08-29 20:54:42 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
using ProjNet.CoordinateSystems;
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Projections
|
|
|
|
|
|
{
|
|
|
|
|
|
public class UtmProjection : GeoApiProjection
|
|
|
|
|
|
{
|
2018-10-03 11:27:55 +02:00
|
|
|
|
private string zone;
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
2018-10-03 11:27:55 +02:00
|
|
|
|
public string Zone
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-10-03 11:27:55 +02:00
|
|
|
|
get { return zone; }
|
2018-08-29 20:54:42 +02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2018-10-03 11:27:55 +02:00
|
|
|
|
if (zone != value)
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-10-03 11:27:55 +02:00
|
|
|
|
if (string.IsNullOrEmpty(value))
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-10-03 11:27:55 +02:00
|
|
|
|
throw new ArgumentException("Invalid UTM zone.");
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-03 11:27:55 +02:00
|
|
|
|
var hemisphere = value[value.Length - 1];
|
|
|
|
|
|
int zoneNumber;
|
|
|
|
|
|
|
|
|
|
|
|
if ((hemisphere != 'N' && hemisphere != 'S') ||
|
|
|
|
|
|
!int.TryParse(value.Substring(0, value.Length - 1), out zoneNumber))
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-10-03 11:27:55 +02:00
|
|
|
|
throw new ArgumentException("Invalid UTM zone.");
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-03 11:27:55 +02:00
|
|
|
|
SetZone(zoneNumber, hemisphere == 'N');
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
2018-10-03 11:27:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
2018-10-03 11:27:55 +02:00
|
|
|
|
public void SetZone(int zoneNumber, bool north)
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
if (zoneNumber < 1 || zoneNumber > 61)
|
2018-10-03 11:27:55 +02:00
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Invalid UTM zone number.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var zoneName = zoneNumber.ToString() + (north ? "N" : "S");
|
|
|
|
|
|
|
|
|
|
|
|
if (zone != zoneName)
|
|
|
|
|
|
{
|
|
|
|
|
|
zone = zoneName;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
CoordinateSystem = ProjectedCoordinateSystem.WGS84_UTM(zoneNumber, north);
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-10-03 11:27:55 +02:00
|
|
|
|
|
|
|
|
|
|
public void SetZone(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
var zoneNumber = Math.Min((int)(Location.NormalizeLongitude(location.Longitude) + 180d) / 6 + 1, 60);
|
|
|
|
|
|
|
|
|
|
|
|
SetZone(zoneNumber, location.Latitude >= 0);
|
|
|
|
|
|
}
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|