Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix geopoint conversion from utm for southern hemisphere #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions geodesy/src/geodesy/utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@ def toMsg(self):
if not self.valid():
raise ValueError('invalid UTM point: ' + str(self))
utm_proj = pyproj.Proj(proj='utm', zone=self.zone, datum='WGS84')

#hemishphere adjustment as per cpp code. May also do this by properly
#specifying pyproj projection for southern hemisphere.
southern_hemisphere_adjustment=0.0
if ord(self.band) < ord('N'):
southern_hemisphere_adjustment=-10000000.0

msg = GeoPoint(altitude=self.altitude)
msg.longitude, msg.latitude = utm_proj(self.easting, self.northing,
inverse=True)
msg.longitude, msg.latitude = utm_proj(self.easting,
self.northing+southern_hemisphere_adjustment,
inverse=True)
return msg

def valid(self):
Expand Down
31 changes: 30 additions & 1 deletion geodesy/tests/test_utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,36 @@ def test_wgs84_utm_conversion_2d(self):
self.assertAlmostEqual(point_xy.x, 622159.338, places = 3)
self.assertAlmostEqual(point_xy.y, 3362168.303, places = 3)
self.assertAlmostEqual(point_xy.z, 0.0, places = 3)


def test_geopoint_from_utm(self):
lat = 30.385315
lon = -97.728524
alt = float('nan')
pt = fromLatLong(lat, lon)
self.assertEqual(str(pt), 'UTM: [622159.338, 3362168.303, nan, 14R]',
msg='conversion failed: ' + str(pt))
self.assertTrue(pt.valid(), msg='invalid UTMPoint: ' + str(pt))
self.assertTrue(pt.is2D(), msg='this UTMPoint should be 2D: ' + str(pt))
self.assertEqual(pt.gridZone(), (14, 'R'))
self.assertEqual(str(pt.toMsg()), str(GeoPoint(lat, lon, alt)),
msg='GeoPoint conversion failed for: ' + str(pt))
point_xy = pt.toPoint()
self.assertAlmostEqual(point_xy.x, 622159.338, places = 3)
self.assertAlmostEqual(point_xy.y, 3362168.303, places = 3)
self.assertAlmostEqual(point_xy.z, 0.0, places = 3)


# Now for southern hemisphere
latS = -27.52279105401131
lonS = 152.8222634237585

ptS = fromLatLong(latS, lonS)
self.assertEqual(ptS.gridZone(), (56, 'J'))
self.assertEqual(str(ptS.toMsg()), str(GeoPoint(latS, lonS)),
msg='GeoPoint conversion failed for: ' + str(pt))



if __name__ == '__main__':
import rosunit
PKG='geodesy'
Expand Down