| 1 |
import os |
| 2 |
import logging |
| 3 |
from gi.repository import Gtk |
| 4 |
from lxml import etree |
| 5 |
from pytrainer.lib.srtmlayer import SrtmLayer |
| 6 |
|
| 7 |
class fixelevation: |
| 8 |
_data = None |
| 9 |
_srtm = SrtmLayer() |
| 10 |
|
| 11 |
def __init__(self, parent = None, pytrainer_main = None, conf_dir = None, options = None): |
| 12 |
self.parent = parent |
| 13 |
self.pytrainer_main = pytrainer_main |
| 14 |
self.options = options |
| 15 |
self.conf_dir = conf_dir |
| 16 |
|
| 17 |
def run(self, aid, activity=None): #TODO Convert to use activity... |
| 18 |
#print activity |
| 19 |
logging.debug(">>") |
| 20 |
gpx_file = "%s/gpx/%s.gpx" % (self.conf_dir, aid) |
| 21 |
ele_fixed = True |
| 22 |
|
| 23 |
if os.path.isfile(gpx_file): |
| 24 |
# Backup original raw data as *.orig.gpx |
| 25 |
#orig_file = open(gpx_file, 'r') |
| 26 |
#orig_data = orig_file.read() |
| 27 |
#orig_file.close() |
| 28 |
#backup_file = open("%s/gpx/%s.orig.gpx" % (self.conf_dir, id), 'w') |
| 29 |
#backup_file.write(orig_data) |
| 30 |
#backup_file.close() |
| 31 |
#GPX file is ok and found, so open it |
| 32 |
logging.debug("ELE GPX file: %s found, size: %d" % (gpx_file, os.path.getsize(gpx_file))) |
| 33 |
|
| 34 |
""" |
| 35 |
Parse GPX file to ElementTree instance. |
| 36 |
""" |
| 37 |
self._data = etree.parse(gpx_file) |
| 38 |
self._xmlns = self._data.getroot().nsmap[None] |
| 39 |
nsmap = self._data.getroot().nsmap |
| 40 |
pyt_ns = "http://sourceforge.net.project/pytrainer/GPX/0/1" |
| 41 |
PYTRAINER = "{%s}" % pyt_ns |
| 42 |
self._trkpt_path = '{%s}trk/{%s}trkseg/{%s}trkpt' % (self._xmlns, self._xmlns, self._xmlns) |
| 43 |
|
| 44 |
def addExt(trackpoint, ele_new): |
| 45 |
#Add new elevation to extension tag |
| 46 |
''' |
| 47 |
<extensions> |
| 48 |
<pytrainer:ele method="srtm_bilinear">31.1</pytrainer:ele> |
| 49 |
</extensions> |
| 50 |
''' |
| 51 |
ext = etree.Element("extensions") |
| 52 |
py_ele = etree.SubElement(ext, PYTRAINER + "ele", method="srtm_bilinear") |
| 53 |
py_ele.text = str(ele_new) |
| 54 |
trackpoint.append(ext) |
| 55 |
|
| 56 |
""" |
| 57 |
Replace elevation from GPX by data from SRTM. |
| 58 |
TODO (Arnd) make a function within class fixelevation out of this for better reuse |
| 59 |
""" |
| 60 |
trackpoints = self._data.findall(self._trkpt_path) |
| 61 |
for trkpt in trackpoints: |
| 62 |
lat = float(trkpt.attrib['lat']) |
| 63 |
lon = float(trkpt.attrib['lon']) |
| 64 |
|
| 65 |
ele = trkpt.find('{%s}ele' % self._xmlns) |
| 66 |
ele_new = self._srtm.get_elevation(lat, lon) |
| 67 |
|
| 68 |
if not ele_new: |
| 69 |
ele_fixed = False |
| 70 |
break |
| 71 |
|
| 72 |
addExt(trkpt, ele_new) |
| 73 |
|
| 74 |
if not ele_fixed: |
| 75 |
# Try Google maps elevation API |
| 76 |
import cjson, urllib2, math |
| 77 |
steps = len(trackpoints) / 300 |
| 78 |
|
| 79 |
path = '' |
| 80 |
lat_prev, long_prev = 0, 0 |
| 81 |
t = 0 |
| 82 |
for t in xrange(0,len(trackpoints),steps): |
| 83 |
lat = float(trackpoints[t].attrib['lat']) |
| 84 |
lon = float(trackpoints[t].attrib['lon']) |
| 85 |
encoded_lat, lat_prev = encode_coord(lat, lat_prev) |
| 86 |
encoded_long, long_prev = encode_coord(lon, long_prev) |
| 87 |
path += encoded_lat + encoded_long |
| 88 |
t += 1 |
| 89 |
url = "http://maps.googleapis.com/maps/api/elevation/json?sensor=true&samples=%d&path=enc:" % int((len(trackpoints) / steps)) |
| 90 |
url += path |
| 91 |
|
| 92 |
try: |
| 93 |
google_ele = cjson.decode(urllib2.urlopen(url).read()) |
| 94 |
if google_ele['status'] == "OK": |
| 95 |
t_idx = 0 |
| 96 |
ele_points = len(google_ele['results']) |
| 97 |
for ele_new in xrange(0,ele_points): |
| 98 |
addExt(trackpoints[t_idx], google_ele['results'][ele_new]['elevation']) |
| 99 |
for intermediate in xrange(ele_new+1, ele_new+steps): |
| 100 |
if intermediate<len(trackpoints): |
| 101 |
if ele_new==ele_points-1: |
| 102 |
calculated = google_ele['results'][ele_new]['elevation'] |
| 103 |
else: |
| 104 |
ele1 = google_ele['results'][ele_new]['elevation'] |
| 105 |
ele2 = google_ele['results'][ele_new+1]['elevation'] |
| 106 |
calculated = (ele1 * (intermediate-ele_new) + ele2 * (steps - (intermediate-ele_new))) / steps |
| 107 |
t_idx += 1 |
| 108 |
addExt(trackpoints[t_idx], calculated) |
| 109 |
t_idx += 1 |
| 110 |
ele_fixed = True |
| 111 |
except urllib2.HTTPError: |
| 112 |
pass |
| 113 |
|
| 114 |
if ele_fixed: |
| 115 |
# Write out to original *.gpx. |
| 116 |
self._data.write( gpx_file, |
| 117 |
encoding=self._data.docinfo.encoding, |
| 118 |
xml_declaration=True, |
| 119 |
pretty_print=False) |
| 120 |
res_msg = "Elevation has been fixed." |
| 121 |
#TODO Expire activity out of pool - so get updated info |
| 122 |
self.pytrainer_main.activitypool.remove_activity_from_cache(aid) |
| 123 |
else: |
| 124 |
res_msg = "Elevation could not be fixed!" |
| 125 |
|
| 126 |
#Show the user the result |
| 127 |
md = Gtk.MessageDialog(self.pytrainer_main.windowmain.window1, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, res_msg) |
| 128 |
md.set_title(_("Elevation Correction Complete")) |
| 129 |
md.set_modal(False) |
| 130 |
md.run() |
| 131 |
md.destroy() |
| 132 |
#TODO reload gpx data in main window |
| 133 |
|
| 134 |
else: |
| 135 |
logging.error("ELE GPX file: %s NOT found!!!" % (gpx_file)) |
| 136 |
logging.debug("<<") |
| 137 |
|
| 138 |
def encode_coord(x, prev): |
| 139 |
val = int(x * 1e5) |
| 140 |
return encode_signed(val - prev), val |
| 141 |
|
| 142 |
def encode_signed(n): |
| 143 |
tmp = n << 1 |
| 144 |
if n < 0: |
| 145 |
tmp = ~tmp |
| 146 |
return encode_unsigned(tmp) |
| 147 |
|
| 148 |
def encode_unsigned(n): |
| 149 |
b = [] |
| 150 |
while n >= 32: |
| 151 |
b.append(n & 31) |
| 152 |
n = n >> 5 |
| 153 |
b = [(c | 0x20) for c in b] |
| 154 |
b.append(n) |
| 155 |
b = [(i + 63) for i in b] |
| 156 |
return ''.join([chr(i) for i in b]) |
| 157 |
|