TecKnow / AppleHealthXMLtoCSV

A program for converting data exported from Apple Health from XML to CSV to facilitate analysis.
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Are the provided routes closed? #11

Closed TecKnow closed 2 years ago

TecKnow commented 2 years ago

The provided data represents dog walks, so presumably most of the routes should start and end at the same location. The distance between the first and last waypoints should be small. How often is this true?

TecKnow commented 2 years ago

The provided routes are often not closed. The average closing distance is hundreds of meters and the maximum is several kilometers. . I may ask the source about that but in the meantime I probably shouldn't implicitly close the routes.

Also note that at least one route file is properly structured but has no points. The file with a closing distance of exactly 0.0 meters also raises my curiosity but I haven't investigated.

import gpxpy
import geopy.distance
from pathlib import Path
from typing import Final
from statistics import mean

DEFAULT_DATA_DIRECTORY: Final = "./apple_health_export"
DATA_PATH: Final = Path(DEFAULT_DATA_DIRECTORY).joinpath("workout-routes")

if __name__ == "__main__":
    if not DATA_PATH.is_dir():
        print("Data path does not point to a directory.")
    closing_distances = list()
    for route_file_path in DATA_PATH.iterdir():
        with route_file_path.open() as route_file:
            gpx = gpxpy.parse(route_file)
            for track in gpx.tracks:
                for segment in track.segments:
                    if len(segment.points) < 2:
                        print(f"file {route_file_path} is too short")
                    else:
                        first_point_gpx = segment.points[0]
                        last_point_gpx = segment.points[-1]
                        first_point = (first_point_gpx.latitude, first_point_gpx.longitude)
                        last_point = (last_point_gpx.latitude, last_point_gpx.longitude)
                        closing_distance = geopy.distance.distance(first_point, last_point).meters
                        closing_distances.append(closing_distance)
    print(
        f"Closing distances:\nmin: {min(closing_distances)}, mean: {mean(closing_distances)}, max: {max(closing_distances)}")