class TrackCompareObserver(GpxObserver): def __init__(self, qtree, max_match_threshold_metres, outputFormatter, poiFetcher=None): # TODO beware multiple instances will overwrite each other. consider tempfile.mktemp. This is only for debugging anyway self.gpxLongStop = gpxWriter(open(gps.lib.gpxUtil.tempFilePath("stop_unknown.GPX", "gpx2txt"), "w")) if not self.gpxLongStop: raise Exception("Failed create long stop gpx file" + gps.lib.gpxUtil.tempFilePath("stop_unknown.GPX", "gpx2txt")) self.segmentsCompare = qtree self.poi = poiFetcher or gps.lib.petitpois.FetchPoi(True) self.max_match_threshold_metres = max_match_threshold_metres self.output = outputFormatter self.log = LogWriter() self.fetchPois = True def start(self): ## TODO doesn't this get called anyway? self.output.start() def end(self): self.output.end() def haveBounds(self, bounds): pass def nextTrack(self, track): super(TrackCompareObserver, self).nextTrack(track) matcher = gpx2txtMatcher(track, self.output, self.max_match_threshold_metres) matcher.gpxLongStop = self.gpxLongStop if self.fetchPois: self.log.dbg("Track: %s" % (track.filename())) self.log.dbg("Fetching POI") tb = track.getBounds() self.log.i("Track bounds are: %s" % tb) self.log.i("Track bounded area is: %s (%s)" % (gps.lib.gpxUtil.area(tb), gps.lib.gpxUtil.width_height(tb))) area = tb.area() / 1000 / 1000 # Don't get crazy large areas if not area < 5000: log.i("Skipping fetching web waypoints as area is very large") else: bltr = tb.BLTR() # Note we are creating another WayPointGatherer, but this one is filtered # We will share the same qtree as the original one wpgFiltered = WayPointGathererFiltered(self.segmentsCompare) # going to split the boundaries in the smaller chunks for poi in self.poi.fetchPoi_gpx_split(*bltr): # TODO maybe move out "hit" pubs to our file (especially when have stopped there) # self.log.i("Wrote poi for track to %s" % (poi)) # Let's be quite precise to include these ones. If any of these points are near points # we already have loaded then they will be rejected. Otherwise we get duplicates listed. dthres = 30 wpgFiltered.Parse(poi, dthres) # self.log.o("TRACK: %s|%s, %s - %s" % (track.filename(), track.name, track.time_first, track.time_last)) # TODO should interpolate the track such that we only get a point every 100 metres # or so to limit number of checks we do # Compare with the db self.output.startTrack(track) # This calls back to "foundPoint" in the derived matcher class above hitStats = matcher.matchAlgo2(track, self.segmentsCompare) matcher.endTrack() hitStatsX = {} hitStatsX["hit"] = hitStats[0] hitStatsX["miss"] = hitStats[1] hitStatsX["match_pct"] = hitStats[2] hitStatsX["stopTimeTotal"] = matcher.stopSpotter.stopTimeTotal() hitStatsX["stops"] = len(matcher.stopSpotter.stops) hitStatsX["distance"] = track.distance hitStatsX["st_pct"] = hitStatsX["mv_pct"] = hitStatsX["movingTime"] = 0 ts = track.duration() if ts: hitStatsX["st_pct"] = (hitStatsX["stopTimeTotal"] / ts) * 100 hitStatsX["mv_pct"] = 100 - hitStatsX["st_pct"] hitStatsX["movingTime"] = ts - hitStatsX["stopTimeTotal"] # format with gpxUtil.duration() self.output.endTrack(hitStatsX) def nextTrackPoint(self, trackPoint): pass def nextRoutePoint(self, routePoint): pass def done(self): self.gpxLongStop.close()
#DUP def printBounds(bounds): log.i("These are the extremities:") log.i("L: %s" % bounds.left()) log.i("R: %s" % bounds.right()) log.i("T: %s" % bounds.top()) log.i("B: %s" % bounds.bottom()) log.i("") if __name__ == "__main__": wpg = WayPointGatherer() wpFiles = getFilenameFromCL(sys.argv[1:]) if not wpFiles: wpFiles = list(WaypointDB().get()) log.dbg("Waypoint files are: %s" % wpFiles) for filename, dthres in wpFiles: wpg.Parse(filename, dthres) # For interest, these are the extreme points bounds = wpg.qtree.getBounds() printBounds(bounds) log.i("%s" % wpg)