diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs index 2f8df08c..288f513a 100644 --- a/MapControl/Shared/MapProjection.cs +++ b/MapControl/Shared/MapProjection.cs @@ -41,6 +41,14 @@ namespace MapControl set; } + /// + /// Creates a MapProjection instance from a CRS identifier string. + /// + public static MapProjection Parse(string crsId) + { + return Factory.GetProjection(crsId); + } + /// /// Gets the type of the projection. /// @@ -54,7 +62,24 @@ namespace MapControl /// /// Gets or sets an optional projection center. /// - 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() + { + } /// /// Gets the relative map scale at the specified geographic coordinates. @@ -158,13 +183,5 @@ namespace MapControl { return CrsId; } - - /// - /// Creates a MapProjection instance from a CRS identifier string. - /// - public static MapProjection Parse(string crsId) - { - return Factory.GetProjection(crsId); - } } } diff --git a/MapControl/Shared/Wgs84AutoTmProjection.cs b/MapControl/Shared/Wgs84AutoTmProjection.cs index 05b6dce7..aa002149 100644 --- a/MapControl/Shared/Wgs84AutoTmProjection.cs +++ b/MapControl/Shared/Wgs84AutoTmProjection.cs @@ -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; } } } diff --git a/MapControl/Shared/Wgs84AutoUtmProjection.cs b/MapControl/Shared/Wgs84AutoUtmProjection.cs index 7ca5f684..cb0cb6e5 100644 --- a/MapControl/Shared/Wgs84AutoUtmProjection.cs +++ b/MapControl/Shared/Wgs84AutoUtmProjection.cs @@ -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; } } } diff --git a/MapProjections/Shared/Wgs84AutoUtmProjection.cs b/MapProjections/Shared/Wgs84AutoUtmProjection.cs index 18c03ce1..99fdaf21 100644 --- a/MapProjections/Shared/Wgs84AutoUtmProjection.cs +++ b/MapProjections/Shared/Wgs84AutoUtmProjection.cs @@ -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; } } } diff --git a/MapProjections/Shared/Wgs84OrthographicProjection.cs b/MapProjections/Shared/Wgs84OrthographicProjection.cs index aa6a04b0..57458f59 100644 --- a/MapProjections/Shared/Wgs84OrthographicProjection.cs +++ b/MapProjections/Shared/Wgs84OrthographicProjection.cs @@ -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); } } } diff --git a/MapProjections/Shared/Wgs84StereographicProjection.cs b/MapProjections/Shared/Wgs84StereographicProjection.cs index 06473746..0ed274df 100644 --- a/MapProjections/Shared/Wgs84StereographicProjection.cs +++ b/MapProjections/Shared/Wgs84StereographicProjection.cs @@ -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); } } }