Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting latitude longitude to decimals

    Hi,

    I'm trying to convert data on distances from this sort of format "547898" to something that is within [-90,90] and [-180,180], so that I can use the "vincenty" command or any other command that will allow me to calculate distances. Is there any way I can do it with Stata? I have a large set of observations and I'm not sure if Excel would be able to use it.

    Thanks a lot.

    Eleonora

  • #2
    What does 547898 represent? Can't be 54 deg 78 min 98 sec. If it's a decimal latitude then you just need to divide by 10000.

    Comment


    • #3
      Eleonora,
      Is "547898" already a distance (in meters for example) or latitude / longitude coordinates? You call it a distance, but if you want to use vincenty (or you might also try geodist from Robert Picard, see this post), the input should be coordinates (because they aim at computing distances).

      Please, post an example of your data (as per FAQ advice #12)

      How many similar variables you have for each observations what are their name (and their source)? If you have only one it could either be a distance (but from which point?) or a concatenation of long / lat coordonates (but uneasy to handle.)
      If you have two variables, as Nick said, it is probably lat / long coordinates and you just need to divide them by 10000.
      Code:
      replace lat = lat / 10000
      Charlie

      Comment


      • #4
        Thank you so much for taking the time to reply!

        I was given the dataset and the variable names for the two coordinates are "oseast1m" and "osnorth1m", but no description of what they mean; they are pair of numbers formed of 6 digits; when I try to compute the distance using "
        Code:
        gen distance  = sqrt((oseast1m-lat)^2+(osnrth1m-lon)^2)
        - where long and lat are the coordinates of the point I want to calculate the distances from, I get numbers that are not even close to the real distance (I have tried converting them into kilometers and miles.)

        In the example below, pcd stands for post code and the following two should be the coordinates for east and north.

        input str7 pcd str6 oseast1m str7 osnrth1m str9 ctry
        "AB1 0AA" "385386" "0801193" "S92000003"
        "AB1 0AB" "385177" "0801314" "S92000003"
        "AB1 0AD" "385053" "0801092" "S92000003"
        "AB1 0AE" "384600" "0799300" "S92000003"
        "AB1 0AF" "384460" "0800660" "S92000003"

        Thanks in advance!

        Eleonora

        Comment


        • #5
          To any old-fashioned British geographer (c'est moi, naturellement) OS means Ordnance Survey and the data are reminiscent of 6-figure eastings and northings. (EDIT: See later post for a comment on 6 and 7 figure postings.)

          In fact AB1 0AA is self-evidently a postcode somewhere near the centre of Aberdeen in Scotland, which is, at present, part of the so-called United Kingdom.

          https://www.doogal.co.uk/ShowMap.php?postcode=AB1%200AA

          Just shows that being cryptic and giving us minimal information only delays good answers. These data are, the curvature of the earth nothwithstanding, essentially Cartesian coordinates. No conversion is necessary, but using double variables for distances would be a good idea.
          Last edited by Nick Cox; 05 Jul 2016, 07:44.

          Comment


          • #6
            I run this after converting the lat and long as suggested in the previous post
            Code:
            vincenty oseast1mdec osnrth1mdec lat_heatdec lon_heatdec, v(v_h) hav(h_h) l(loc_h)
            where the variables ending in "dec" are the modified one (divided by 10000). lat_heatdec and lon_heatdec are the coordinates of the point I want to get the distance from. I have attached a screenshot of my results; the values that I get are not close to the real distance (for example, for the cell selected, it should be approximately 6.4 miles or about 10 kilometers). Click image for larger version

Name:	image_5413.png
Views:	1
Size:	307.4 KB
ID:	1348125

            Comment


            • #7
              Sorry about "being cryptic" but I genuinely don't know anything about geo coordinates and I've been left alone doing this. It was not my intention to sound shady! Just looking for help

              Comment


              • #8
                Your #6 crossed with my #5.

                OS grid references are emphatically not latitudes and longitudes. (A simple check is that Aberdeen is not 8 deg N of the Equator!!!)

                I can't say why your Eastings and Northings are 6 and 7 figures, respectively.

                Dividing by 10000 gives you nonsense. vincenty will indeed give you nonsense.

                There is no need to convert to degrees latitude and longitude, and you won't do it correctly without specific routines for OS grid references.

                A simple trick often omitted is to plot Northings and Eastings as a scatter plot. It often clarifies what the data are.

                PS: Not knowing about geographic coordinates is fine by us. A zillion things mentioned here are above my head, such as most of the economics or finance stuff that many posters assume that everyone knows. But not telling us as much about your data as you can just slows threads down. No more, no less.
                Last edited by Nick Cox; 05 Jul 2016, 07:49.

                Comment


                • #9
                  I see, thank you! Any idea how I could go about calculating distances using the Eastings and Northings that I have?

                  Comment


                  • #10
                    As in your earlier post, it's just an application of Pythagoras' theorem but string variables need to be made numeric and you need to make the 6 figure and 7 figure references consistent (possibly by dividing the latter by 10, but check sample places with local Ordnance Survey maps or anything showing the national grid).

                    Comment


                    • #11
                      I really appreciate the help but I'm still confused. I've divided the 7 figure by 10, calculated distances again using Pythagoras' theorem but am unable to interpret the number I get. Click image for larger version

Name:	Schermata 2016-07-05 alle 15.11.15.png
Views:	1
Size:	295.3 KB
ID:	1348139

                      Comment


                      • #12
                        I'm still confused as to the type of coordinates stored in lat and lon in #4. In order to calculate distances, both sets of coordinates must be expressed in the same reference system. The variable names suggest that these are latitude and longitudes and if that's correct, then one set of coordinates must be converted to match the other before distances can be calculated.

                        If oseast1m and osnrth1m are indeed "OSGB36 National grid coordinates in metres of Eastings and Northings", then the following free tool can be used to convert them to "ETRS89 Geodetic ( Decimal Degrees )", which is equivalent to WGS84 (GPS coordinates):

                        Code:
                        https://www.ordnancesurvey.co.uk/gps/transformation/batch
                        Using this tool with the data in #4 (with some post processing to parse the output), the results are:

                        Code:
                                        v1         lat          lon  
                            385386 0801193   57.101459   -2.2428581  
                            385177 0801314   57.102539   -2.2463149  
                            385053 0801092   57.100541   -2.2483483  
                            384600 0799300   57.084429   -2.2557142  
                            384460 0800660   57.096641   -2.2581089
                        If you locate these points using Google Maps (e.g. search for 57.101459, -2.2428581), you find that they are in Peterculter, a suburb of Aberdeen, Scotland.

                        You can then use geodist (from SSC) to calculate distances. If the purpose of the exercise is a nearest neighbor problem (e.g. find the nearest hospital or points within 1km), then you can use geonear (from SSC). If you have a large number of points, you will find geonear invaluable.

                        Comment


                        • #13
                          Thank you Robert. Unfortunately the tool you linked only supports files up to 2MB while mine is way bigger. Any alternative?

                          PS: I myself am confused about the type of coordinates I have because I was given no description at all.

                          Comment


                          • #14
                            The file you feed only needs the values of oseast1m and osnrth1m. That's about 15 bytes per observation, which means that you can feed it more than 130,000 points at a time. If you have more than that, you can do it in parts.

                            You still aren't giving any clue about the lat and lon variables in you refer to in #4.

                            Comment


                            • #15
                              I am trying to calculate the distance between houses and the Heathrow and Gatwick airports. oseast1m and osnrth1m contain coordinates for post codes I have selected. Lat and long in #4 represent coordinates of one of the airports, supposedly in the same coordinates as oseast1m and osnrth1m (all I did was googling coordinates and converting them to eastings and northings).

                              Comment

                              Working...
                              X