Turbo87 / utm

Bidirectional UTM-WGS84 converter for python
http://pypi.python.org/pypi/utm
MIT License
486 stars 101 forks source link

Counterintuitive northing near equator with force_zone_number, force_zone_letter #94

Open m-sasha opened 1 year ago

m-sasha commented 1 year ago

I have some geographic shapes that I'm trying to do geometry on (find distances, intersections etc). To do that I'm converting all the lat-lon coordinates to UTM, then using the python Shapely library to do the geometry, and then converting the results back to lat-lon. My shapes are small, but they nevertheless sometimes cross UTM zone boundaries. I can live with the inaccuracy induced by converting coordinates in a nearby zone, but utm seems to intentionally wrap negative latitudes, breaking my conversion completely. A short segment from (lat=0.01, lon=0) to (lat=-0.01, lon=0) becomes a huge segment in UTM from northing 1000 to northing 10_000_000:

>>> import utm
>>> _, _, number, letter = utm.from_latlon(0.01, 0)
>>> number, letter
(31, 'N')
>>> utm.from_latlon(0.01, 0, number, letter)
(166021.4482414365, 1106.8275599412982, 31, 'N')
>>> utm.from_latlon(-0.01, 0, number, letter)
(166021.4482414365, 9998893.17244006, 31, 'N')

I would expect that specifying force_zone_number and force_zone_letter would disable this behaviour. The relevant code appears to be in from_lat_lon:

if mixed_signs(latitude):
    raise ValueError("latitudes must all have the same sign")
elif negative(latitude):
    northing += 10000000
m-sasha commented 1 year ago

The behavior seems inconsistent w.r.t. easting and northing. With easting it does the "right" thing by returning continuous values across UTM zone boundaries:

>>> utm.from_latlon(0.01, 0.01, 31, 'N')
(167135.73011805036, 1106.8173845648219, 31, 'N')
>>> utm.from_latlon(0.01, -0.01, 31, 'N')
(164907.15610373177, 1106.8377694498813, 31, 'N')
>>> utm.from_latlon(0.01, -0.01)
(832864.2698819496, 1106.8173845648219, 30, 'N')
sdhiscocks commented 1 year ago

Related PR #46