Skip to content

Commit

Permalink
DFReader: add a get_latlon method to DFMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Jul 6, 2024
1 parent d496685 commit bd6fa57
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions DFReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ def set_mult_ids(self, mult_ids, mult_lookup):
if mult_ids[i] in mult_lookup:
unitmult = mult_lookup[mult_ids[i]]
# Combine the multipler and unit to derive the real unit
if unitmult in MULT_TO_PREFIX:
self.units[i] = MULT_TO_PREFIX[unitmult]+self.units[i]
else:
self.units[i] = "%.4g %s" % (unitmult, self.units[i])
self.msg_mults[i] = unitmult
# if unitmult in MULT_TO_PREFIX:
# self.units[i] = MULT_TO_PREFIX[unitmult]+self.units[i]
# else:
# self.units[i] = "%.4g %s" % (unitmult, self.units[i])

def get_unit(self, col):
'''Return the unit for the specified field'''
Expand Down Expand Up @@ -255,7 +256,7 @@ def __getattr__(self, field):
if self.fmt.msg_mults[i] > 0.0 and self.fmt.msg_mults[i] < 1.0:
divisor = 1/self.fmt.msg_mults[i]
v /= divisor
else:
elif self.fmt.msg_mults[i] > 1:
v *= self.fmt.msg_mults[i]
return v

Expand Down Expand Up @@ -293,6 +294,46 @@ def __str__(self):
ret = ret[:-2]
return ret + '}'

def get_multiplied_field_value(self, field):
v = getattr(self, field)
if getattr(self, "_apply_multiplier", False) is True:
return v
# not applied already...
i = self.fmt.colhash[field]
mult = self.fmt.msg_mults[i]
if not mult:
return v
# For reasons relating to floating point accuracy, you get a more
# accurate result by dividing by 1e2 or 1e7 than multiplying by
# 1e-2 or 1e-7
if mult > 0.0 and mult < 1.0:
divisor = 1/mult
return v / divisor

return v * mult

def get_latitude(self):
for i in 'Lat', 'lat':
try:
return self.get_multiplied_field_value(i)
except AttributeError:
continue
raise AttributeError("No latitude found")

def get_longitude(self):
for i in 'Lon', 'Lng', 'lon', 'lng':
try:
return self.get_multiplied_field_value(i)
except AttributeError:
continue
raise AttributeError("No longitude found")

def get_latlon(self):
'''return a mavutil.location object for the first location present in
the object'''
latlon = (self.get_latitude(), self.get_longitude())
return latlon

def dump_verbose_bitmask(self, f, c, val, field_metadata):
try:
try:
Expand Down

0 comments on commit bd6fa57

Please sign in to comment.