code update

This commit is contained in:
Ieuan Walker
2019-07-13 00:46:23 +01:00
parent 038f00fef0
commit 56fb9b1f1d
7 changed files with 23 additions and 60 deletions

View File

@@ -5,7 +5,7 @@ namespace DemoProject
{
class Program
{
static void Main(string[] args)
static void Main()
{
EastingNorthingToLatitudeLongitude.Example();

View File

@@ -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

View File

@@ -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<int, OstnDataRecord> ostnData = GetOstnData(ostnVersion);

View File

@@ -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
/// <param name="minutes"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Helper function to reverse the sign of a value. Helps code to be more readable.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static double Negate(double value) => value * -1.0;
}
}

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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);
/// <summary>
/// This seven parameter method can be used to transform coordinates between datums.
/// </summary>