IeuanWalker/GeoUK

[Request] 10-digit Osgb36 from WGS84 LatLon

Closed this issue · 3 comments

I'm currently working on a project that requires converting WGS84 LatLon coordinates to Osgb36 coordinates.

While the library does facilitate this, it's unfortunately only possible to create 6-digit Osgb36 values:
e.g. (note: the below aren't real values)
LatLon: 50.5253453, 1.535435353
becomes
Osgb36: NR395295

Ideally I need this to be 10-digits (NR3954129562) so the ability to specify the conversion accuracy as a function parameter would be fantastic.

As an aside to this, assuming these changes can't be made would I be correct in thinking that accuracy is lost during the rounding stage on the last line of the code clipped below? (GeoUK/GeoUK/Coordinates/Osgb36.cs)

public string MapReference
        {
            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;

                string bngSquare = GetBngSquare(easting, northing);

                //get the number of complete 500k squares
                int indexNorthing = (int)Math.Floor(northing / 500000);
                int indexEasting = (int)Math.Floor(easting / 500000);

                //reduce E and N by the number of 500k squares
                northing -= indexNorthing * 500000;
                easting -= indexEasting * 500000;

                //reduce by the number of 100k squares within the 500k square.
                indexNorthing = (int)Math.Floor(northing) / 100000;
                indexEasting = (int)Math.Floor(easting) / 100000;

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

Thanks,
Sam

@Hanayou
Hi,

I'm not to sure about this side of things sorry. The only involvement i have had with this project was porting it from .net framework to .net standard, so I'm not too familiar with the code itself.

In my projects i only need to convert from Longitude/ Latitude to Easting/ Northing.

But feel free to create a pull request if you find a solution.

Thanks, Ieuan Walker.

Hi Hanyou,

I had the same issue. The grid references at 6fig are rounded up, you can manipulate the bngEN.Easting & bngEN.Northings to make them fit the format you need.

The first digit for the eastings and northing is converted into the Designation letters so you just need to cut out the parts you need, I used a substring for this.

For an 10 fig grid I used the following code:

return mapReference.Substring(0,2) + " " + bngEN.Easting.ToString().Substring(1,5) + " " + bngEN.Northing.ToString().Substring(1, 5);

Not the best solution - but it works! Smile | :)

Hi Hanyou,

I had the same issue. The grid references at 6fig are rounded up, you can manipulate the bngEN.Easting & bngEN.Northings to make them fit the format you need.

The first digit for the eastings and northing is converted into the Designation letters so you just need to cut out the parts you need, I used a substring for this.

For an 10 fig grid I used the following code:

return mapReference.Substring(0,2) + " " + bngEN.Easting.ToString().Substring(1,5) + " " + bngEN.Northing.ToString().Substring(1, 5);

Not the best solution - but it works! Smile | :)

I originally worked around this issue months back by changing the :000 to :00000 in the last line I highlighted in my original issue. I had to revisit this in the last few and wanted to use the nuget package directly rather than manage my own copy of the source, so this solution worked a charm - great timing!