0

I am trying to convert a set of local co ordinates into lat/long and vice versa and I have the code for it taken from this site itself , the issue is When I do these conversions they are not resulting the same.

converting local co ordinates to lat long:

import pyproj
import utm
x = -20509.2462745155
y = -125891.862952247
p = pyproj.Proj("+proj=tmerc +lat_0=34.863361375 +lon_0=138.273186441 +k_0=1.000008632 +a=6378137.0 +b=6356752.314200002 +x_0=-20737.317 +y_0=-126072.022 +units=m +no_defs")
lon, lat = p(x, y, inverse=True)
print(lat,lon)  = 34.864985295652794 138.27568068953587

I also tried the inverse of the above code and it is not working as well.

lat =34.864985295652794
lon= 138.27568068953587
p = pyproj.Proj("+proj=tmerc +lat_0=34.863361375 +lon_0=138.273186441 +k_0=1.000008632 +a=6378137.0 +b=6356752.314200002 +x_0=-20737.317 +y_0=-126072.022 +units=m +no_defs")
nx,ny=p(lat,lon,inverse=True)
print(nx,ny) = 138.50356927081015 36.00068760121446

converting lat long into local coordinates using UTM package.

x, y, zone, ut = utm.from_latlon(34.864985295652794, 138.27568068953587)
print(x,y) = 250960.9300530484 3861456.658975485

as you can see the second piece of code is not exactly returning the X and Y values from the first code snippet.

I am not sure why this is happening , also I can notice that the first couple of values match. If this is not the proper way to perform this operation then , I would like to know how to approach this.

6
  • But you are using 2 different projections, what happens if you use p instead of utm
    – Ian Turton
    Jul 20, 2020 at 17:21
  • @IanTurton the How do I do that? I couldnt find a way in the UTM package for me to input the proj string
    – Anarach
    Jul 20, 2020 at 17:28
  • How about x, y = p(lat, lon) ?
    – Ian Turton
    Jul 20, 2020 at 18:42
  • @IanTurton Doesnt work Ian , I have edited the question.
    – Anarach
    Jul 21, 2020 at 4:53
  • but you don't want the inverse the 2nd time so set inverse=False
    – Ian Turton
    Jul 21, 2020 at 8:06

1 Answer 1

3

You are mixing and matching two distinct projections which why they don't match. You need to do something like this:

>>> import pyproj
>>> x = -20509.2462745155
>>> y = -125891.862952247
>>> lon, lat = p(x, y, inverse=True)
>>> print(lat,lon)
(34.864985295652794, 138.27568068953587)
>>> nx, ny = p(lon, lat, inverse=False)
>>> print(nx, ny)
(-20509.246274514833, -125891.8629522473)
>>> print (nx-x, ny-y)
(6.657501216977835e-10, -3.055902197957039e-10)
>>>

Which seems close enough to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.