2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
location.py: definition of CoreLocation class that is a member of the
|
|
|
|
Session object. Provides conversions between Cartesian and geographic coordinate
|
|
|
|
systems. Depends on utm contributed module, from
|
|
|
|
https://pypi.python.org/pypi/utm (version 0.3.0).
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-08-07 19:58:51 +01:00
|
|
|
from core import logger
|
2013-08-29 15:21:13 +01:00
|
|
|
from core.conf import ConfigurableManager
|
2017-04-25 16:45:34 +01:00
|
|
|
from core.enumerations import RegisterTlvs
|
2013-08-29 15:21:13 +01:00
|
|
|
from core.misc import utm
|
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
class CoreLocation(ConfigurableManager):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Member of session class for handling global location data. This keeps
|
|
|
|
track of a latitude/longitude/altitude reference point and scale in
|
|
|
|
order to convert between X,Y and geo coordinates.
|
|
|
|
|
|
|
|
TODO: this could be updated to use more generic
|
|
|
|
Configurable/ConfigurableManager code like other Session objects
|
|
|
|
"""
|
|
|
|
name = "location"
|
|
|
|
config_type = RegisterTlvs.UTILITY.value
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Creates a MobilityManager instance.
|
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
ConfigurableManager.__init__(self)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.reset()
|
|
|
|
self.zonemap = {}
|
2017-05-03 17:30:49 +01:00
|
|
|
self.refxyz = (0.0, 0.0, 0.0)
|
|
|
|
self.refscale = 1.0
|
|
|
|
self.zoneshifts = {}
|
|
|
|
self.refgeo = (0.0, 0.0, 0.0)
|
2013-08-29 15:21:13 +01:00
|
|
|
for n, l in utm.ZONE_LETTERS:
|
|
|
|
self.zonemap[l] = n
|
|
|
|
|
|
|
|
def reset(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Reset to initial state.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
# (x, y, z) coordinates of the point given by self.refgeo
|
|
|
|
self.refxyz = (0.0, 0.0, 0.0)
|
|
|
|
# decimal latitude, longitude, and altitude at the point (x, y, z)
|
|
|
|
self.setrefgeo(0.0, 0.0, 0.0)
|
|
|
|
# 100 pixels equals this many meters
|
|
|
|
self.refscale = 1.0
|
|
|
|
# cached distance to refpt in other zones
|
|
|
|
self.zoneshifts = {}
|
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
def configure_values(self, config_data):
|
|
|
|
"""
|
|
|
|
Receive configuration message for setting the reference point
|
|
|
|
and scale.
|
|
|
|
|
|
|
|
:param core.conf.ConfigData config_data: configuration data for carrying out a configuration
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
values = config_data.data_values
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if values is None:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.info("location data missing")
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
|
|
|
values = values.split('|')
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# Cartesian coordinate reference point
|
2017-04-25 16:45:34 +01:00
|
|
|
refx, refy = map(lambda x: float(x), values[0:2])
|
2013-08-29 15:21:13 +01:00
|
|
|
refz = 0.0
|
|
|
|
self.refxyz = (refx, refy, refz)
|
|
|
|
# Geographic reference point
|
2017-04-25 16:45:34 +01:00
|
|
|
lat, lon, alt = map(lambda x: float(x), values[2:5])
|
|
|
|
self.setrefgeo(lat, lon, alt)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.refscale = float(values[5])
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.info("location configured: (%.2f,%.2f,%.2f) = (%.5f,%.5f,%.5f) scale=%.2f" %
|
|
|
|
(self.refxyz[0], self.refxyz[1], self.refxyz[2], self.refgeo[0],
|
|
|
|
self.refgeo[1], self.refgeo[2], self.refscale))
|
|
|
|
logger.info("location configured: UTM(%.5f,%.5f,%.5f)" %
|
|
|
|
(self.refutm[1], self.refutm[2], self.refutm[3]))
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def px2m(self, val):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Convert the specified value in pixels to meters using the
|
|
|
|
configured scale. The scale is given as s, where
|
|
|
|
100 pixels = s meters.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param val: value to use in converting to meters
|
|
|
|
:return: value converted to meters
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
return (val / 100.0) * self.refscale
|
|
|
|
|
|
|
|
def m2px(self, val):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Convert the specified value in meters to pixels using the
|
|
|
|
configured scale. The scale is given as s, where
|
|
|
|
100 pixels = s meters.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param val: value to convert to pixels
|
|
|
|
:return: value converted to pixels
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if self.refscale == 0.0:
|
|
|
|
return 0.0
|
|
|
|
return 100.0 * (val / self.refscale)
|
|
|
|
|
|
|
|
def setrefgeo(self, lat, lon, alt):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Record the geographical reference point decimal (lat, lon, alt)
|
|
|
|
and convert and store its UTM equivalent for later use.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param lat: latitude
|
|
|
|
:param lon: longitude
|
|
|
|
:param alt: altitude
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.refgeo = (lat, lon, alt)
|
|
|
|
# easting, northing, zone
|
2017-05-03 17:30:49 +01:00
|
|
|
e, n, zonen, zonel = utm.from_latlon(lat, lon)
|
2017-04-25 16:45:34 +01:00
|
|
|
self.refutm = ((zonen, zonel), e, n, alt)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def getgeo(self, x, y, z):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Given (x, y, z) Cartesian coordinates, convert them to latitude,
|
|
|
|
longitude, and altitude based on the configured reference point
|
|
|
|
and scale.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param x: x value
|
|
|
|
:param y: y value
|
|
|
|
:param z: z value
|
|
|
|
:return: lat, lon, alt values for provided coordinates
|
|
|
|
:rtype: tuple
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
# shift (x,y,z) over to reference point (x,y,z)
|
2017-05-03 17:30:49 +01:00
|
|
|
x -= self.refxyz[0]
|
2013-08-29 15:21:13 +01:00
|
|
|
y = -(y - self.refxyz[1])
|
|
|
|
if z is None:
|
|
|
|
z = self.refxyz[2]
|
|
|
|
else:
|
2017-04-25 16:45:34 +01:00
|
|
|
z -= self.refxyz[2]
|
2013-08-29 15:21:13 +01:00
|
|
|
# use UTM coordinates since unit is meters
|
|
|
|
zone = self.refutm[0]
|
|
|
|
if zone == "":
|
2017-05-03 17:30:49 +01:00
|
|
|
raise ValueError("reference point not configured")
|
2013-08-29 15:21:13 +01:00
|
|
|
e = self.refutm[1] + self.px2m(x)
|
|
|
|
n = self.refutm[2] + self.px2m(y)
|
|
|
|
alt = self.refutm[3] + self.px2m(z)
|
|
|
|
(e, n, zone) = self.getutmzoneshift(e, n)
|
|
|
|
try:
|
|
|
|
lat, lon = utm.to_latlon(e, n, zone[0], zone[1])
|
|
|
|
except utm.OutOfRangeError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.exception("UTM out of range error for n=%s zone=%s xyz=(%s,%s,%s)", n, zone, x, y, z)
|
2017-05-03 17:30:49 +01:00
|
|
|
lat, lon = self.refgeo[:2]
|
2017-04-25 16:45:34 +01:00
|
|
|
# self.info("getgeo(%s,%s,%s) e=%s n=%s zone=%s lat,lon,alt=" \
|
2013-08-29 15:21:13 +01:00
|
|
|
# "%.3f,%.3f,%.3f" % (x, y, z, e, n, zone, lat, lon, alt))
|
2017-04-25 16:45:34 +01:00
|
|
|
return lat, lon, alt
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def getxyz(self, lat, lon, alt):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Given latitude, longitude, and altitude location data, convert them
|
|
|
|
to (x, y, z) Cartesian coordinates based on the configured
|
|
|
|
reference point and scale. Lat/lon is converted to UTM meter
|
|
|
|
coordinates, UTM zones are accounted for, and the scale turns
|
|
|
|
meters to pixels.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param lat: latitude
|
|
|
|
:param lon: longitude
|
|
|
|
:param alt: altitude
|
|
|
|
:return: converted x, y, z coordinates
|
|
|
|
:rtype: tuple
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
# convert lat/lon to UTM coordinates in meters
|
2017-04-25 16:45:34 +01:00
|
|
|
e, n, zonen, zonel = utm.from_latlon(lat, lon)
|
|
|
|
rlat, rlon, ralt = self.refgeo
|
2013-08-29 15:21:13 +01:00
|
|
|
xshift = self.geteastingshift(zonen, zonel)
|
|
|
|
if xshift is None:
|
|
|
|
xm = e - self.refutm[1]
|
|
|
|
else:
|
|
|
|
xm = e + xshift
|
|
|
|
yshift = self.getnorthingshift(zonen, zonel)
|
|
|
|
if yshift is None:
|
|
|
|
ym = n - self.refutm[2]
|
|
|
|
else:
|
|
|
|
ym = n + yshift
|
|
|
|
zm = alt - ralt
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# shift (x,y,z) over to reference point (x,y,z)
|
|
|
|
x = self.m2px(xm) + self.refxyz[0]
|
|
|
|
y = -(self.m2px(ym) + self.refxyz[1])
|
|
|
|
z = self.m2px(zm) + self.refxyz[2]
|
2017-04-25 16:45:34 +01:00
|
|
|
return x, y, z
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def geteastingshift(self, zonen, zonel):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
If the lat, lon coordinates being converted are located in a
|
2013-08-29 15:21:13 +01:00
|
|
|
different UTM zone than the canvas reference point, the UTM meters
|
|
|
|
may need to be shifted.
|
2017-04-25 16:45:34 +01:00
|
|
|
This picks a reference point in the same longitudinal band
|
2013-08-29 15:21:13 +01:00
|
|
|
(UTM zone number) as the provided zone, to calculate the shift in
|
|
|
|
meters for the x coordinate.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param zonen: zonen
|
|
|
|
:param zonel: zone1
|
|
|
|
:return: the x shift value
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
rzonen = int(self.refutm[0][0])
|
2017-05-03 17:30:49 +01:00
|
|
|
# same zone number, no x shift required
|
2013-08-29 15:21:13 +01:00
|
|
|
if zonen == rzonen:
|
2017-05-03 17:30:49 +01:00
|
|
|
return None
|
2013-08-29 15:21:13 +01:00
|
|
|
z = (zonen, zonel)
|
2017-05-03 17:30:49 +01:00
|
|
|
# x shift already calculated, cached
|
2013-08-29 15:21:13 +01:00
|
|
|
if z in self.zoneshifts and self.zoneshifts[z][0] is not None:
|
2017-05-03 17:30:49 +01:00
|
|
|
return self.zoneshifts[z][0]
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
rlat, rlon, ralt = self.refgeo
|
2017-05-03 17:30:49 +01:00
|
|
|
# ea. zone is 6deg band
|
|
|
|
lon2 = rlon + 6 * (zonen - rzonen)
|
|
|
|
# ignore northing
|
|
|
|
e2, n2, zonen2, zonel2 = utm.from_latlon(rlat, lon2)
|
2013-08-29 15:21:13 +01:00
|
|
|
# NOTE: great circle distance used here, not reference ellipsoid!
|
|
|
|
xshift = utm.haversine(rlon, rlat, lon2, rlat) - e2
|
|
|
|
# cache the return value
|
|
|
|
yshift = None
|
|
|
|
if z in self.zoneshifts:
|
|
|
|
yshift = self.zoneshifts[z][1]
|
|
|
|
self.zoneshifts[z] = (xshift, yshift)
|
|
|
|
return xshift
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def getnorthingshift(self, zonen, zonel):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
If the lat, lon coordinates being converted are located in a
|
2013-08-29 15:21:13 +01:00
|
|
|
different UTM zone than the canvas reference point, the UTM meters
|
|
|
|
may need to be shifted.
|
|
|
|
This picks a reference point in the same latitude band (UTM zone letter)
|
|
|
|
as the provided zone, to calculate the shift in meters for the
|
|
|
|
y coordinate.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param zonen: zonen
|
|
|
|
:param zonel: zone1
|
|
|
|
:return: calculated y shift
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
rzonel = self.refutm[0][1]
|
2017-05-03 17:30:49 +01:00
|
|
|
# same zone letter, no y shift required
|
2013-08-29 15:21:13 +01:00
|
|
|
if zonel == rzonel:
|
2017-05-03 17:30:49 +01:00
|
|
|
return None
|
2013-08-29 15:21:13 +01:00
|
|
|
z = (zonen, zonel)
|
2017-05-03 17:30:49 +01:00
|
|
|
# y shift already calculated, cached
|
2013-08-29 15:21:13 +01:00
|
|
|
if z in self.zoneshifts and self.zoneshifts[z][1] is not None:
|
2017-05-03 17:30:49 +01:00
|
|
|
return self.zoneshifts[z][1]
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
rlat, rlon, ralt = self.refgeo
|
2013-08-29 15:21:13 +01:00
|
|
|
# zonemap is used to calculate degrees difference between zone letters
|
|
|
|
latshift = self.zonemap[zonel] - self.zonemap[rzonel]
|
2017-05-03 17:30:49 +01:00
|
|
|
# ea. latitude band is 8deg high
|
|
|
|
lat2 = rlat + latshift
|
2017-04-25 16:45:34 +01:00
|
|
|
e2, n2, zonen2, zonel2 = utm.from_latlon(lat2, rlon)
|
2013-08-29 15:21:13 +01:00
|
|
|
# NOTE: great circle distance used here, not reference ellipsoid
|
|
|
|
yshift = -(utm.haversine(rlon, rlat, rlon, lat2) + n2)
|
|
|
|
# cache the return value
|
|
|
|
xshift = None
|
|
|
|
if z in self.zoneshifts:
|
|
|
|
xshift = self.zoneshifts[z][0]
|
|
|
|
self.zoneshifts[z] = (xshift, yshift)
|
|
|
|
return yshift
|
|
|
|
|
|
|
|
def getutmzoneshift(self, e, n):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Given UTM easting and northing values, check if they fall outside
|
2013-08-29 15:21:13 +01:00
|
|
|
the reference point's zone boundary. Return the UTM coordinates in a
|
|
|
|
different zone and the new zone if they do. Zone lettering is only
|
|
|
|
changed when the reference point is in the opposite hemisphere.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param e: easting value
|
|
|
|
:param n: northing value
|
|
|
|
:return: modified easting, northing, and zone values
|
|
|
|
:rtype: tuple
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
zone = self.refutm[0]
|
2017-04-25 16:45:34 +01:00
|
|
|
rlat, rlon, ralt = self.refgeo
|
2013-08-29 15:21:13 +01:00
|
|
|
if e > 834000 or e < 166000:
|
2017-04-25 16:45:34 +01:00
|
|
|
num_zones = (int(e) - 166000) / (utm.R / 10)
|
2013-08-29 15:21:13 +01:00
|
|
|
# estimate number of zones to shift, E (positive) or W (negative)
|
|
|
|
rlon2 = self.refgeo[1] + (num_zones * 6)
|
2017-04-25 16:45:34 +01:00
|
|
|
e2, n2, zonen2, zonel2 = utm.from_latlon(rlat, rlon2)
|
2013-08-29 15:21:13 +01:00
|
|
|
xshift = utm.haversine(rlon, rlat, rlon2, rlat)
|
|
|
|
# after >3 zones away from refpt, the above estimate won't work
|
|
|
|
# (the above estimate could be improved)
|
|
|
|
if not 100000 <= (e - xshift) < 1000000:
|
|
|
|
# move one more zone away
|
2017-04-25 16:45:34 +01:00
|
|
|
num_zones = (abs(num_zones) + 1) * (abs(num_zones) / num_zones)
|
2013-08-29 15:21:13 +01:00
|
|
|
rlon2 = self.refgeo[1] + (num_zones * 6)
|
2017-04-25 16:45:34 +01:00
|
|
|
e2, n2, zonen2, zonel2 = utm.from_latlon(rlat, rlon2)
|
2013-08-29 15:21:13 +01:00
|
|
|
xshift = utm.haversine(rlon, rlat, rlon2, rlat)
|
|
|
|
e = e - xshift
|
|
|
|
zone = (zonen2, zonel2)
|
|
|
|
if n < 0:
|
|
|
|
# refpt in northern hemisphere and we crossed south of equator
|
|
|
|
n += 10000000
|
|
|
|
zone = (zone[0], 'M')
|
|
|
|
elif n > 10000000:
|
|
|
|
# refpt in southern hemisphere and we crossed north of equator
|
|
|
|
n -= 10000000
|
|
|
|
zone = (zone[0], 'N')
|
2017-04-25 16:45:34 +01:00
|
|
|
return e, n, zone
|