python – lat/lon to utm to lat/lon is extremely flawed, how come?

python – lat/lon to utm to lat/lon is extremely flawed, how come?

Ive created a small UTM conversion library for Python last week and uploaded it to the Python Package Index:

I have compared it to using pyproj and it is faster and more accurate. Given your sample data, this is the result:

>>> import utm

>>> u = utm.from_latlon(47.9941214, 7.8509671)
>>> print u
(414278, 5316285, 32, T)

>>> print utm.to_latlon(*u)
(47.994157948891505, 7.850963967574302)

UPDATE: Richards answer below describes the real solution for this issue.

The error is in your code.

First off, the PyProj issue listed in one of the other answers is real. You should check your epsg file and make sure it includes the line

<2392> +proj=tmerc +lat_0=0 +lon_0=24 +k=1.000000 +x_0=2500000 +y_0=0 +ellps=intl +towgs84=-90.7,-106.1,-119.2,4.09,0.218,-1.05,1.37 +units=m +no_defs no_defs <>

Note the towgs84 parameter.

Your problem with PyProj stems from mis-using the projection command.

If we take 47.9941214N, 7.8509671E and convert to UTM we get Zone 32, 414278 Easting, 5316286 Northing.

You perform the following PyProj operations:

p = pyproj.Proj(proj=utm, zone=32, ellps=WGS84)
>>> x,y = p(47.9941214, 7.8509671)
>>> print x,y
5159550.36822 1114087.43925
>>> print p(x,y,inverse=True)
(47.971558538495991, 7.8546573140162605)

But, if we consult the PyProj documentation, we see the following:

Calling a Proj class instance with the arguments lon, lat will convert
lon/lat (in degrees) to x/y native map projection coordinates (in

Lets try running the OPs PyProj operations again, but switch the order of the lon/lat arguments:

p = pyproj.Proj(proj=utm, zone=32, ellps=WGS84)
>>> x,y = p(7.8509671, 47.9941214)
>>> print x,y
414278.16731 5316285.59492
>>> print p(x,y,inverse=True)
(7.850967099999812, 47.994121399999784)

The operation inverts itself (pretty much) perfectly!

To answer the first part of your question, if you look in at the definition of UTMtoLL, you find the following:

UTMtoLL(ReferenceEllipsoid, northing, easting, zone)

Yet you use UTMtoLL(23, leftUTM, topUTM, zone) where leftUTM is an Easting and topUTM is a Northing.

Therefore, in the case of both your first script and PyProj, youve used the wrong order of arguments.

Its a good reminder to always double- (or triple-) check your work before suggesting that someone elses is wrong. That said, Pythons documentation is not the greatest and PyProjs documentation in this instance is cryptic at best. A nice web-based explanation of this command and accompanied by examples of its usage would have probably prevented angst on your part.

python – lat/lon to utm to lat/lon is extremely flawed, how come?

I have no problem with pyproj, try the following code

from pyproj import Proj

Lat = 52.063098675
Lon = -114.132980348 #Calgary

ZoneNo = 11 #Manually input, or calcuated from Lat Lon
myProj = Proj(+proj=utm +zone=+ZoneNo+,
+north +ellps=WGS84 +datum=WGS84 +units=m +no_defs) #north for north hemisphere
UTMx, UTMy = myProj(Lon, Lat)


#UTM ==> Lat Lon:
ZoneNo = 11 #Manually input or from other sources
myProj = Proj(+proj=utm +zone=+
ZoneNo+, +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs)
Lon2, Lat2 = myProj(UTMx, UTMy,inverse=True)

print Lat2
print Lon2

Leave a Reply

Your email address will not be published.