Stahovani elevace
This commit is contained in:
+13
-32
@@ -1,11 +1,13 @@
|
||||
import asyncio
|
||||
import sys
|
||||
from sqlalchemy import select
|
||||
|
||||
sys.path.insert(0, "/app/src")
|
||||
from paths import FLIGHT_GPX_TRACK_PATH # noqa
|
||||
from database import async_session, models # noqa
|
||||
from external.elevation import elevation_api # noqa
|
||||
from external.elevation import elevation_api, update_track_points_elevation # noqa
|
||||
from external.gpx_parser import GPXParser # noqa
|
||||
from database.transaction import get_session
|
||||
|
||||
|
||||
async def add_elevation_to_photos():
|
||||
@@ -32,44 +34,23 @@ async def add_elevation_to_photos():
|
||||
|
||||
|
||||
async def add_elevation_to_tracks():
|
||||
async with async_session() as session:
|
||||
flights = (await session.scalars(
|
||||
select(models.Flight)
|
||||
.filter(models.Flight.has_terrain_elevation.is_(False))
|
||||
.filter(models.Flight.gpx_track_filename.isnot(None))
|
||||
async with get_session() as db:
|
||||
track_points = (await db.session.scalars(
|
||||
select(models.TrackPoint)
|
||||
.filter(models.TrackPoint.terrain_elevation.is_(None))
|
||||
)).all()
|
||||
|
||||
if not flights:
|
||||
if not track_points:
|
||||
print("all done")
|
||||
return
|
||||
|
||||
for flight in flights:
|
||||
gpx_file = f"{FLIGHT_GPX_TRACK_PATH}/{flight.gpx_track_filename}"
|
||||
try:
|
||||
gpx = GPXParser(gpx_file)
|
||||
except OSError:
|
||||
print(f"ERROR, cannot open {gpx_file=}, skipping")
|
||||
continue
|
||||
|
||||
coordinates = await gpx.get_coordinates_all()
|
||||
elevation = await elevation_api.get_elevation_for_points(coordinates)
|
||||
|
||||
gpx_with_elevation = gpx.add_terrain_elevation(elevation)
|
||||
|
||||
output_name = f"terrain_{flight.gpx_track_filename[-30:]}"
|
||||
gpx.write(gpx_with_elevation, output=f"{FLIGHT_GPX_TRACK_PATH}/{output_name}")
|
||||
await models.Flight.update(
|
||||
db_session=session, obj=flight, data={
|
||||
"has_terrain_elevation": True,
|
||||
"gpx_track_filename": output_name
|
||||
}
|
||||
)
|
||||
await session.flush()
|
||||
await session.commit()
|
||||
await update_track_points_elevation(db, track_points)
|
||||
|
||||
|
||||
async def run_all():
|
||||
await asyncio.gather(add_elevation_to_photos(), add_elevation_to_tracks())
|
||||
await asyncio.gather(
|
||||
add_elevation_to_photos(),
|
||||
add_elevation_to_tracks()
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user