def _gpx_for_logs(self, logs): g = GPX() track = GPXTrack() track.source = 'pizero-gpslog %s' % VERSION g.tracks.append(track) seg = GPXTrackSegment() track.segments.append(seg) for item in logs: try: tpv = item['tpv'][0] sky = item['sky'][0] p = GPXTrackPoint(latitude=tpv['lat'], longitude=tpv['lon'], elevation=tpv['alt'], time=TIME_TYPE.from_string(tpv['time']), speed=tpv['speed'], horizontal_dilution=sky.get('hdop', None), vertical_dilution=sky.get('vdop', None), position_dilution=sky.get('pdop', None)) if tpv['mode'] == 2: p.type_of_gpx_fix = '2d' elif tpv['mode'] == 3: p.type_of_gpx_fix = '3d' if 'satellites' in sky: p.satellites = len(sky['satellites']) seg.points.append(p) except Exception: sys.stderr.write('Exception loading line %d:\n' % item['lineno']) raise return g
def main(gps_log, lawnmower_log): with open(gps_log) as fp: gpx = GPX() #Fill in the Waypoints and what the lawnmower saw with open(lawnmower_log) as fpi: # Create track for lawnmower gpx_track = GPXTrack(name="Lawmower GPS Log") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) for l in fpi: m = re.match(lawnmower_wpt_fmt, l) if m: point_num = int(m.group(2)) lat = float(m.group(3)) lon = float(m.group(4)) gpx.waypoints.append(GPXWaypoint(lat,lon,description="P%d" % point_num, type="Lawnmower")) m = re.match(lawnmower_gps_fmt, l) if m: ctime = parse_time(m.group(1)) lat = float(m.group(2)) lon = float(m.group(3)) gpx_segment.points.append(GPXTrackPoint(lat,lon,time=ctime)) # Create track of raw GPS log: gpx_track = GPXTrack(name="GPS Log") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) for l in fp: m = re.match(gps_fmt, l) if m: ctime = parse_time(m.group(1)) lat = float(m.group(2)) lon = float(m.group(3)) #'time' since last DGPS fix is group 4 hdop = float(m.group(5)) fix_quality = int(m.group(6)) num_satellites = int(m.group(7)) gpx_segment.points.append(GPXTrackPoint(lat,lon,horizontal_dilution=hdop,time=ctime)) with open("lawnmower_log.gpx", "w") as fpo: print(gpx.to_xml(), file=fpo)
def gpx_track_create(gpx, gpx_obj_namef): ''' Create tracks in our GPX :param gpx: :param gpx_obj_namef: :return: ''' gpx_track = {} print('track name: ', gpx_obj_namef) gpx_track[gpx_obj_namef] = GPXTrack(name=gpx_obj_namef) gpx_track[gpx_obj_namef].name = gpx_obj_namef # noticed that constructor not apply it gpx.tracks.append(gpx_track[gpx_obj_namef]) # Create segment in this GPX track: return gpx_track
def main(naza_log): with open(naza_log) as fp: gpx = GPX() # Create track of raw GPS log: gpx_track = GPXTrack(name="GPS Log") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) for l in fp: row = l.split(",") ctime = datetime.strptime(row[0], time_fmt) lat, lon, alt = float(row[1]), float(row[2]), float(row[3]) speed, cog, calheading = float(row[4]), float(row[5]), float( row[6]) hdop, vdop = float(row[7]), float(row[8]) fixtype, numsat = int(row[9]), int(row[10]) gpx_segment.points.append( GPXTrackPoint(lat, lon, alt, horizontal_dilution=hdop, vertical_dilution=vdop, speed=speed, name=str(calheading), time=ctime)) with open("naza_log.gpx", "w") as fpo: print(gpx.to_xml(), file=fpo)
def observations_to_gpx( observations: List[JsonResponse], output_file: str = "observations.gpx", track: bool = True ): """Convert a list of observations to a set of GPX waypoints or a GPX track Args: observations: JSON observations output_file: File path to write to track: Create an ordered GXP track; otherwise, create unordered GPX waypoints """ gpx = GPX() logger.info(f"Converting {len(observations)} to GPX points") points = [observation_to_gpx_point(obs, track=track) for obs in observations] if track: gpx_track = GPXTrack() gpx.tracks.append(gpx_track) gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) gpx_segment.points = points else: gpx.waypoints = points # Save to file logger.info(f"Writing GPX data to {output_file}") with open(output_file, "w") as f: f.write(gpx.to_xml())
def main(gps_log): with open(gps_log) as fp: gpx = GPX() # Create track of raw GPS log: gpx_track = GPXTrack(name="GPS Log") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) for l in fp: m = re.match(gps_fmt, l) if m: ctime = parse_time(m.group(1)) lat = float(m.group(2)) lon = float(m.group(3)) #speed = float(m.group(4)) bearing = m.group(5) #alt = float(m.group(6)) speed = 0 alt = 0 gpx_segment.points.append(GPXTrackPoint(lat,lon,time=ctime, speed=speed, elevation=alt, name=bearing)) with open("gps_log.gpx", "w") as fpo: print(gpx.to_xml(), file=fpo)
def test_create_work_as_expected(self, gpxpy_mock): # Given some fake GPX data pnt1 = GPXTrackPoint(latitude=1, longitude=1, time=datetime(2016, 1, 1)) pnt2 = GPXTrackPoint(latitude=2, longitude=2, time=datetime(2016, 1, 2)) segment = GPXTrackSegment(points=[pnt1, pnt2]) track = GPXTrack() track.segments = [segment] gpx = GPX() gpx.tracks = [track] # Given a mock gpxpy that returns the fake data gpxpy_mock.parse.return_value = gpx trackpoint_mock = Mock() up_file = Mock() up_file.read.return_value.decode.return_value = sentinel.gpx_raw # When creating trackpoints tps = create_trackpoints(sentinel.track, up_file, trackpoint_mock) # Then the correct stuff happens assert len(tps) == 2 trackpoint_mock.assert_any_call(lat=1, lon=1, sog=0, timepoint=datetime(2016, 1, 1, tzinfo=pytz.UTC), track=sentinel.track) trackpoint_mock.assert_any_call(lat=2, lon=2, sog=1.819738796736955, timepoint=datetime(2016, 1, 2, tzinfo=pytz.UTC), track=sentinel.track) gpxpy_mock.parse.assert_called_once_with(sentinel.gpx_raw) up_file.read.assert_called_once_with() up_file.read.return_value.decode.assert_called_once_with('utf-8')
def _gpx_for_logs(self): g = GPX() track = GPXTrack() track.source = 'pizero-gpslog gmc-500' g.tracks.append(track) seg = GPXTrackSegment() track.segments.append(seg) prev_alt = 0.0 for idx, item in enumerate(self.lines): try: tpv = item['tpv'][0] sky = item['sky'][0] alt = tpv.get( 'alt', item['gst'][0].get('alt', prev_alt) ) prev_alt = alt p = GPXTrackPoint( latitude=tpv['lat'], longitude=tpv['lon'], elevation=alt, time=TIME_TYPE.from_string(tpv['time']), speed=tpv['speed'], horizontal_dilution=sky.get('hdop', None), vertical_dilution=sky.get('vdop', None), position_dilution=sky.get('pdop', None) ) if tpv['mode'] == 2: p.type_of_gpx_fix = '2d' elif tpv['mode'] == 3: p.type_of_gpx_fix = '3d' if 'satellites' in sky: p.satellites = len(sky['satellites']) cpm = item.get('_extra_data', {}).get('data', {}).get('cpm', 0) seg.points.append(p) except Exception: logger.error( 'Error loading line %d: %s', idx, item ) raise return g
def test_latitude_calculation(self): gpxpy_instance = GPX() gpxpy_instance.tracks.append(GPXTrack()) gpxpy_instance.tracks[0].segments.append(GPXTrackSegment()) points = gpxpy_instance.tracks[0].segments[0].points longitude_distance_km = 111.31949079327357 self.assertEqual(lon2kilometers(lon_count=1), longitude_distance_km) self.assertEqual(kilometers2lon_count(longitude_distance_km), 1) for lon_count in range(10): distance_km = lon2kilometers(lon_count) self.assert_equal_rounded(distance_km, longitude_distance_km * lon_count) self.assert_equal_rounded(kilometers2lon_count(distance_km), lon_count, decimal_places=0)
def gpxdump(pts): gpx = GPX() # Create track gpx_track = GPXTrack(name="Lawnmower pattern") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) for pt in pts: gpx_segment.points.append(GPXTrackPoint(pt.lat, pt.lon)) with open("lawnmower.gpx", "w") as fpo: print(gpx.to_xml(), file=fpo)
def gpxdump(pts): gpx = GPX() # Create track gpx_track = GPXTrack(name="Spiral pattern") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) for pt in pts: gpx_segment.points.append( GPXTrackPoint(pt.lat, pt.lon, elevation=pt.alt)) with open("spiral.gpx", "w") as fpo: print(gpx.to_xml(), file=fpo)
def to_gpx(observations: AnyObservations, filename: str = None, track: bool = True) -> str: """Convert a list of observations to a set of GPX waypoints or a GPX track Example: >>> from pyinaturalist import get_observations >>> from pyinaturalist_convert import to_gpx >>> >>> results = get_observations( ... project_id=36883, # ID of the 'Sugarloaf Ridge State Park' project ... created_d1='2020-01-01', # Get observations from January 2020... ... created_d2='2020-09-30', # ...through September 2020 ... geo=True, # Only get observations with coordinates ... geoprivacy='open', # Only get observations with public coordinates ... page='all', # Paginate through all response pages ... ) >>> to_gpx(results, '~/tracks/observations-36883.gpx') Args: observations: JSON observations filename: Optional file path to write to track: Create an ordered GPX track; otherwise, create unordered GPX waypoints Returns: GPX XML as a string """ gpx = GPX() points = [ to_gpx_point(obs, track=track) for obs in to_dict_list(observations) ] if track: gpx_track = GPXTrack() gpx.tracks.append(gpx_track) gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) gpx_segment.points = points else: gpx.waypoints = points gpx_xml = gpx.to_xml() if filename: write(gpx_xml, filename) return gpx_xml
def generate_gpx_track(track_length_km, point_count, pace_min, start_longitude=0, start_date=datetime(2018, 5, 30, 10, 00)): distance_km = track_length_km / point_count print("km between points:", distance_km) longitude_diff = kilometers2lon_count(distance_km) print("longitude diff:", longitude_diff) time_delta = timedelta(minutes=(pace_min * distance_km)) print("time delta:", time_delta) gpxpy_instance = GPX() gpxpy_instance.tracks.append(GPXTrack()) gpxpy_instance.tracks[0].segments.append(GPXTrackSegment()) points = gpxpy_instance.tracks[0].segments[0].points points.append( GPXTrackPoint(latitude=0, longitude=start_longitude, elevation=0, time=start_date)) print("Start point:", points[-1]) td = 0 current_longitude = start_longitude current_datetime = start_date for point_no in range(point_count): current_longitude += longitude_diff current_datetime += time_delta points.append( GPXTrackPoint(latitude=0, longitude=current_longitude, elevation=0, time=current_datetime)) print("point %i: %s" % (point_no + 1, points[-1])) print("\ttime diff:", points[-1].time_difference(points[-2])) print("\tdistance 2d:", points[-1].distance_2d(points[-2])) print("\tdistance 3d:", points[-1].distance_3d(points[-2])) td += points[-1].distance_3d(points[-2]) print("\t", td) return gpxpy_instance
def add_points(self, points) ->None: """Adds points to last segment in the last track. If no track is allocated yet and points is not an empty list, allocates a track. Args: points (list(GPXTrackPoint): The points to be added """ if points: if self.__gpx.tracks: # make sure the same points are not added twice assert points != self.__gpx.tracks[-1].segments[-1].points[-len(points):] self._load_full() if not self.__gpx.tracks: self.__gpx.tracks.append(GPXTrack()) self.__gpx.tracks[0].segments.append(GPXTrackSegment()) self.__gpx.tracks[-1].segments[-1].points.extend(points) self.dirty = 'gpx'
def create_track_from_gpx_track(cls, track: GPXTrack, title: str, sport_type: str, user_id: int) -> Track: """ Create a new Track and coresponding TrackPoint objects from a GPXTrack - Removes empty trackpoints - Calculates ascent/descent, min/max elevation, min/max + center latitude/longitude """ track.remove_empty() bounds: GPXBounds = track.get_bounds() center: Location = track.get_center() min_max_elevation: MinimumMaximum = track.get_elevation_extremes() # moving_data: MovingData = track.get_moving_data() uphill_downhill: UphillDownhill = track.get_uphill_downhill() new_track: Track = cls( title=title, sport_type=sport_type, center_latitude=center.latitude, center_longitude=center.longitude, min_latitude=bounds.min_latitude, min_longitude=bounds.min_longitude, max_latitude=bounds.max_latitude, max_longitude=bounds.max_longitude, distance=track.length_3d(), min_elevation=min_max_elevation.minimum, max_elevation=min_max_elevation.maximum, ascent=uphill_downhill.uphill, descent=uphill_downhill.downhill, user_id=user_id, ) parsed_track_points: Union[List[TrackPoint], List] = [] segment: GPXTrackSegment for segment in track.segments: parsed_track_points = TrackPoint.create_track_points_from_gpx_segment( segment) new_track.track_points.extend(parsed_track_points) # Reduce points to mitigate url length limits when requesting map # Shorten polyline if track is longer than 50 miles if new_track.distance > 80467: segment.reduce_points(200) else: segment.reduce_points(100) # convert updated segment to coordinates coordinates = [(p.latitude, p.longitude) for p in segment.points] new_track.polyline = polyline.encode(coordinates, 5) # request img file from google maps / upload to cloudinary url = create_url(new_track.polyline, 175) r = requests.get(url, stream=True) if r.status_code == 200: with open("./app/utility/img.png", "wb") as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) img = None try: img = upload("./app/utility/img.png", tags="150-static-map") except Exception as e: print(e) new_track.map_175px_img_url = img["url"] os.remove("./app/utility/img.png") return new_track
def main(wpt_log): with open(wpt_log) as fp: gpx = GPX() name, ext = os.path.splitext(wpt_log) # Create track for the waypoints gpx_track = GPXTrack(name="Waypoints log") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) #An empty detected object det = {} for l in fp: m = re.match(gen_fmt, l) if m: if m.group(2).startswith("Waypoint"): m2 = re.match(wpt_fmt,m.group(3)) if m2: lat = float(m2.group(1)) lon = float(m2.group(2)) alt = float(m2.group(3)) if m2.group(4): roi_lat = float(m2.group(4)) roi_lon = float(m2.group(5)) roi_alt = float(m2.group(6)) #TODO ??? wpt = GPXWaypoint(lat, lon, alt, description="W" + m.group(2)[9:], type="Waypoint") gpx.waypoints.append(wpt) elif m.group(2) == "At": m2 = re.match(gps_fmt, m.group(3)) if m2: ctime = parse_time(m.group(1)) lat = float(m2.group(1)) lon = float(m2.group(2)) alt = float(m2.group(3)) try: hdg = m2.group(4) except ValueError: hdg = None gpx_segment.points.append(GPXTrackPoint(lat,lon,alt,time=ctime,comment=hdg)) elif m.group(2).startswith("Detected"): id = m.group(3)[3:] det = {"ID" : id} elif m.group(2) == "Location": m2 = re.match(gps_fmt, m.group(3)) if m2: det["time"] = parse_time(m.group(1)) det["lat"] = float(m2.group(1)) det["lon"] = float(m2.group(2)) det["alt"] = float(m2.group(3)) try: det["hdg"] = m2.group(4) except ValueError: det["hdg"] = None elif m.group(2) == "Image": n = os.path.basename(m.group(3)) det["img"] = "pics/" + n poi = GPXWaypoint(det["lat"], det["lon"], det["alt"], time=det["time"], comment=det["hdg"], symbol=det["img"], description="POI" + det["ID"], type="Point of interest") gpx.waypoints.append(poi) print(gpx.to_xml())
from gpxpy.gpx import GPX, GPXTrack, GPXTrackSegment, GPXTrackPoint from pandas import read_csv, to_datetime basename = '2018-06-07' df = read_csv('{0}.csv'.format(basename)) df['timestamp']= to_datetime(df.time) gpx_obj = GPX() gpx_track = GPXTrack() gpx_obj.tracks.append(gpx_track) gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) gpx_segment.points.extend( ( GPXTrackPoint( time=row.timestamp, latitude=row.latitude, longitude=row.longitude ) for row in df.itertuples() ) ) with open('{0}.gpx'.format(basename), 'w') as gpx_file: gpx_file.write(gpx_obj.to_xml())
def main(gps_log, waypoints_log): with open(gps_log) as fp: gpx = GPX() #Fill in the Waypoints and what the lawnmower saw with open(waypoints_log) as fpi: # Create track for lawnmower gpx_track = GPXTrack(name="Waypoints GPS Log") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) waypoints = [] for l in fpi: m = re.match(waypts_wpt_fmt, l) if m: waypoints.append((m.group(2), m.group(3))) m = re.match(waypts_gps_fmt, l) if m: ctime = parse_time(m.group(1)) lat = float(m.group(2)) lon = float(m.group(3)) gpx_segment.points.append( GPXTrackPoint(lat, lon, time=ctime)) waypoints = f7(waypoints) for i in range(len(waypoints)): lat, lon = float(waypoints[i][0]), float(waypoints[i][1]) gpx.waypoints.append( GPXWaypoint(lat, lon, description="P%d" % (i + 1), type="Waypoints")) # Create track of raw GPS log: gpx_track = GPXTrack(name="GPS Log") gpx.tracks.append(gpx_track) # Create segment in GPX track: gpx_segment = GPXTrackSegment() gpx_track.segments.append(gpx_segment) for l in fp: m = re.match(gps_fmt, l) if m: ctime = parse_time(m.group(1)) lat = float(m.group(2)) lon = float(m.group(3)) #'time' since last DGPS fix is group 4 hdop = float(m.group(5)) fix_quality = int(m.group(6)) num_satellites = int(m.group(7)) gpx_segment.points.append( GPXTrackPoint(lat, lon, horizontal_dilution=hdop, time=ctime)) with open("waypoints_log.gpx", "w") as fpo: print(gpx.to_xml(), file=fpo)