Announcement

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

  • Latitude and Longitude Variable Conversion

    Hello


    I am working on the survey data having Latitude and Longitude information.

    In the survey files, I have comes up with the following variables.

    latitude N/S
    latitude (degree)
    latitude (decimal degree)
    longitude (E/W)
    longitude (degree)
    longitude (decimal degree)



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 country str1 lat float(lat_deg lat_decimal_degree) str1 lon float(lon_deg lon_decimal_degree)
    "GHA" "N" 4 91358 "W" 1 78570
    "GHA" "N" 4 91917 "W" 1 72983
    "GHA" "N" 4 91378 "W" 1 78345
    "GHA" "N" 4 92293 "W" 1 73002
    "GHA" "N" 4 91370 "W" 1 78452
    "GHA" "N" 4 92293 "W" 1 73002
    "GHA" "N" 4 91522 "W" 1 78408
    "GHA" "N" 4 91358 "W" 1 78570
    "GHA" "N" 4 91567 "W" 1 78482
    "GHA" "N" 4 91358 "W" 1 78570
    "GHA" "N" 4 91493 "W" 1 78372
    "GHA" "N" 4 90875 "W" 1 83342
    "GHA" "N" 4 90880 "W" 1 83243
    "GHA" "N" 4 90837 "W" 1 83178
    "GHA" "N" 4 90988 "W" 1 83243
    "GHA" "N" 4 90950 "W" 1 83257
    "GHA" "N" 4 90980 "W" 1 83285
    "GHA" "N" 4 90920 "W" 1 83310
    "GHA" "N" 4 90840 "W" 1 83380
    "GHA" "N" 4 90925 "W" 1 83383
    "GHA" "N" 4 90980 "W" 1 83285
    "GHA" "N" 4 90988 "W" 1 83243
    "GHA" "N" 4 90840 "W" 1 83380
    "GHA" "N" 4 90935 "W" 1 83457
    "GHA" "N" 4 90920 "W" 1 83310
    "GHA" "N" 4 90928 "W" 1 83300
    "GHA" "N" 4 90938 "W" 1 83382
    "GHA" "N" 4 90938 "W" 1 83382
    "GHA" "N" 6 40832 "W" 3 20008
    "GHA" "N" 6 40633 "W" 3 20105
    "GHA" "N" 6 40688 "W" 3 19977
    "GHA" "N" 6 40688 "W" 3 19977
    "GHA" "N" 6 40677 "W" 3 20005
    "GHA" "N" 6 40870 "W" 3 20138
    "GHA" "N" 6 40968 "W" 3 20148
    "GHA" "N" 6 40688 "W" 3 19977
    "GHA" "N" 6 40677 "W" 3 20005
    "GHA" "N" 6 40677 "W" 3 20005
    "GHA" "N" 6 40832 "W" 3 20008
    "GHA" "N" 6 40805 "W" 3 19988
    "GHA" "N" 6 68313 "W" 3 16570
    "GHA" "N" 6 67935 "W" 3 19605
    "GHA" "N" 6 69527 "W" 3 17302
    "GHA" "N" 6 69022 "W" 3 15700
    "GHA" "N" 6 68670 "W" 3 20480
    "GHA" "N" 6 26383 "W" 2 86882
    "GHA" "N" 6 26397 "W" 2 86885
    "GHA" "N" 6 32110 "W" 2 84437
    "GHA" "N" 6 26395 "W" 2 86890
    "GHA" "N" 6 27545 "W" 2 85465
    "GHA" "N" 6 30815 "W" 2 84898
    "GHA" "N" 6 30787 "W" 2 83692
    "GHA" "N" 6 28085 "W" 2 85920
    "GHA" "N" 6 26720 "W" 2 85912
    "GHA" "N" 6 26397 "W" 2 86885
    "GHA" "N" 6 26067 "W" 2 87473
    "GHA" "N" 6  3122 "W" 2 95500
    "GHA" "N" 6  2138 "W" 2 95082
    "GHA" "N" 6  3122 "W" 2 95500
    "GHA" "N" 6  1722 "W" 2 93738
    "GHA" "N" 6   952 "W" 2 95677
    "GHA" "N" 6  3122 "W" 2 95500
    "GHA" "N" 6  1722 "W" 2 93738
    "GHA" "N" 6  3122 "W" 2 95500
    "GHA" "N" 6  3093 "W" 2 95500
    "GHA" "N" 6  3037 "W" 2 95367
    "GHA" "N" 5 42167 "W" 2 69112
    "GHA" "N" 6 12005 "W" 3  3398
    "GHA" "N" 6 12020 "W" 3  3407
    "GHA" "N" 6 12005 "W" 3  3398
    "GHA" "N" 6 11990 "W" 3  3465
    "GHA" "N" 6 12005 "W" 3  3390
    "GHA" "N" 6 12013 "W" 3  3448
    "GHA" "N" 6 12005 "W" 3  3398
    "GHA" "N" 6 12000 "W" 3  3363
    "GHA" "N" 6 12003 "W" 3  3412
    "GHA" "N" 6 12005 "W" 3  3398
    "GHA" "N" 6 12052 "W" 3  3408
    "GHA" "N" 6 12120 "W" 3  3430
    "GHA" "N" 6 12005 "W" 3  3398
    "GHA" "N" 5 42196 "W" 2 69079
    "GHA" "N" 5 42196 "W" 2 69079
    "GHA" "N" 5 42196 "W" 2 69079
    "GHA" "N" 5 42150 "W" 2 69010
    "GHA" "N" 5 42162 "W" 2 69073
    "GHA" "N" 5 42143 "W" 2 69000
    "GHA" "N" 5 42120 "W" 2 69092
    "GHA" "N" 5 42120 "W" 2 69092
    "GHA" "N" 5 42120 "W" 2 69092
    "GHA" "N" 5 42196 "W" 2 69079
    "GHA" "N" 5 42150 "W" 2 69010
    "GHA" "N" 5 42150 "W" 2 69010
    "GHA" "N" 5  9067 "W" 2 63385
    "GHA" "N" 5  9123 "W" 2 63348
    "GHA" "N" 5  9152 "W" 2 63408
    "GHA" "N" 5  9223 "W" 2 63370
    "GHA" "N" 5  9218 "W" 2 63400
    "GHA" "N" 5  9247 "W" 2 63395
    "GHA" "N" 5  9253 "W" 2 63477
    "GHA" "N" 5  9195 "W" 2 63415
    end

    I want to have the Latitude and Longitude variables like this, please

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str41 country double(latitude longitude) float head
    "GHA"  5.4212 -2.69092 1
    "GHA" 5.42162 -2.69073 1
    "GHA" 5.42196 -2.69079 1
    "GHA" 5.42167 -2.69112 1
    "GHA" 5.42196 -2.69079 1
    "GHA" 5.42196 -2.69079 1
    "GHA"  5.4215  -2.6901 1
    "GHA" 5.42196 -2.69079 0
    "GHA"  5.4215  -2.6901 1
    "GHA"  5.4215  -2.6901 1
    "GHA" 5.42143    -2.69 1
    "GHA" 5.36698     -2.2 1
    "GHA" 5.27927 -2.37433 0
    "GHA" 5.36698     -2.2 1
    "GHA"  5.2791 -2.37483 1
    "GHA" 5.27885 -2.37397 1
    "GHA" 5.27885 -2.37397 1
    "GHA" 5.44042 -2.14168 1
    "GHA" 5.27885 -2.37397 1
    "GHA" 5.27885 -2.37397 1
    "GHA" 5.27885 -2.37397 1
    "GHA" 5.36642    -2.27 1
    "GHA" 5.43912 -2.14168 0
    "GHA" 5.43953  -2.1432 1
    "GHA" 5.36698     -2.2 1
    "GHA" 5.44073  -2.1422 1
    "GHA" 5.27885 -2.37397 1
    "GHA" 5.43932 -2.14253 1
    "GHA" 5.27885 -2.37397 0
    "GHA"  5.4408 -2.14378 1
    "GHA" 5.44163  -2.1421 1
    "GHA" 5.43902  -2.1418 1
    "GHA" 5.27885 -2.37397 1
    "GHA" 5.43905 -2.14105 0
    "GHA" 5.43838   -2.141 1
    "GHA" 5.43932 -2.14253 1
    "GHA"  5.2791 -2.37483 1
    "GHA"  5.2977  -1.9928 1
    "GHA" 5.46907 -1.91687 0
    "GHA" 5.48602 -1.53282 1
    "GHA" 5.36425 -1.99918 1
    "GHA" 5.48593 -1.53305 1
    "GHA" 5.47025  -1.9167 1
    "GHA" 5.46592  -1.9152 1
    "GHA" 5.36343 -1.99942 1
    "GHA" 5.12302 -1.61443 1
    "GHA" 5.48662 -1.53373 1
    "GHA" 5.29682 -1.99228 1
    "GHA" 5.12372 -1.61257 1
    "GHA"  5.1233 -1.61282 1
    "GHA"   5.367 -1.99993 0
    "GHA" 5.48612 -1.53388 1
    "GHA" 5.47028 -1.91537 1
    "GHA" 5.29697 -1.99207 1
    "GHA" 5.48722 -1.53265 1
    "GHA" 5.29758 -1.99235 1
    "GHA" 5.29758 -1.99235 1
    "GHA" 5.48718 -1.53438 1
    "GHA" 5.46937 -1.91808 0
    "GHA" 5.12242 -1.61272 0
    "GHA" 5.12095 -1.61293 0
    "GHA" 5.29793 -1.99317 1
    "GHA" 5.48733 -1.53438 1
    "GHA" 5.47082 -1.91432 1
    "GHA" 5.29838 -1.99237 0
    "GHA"  5.1234  -1.6134 1
    "GHA" 5.48618 -1.53265 0
    "GHA" 5.46598  -1.9152 1
    "GHA" 5.47073 -1.91628 1
    "GHA" 5.29838 -1.99237 1
    "GHA" 5.48718  -1.5327 0
    "GHA" 5.46973 -1.91313 1
    "GHA"   5.367  -1.9992 1
    "GHA" 5.48383 -1.53358 1
    "GHA" 5.36425 -1.99918 1
    "GHA"  5.4851 -1.53273 1
    "GHA" 5.12305 -1.61642 1
    "GHA" 5.12133 -1.61338 1
    "GHA" 5.46698 -1.91635 1
    "GHA" 5.12337 -1.61338 1
    "GHA"  5.3669 -1.99942 1
    "GHA"  5.4856 -1.53257 1
    "GHA" 5.12272 -1.61427 0
    "GHA" 5.46987 -1.91677 1
    "GHA" 5.12337 -1.61338 1
    "GHA"  5.1258 -1.61103 1
    "GHA" 5.47067 -1.91613 1
    "GHA" 5.29837 -1.99237 0
    "GHA" 5.12045 -1.61412 0
    "GHA" 5.12223 -1.61425 0
    "GHA" 5.48775  -1.5332 0
    "GHA" 5.48737 -1.53347 1
    "GHA" 5.48582 -1.53387 1
    "GHA" 5.29682 -1.99228 0
    "GHA" 5.48685 -1.53292 1
    "GHA"  5.2975 -1.99247 0
    "GHA" 5.36698 -1.99968 1
    "GHA" 5.16197 -1.42832 1
    "GHA"  5.1491 -1.28813 1
    "GHA" 5.13367 -1.27412 1
    end


  • #2
    Hi this is the formula everything is fine except the last line I have to change the longitude values to NEGATIVE if lon take value W


    cap gen lon1=.
    cap egen longitude2 = concat(lon_deg lon1 lon_decimal_degree)
    cap encode longitude2, generate(longitude)
    drop longitude2 lon1
    replace longitude = -1*longitude if lon=="W"

    Comment


    • #3
      @Hemanshu Kumar

      Hemanshu Kumar

      Comment


      • #4
        Sorry, I am not sure I understand. Are you trying to convert decimals to degrees/seconds etc format? Or are you just trying to get negative/positive numbers instead of S/N and similarly for E/W?

        Your second data extract does not seem to correspond to the first.

        Comment


        • #5
          I have three variables regarding longitude

          lon float(lon_deg lon_decimal_degree) from these three variables I have to convert a variable longitude = lon_deg.lon_decimal_degree and longitude will take value negitive if lon take value "W".

          Comment


          • #6
            Originally posted by Hemanshu Kumar View Post
            Sorry, I am not sure I understand. Are you trying to convert decimals to degrees/seconds etc format? Or are you just trying to get negative/positive numbers instead of S/N and similarly for E/W?

            Your second data extract does not seem to correspond to the first.
            Yes the second data extract is different I have only pasted to show what Type I want to get

            Comment


            • #7
              The key to your woes is the geopandas library in Python.

              Comment


              • #8
                Originally posted by Jared Greathouse View Post
                The key to your woes is the geopandas library in Python.
                Thanks but I am looking to do it Stata please

                Comment


                • #9
                  Code:
                  gen double latitude = real(strofreal(lat_deg) + "." + strofreal(lat_decimal_degree))*cond(lat == "S",-1,1)
                  gen double longitude = real(strofreal(lon_deg) + "." + strofreal(lon_decimal_degree))*cond(lon == "W",-1,1)
                  which produces, for example:
                  Code:
                  . list in 1/5, noobs ab(20) sep(0)
                  
                    +----------------------------------------------------------------------------------------------------------+
                    | country   lat   lat_deg   lat_decimal_degree   lon   lon_deg   lon_decimal_degree   latitude   longitude |
                    |----------------------------------------------------------------------------------------------------------|
                    |     GHA     N         4                91358     W         1                78570    4.91358     -1.7857 |
                    |     GHA     N         4                91917     W         1                72983    4.91917    -1.72983 |
                    |     GHA     N         4                91378     W         1                78345    4.91378    -1.78345 |
                    |     GHA     N         4                92293     W         1                73002    4.92293    -1.73002 |
                    |     GHA     N         4                91370     W         1                78452     4.9137    -1.78452 |
                    +----------------------------------------------------------------------------------------------------------+

                  Comment


                  • #10
                    What is the projection of the original coordinates?

                    Comment

                    Working...
                    X