def test_track(self): track = OsmGpsMap.MapTrack() self.osm.track_add(track) color = Gdk.RGBA(1, 0, 1, 0) track.set_property('color', color) color2 = track.get_color() self.assertEqual(color, color2) points = [] for x in range(0, 5): point = OsmGpsMap.MapPoint.new_degrees(self.lat + x, self.lon + x) track.add_point(point) points.append(point) self.assertEqual(track.n_points(), 5) self.assertEqual(track.get_length(), 522318.175858659) track.remove_point(3) self.assertEqual(track.n_points(), 4) self.assertEqual(type(track.get_point(3)), OsmGpsMap.MapPoint) returned_points = track.get_points() self.assertEqual(len(returned_points), 4) self.osm.track_remove(track)
def add_track(self, lat, lon, key, color=None): if key not in self.tracks: track = OsmGpsMap.MapTrack() self.tracks[key] = track self.show_track(key) else: track = self.tracks[key] point = OsmGpsMap.MapPoint.new_degrees(lat, lon) track.add_point(point) if color is not None: self.set_track_color(key, color)
def plotButton(self, event): # def prevent_add_point(self,x): # win.i = True # return True self.i = False def plot(self, event): if isinstance(event, int): # Map or points have changed win.i = True return True if (event.type == Gdk.EventType.BUTTON_RELEASE and event.button == 1 or (event.button == 1 and event.get_state() & Gdk.ModifierType.CONTROL_MASK)) and not win.i: pt = win.osm.get_event_location(event) win.route.add_point(pt) win.calc_track_length(win.route, 0) win.i = False # self.coords.append([pt.get_degrees()[1],pt.get_degrees()[0]]) if self.plot_button.get_active(): self.get_window().set_cursor(Gdk.Cursor(Gdk.CursorType.CROSS)) self.plot_button.set_label('Plotting...') self.route = OsmGpsMap.MapTrack(editable=True, color=Gdk.RGBA(0, 0, 255, 1), alpha=.8, line_width=2) # self.coords = [] self.handler1 = self.route.connect('point-changed', self.calc_track_length) self.osm.track_add(self.route) self.handler = self.osm.connect_after('button-release-event', plot) self.handler2 = self.osm.connect('changed', plot, 0) self.handler3 = self.route.connect('point-changed', plot) self.handler4 = self.route.connect('point-inserted', plot) else: self.get_window().set_cursor(Gdk.Cursor(Gdk.CursorType.ARROW)) self.plot_button.set_label('Plot track') self.osm.track_remove(self.route) self.route = [] self.len_label.set_text('') self.osm.disconnect(self.handler) self.osm.disconnect(self.handler1) self.osm.disconnect(self.handler2) self.osm.disconnect(self.handler3) self.osm.disconnect(self.handler4) return
def addTrace(self, trace): filename = trace.get_full_path() if filename not in self._tracks: gpstracks = [] for track in trace.get_points(): for segment in track: gpstrack = OsmGpsMap.MapTrack() gpstrack.props.alpha = 0.8 for rlat,rlon in segment: gpstrack.add_point(OsmGpsMap.MapPoint.new_radians(rlat, rlon)) gpstracks.append(gpstrack) self._tracks[filename] = (trace, gpstracks) self.model.append( (trace.get_display_name(), filename) ) self.emit("track-added", trace, gpstracks)
def __init__(self): super(Window, self).__init__() self.connect('destroy', Gtk.main_quit) self.set_default_size(500, 500) self.set_title('OsmGpsMap test') punto_cota_cota = [-16.538275, -68.069592] self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) self.add(self.box) self.map = osmgpsmap.Map() self.box.pack_start(self.map, True, True, 0) self.layer_osd = osmgpsmap.MapOsd(show_dpad=True, show_zoom=True, show_crosshair=True, show_coordinates=True, show_scale=True) self.map.layer_add(self.layer_osd) max_zoom, min_zoom = self.map.props.max_zoom, self.map.props.min_zoom print(max_zoom, min_zoom) self.map.set_center_and_zoom(punto_cota_cota[0], punto_cota_cota[1], max_zoom) self.map_track = osmgpsmap.MapTrack() self.point = osmgpsmap.MapPoint() self.point.set_degrees(punto_cota_cota[0], punto_cota_cota[1]) self.map_track.add_point(self.point) # print(self.map_track.get_points()) self.map.track_add(self.map_track) # self.box.pack_start(self.map_track, True, True, 0) # self.grid = Gtk.Grid() # self.box.pack_start(self.grid, True, True, 0) self.lbl_coords = Gtk.Label("Coordenadas") self.box.pack_start(self.lbl_coords, False, True, 0)
def ors_call(self, dummy): try: self.osm.track_remove(self.orsRoute) except: pass try: sel = self.ors_profile.get_active_iter() if sel is not None: model = self.ors_profile.get_model() orsProfile = model[sel][0] except: orsProfile = 'driving-car' if self.pref_select_fastest.get_active(): pref = 'fastest' else: pref = 'shortest' try: Route = [] for i in range(len(self.via_route)): Route.append([self.via_route[i][1], self.via_route[i][0]]) Route.insert(0, [self.start_route[1], self.start_route[0]]) Route.append([self.end_route[1], self.end_route[0]]) body = { "coordinates": Route, "elevation": "true", "preference": pref } except: return headers = { 'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8', 'Authorization': '5b3ce3597851110001cf624831f2d1f9129542dfbd9a148cd579f14b', 'Content-Type': 'application/json; charset=utf-8' } call = requests.post( 'https://api.openrouteservice.org/v2/directions/' + orsProfile + '/geojson', json=body, headers=headers, timeout=10) # print(call.status_code, call.reason) # print(call.text) if call.status_code == 200: self.route_json = json.loads(call.text) self.coords = self.route_json['features'][0]['geometry'][ 'coordinates'] self.orsRoute = OsmGpsMap.MapTrack(editable=True, alpha=1, line_width=2) self.orsRoute.connect('point-changed', self.edit) for i in range(len(self.coords)): pt = OsmGpsMap.MapPoint() pt.set_degrees(float(self.coords[i][1]), float(self.coords[i][0])) self.orsRoute.add_point(pt) self.osm.track_add(self.orsRoute) self.calc_track_length(self.orsRoute, 0) # 0 is for the dummy arg bbox = self.route_json['features'][0]['bbox'] self.osm.zoom_fit_bbox(bbox[1], bbox[4], bbox[0], bbox[3]) self.instruction = '' for i in range( len(self.route_json['features'][0]['properties'] ['segments'])): for j in range( len(self.route_json['features'][0]['properties'] ['segments'][i]['steps'])): step = self.route_json['features'][0]['properties'][ 'segments'][i]['steps'][j] stepDistance = step['distance'] if stepDistance > 1000: stepDistance = str( round((stepDistance / 1609.34) * 100) / 100) + ' miles' else: stepDistance = str(stepDistance) + 'm' self.instruction = self.instruction + '- ' + step[ 'instruction'] + ' (' + stepDistance + ')\n'
def upload_gpx(self, gpx_button): dialog = Gtk.FileChooserDialog(title="Please choose a GPX file", parent=self, action=Gtk.FileChooserAction.OPEN) dialog.add_buttons( Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK, ) filter = Gtk.FileFilter() filter.set_name("GPX") filter.add_pattern("*.gpx") dialog.add_filter(filter) filter = Gtk.FileFilter() filter.set_name("All files") filter.add_pattern("*") dialog.add_filter(filter) response = dialog.run() if response == Gtk.ResponseType.OK: gpx_file = open(dialog.get_filename(), 'r') gpx = gpxpy.parse(gpx_file) tr = [] self.coords = [] try: for track in gpx.tracks: for segment in track.segments: for pt in segment.points: lat = '{0}'.format(pt.latitude, pt.longitude, pt.elevation) lon = '{1}'.format(pt.latitude, pt.longitude, pt.elevation) elev = '{2}'.format(pt.latitude, pt.longitude, pt.elevation) self.coords.append([lon, lat, elev]) pt = OsmGpsMap.MapPoint() pt.set_degrees(float(lat), float(lon)) tr.append(pt) except: pass try: for waypoint in gpx.waypoints: lat = '{0}'.format(waypoint.latitude, waypoint.longitude, waypoint.elevation) lon = '{1}'.format(waypoint.latitude, waypoint.longitude, waypoint.elevation) elev = '{2}'.format(waypoint.latitude, waypoint.longitude, waypoint.elevation) self.coords.append([lon, lat, elev]) waypoint = OsmGpsMap.MapPoint() waypoint.set_degrees(float(lat), float(lon)) tr.append(waypoint) except: pass try: for trk in gpx.routes: # for track in gpx.tracks: # for segment in track.segments: for pt in trk.points: lat = '{0}'.format(pt.latitude, pt.longitude, pt.elevation) lon = '{1}'.format(pt.latitude, pt.longitude, pt.elevation) elev = '{2}'.format(pt.latitude, pt.longitude, pt.elevation) self.coords.append([lon, lat, elev]) pt = OsmGpsMap.MapPoint() pt.set_degrees(float(lat), float(lon)) tr.append(pt) except: pass if len(tr) > 0: self.route = OsmGpsMap.MapTrack(color=Gdk.RGBA(0, 0, 100, 1), line_width=3, alpha=1) for pt in tr: self.route.add_point(pt) self.osm.track_add(self.route) try: self.calc_track_length(self.route, 0) # 0 is for the dummy arg except: self.len_label.set_text('No length') # self.route.connect('point-changed', self.calc_track_length) beg = tr[0].get_degrees() end = tr[len(tr) - 1].get_degrees() self.osm.zoom_fit_bbox(beg[0], end[0], beg[1], end[1]) try: st = GdkPixbuf.Pixbuf.new_from_file_at_size( self.path + '/images/marker-start-icon-2x.png', 50, 50) self.startImage = self.osm.image_add(beg[0], beg[1], st) nd = GdkPixbuf.Pixbuf.new_from_file_at_size( self.path + '/images/marker-end-icon-2x.png', 50, 50) self.endImage = self.osm.image_add(end[0], end[1], nd) except: pass else: self.len_label.set_text('Can\'t read GPX') # elif response == Gtk.ResponseType.CANCEL: # print("Cancel clicked") dialog.destroy()