2

I have tried many ways to convert UTM to lon/lat, and getting always the same weird result.

Getting UTM and lon/lat for a point using GoogleEarth:

from pyproj import Proj

lon = -40.729124
lat = -20.967705

z = 24
l = 'k'
posx = 320235.13
posy = 7680455.66

myProj = Proj("+proj=utm +zone=24K, +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

clon, clat = myProj(posx, posy, inverse=True)

print(clon, clat)

I get...

-43.534213641680765 69.17434868536577

What do I missing?

3
  • Maybe you should try "24" instead of "24k", or you may add the -I (ie. upper i) flag instead.
    – Gery
    Commented May 12, 2022 at 13:01
  • By running your code line-by-line in Python window I can see pyproj.exceptions.CRSError: Invalid projection: +proj=utm +zone=24K, +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs +type=crs: (Internal Proj Error: proj_create: Error 1027 (Invalid value for an argument): utm: Invalid value for zone). Fix the syntax as advised in the answer by @sutan.
    – user30184
    Commented May 12, 2022 at 13:39
  • I copied the code from a working notebook! I removed the 'k' and got the same result. By the way, I installed the 'UTM' package and got the right result. But I wonder what could be the issue here using Proj! Commented May 12, 2022 at 16:14

2 Answers 2

4

try "+proj=utm +zone=24 +south +datum=WGS84 +units=m +no_defs ". use epsg.io to identify the appropriate proj4 input, look at the bottom of the page.

another alternative is just use epsg code which is easier like this,

Proj('EPSG:32724')


alternatively using geopandas

from geopandas import GeoSeries
from shapely.geometry import Point

posx = 320235.13
posy = 7680455.66

# utm 24s is equivalent to epsg:32724
lon, lat = GeoSeries([Point(posx, posy)], crs='EPSG:32724').to_crs('epsg:4326')[0].coords[0] 

print(lon, lat)

# this prints -20.967705012999282 -40.729123995168585  
3
  • Thanks for the alternative. Works nicely too! Commented May 21, 2022 at 13:33
  • good to know it works Carlos. if I may ask, please click the "check" on the answer to confirm that the answers work (: @CarlosAugustoFranaSchettini It'd be really helpful. thanks!
    – sutan
    Commented May 22, 2022 at 21:50
  • 1
    Checked. It works when I removed '+ellps=WGS84' from the 'Proj'. I copy the code from ocefpaf.github.io/python4oceanographers/blog/2013/12/16/utm, and there it seems it works well! Commented May 25, 2022 at 11:21
4

After trying your code I could get an error:

pyproj.exceptions.CRSError: Invalid projection: +proj=utm +zone=24K, +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs +type=crs: (Internal Proj Error: proj_create: Error 1027 (Invalid value for an argument): utm: Invalid value for zone)

Which tells explicitly : "Invalid value for zone".

However, as soon as you erase K, it can be validated.

Hereafter, it is suggested to work with the Transformer class, that takes into account datum shifts.

from pyproj import CRS
from pyproj import Transformer

posx, posy = 320235.13, 7680455.66

from_crs = CRS.from_proj4("+proj=utm +zone=24 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
to_crs = CRS.from_epsg(4326)

proj = Transformer.from_crs(from_crs, to_crs, always_xy=True)
coordinates = proj.transform(posx, posy)

print(coordinates) #(-40.729123995168585, -20.967705012999282)
2
  • 1
    This is a useful reference to add to your answer: pyproj4.github.io/pyproj/stable/…
    – snowman2
    Commented May 13, 2022 at 2:13
  • Thank you. It is curious that I don't get any error message... The code runs and gives me the wrong result, with our without 'k'. Your suggested code worked perfectly! Commented May 21, 2022 at 13:28

Your Answer

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

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