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())
Example #2
0
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)
Example #3
0
def main():
    p = argparse.ArgumentParser()
    p.add_argument('-i', '--in-file', type=argparse.FileType('r', encoding='utf-8'), default='-')
    p.add_argument('-o', '--out-file', type=argparse.FileType('w', encoding='utf-8'), default='-')
    args = p.parse_args()

    gpx_doc = GPX()
    gpx_doc.creator = 'Flight points GPX creator'
    gpx_doc.name = 'Israel flying navigation points'

    points = json.load(args.in_file)

    for point in points:
        wpt = GPXWaypoint(
            latitude=point['lat'],
            longitude=point['lon'],
            name=point['name'],
            description=point['code'],
            type=KINDS.get(point['kind'], point['kind'])
        )
        gpx_doc.waypoints.append(wpt)

    args.out_file.write(gpx_doc.to_xml())

    print('Done.')
Example #4
0
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)
Example #5
0
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)
Example #6
0
    def to_gpx(self):
        gpx = GPX()
        for building in self.buildings:
            gpx.waypoints.append(building.to_gpx_trackpoint())

        gpx_xml = gpx.to_xml()
        return etree.tostring(etree.fromstring(gpx_xml.encode('utf-8'),
                              parser=etree.XMLParser(encoding='utf-8')),
                              pretty_print=True).decode('utf-8')
Example #7
0
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)
Example #8
0
 def get(self):
     vessel = Vessel.get_key().get()
     wpt_qry = Waypoint.query(ancestor=vessel.key).order(-Waypoint.report_date, -Waypoint.received_date)
     waypoints = wpt_qry.fetch(configdata.MAX_WAYPOINTS)
     gpx = GPX()
     for waypoint in waypoints:
         wpt = GPXWaypoint(waypoint.position.lat, waypoint.position.lon)
         wpt.time = waypoint.received_date
         if waypoint.comment:
             wpt.description = waypoint.comment.encode(encoding="utf-8", errors="ignore")
         gpx.waypoints.append(wpt)
     self.response.write(gpx.to_xml())
Example #9
0
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)
Example #10
0
def main(csv_file):
    with open(csv_file) as f:
        lines = f.readlines()

    csv_lines = lines[1:]

    gpx = GPX()

    for tree_csv in csv_lines:
        gpx.waypoints.append(csv_line_to_gpx(tree_csv))

    output_file = open('canopymaxima_trees.gpx', 'w')
    output_file.write(gpx.to_xml())
    output_file.close()
Example #11
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)
Example #12
0
def summits_get_gpx():
    gpx = GPX()
    if 'rids' in request.args:
        rids = request.args['rids'].split(',')
        gpx.waypoints = \
            (s.to_gpx() for s in g.summits_dao.get_by_ridge(rids=rids))
    else:
        gpx.waypoints = \
            (s.to_gpx() for s in g.summits_dao.get_all())

    resp = make_response(gpx.to_xml())
    resp.mimetype = 'application/gpx+xml'
    resp.headers['Content-Disposition'] = \
        "attachment; filename=summits.gpx"
    return resp
Example #13
0
def summits_get_gpx():
    gpx = GPX()
    if 'rids' in request.args:
        rids = request.args['rids'].split(',')
        gpx.waypoints = \
            (s.to_gpx() for s in g.summits_dao.get_by_ridge(rids=rids))
    else:
        gpx.waypoints = \
            (s.to_gpx() for s in g.summits_dao.get_all())

    resp = make_response(gpx.to_xml())
    resp.mimetype = 'application/gpx+xml'
    resp.headers['Content-Disposition'] = \
        "attachment; filename=summits.gpx"
    return resp
Example #14
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)
Example #15
0
    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
Example #16
0
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)
Example #17
0
    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')
Example #18
0
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
Example #19
0
 def __starting_Gpx(self, parsed):
     """builds an initial Gpx object"""
     segment = gpxpy.gpx.GPXTrackSegment()
     segment.points = self.__points(parsed['points'])
     track = gpxpy.gpx.GPXTrack()
     track.segments.append(segment)
     result =GPX()
     result.tracks.append(track)
     return result
Example #20
0
def clean_extensions(g: gpx.GPX) -> None:
    g.extensions = []
    for rte in g.routes:
        rte.extensions = []
        for pt in rte.points:
            pt.extensions = []
    for t in g.tracks:
        t.extensions = []
        for s in t.segments:
            s.extensions = []
            for p in s.points:
                p.extensions = []
Example #21
0
    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)
Example #22
0
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
Example #23
0
 def __init__(self, backend=None, id_in_backend: str = None, gpx=None):
     self._loading = False
     self._loaded = backend is None or id_in_backend is None
     self.__dirty = set()
     self._batch_changes = False
     self.__what = self.legal_what[0]
     self.__public = False
     self.id_in_backend = id_in_backend
     self.__backend = None
     self.__gpx = gpx or GPX()
     if gpx:
         self._parse_keywords()
     if backend is not None:
         if gpx is not None:
             raise Exception('Cannot accept backend and gpx')
     self.__backend = backend
     if backend is not None and not backend._has_item(id_in_backend): # pylint:disable=protected-access
         # do not say self in backend because that would do a full load of self.
         backend.append(self)
Example #24
0
    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
Example #25
0
    def test_init(self):
        """test initialisation"""
        Activity()
        with Directory(cleanup=True) as backend:
            with self.assertRaises(Exception):
                Activity(backend, gpx=GPX())
            Activity(backend)
            self.assertEqual(len(backend), 1)

        with self.temp_backend(Directory, count=2, cleanup=True,
                               status=False) as backend:
            Activity(backend)
            self.assertEqual(len(backend), 3)

        test_url = tempfile.mkdtemp(prefix=Directory.prefix)
        self.assertTrue(os.path.exists(test_url))
        os.rmdir(test_url)
        self.assertFalse(os.path.exists(test_url))
        try:
            with Directory(url=test_url, cleanup=True):
                self.assertTrue(os.path.exists(test_url))
        finally:
            os.rmdir(test_url)
parser.add_argument('--output', dest='output', action='store', default='latitude.gpx',
                    help='the file to store the output in')

args = parser.parse_args()

# Load the location history from the JSON file -- note that the most recent
# locations are first -- at least when I downloaded mine -- so the order 
# must be reversed.
with open(args.json_filename[0], 'rb') as f:
    history = json.load(f)['data']['items'][::-1]

print('Loaded {filename} with {N} locations.'.format(N=len(history),
                                                     filename=args.json_filename[0]))


gpx = GPX()
gpx_track = GPXTrack()
gpx.tracks.append(gpx_track)

last_timestampMs = 0.0
for point in history:
    timestampMs = long(point['timestampMs'])
    if timestampMs - last_timestampMs > (1000 * 3600 * args.segment_gap):
        gpx_segment = GPXTrackSegment()
        gpx_track.segments.append(gpx_segment)
    last_timestampMs = timestampMs

    gpx_point = GPXTrackPoint(point['latitude'], point['longitude'],
            time=datetime.utcfromtimestamp(timestampMs / 1000.0))
    gpx_segment.points.append(gpx_point)
Example #27
0
def save_to_gpx(nav_df: pd.DataFrame, fileOutPN, gpx_obj_namef=None, waypoint_symbf=None, cfg_proc=None, gpx=None):  #
    """
    Save navigation from dataframe to *.gpx file. track or waypoints.
    Generate waypoints names and selects symbols from cfg['out']['gpx_symbols'] based on current row in nav_df
    :param nav_df: DataFrame with fields:
        if waypoint_symbf: itbl, ...
    :param fileOutPN:       *.gpx file full name without extension. Set None to not write (useful if need only gpx)
    :param gpx_obj_namef:   str or fun(waypoint number). If None then we set it to fileOutPN.stem
    :param waypoint_symbf:  str or fun(nav_df record = row). If None saves track
    :param cfg_proc:
        'simplify_tracks_error_m'
        'dt_per_file'
        'b_missed_coord_to_zeros'
        period_segments or period_tracks: to split track by this in one file
    :param gpx: gpx object to update. If None (default) then will be created here, updated and saved
    :return: None
    """

    if nav_df.empty:
        l.warning('no data')
        return
    if gpx_obj_namef is None:
        gpx_obj_namef = Path(fileOutPN).stem
    if cfg_proc is None:
        cfg_proc = {'dt_per_file': None}
    elif not 'dt_per_file' in cfg_proc:
        cfg_proc['dt_per_file'] = None
    if gpx is None:
        gpx = GPX()

    if waypoint_symbf:
        # , fun_symbol= 'Waypoint', fun_name= str
        if isinstance(waypoint_symbf, str):
            s = waypoint_symbf
            waypoint_symbf = lambda x: s
        b_useDepEcho = 'DepEcho' in nav_df.columns and any(nav_df['DepEcho'])

        w_names = set()
        # w_name = None # same perpose for not all conditions but faster
        # nav_dft= nav_df.reset_index().set_index('itbl', drop=False, append=True) #, inplace=True
        # for t in range(nav_dft.itbl.min(), nav_dft.itbl.max()+1):  #.ptp() = -
        for t, nav_dft in nav_df.groupby(['itbl']):  # .reset_index()
            for i, r in enumerate(nav_dft.itertuples()):  # .loc[t] name=None
                str_time_short = '{:%d %H:%M}'.format(r.Index.to_pydatetime())
                timeUTC = r.Index.tz_convert(None).to_pydatetime()
                str_time_long = '{:%d.%m.%y %H:%M:%S}'.format(timeUTC)
                name = gpx_obj_namef if isinstance(gpx_obj_namef, str) else gpx_obj_namef(i, r, t)

                # remove duplicates by add letter
                name_test_dup = name
                i_dup = 0
                while name_test_dup in w_names:  # name== w_name or :
                    name_test_dup = name + chr(97 + i_dup)  # chr(97) = 'a'
                    i_dup += 1
                else:
                    name = name_test_dup
                w_names.add(name)

                gpx_waypoint = GPXWaypoint(
                    latitude=r.Lat,
                    longitude=r.Lon,
                    time=timeUTC,
                    description=str_time_long,
                    comment=str_time_short,
                    name=name,
                    symbol=waypoint_symbf(r),
                    elevation=-r.DepEcho if b_useDepEcho and np.isfinite(
                        r.DepEcho) else None)  # , description=, type=, comment=
                # if not i_dup:
                #     w_name = name  # to check duplicates on next cycle

                gpx.waypoints.append(gpx_waypoint)
        if isinstance(gpx_obj_namef, str):
            gpx.description = gpx_obj_namef
        if fileOutPN:
            gpx.author_email = '*****@*****.**'
            write_file(fileOutPN, gpx.to_xml())
    else:  # tracks

        # loc= np.zeros_like(nav_df.index, dtype= int)
        # Lat= np.zeros_like(nav_df.index, dtype= np.float64)
        # Lon= np.zeros_like(nav_df.index, dtype= np.float64)
        # T= np.zeros_like(nav_df.index, dtype= pd.Timedelta)

        b_have_depth = ('DepEcho' in nav_df.columns)
        #b_have_speed = ('Speed' in nav_df.columns)
        period_split = cfg_proc.get('period_segments') or cfg_proc.get('period_tracks')
        if period_split:
            period_split = pd_period_to_timedelta(period_split)
            t_intervals_start = pd.date_range(
                start=nav_df.index[0].normalize(),
                end=max(nav_df.index[-1],
                        nav_df.index[-1].normalize() + period_split),
                freq=period_split)[1:]  # make last t_interval_start >= all_data[-1]
            #format_time =
        else:
            t_intervals_start = nav_df.index[-1:]  # series with 1 last value
        t_interval_end = nav_df.index[0]
        n_intervals_without_data = 0
        part = 0
        nav_df = nav_df.tz_convert('utc', copy=False)
        Tprev = nav_df.index[0].to_pydatetime()
        Tcur = Tprev
        if not cfg_proc.get('period_tracks'):
            gpx_track = gpx_track_create(gpx, gpx_obj_namef)
        for t_interval_start in t_intervals_start:
            t_interval = slice(t_interval_end, t_interval_start)  # from previous last
            # USEtime = [[t_interval_end.isoformat(), t_interval_start.isoformat()]]
            nav_df_cur = nav_df.truncate(t_interval_end, t_interval_start, copy=True)
            t_interval_end = t_interval_start
            # load_interval
            if not len(nav_df_cur):
                print('empty interval')
                n_intervals_without_data += 1
                if n_intervals_without_data > 30:
                    print('30 intervals without data => think it is the end')
                    break
                continue
            gpx_segment = GPXTrackSegment()
            if cfg_proc.get('period_tracks'):
                fmt = '%y-%m-%d' if t_interval_start.second==0 and t_interval_start.hour==0 else '%y-%m-%d %H:%M'
                track_name = f'{gpx_obj_namef} {t_interval_start:{fmt}}'
                gpx_track = gpx_track_create(gpx, track_name)
                gpx_track[track_name].segments.append(gpx_segment)
            else:
                gpx_track[gpx_obj_namef].segments.append(gpx_segment)

            for i, r in enumerate(nav_df_cur.itertuples()):
                Tcur = r.Index.to_pydatetime()
                gpx_point = GPXTrackPoint(
                    latitude=r.Lat, longitude=r.Lon,
                    elevation=r.DepEcho if b_have_depth and not np.isnan(r.DepEcho) else None,
                    time=Tcur)  # , speed= speed_b, comment= Comment
                gpx_segment.points.append(gpx_point)
                # if i==1:
                # gpx.description= gpx_obj_namef
                # gpx.author_email= '*****@*****.**'
                # gpxxml= gpx.to_xml()
                # tree = ET.parse(gpxxml)
                # root = tree.getroot()

            if cfg_proc.get('simplify_tracks_error_m'):
                try:
                    gpx_segment.points = gpxpy_simplify_polyline(gpx_segment.points,
                                                                 cfg_proc['simplify_tracks_error_m'])
                except RecursionError as e:
                    recursion_limit = sys.getrecursionlimit()
                    l.error('Check time in data! Why increasing old recursion limit (%s) is needed? Trying: x10...',
                            recursion_limit)
                    try:
                        sys.setrecursionlimit(recursion_limit * 10)
                        gpx_segment.points = gpxpy_simplify_polyline(gpx_segment.points,
                                                                     cfg_proc['simplify_tracks_error_m'])
                        l.warning('now track simplified successfuly')
                    except Exception as e:
                        l.exception('not succes. skip simplifying tracks', recursion_limit)

            if cfg_proc['dt_per_file'] and Tcur - Tprev > cfg_proc['dt_per_file']:  # save to next file
                part += 1
                if fileOutPN:
                    gpx_proc_and_save(gpx, gpx_obj_namef, cfg_proc, f'{fileOutPN}part{part}')
                gpx_track = gpx_track_create(gpx, gpx_obj_namef)
                Tprev = Tcur
        if fileOutPN:
            gpx_proc_and_save(gpx, gpx_obj_namef, cfg_proc, fileOutPN)

    return gpx
Example #28
0
def process():

    print csv.list_dialects()
    dr = DictReader(
        codecs.open(
            FILENAME,
            "rb",
            "utf-8",
            "ignore"
        ),
        dialect='excel-tab'
    )
#   lines = open(FILENAME).readlines()
#   print len(lines)
#   print lines[0]
#   print lines[1]
    print dr.fieldnames

    waypoints = []
    for index, row in enumerate(dr):

        def fixup_row(key):
            t = row[key]
#           t = t.replace(chr(0x92), "'")
#           t = t.replace(chr(0x93), '"')
#           t = t.replace(chr(0x94), '"')
#           t = t.replace(chr(0x95), "-")
#           t = t.replace(chr(0xA0), " ")
            return t

        name = u"STL%03d" % index
#       name = fixup_row('Site')

#       Code = u"STL%03d" % index

        description = fixup_row('Site')

        latlon = row['GPS']
        if latlon:
            lat, lon = latlon.split()
        else:
            lat, lon = (0.0, 0.0)

        comment = u"; ".join([
            fixup_row('Location on Site'),
            fixup_row('First Screen'),
            fixup_row('Street Address'),
            fixup_row('City'),
            fixup_row('ST'),
            fixup_row('Zip'),
        ])

        waypoint = GSAKWaypoint(
            lat,
            lon,
            name=name,
            description=description,
            comment=comment,
            type="Geocache|Unknown Cache",
#           Code=Code
        )
#       print repr(waypoint)

        waypoints.append(waypoint)

    # create the gpx file
    gpx = GPX(waypoints)
    gpx.author = "Robert L. Oelschlaeger"
    gpx.email = "*****@*****.**"
    gpx.creator = "cake.py"
    gpx.description = "Location of St. Louis 250th Birthday Celebration cakes"

    # write it
    outfile = codecs.open(OUTFILENAME, "wb", "utf-8", "replace")
#   print dir(outfile)
    outfile.write(gpx.to_xml())
    outfile.close()

    # tell user
    print "output is in %s" % OUTFILENAME
Example #29
0
import glob
from lxml import etree

from gpxpy.gpx import GPX

from scrappers.famousredwoods.redwoodhtmlpage import RedwoodHTMLPage

if __name__ == '__main__':
    redwood_html_pages = []
    redwood_html_pages += glob.glob("/misc/website-dl/famousredwoods.com/*.html")
    redwood_html_pages += glob.glob("/misc/website-dl/famousredwoods.com/*/index.html")

    gpx = GPX()

    for html_file in redwood_html_pages:
        try:
            redwood_html_page = RedwoodHTMLPage(html_file)
            gpx.waypoints.append(redwood_html_page.to_gpx_trackpoint())
        except (IndexError, AttributeError):
            print("Skipping " + html_file)

    gpx_string = etree.tostring(
        etree.fromstring(gpx.to_xml().encode('utf-8'), parser=etree.XMLParser(encoding='utf-8')),
        pretty_print=True).decode('utf-8')

    gpx_file = open('famousredwood-trees-with-descriptions.gpx', 'w')
    gpx_file.write(gpx_string)
Example #30
0
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)
Example #31
0
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())
Example #32
0
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())