Announcement

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

  • Convert xy coordinate in meters to degrees in Stata

    Dear Statalisters,

    I am trying to convert coordinates in meters to xy latitude and longitude in Stata. I use Robert Picard's [geoinpoly] package to generate the xy coordinates so I can merge them with the shapefile.

    The coordinates look like this:
    _ID _X _Y
    1 722289.86 1194827.3
    2 724874.6 1194249.9
    3 713779.37 1193967.1
    4 719852.11 1195935.7

    When I run the command:

    geoinpoly _Y _X using "ttcoord.dta"


    I get the error code 198:
    latitude _Y must be between -90 and 90
    r(198);

    Even if I try using the option noprojection, I still get the same error.

    Any advice would be greatly appreciated.
    Thank you
    Erik

  • #2
    I do not understand. The geoinpoly command (from SSC) is used to match points to shapefile polygons. It does not "convert coordinates in meters to xy latitude and longitude". The geo2xy command (also from SSC) can convert geographic coordinates (latitude/longitude) to (x,y) coordinates using one of the supported projections. geo2xy does not however provide the inverse ability (convert (x,y) to lat/lon).

    Also, you will not get the error you report if you use the noprojection option with geoinpoly.

    Code:
    . * see geo2xy help file on how to get the ancillary datasets
    . * convert lat/lon to (x,y)
    . use "geo2xy_world_coor.dta", clear
    
    . sum
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
             _ID |     11,414    80.46119      56.205          1        177
              _X |     11,127    10.51658    80.65838       -180        180
              _Y |     11,127    15.13631    41.56425        -90   83.64513
    
    . geo2xy _Y _X, replace projection(mercator)
    
    . sum
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
             _ID |     11,414    80.46119      56.205          1        177
              _X |     11,127     1170700     8978850  -2.00e+07   2.00e+07
              _Y |     10,766     2438650     5821411  -2.08e+07   1.84e+07
    
    . save "world_mercator.dta", replace
    file world_mercator.dta saved
    
    . 
    . clear
    
    . input _ID _X _Y
    
               _ID         _X         _Y
      1. 1 722289.86 1194827.3
      2. 2 724874.6 1194249.9
      3. 3 713779.37 1193967.1
      4. 4 719852.11 1195935.7
      5. end
    
    . rename _ID id
    
    . geoinpoly _Y _X using "world_mercator.dta", noproj
    
    . list
    
         +-------------------------------+
         | id         _X        _Y   _ID |
         |-------------------------------|
      1. |  1   722289.9   1194827   116 |
      2. |  2   724874.6   1194250   116 |
      3. |  3   713779.4   1193967   116 |
      4. |  4   719852.1   1195936   116 |
         +-------------------------------+
    
    .
    Note that while the example above executes without error, the results are not valid until you can confirm that the point (x,y) coordinates come from exactly the same projection as the one for the shapefile.

    Comment


    • #3
      Dear Robert,
      Apologies and thanks for the clarification. I now understand my issue, I should have tried the geo2xy command. However, if geo2xy does not convert xy coordinates to lat/long, I am back to square one. I wonder if there exists a package in Stata that does that: convert xy coordinates to lat/long?
      Thanks very much.

      Comment


      • #4
        Not that I know of unfortunately.

        Comment


        • #5
          Hi Erik.

          Did you find how to convert coordinates in meters to X Y latitude and longitude in Stata? I'm facing the same problem and would like to find a solution.

          Comment

          Working...
          X