Update Osgb36.cs with variable precision grid reference (#45)

* Update Osgb36.cs

Replace fixed imprecise grid reference with variable precision one

* Compatible default OS Digits

Alter default OS Digits to be compatible with existing code

* Refactor OsDigits initialization and GetBngSquare logic

- Initialize OsDigits property to OsDigitsType.OsDigits6 by default.
- Remove redundant assignment of OsDigits in the constructor.
- Reformat switch statement for clarity.
- Improve readability of conditional check in GetBngSquare method.

* Update terminology from "metre" to "meter"

Changed comments for OS Grid Reference digit types to use "meter" instead of "metre" for 6, 8, 10, and 12 digit references, aligning with American English conventions.

* Update project to target .NET 9.0 and improve formatting

The project file `GeoUK.OSTN.XUnit.csproj` has been updated to target .NET 9.0 instead of .NET Core 2.2. The structure of the `<PropertyGroup>` and `<ItemGroup>` elements has been adjusted for better formatting, while the content remains unchanged. Additionally, the `<None>` and `<Content>` items have been reformatted for consistency, and the `<PackageReference>` and `<ProjectReference>` elements have been improved for readability.

---------

Co-authored-by: Ieuan Walker <walker.ieuan@gmail.com>
This commit is contained in:
markgloverswaybtinternetcom
2025-07-11 11:21:35 +01:00
committed by GitHub
parent 27a4341f91
commit 2e41a3d860
2 changed files with 106 additions and 66 deletions

View File

@@ -12,6 +12,26 @@ namespace GeoUK.Coordinates
/// </remarks>
public class Osgb36 : EastingNorthing
{
/// <summary> Number of Digits in OS Grid Reference</summary>
public enum OsDigitsType
{
/// <summary>2 Digits OS Grid Ref 10 km accuracy</summary>
OsDigits2,
/// <summary>4 Digits OS Grid Ref 1 km accuracy</summary>
OsDigits4,
/// <summary>6 Digits OS Grid Ref 100 meter accuracy</summary>
OsDigits6,
/// <summary>8 Digits OS Grid Ref 10 meter accuracy</summary>
OsDigits8,
/// <summary>10 Digits OS Grid Ref 1 meter accuracy</summary>
OsDigits10,
/// <summary>12 Digits OS Grid Ref 0.1 meter accuracy</summary>
OsDigits12
}
/// <summary> Number of Digits in OS Grid Reference</summary>
public static OsDigitsType OsDigits { get; set; } = OsDigitsType.OsDigits6;
/// <summary>
/// Initializes a new instance of the <see cref="Coordinates.Osgb36Cordinates"/> class.
/// </summary>
@@ -67,11 +87,6 @@ namespace GeoUK.Coordinates
{
get
{
/*
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;
@@ -91,10 +106,34 @@ namespace GeoUK.Coordinates
northing -= indexNorthing * 100000;
easting -= indexEasting * 100000;
northing = Math.Round(northing / 100);
easting = Math.Round(easting / 100);
return $"{bngSquare}{Math.Round(easting):000}{Math.Round(northing):000}";
switch(OsDigits)
{
case OsDigitsType.OsDigits2: //10km(2 - figure) Grid Reference: SO84 = 380000 Easting 240000 Northing
northing = Math.Round(northing / 10000);
easting = Math.Round(easting / 10000);
return $"{bngSquare}{Math.Round(easting):0}{Math.Round(northing):0}";
case OsDigitsType.OsDigits4: //1km(4 - figure) Grid Reference: NS2468 = 224000 Easting 668000 Northing
northing = Math.Round(northing / 1000);
easting = Math.Round(easting / 1000);
return $"{bngSquare}{Math.Round(easting):00}{Math.Round(northing):00}";
default:
case OsDigitsType.OsDigits6: // 100m (6-figure) Grid Reference: TL123456 = 512300 Easting 245600 Northing
northing = Math.Round(northing / 100);
easting = Math.Round(easting / 100);
return $"{bngSquare}{Math.Round(easting):000}{Math.Round(northing):000}";
case OsDigitsType.OsDigits8:// 10m (8-figure) Grid Reference
northing = Math.Round(northing / 10);
easting = Math.Round(easting / 10);
return $"{bngSquare}{Math.Round(easting):0000}{Math.Round(northing):0000}";
case OsDigitsType.OsDigits10:// 1m (10-figure) Grid Reference
northing = Math.Round(northing);
easting = Math.Round(easting);
return $"{bngSquare}{Math.Round(easting):00000}{Math.Round(northing):00000}";
case OsDigitsType.OsDigits12:// 0.1m (12-figure) Grid Reference
northing = Math.Round(northing * 10);
easting = Math.Round(easting * 10);
return $"{bngSquare}{Math.Round(easting):00000}{Math.Round(northing):00000}";
}
}
}
@@ -109,7 +148,8 @@ namespace GeoUK.Coordinates
string result = string.Empty;
//test for our upper and lower limits
if (easting < 0 || easting > 700000 || northing < 0 || northing > 1300000) return result;
if(easting < 0 || easting > 700000 || northing < 0 || northing > 1300000)
return result;
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' };

View File

@@ -1,64 +1,64 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_ETRS89toIG.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_IGtoETRS89.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_ETRS89toIG.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_IGtoETRS89.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestFiles_README.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_ETRStoOSGB.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_OSGBtoETRS.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_ETRStoOSGB.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_OSGBtoETRS.txt" />
</ItemGroup>
<ItemGroup>
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_ETRS89toIG.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_IGtoETRS89.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_ETRS89toIG.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_IGtoETRS89.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestFiles_README.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_ETRStoOSGB.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_OSGBtoETRS.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_ETRStoOSGB.txt" />
<None Remove="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_OSGBtoETRS.txt" />
</ItemGroup>
<ItemGroup>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_ETRS89toIG.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_IGtoETRS89.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_ETRS89toIG.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_IGtoETRS89.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestFiles_README.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_ETRStoOSGB.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_OSGBtoETRS.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_ETRStoOSGB.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_OSGBtoETRS.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_ETRS89toIG.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestInput_IGtoETRS89.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_ETRS89toIG.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSGM15_NI_TestOutput_IGtoETRS89.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestFiles_README.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_ETRStoOSGB.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestInput_OSGBtoETRS.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_ETRStoOSGB.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestDataFiles_OSGM15_OSTN15\OSTN15_OSGM15_TestOutput_OSGBtoETRS.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\GeoUK.OSTN\GeoUK.OSTN.csproj" />
<ProjectReference Include="..\..\GeoUK\GeoUK.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\GeoUK.OSTN\GeoUK.OSTN.csproj" />
<ProjectReference Include="..\..\GeoUK\GeoUK.csproj" />
</ItemGroup>
</Project>