From 56fb9b1f1db7eaaa5f2a930aaa6538264dc4ffac Mon Sep 17 00:00:00 2001 From: Ieuan Walker Date: Sat, 13 Jul 2019 00:46:23 +0100 Subject: [PATCH] code update --- Demo/DemoProject/Program.cs | 2 +- GeoUK.OSTN.Tests/TransformTests.cs | 2 +- GeoUK.OSTN/Transform.cs | 8 +++---- GeoUK/Convert.cs | 34 ------------------------------ GeoUK/Coordinates/Osgb36.cs | 33 ++++++++++++++--------------- GeoUK/OSTN02Transform.cs | 2 +- GeoUK/Transform.cs | 2 -- 7 files changed, 23 insertions(+), 60 deletions(-) diff --git a/Demo/DemoProject/Program.cs b/Demo/DemoProject/Program.cs index fc89001..e68f089 100644 --- a/Demo/DemoProject/Program.cs +++ b/Demo/DemoProject/Program.cs @@ -5,7 +5,7 @@ namespace DemoProject { class Program { - static void Main(string[] args) + static void Main() { EastingNorthingToLatitudeLongitude.Example(); diff --git a/GeoUK.OSTN.Tests/TransformTests.cs b/GeoUK.OSTN.Tests/TransformTests.cs index 3a42210..cd239a6 100644 --- a/GeoUK.OSTN.Tests/TransformTests.cs +++ b/GeoUK.OSTN.Tests/TransformTests.cs @@ -125,7 +125,7 @@ namespace GeoUK.OSTN.Tests foreach (DataPoint dataPoint in inputData) { - LatitudeLongitude transformation = Transform.OsgbToEtrs89(new Osgb36(dataPoint.X, dataPoint.Y), OstnVersionEnum.OSTN15); + LatitudeLongitude transformation = Transform.OsgbToEtrs89(new Osgb36(dataPoint.X, dataPoint.Y)); // Comparing values with a precision of 3 decimals, as they are given in the output file. bool latitudesEqual = outputData[dataPoint.PointID].X diff --git a/GeoUK.OSTN/Transform.cs b/GeoUK.OSTN/Transform.cs index 31ef87a..498bdf1 100644 --- a/GeoUK.OSTN/Transform.cs +++ b/GeoUK.OSTN/Transform.cs @@ -26,7 +26,7 @@ namespace GeoUK.OSTN private static Osgb36 Etrs89ToOsgb(EastingNorthing coordinates, double ellipsoidHeight, OstnVersionEnum ostnVersion = OstnVersionEnum.OSTN15) { - Shifts shifts = GetShifts(coordinates, ellipsoidHeight, ostnVersion); + Shifts shifts = GetShifts(coordinates, ostnVersion); double easting = coordinates.Easting + shifts.Se; double northing = coordinates.Northing + shifts.Sn; @@ -46,14 +46,14 @@ namespace GeoUK.OSTN double errorE = double.MaxValue; EastingNorthing enCoordinates = null; - Shifts shiftsA = GetShifts(coordinates, coordinates.Height, ostnVersion); + Shifts shiftsA = GetShifts(coordinates, ostnVersion); //0.0001 error meters int iter = 0; while ((errorN > 0.0001 || errorE > 0.0001) && iter < 10) { enCoordinates = new EastingNorthing(coordinates.Easting - shiftsA.Se, coordinates.Northing - shiftsA.Sn); - Shifts shiftsB = GetShifts(enCoordinates, coordinates.Height, ostnVersion); + Shifts shiftsB = GetShifts(enCoordinates, ostnVersion); errorE = Math.Abs(shiftsA.Se - shiftsB.Se); errorN = Math.Abs(shiftsA.Sn - shiftsB.Sn); @@ -65,7 +65,7 @@ namespace GeoUK.OSTN return Convert.ToLatitudeLongitude(new Wgs84(), new BritishNationalGrid(), enCoordinates); } - private static Shifts GetShifts(EastingNorthing coordinates, double ellipsoidHeight, OstnVersionEnum ostnVersion) + private static Shifts GetShifts(EastingNorthing coordinates, OstnVersionEnum ostnVersion) { //See OS Document: Transformations and OSGM02/OSGM15 user guide chapter 3 Dictionary ostnData = GetOstnData(ostnVersion); diff --git a/GeoUK/Convert.cs b/GeoUK/Convert.cs index 1415d55..2ceace3 100644 --- a/GeoUK/Convert.cs +++ b/GeoUK/Convert.cs @@ -2,7 +2,6 @@ using GeoUK.Coordinates; using GeoUK.Ellipsoids; using GeoUK.Projections; using System; -using System.Globalization; namespace GeoUK { @@ -282,38 +281,5 @@ namespace GeoUK /// /// public static double ToDecimalDegrees(int degrees, double minutes) => degrees + (minutes / 60.0); - - private static double Div(double value, double divisor) - { - //make the division - double dblResult = value / divisor; - - //do all calculations on positive numbers - bool blnNegative = false; - if (dblResult < 0) - { - blnNegative = true; - dblResult *= -1; - } - - //see if there is any remainder - dblResult = dblResult % 1 > 0 - ? Math.Ceiling(dblResult) - 1 - : System.Convert.ToInt32(dblResult, CultureInfo.InvariantCulture); - - if (blnNegative) - { - dblResult = Negate(dblResult); - } - - return dblResult; - } - - /// - /// Helper function to reverse the sign of a value. Helps code to be more readable. - /// - /// - /// - private static double Negate(double value) => value * -1.0; } } \ No newline at end of file diff --git a/GeoUK/Coordinates/Osgb36.cs b/GeoUK/Coordinates/Osgb36.cs index a2f8442..4029c69 100644 --- a/GeoUK/Coordinates/Osgb36.cs +++ b/GeoUK/Coordinates/Osgb36.cs @@ -72,7 +72,7 @@ namespace GeoUK.Coordinates 10km (2-figure) Grid Reference: SO84 = 380000 Easting 240000 Northing 1km (4-figure) Grid Reference: NS2468 = 224000 Easting 668000 Northing 100m (6-figure) Grid Reference: TL123456 = 512300 Easting 245600 Northing - */ + */ double easting = Easting; double northing = Northing; @@ -110,27 +110,26 @@ namespace GeoUK.Coordinates string result = string.Empty; //test for our upper and lower limits - if (easting >= 0 && easting < 700000 && northing >= 0 && northing < 1300000) - { - char[] firstChar = new char[6] { 'S', 'N', 'H', 'T', 'O', 'J' }; - char[] secondChar = new char[25] { 'V', 'Q', 'L', 'F', 'A', 'W', 'R', 'M', 'G', 'B', 'X', 'S', 'N', 'H', 'C', 'Y', 'T', 'O', 'J', 'D', 'Z', 'U', 'P', 'K', 'E' }; + if (!(easting >= 0) || !(easting < 700000) || !(northing >= 0) || !(northing < 1300000)) return result; - //calculate the first letter - int indexNorthing = (int)Math.Floor(northing / 500000); - int indexEasting = (int)Math.Floor(easting / 500000); + char[] firstChar = { 'S', 'N', 'H', 'T', 'O', 'J' }; + char[] secondChar = { 'V', 'Q', 'L', 'F', 'A', 'W', 'R', 'M', 'G', 'B', 'X', 'S', 'N', 'H', 'C', 'Y', 'T', 'O', 'J', 'D', 'Z', 'U', 'P', 'K', 'E' }; - //get the first char - char chr1 = firstChar[(indexEasting * 3) + indexNorthing]; + //calculate the first letter + int indexNorthing = (int)Math.Floor(northing / 500000); + int indexEasting = (int)Math.Floor(easting / 500000); - //to get the second letter we subtract the number of 500km sectors calculated above - indexNorthing = (int)Math.Floor((northing - (indexNorthing * 500000)) / 100000); - indexEasting = (int)Math.Floor((easting - (indexEasting * 500000)) / 100000); + //get the first char + char chr1 = firstChar[(indexEasting * 3) + indexNorthing]; - //get the second char - char chr2 = secondChar[(indexEasting * 5) + indexNorthing]; + //to get the second letter we subtract the number of 500km sectors calculated above + indexNorthing = (int)Math.Floor((northing - (indexNorthing * 500000)) / 100000); + indexEasting = (int)Math.Floor((easting - (indexEasting * 500000)) / 100000); - result = $"{chr1}{chr2}"; - } + //get the second char + char chr2 = secondChar[(indexEasting * 5) + indexNorthing]; + + result = $"{chr1}{chr2}"; return result; } } diff --git a/GeoUK/OSTN02Transform.cs b/GeoUK/OSTN02Transform.cs index 0df19ce..a4f9827 100644 --- a/GeoUK/OSTN02Transform.cs +++ b/GeoUK/OSTN02Transform.cs @@ -48,7 +48,7 @@ namespace GeoUK string csvRecord = tr.ReadLine(); for (int index = 0; index < 4; index++) { - if (!csvRecord.StartsWith(recordNumbers[index].ToString().Trim() + ",", StringComparison.Ordinal)) + if (csvRecord != null && !csvRecord.StartsWith(recordNumbers[index].ToString().Trim() + ",", StringComparison.Ordinal)) continue; //don't use add as we need to keep these in same order as record numbers diff --git a/GeoUK/Transform.cs b/GeoUK/Transform.cs index c7915ff..b7fe7cd 100644 --- a/GeoUK/Transform.cs +++ b/GeoUK/Transform.cs @@ -184,8 +184,6 @@ namespace GeoUK private static double ToRadians(double degrees) => degrees * (Math.PI / 180.0); - private static double ToDegrees(double radians) => radians * (180.0 / Math.PI); - /// /// This seven parameter method can be used to transform coordinates between datums. ///