B4A Library GPStoOSGB library

Mapping and location services have a large emphasis in Android. Most phones appear to be equipped with GPS and, as with Basic4ppc, I am sure that people will be writing own GPS applications. Useful only for those in Great Britain this is a port of my Basic4ppc GPStoOSGB library.

For those who don't know the Ordnance Survey is Great Britains national mapping and survey service and is the primary source of map data for the UK. The major feature of the library is the transformation of GPS data to and from the OS National Grid Reference which is the main way that map locations are specified in the UK. We don't use lat/long we use the National Grid and it is printed on all OS maps as a grid as the primary location mechanism. Lat/long is included but only as small crosses at spot points for cross reference to the grid.

Libraries such as this are less useful for Android (and for Basic4ppc version 6.9) than they used to be for the weakly typed Basic4ppc as the strong typing means that by using numeric types the performance of normal application code is only a little slower than library code. When code modules are added to Basic4android the encapsulation of functionality such as this library can be as well done in a module as in a library.
 

Attachments

  • GPStoOSGB1.0.zip
    14 KB · Views: 1,223

stu14t

Active Member
Licensed User
Longtime User
agraham

I'd just like to say thanks for this library, I find it immensely useful.

I'm a highway engineer in the UK and OSGB is how we locate every asset, scheme and many other things on the road network.

I was going to attempt to write my own lib using this code but you've done it all for me.

I've now created a range of apps that rely on this conversion library.

:sign0098:
 

PCowling

Member
Licensed User
Longtime User
Stu

Is it possible to have a look at some of your projects please?

I could do with some help and examples using the lib.

Thanks

P Cowling
 

stu14t

Active Member
Licensed User
Longtime User
No problems at all.

What information do you need specifically and I'll show you how I've done it?

Here is the code to extract the Eastings and Northings:
B4X:
Sub GPS_LocationChanged (Location1 As Location)
    Lat1 = Location1.Latitude
   Long1 = Location1.Longitude
   Altitude = Location1.Altitude
   Dim str As String
   Dim digits As Int
   Dim sep As String
   OSGB.WGS84toOSGB36(Lat1, Long1, Altitude)
   str = OSGB.OSGB36toNGR(OSGB.NewLatitude, OSGB.NewLongitude)
   digits = 5   
   sep = " "
   ngr = "GridRef: " & str.SubString2(0, 2) & sep & str.SubString2(2, digits + 2)
   ngr = ngr & sep & str.SubString2(7, digits + 7) & Chr(13) & Chr(10)
   East = NumberFormat2(OSGB.Easting, 1, 0, 0, False)
   North = NumberFormat2(OSGB.Northing, 1, 0, 0, False) 
End Sub
 
Last edited:

PCowling

Member
Licensed User
Longtime User
Thanks that's really kind.

All I am trying to do is see how to take the GPS data from the phone and store/display the location as a OS 6 figure grid with accompanying letters.

Spent a lot of time some time ago converting some script I found to do this. But I struggled with getting input format correct from the GPS. Other that this it did work.

Thought Id revisit this project with this lib.

Thanks

P Cowling
 

stu14t

Active Member
Licensed User
Longtime User
The code above will supply you with all the data you require.

The ngr string will include the two letter suffix including the 5 digit number and the East and North variables will return the 6 digit Eastings and Northings numeric values.
 

johnaaronrose

Active Member
Licensed User
Longtime User
Mapping and location services have a large emphasis in Android. Most phones appear to be equipped with GPS and, as with Basic4ppc, I am sure that people will be writing own GPS applications. Useful only for those in Great Britain this is a port of my Basic4ppc GPStoOSGB library.

For those who don't know the Ordnance Survey is Great Britains national mapping and survey service and is the primary source of map data for the UK. The major feature of the library is the transformation of GPS data to and from the OS National Grid Reference which is the main way that map locations are specified in the UK. We don't use lat/long we use the National Grid and it is printed on all OS maps as a grid as the primary location mechanism. Lat/long is included but only as small crosses at spot points for cross reference to the grid.

Libraries such as this are less useful for Android (and for Basic4ppc version 6.9) than they used to be for the weakly typed Basic4ppc as the strong typing means that by using numeric types the performance of normal application code is only a little slower than library code. When code modules are added to Basic4android the encapsulation of functionality such as this library can be as well done in a module as in a library.

I've just checked a location's details using the 'calculator' on
B4X:
http://www.movable-type.co.uk/scripts/latlong-gridref.html

It gives slightly different figures (i.e the 5th & 6th decimal digits) for latitude (for OS ref SO 91370 95550) from those in your demo project (which I've amended to show 6 decimal digits for both OSGB36's &WGS84's latitude & longitude. I've hand calculated that webpage's latitude & longitude as the webpage gave the results in minutes & seconds. Can you explain the differences?

The webpage's results are:
OSGB36: Lat=52deg33'26.4943"=52.557360degN, Lon=2deg7'38.3102"W=2.127308degW
WGS84: Lat=52deg33'27.7832"=52.557771degN, Lon=2deg7'43.4101"W=2.128725degW

Your project's results are:
OSGB36: Lat=52.557346deg, Lon=-2.127308deg
WGS84: Lat=52.557704deg, Lon=-2.128725deg

PS I'd like to use your OSGB library in Gambas. Gambas is a similar RAD to Visual Basic (with a similar language but OO) running on the major Linux distros for PCs (thus not Android). It has components which are libraries written in C/C++/Gambas. I presume that OSGB is written in Java. If so, what do you estimate the difficulty of converting it to (e.g.) Gambas?
 

agraham

Expert
Licensed User
Longtime User
Can you explain the differences?
No, although we both claim to have used formulae provided by the OS his osGridToLatLong() algorithm seems to be different from the one I used in NGRtoOSGB36() and has some form of successive approximation loop within it whereas as mine is a straight numeric calculation. I have no idea which is more accurate.

Unless you need a native library for speed I would have thought that transcribing the code given on that web site to Gambas code would be trivial, if slightly tedious. He also links to another of his pages which has code for converting between WGS84 and OSGB36. I'm assuming that Gambas implements a double length floating point number type.
 

johnaaronrose

Active Member
Licensed User
Longtime User
No, although we both claim to have used formulae provided by the OS his osGridToLatLong() algorithm seems to be different from the one I used in NGRtoOSGB36() and has some form of successive approximation loop within it whereas as mine is a straight numeric calculation. I have no idea which is more accurate.

Unless you need a native library for speed I would have thought that transcribing the code given on that web site to Gambas code would be trivial, if slightly tedious. He also links to another of his pages which has code for converting between WGS84 and OSGB36. I'm assuming that Gambas implements a double length floating point number type.

So it looks like your version may be better. Is it possible for you to send me the code for OSGB as it would be simpler to convert that to Gambas as opposed to the Javascript (which I don''t understand)? I would be happy to supply you with the resultant Gambas component library and my app using it.
 

johnaaronrose

Active Member
Licensed User
Longtime User
I have versions in both C# and Java which to the unfamiliar eye look almost identical to JavaScript (that's because they are!)

Thanks for those routines. I've found Roger Muggleton's Carabus website containing calculators for these functions. I sent him an email before I noticed that your code acknowledged that it was derived from Roger's work. I said that there was a difference in the 6th decimal digit for latitude between his & Chris Veness (the author of the scripts at the movable-type website referred to in my first posting). I also said that someone else (namely you) had a difference in the 5th & 6th decimal digits of the latitude from Roger & Chris. I'm awaiting his reply. I've had no reply from Chris when I asked him to explain the differences. I'd like to have the B4A library & the Gambas code (BTW that will have to be done as a module written in Gambas as a Component is for a Control - view in B4A terms).

Could you point me to the code in your routines & the equivalent code in Chris's (i.e. the successive approximation loop in the movable-type website) which you previously stated as being different?

PS my wanting consistency to at least 6 decimal digits is due to the mathematician in me which finds lack of precision to be highly irritating!
 

johnaaronrose

Active Member
Licensed User
Longtime User
Attached is a converter (named GPSOSGB) from any of OSGB National Grid Reference, OSGN Easting & Northing, OSGB36 Latitude & Longitude, and GPS (i.e. WGS84) Latitude & Longitude. It displays the conversions for the other above 'types'. As expected it calls the GPStoOSGB Library.
 

Attachments

  • GPSOSGB.apk
    172.1 KB · Views: 203
Top