Пример #1
0
def filter_gpx(gpx_content: GPX,
               filter: Callable[[GPXTrackPoint, GPXTrackPoint], bool]) -> GPX:
    removed_points_count = 0
    removed_segments_count = 0
    removed_tracks_count = 0

    new_tracks = []
    for track in tqdm(
            gpx_content.tracks,
            desc="Removing points using specified filter: {}".format(filter)):
        new_segments = []

        for segment in track.segments:
            new_points = []
            last_point = None
            for point in segment.points:
                if filter(last_point, point):
                    new_points.append(point)
                last_point = point

            if len(new_points) < len(segment.points):
                removed_points_count += len(segment.points) - len(new_points)
                segment.points = new_points

            if len(new_points) != 0:
                new_segments.append(segment)

        if len(new_segments) < len(track.segments):
            removed_segments_count += len(track.segments) - len(new_segments)
            track.segments = new_segments

        if len(new_segments) != 0:
            new_tracks.append(track)

    if len(new_tracks) < len(gpx_content.tracks):
        removed_tracks_count += len(gpx_content.tracks) - len(new_tracks)
        gpx_content.tracks = new_tracks

    print("Removed points: {}".format(removed_points_count))
    print("Removed segments: {}".format(removed_segments_count))
    print("Removed tracks: {}".format(removed_tracks_count))

    return gpx_content
Пример #2
0
    def iterparse(self) -> GPX:
        """
        Incremental reading for large gpx files using xml.etree.ElementTree.iterparse().
        Loads data from filehandler to gpx object.

        :return: gpx with loaded data
        """
        points: List[GPXTrackPoint] = []
        segments: List[GPXTrackSegment] = []
        tracks: List[GPXTrack] = []
        name: str = ''
        number: str = ''
        time: str = ''
        file_size = fstat(self._source.fileno()).st_size
        with tqdm(total=file_size,
                  unit_scale=True,
                  unit='b',
                  desc="Loading gpx") as pbar:
            for _, elem in iterparse(self._source):
                if 'name' in elem.tag:
                    name = elem.text
                elif 'number' in elem.tag:
                    number = elem.text
                elif 'time' in elem.tag:
                    time = elem.text
                elif 'trkpt' in elem.tag:
                    points.append(
                        GPXTrackPoint(float(elem.attrib['lat']),
                                      float(elem.attrib['lon']), time))
                elif 'trkseg' in elem.tag:
                    segments.append(GPXTrackSegment([p for p in points]))
                    points.clear()
                elif 'trk' in elem.tag:
                    tracks.append(GPXTrack(name, number,
                                           [s for s in segments]))
                    segments.clear()
                elem.clear()
                pbar.update(52)
            self._gpx = GPX(tracks=tracks)
        return self._gpx
Пример #3
0
def save(
    fn: str,
    gpx: GPX,
) -> None:
    with open(fn, 'w') as fh:
        gpx.write_to_file(fh)
Пример #4
0
 def __init__(self, file: IO) -> None:
     self._source: IO = file
     self._gpx: GPX = GPX()
Пример #5
0
def save_gpx(data: GPX, filepath: str):
    print_info("Saving GPX file to: {}".format(os.path.realpath(filepath)))
    with open(filepath, 'w') as outfile:
        # outfile.write(data.to_xml())
        data.write_to_file(outfile)
    print_info("{} tracks saved".format(len(data.tracks)))