Add insert locations

This commit is contained in:
2024-08-30 22:52:33 +02:00
parent 58b9f42799
commit 2e9d88c8a8
4 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
import argparse
import asyncio
import json
import sys
from datetime import datetime
from pathlib import Path
current_file_path = Path(__file__).resolve().parent
sys.path.append(str(current_file_path / ".." / ".." / ".."))
from src.util.location_recorder import LocationData, LocationRecorder # noqa: E402
# Create an argument parser
parser = argparse.ArgumentParser(description="Google Location Reader")
# Add an argument for the JSON file path
parser.add_argument("--json-file", type=str, help="Path to the JSON file")
# Parse the command-line arguments
args = parser.parse_args()
json_file_path: str = args.json_file
db_path = current_file_path / ".." / ".." / ".." / "temp_data" / "test.db"
location_recorder = LocationRecorder(db_path=str(db_path))
# Open the JSON file
with Path.open(json_file_path) as json_file:
data = json.load(json_file)
locations: list[dict] = data["locations"]
print(type(locations), len(locations))
async def insert() -> None:
nr_waypoints = 0
await location_recorder.create_db_engine()
locations_dict: dict[datetime, LocationData] = {}
for location in locations:
nr_waypoints += 1
try:
latitude: float = location["latitudeE7"] / 1e7
longitude: float = location["longitudeE7"] / 1e7
except KeyError:
continue
altitude: float = location.get("altitude", None)
try:
date_time = datetime.strptime(location["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z")
except ValueError:
date_time = datetime.strptime(location["timestamp"], "%Y-%m-%dT%H:%M:%S%z")
locations_dict[date_time] = LocationData(
latitude=latitude,
longitude=longitude,
altitude=altitude,
)
await location_recorder.insert_locations("Tianyu", locations=locations_dict)
print(nr_waypoints)
await location_recorder.dispose_db_engine()
asyncio.run(insert())

View File

@@ -0,0 +1,44 @@
import argparse
import asyncio
import sys
from datetime import UTC
from pathlib import Path
import gpxpy
import gpxpy.gpx
current_file_path = Path(__file__).resolve().parent
sys.path.append(str(current_file_path / ".." / ".." / ".."))
from src.util.location_recorder import LocationData, LocationRecorder # noqa: E402
parser = argparse.ArgumentParser(description="GPX Location Reader")
parser.add_argument("--gpx-file", type=str, help="Path to the GPX file")
args = parser.parse_args()
gpx_location = args.gpx_file
gpx_file = Path.open(gpx_location)
gpx = gpxpy.parse(gpx_file)
db_path = current_file_path / ".." / ".." / ".." / "temp_data" / "test.db"
location_recorder = LocationRecorder(db_path=str(db_path))
async def iterate_and_insert() -> None:
nr_waypoints = 0
await location_recorder.create_db_engine()
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
nr_waypoints += 1
print(f"Point at ({point.latitude},{point.longitude}) -> {point.time}")
point.time = point.time.replace(tzinfo=UTC)
location_data = LocationData(latitude=point.latitude, longitude=point.longitude, altitude=point.elevation)
await location_recorder.insert_location(person="Tianyu", date_time=point.time, location=location_data)
await location_recorder.dispose_db_engine()
print(nr_waypoints)
asyncio.run(iterate_and_insert())