Added MapProjection.CenterChanged method

This commit is contained in:
ClemensFischer 2026-01-17 16:06:50 +01:00
parent 2817ecda7d
commit 31b38d5049
6 changed files with 67 additions and 89 deletions

View file

@ -41,6 +41,14 @@ namespace MapControl
set;
}
/// <summary>
/// Creates a MapProjection instance from a CRS identifier string.
/// </summary>
public static MapProjection Parse(string crsId)
{
return Factory.GetProjection(crsId);
}
/// <summary>
/// Gets the type of the projection.
/// </summary>
@ -54,7 +62,24 @@ namespace MapControl
/// <summary>
/// Gets or sets an optional projection center.
/// </summary>
public virtual Location Center { get; protected internal set; } = new Location();
public Location Center
{
get => field ??= new Location();
protected internal set
{
var longitude = Location.NormalizeLongitude(value.Longitude);
if (field == null || !field.Equals(value.Latitude, longitude))
{
field = new Location(value.Latitude, longitude);
CenterChanged();
}
}
}
protected virtual void CenterChanged()
{
}
/// <summary>
/// Gets the relative map scale at the specified geographic coordinates.
@ -158,13 +183,5 @@ namespace MapControl
{
return CrsId;
}
/// <summary>
/// Creates a MapProjection instance from a CRS identifier string.
/// </summary>
public static MapProjection Parse(string crsId)
{
return Factory.GetProjection(crsId);
}
}
}

View file

@ -17,14 +17,9 @@
CrsId = crsId;
}
public override Location Center
protected override void CenterChanged()
{
get => base.Center;
protected internal set
{
base.Center = value;
CentralMeridian = value.Longitude;
}
CentralMeridian = Center.Longitude;
}
}
}

View file

@ -29,28 +29,18 @@ namespace MapControl
}
}
public override Location Center
protected override void CenterChanged()
{
get => base.Center;
protected internal set
var zone = (int)Math.Floor(Center.Longitude / 6d) + 31;
var hemisphere = Center.Latitude >= 0d ? Hemisphere.North : Hemisphere.South;
if (Zone != zone || Hemisphere != hemisphere)
{
if (!base.Center.Equals(value))
SetZone(zone, hemisphere);
if (!string.IsNullOrEmpty(autoCrsId))
{
base.Center = value;
var lon = Location.NormalizeLongitude(value.Longitude);
var zone = (int)Math.Floor(lon / 6d) + 31;
var hemisphere = value.Latitude >= 0d ? Hemisphere.North : Hemisphere.South;
if (Zone != zone || Hemisphere != hemisphere)
{
SetZone(zone, hemisphere);
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = autoCrsId;
}
}
CrsId = autoCrsId;
}
}
}

View file

@ -27,28 +27,18 @@ namespace MapControl.Projections
}
}
public override Location Center
protected override void CenterChanged()
{
get => base.Center;
protected set
var zone = (int)Math.Floor(Center.Longitude / 6d) + 31;
var hemisphere = Center.Latitude >= 0d ? Hemisphere.North : Hemisphere.South;
if (Zone != zone || Hemisphere != hemisphere)
{
if (!base.Center.Equals(value))
SetZone(zone, hemisphere);
if (!string.IsNullOrEmpty(autoCrsId))
{
base.Center = value;
var lon = Location.NormalizeLongitude(value.Longitude);
var zone = (int)Math.Floor(lon / 6d) + 31;
var hemisphere = value.Latitude >= 0d ? Hemisphere.North : Hemisphere.South;
if (Zone != zone || Hemisphere != hemisphere)
{
SetZone(zone, hemisphere);
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = autoCrsId;
}
}
CrsId = autoCrsId;
}
}
}

View file

@ -1,5 +1,4 @@

using System.Globalization;
using System.Globalization;
namespace MapControl.Projections
{
@ -7,17 +6,12 @@ namespace MapControl.Projections
{
public Wgs84OrthographicProjection()
{
Center = base.Center;
CenterChanged();
}
public override Location Center
protected override void CenterChanged()
{
get => base.Center;
protected set
{
base.Center = value;
var wktFormat =
var wktFormat =
"PROJCS[\"WGS 84 / World Mercator\"," +
WktConstants.GeogCsWgs84 + "," +
"PROJECTION[\"Orthographic\"]," +
@ -28,9 +22,8 @@ namespace MapControl.Projections
"AXIS[\"Northing\",NORTH]" +
"AUTHORITY[\"AUTO2\",\"42003\"]]";
CoordinateSystemWkt = string.Format(
CultureInfo.InvariantCulture, wktFormat, value.Latitude, value.Longitude);
}
CoordinateSystemWkt = string.Format(
CultureInfo.InvariantCulture, wktFormat, Center.Latitude, Center.Longitude);
}
}
}

View file

@ -1,5 +1,4 @@

using System.Globalization;
using System.Globalization;
namespace MapControl.Projections
{
@ -7,30 +6,24 @@ namespace MapControl.Projections
{
public Wgs84StereographicProjection()
{
Center = base.Center;
CenterChanged();
}
public override Location Center
protected override void CenterChanged()
{
get => base.Center;
protected set
{
base.Center = value;
var wktFormat =
"PROJCS[\"WGS 84 / World Mercator\"," +
WktConstants.GeogCsWgs84 + "," +
"PROJECTION[\"Oblique_Stereographic\"]," +
"PARAMETER[\"latitude_of_origin\",{0:0.########}]," +
"PARAMETER[\"central_meridian\",{1:0.########}]," +
"UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]]," +
"AXIS[\"Easting\",EAST]," +
"AXIS[\"Northing\",NORTH]" +
"AUTHORITY[\"AUTO2\",\"97002\"]]";
var wktFormat =
"PROJCS[\"WGS 84 / World Mercator\"," +
WktConstants.GeogCsWgs84 + "," +
"PROJECTION[\"Oblique_Stereographic\"]," +
"PARAMETER[\"latitude_of_origin\",{0:0.########}]," +
"PARAMETER[\"central_meridian\",{1:0.########}]," +
"UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]]," +
"AXIS[\"Easting\",EAST]," +
"AXIS[\"Northing\",NORTH]" +
"AUTHORITY[\"AUTO2\",\"97002\"]]";
CoordinateSystemWkt = string.Format(
CultureInfo.InvariantCulture, wktFormat, value.Latitude, value.Longitude);
}
CoordinateSystemWkt = string.Format(
CultureInfo.InvariantCulture, wktFormat, Center.Latitude, Center.Longitude);
}
}
}