Android Question Converting Coordinates

Terradrones

Active Member
Hi All

I desperately need help. I see that my old Posting has been closed.

I need to convert from any Coordinade system to Lats\Longs and back again.

I have looked at Spatialite, but don't have a clue what is going on there...I think old-age is making me stupid.

Here is my code, which is a Class:

B4X:
[/

Sub Class_Globals
    Private Spa As Spatialite_Database
    
'    Dim PI As Double = 3.1415926535897932385
'    Dim RADIAN As Double = 2.0 * PI / 360    
'    
'    Dim Local_srid As String
    'Dim Data_File As String
    Dim spconstants As Spatialite_Constants

'    Initialize(File.DirRootExternal & "/CEASER/Data/" ,Data_File,"READONLY")
'    Spa.Open(File.DirInternal, "mm.sqlite", spconstants.SQLITE_OPEN_READONLY)

End Sub

Public Sub Initialize '(DPATH As String ,DBASE As String , OPEN_STYLE As String)
    Dim DPath, DBase, Open_Style As String

    Spa.Initialize
    
    DPath=File.DirRootExternal & "/CEASER/Data"
    DBase="Projections.csv"
    'File looks like this:
    'EPSG    Continent    Country    Grid
    '5825    Oceania    AU-CT    AGD66 / ACT Standard Grid
    '20249    Oceania    AU-#M    AGD66 / AMG zone 49
    '20250    Oceania    AU-#M    AGD66 / AMG zone 50

    Open_Style="ReadOnly"

    'Initialize(DPath ,DBase,"READONLY")
    
    Spa.Open(DPath, DBase, spconstants.SQLITE_OPEN_READONLY)

    Select Open_Style
        Case "READWRITE"
            Spa.Open(DPath, DBase, spconstants.SQLITE_OPEN_READWRITE)
        Case "READONLY"
            Spa.Open(DPath, DBase, spconstants.SQLITE_OPEN_READONLY)
    End Select
End Sub

Sub Get_point_from_local( X As Double , Y As Double) As String
    Dim sqStm As Spatialite_TableResult
    Dim result As String
    Dim sql1 As String

    sql1 = "SELECT ST_AsText(ST_Transform(ST_GeomFromText('POINT("
    sql1 = sql1 & X & " " & Y & ")'," & CGlobals.EPSG & "),4326)) As trans_geom;"
    'Globals.EPSG =2048

    Try
        sqStm = Spa.GetTable(sql1)
        result = sqStm.Rows(0,0)
    Catch
        Log(LastException.Message)
        MsgboxAsync(LastException.Message & CRLF & "Wrong EPSG code" ,"ATTENTION")
        'Local_srid="2048"
        Return "0000000000000000000"
    End Try
    If result=Null Or result="" Then
        Return "0000000000000000000"
    End If
    Return result
End Sub

'''transform lat lon to local
Sub Get_point_to_Local( lat As Double , lon As Double) As String
    Dim sqStm As Spatialite_TableResult
    Dim result As String
    Dim sql1 As String
    sql1 = "SELECT ST_AsText(ST_Transform(ST_GeomFromText('POINT("
    sql1 = sql1 & lon & " " & lat & ")',4326),"
    sql1 = sql1 & CGlobals.EPSG & ")) As trans_geom;"
    sqStm = Spa.GetTable(sql1)
    result = sqStm.Rows(0,0)
    Return result
End Sub
]
 

derez

Expert
Licensed User
Longtime User
I need to convert from any Coordinade system to Lats\Longs and back again.
Navigation library lets you convert from UTM to GEO and from GEO to UTM
 
Upvote 0

Terradrones

Active Member
Navigation library lets you convert from UTM to GEO and from GEO to UTM
Hi Derez

Thank you for your reply, but that Library only converts between Lat\Long and UTM (and Vice Versa).

I need help please in using Spatialite, so that I can convert from any Coordinate System to any other Coordinate System.

Thanks
Michael
 
Upvote 0

roumei

Active Member
Licensed User
I have no experience with Spatialite but I do think that they are using the PROJ.4 library to convert between the projections. I can't help you with that, though.

My advice would be to implement the calculations yourself (only for the necessary conversions). This is the reference book for all projection types including all the formulas:

The parameters for all EPSG codes can be found here:
 
Upvote 0

Terradrones

Active Member
I have no experience with Spatialite but I do think that they are using the PROJ.4 library to convert between the projections. I can't help you with that, though.

My advice would be to implement the calculations yourself (only for the necessary conversions). This is the reference book for all projection types including all the formulas:

The parameters for all EPSG codes can be found here:
Thank you Roumei.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
My advice would be to implement the calculations yourself ...
I tend to agree with @roumei. But one thing that you have never mentioned in this or your previous thread is how you know which Cartesian grid you are working with. If I simply give you a grid coordinate pair (Easting/Northing for example) you cannot convert that to Lat/Lon unless you know which grid system I am using. I do not understand how even Spatialite could know that. Most places on Earth are covered by more than one grid system. One will be UTM, for instance, and it is quite likely that another will be obsolescent.

So, my question is, how much do you know about the Cartesian grid system that is being used?
 
Upvote 0

Terradrones

Active Member
I tend to agree with @roumei. But one thing that you have never mentioned in this or your previous thread is how you know which Cartesian grid you are working with. If I simply give you a grid coordinate pair (Easting/Northing for example) you cannot convert that to Lat/Lon unless you know which grid system I am using. I do not understand how even Spatialite could know that. Most places on Earth are covered by more than one grid system. One will be UTM, for instance, and it is quite likely that another will be obsolescent.

So, my question is, how much do you know about the Cartesian grid system that is being used?
Hi Brian

When the Surveyor uses the program for the first time, I have a function called "System Setup". Here the Surveyor will select which type and make of Dumpy Level, Total Station and GPS the person will be using. There are other settings as well, such as Temperature, Prism Constant, Scale Factor, etc.

On the last Panel, the Surveyor selects on which Continent he\she is located, then the Country and finally the System. As an example in South Africa it will be Haartebeeshoek94 and Lo19 where I stay in Paarl. Using these 3 variables, I read the EPSG code from my Database (I have about 3600 EPSG Codes in the Database).

Also, the Surveyor cannot use any of the other functions in the program until such time that the "System Setup" has been completed.

I do not know if I am misreading the Forum, but knowing the EPSG Code, one can convert coordinates from one system to another.

Thanks
Michael
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Okay - that is a good start, providing the surveyor has that knowledge.

I have had a quick look at Spatialite and I can see that it is capable of doing the job that you want, but like a lot of open-source projects it has become so bloated that it is hard to see the wood for the trees (in my opinion). I will look at it a little longer and see if I can work something out, but it won't be quick and hopefully someone else on the Forum has been there already and will post something more helpful. I am almost certainly older than you are so if old-age is a debilitating factor then we are both in trouble.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Hi Michael - I am back, temporarily.

I spent some time yesterday looking at Spatialite. I started, as you did, with warwound's library, but I tried to get it running with B4J as that would be easier to work with. Unfortunately even at 14Mbyte the library doesn't have enough resources to do the job on its own and I could not work out what else was required. I looked at installing Spatialite itself under Windows, but again it requires additional resources and the guidance assumes that you are using Visual Studio, so that was not a useful path.

I also tried the direct conversion to WGS84 from Gauss Conform coordinates. At least that produced results but I have an error that isn't easy to find. Also it won't lead to a solution that can handle hundreds of SRIDs.

I am going to press on today and go back to warwound's library and Android. A couple of questions :

Did you manage to get that library working with Android? That is, were you able to run queries, even if the query results were not what you were looking for?
 
Upvote 0

Terradrones

Active Member
Hi Brian, yes I managed to convert from Lat\Long to Grid Coords and vice versa with the File that George (Gvoulg) sent me. It is only the accuracies that is a worrying factor.

If you give me your email address, then I can mail that file to you.

I am going to look again what I have done in VB.net compact, as it worked.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
An update to my earlier feedback. I have created a new Spatialite database (not yet loaded into any Android app) and have been looking inside it - specifically to see how it stores the SRID parameters. I started with one that I know well (OSGB) and I could see all the grid parameters. Then I looked up Hartebeesthoek94, which will be of more interest to you, and although there is an entry the parameter data is missing. There should be a different set of parameters for each two degree shift in longitude (eg Hartebeesthoek94 Lo19) but these do not appear to exist in the database.

I am wondering now if Spatialite is going to be of any use at all - whether in its basic state it can do the conversions that you require. You have mentioned that you have a long (3500?) list of grid definitions - is that simply a list of grid titles and their EPSG numbers, or does it include the specific grid parameters (False Northing, Easting, scale factor etc)?
 
Upvote 0

Terradrones

Active Member
I have replaced my "Projection File" with that of George.

I converted the coordinates between Malmesbury and Mooresburg on the N7 and they all plotted fine.

I am attaching the Projection File that I used in my VB.net Project.

Busy trying to figure out what I did in VB.net compact, because that worked.
 

Attachments

  • ProjectionsDB.zip
    96.2 KB · Views: 72
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Another update : I have just run the code provided by @gvoulg in your previous thread through a primitive Android app containing a Spatialite database. This transformed a WGS84 Lat-Lon value to Ordnance Survey Great Britain grid coordinates.

This is good news but, as I said in my previous post, does not mean that I can meet your primary objective.

Happy New Year.

[EDIT : As I suspected, replacing the UK data with the WGS84 coordinates for Cape Town and EPSG 2048 results in a crash.]
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Hi @Terradrones . I feel that I owe you an explanation about yesterday. You must have wondered why I was ignoring your posts - the reason was that I wasn't seeing them. I could see only my own posts, even though I was checking on three devices (PC, 'phone and tablet) and as late as 10:30pm, after midnight your time. It is very strange, but I have noticed something similar in other threads lately.

Anyway, it seems that you have solved your problem, whatever it was, and were more successful that I. Once again, Happy New Year.
 
Upvote 0
Top