Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Backend: kml color and waypoint fixes #129

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""drop_waypoints_unique_constraint

Revision ID: d53db4ccb3fc
Revises: 1a8780dd5bbc
Create Date: 2024-02-12 16:40:59.175048

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "d53db4ccb3fc"
down_revision: Union[str, None] = "1a8780dd5bbc"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.drop_constraint("waypoints_route_id_lat_lon_key", "waypoints", type_="unique")


def downgrade() -> None:
op.execute(
"""
DROP TABLE IF EXISTS public.waypoints;
"""
)
32 changes: 22 additions & 10 deletions backend/src/handlers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,6 @@ def get_kml(req: Request):
d = kml.Document(ns, "3.14", "Routes", "Routes for the OreCart app.")
k.append(d)

style = kml.Style(id="route-outline")
style.append_style(
LineStyle(color="ff0000ff", width=2)
) # Red outline in AABBGGRR hex format
style.append_style(PolyStyle(fill=0)) # No fill

for route in routes:
stop_ids = [
route_stop.stop_id
Expand All @@ -118,7 +112,18 @@ def get_kml(req: Request):

description = f"<![CDATA[{stop_divs}]]>"

p = kml.Placemark(ns, route.name, route.name, route.name, description)
style = kml.Style(id="route-outline")
style.append_style(
LineStyle(color="#00" + route.color[1:], width=2)
) # Red outline in AABBGGRR hex format
style.append_style(PolyStyle(fill=0)) # No fill
p = kml.Placemark(
ns=ns,
id=route.name,
name=route.name,
description=description,
styles=[style],
)
p.geometry = Polygon([(w.lon, w.lat, 0) for w in route.waypoints])

p.append_style(style)
Expand Down Expand Up @@ -274,14 +279,21 @@ async def create_route(req: Request, kml_file: UploadFile):
stop_id_map[stop_name] = stop_model.id

for route_name, route in routes.items():
route_model = Route(name=route_name)
# Get polygon color
color = "#000000"
for style in route.styles():
if isinstance(style, PolyStyle):
color = style.color
break
route_model = Route(name=route_name, color=color)
session.add(route_model)
session.flush()

added = set()

for coords in route.geometry.exterior.coords:
if coords in added:
for i, coords in enumerate(route.geometry.exterior.coords):
# Ignore dupes except for the last one that completes the polygon
if coords in added and i != len(route.geometry.exterior.coords) - 1:
print(f"skipping duplicate {coords}")
continue
print(f"adding {coords}")
Expand Down
5 changes: 1 addition & 4 deletions backend/src/model/waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

class Waypoint(Base):
__tablename__ = "waypoints"
__table_args__ = (
ForeignKeyConstraint(["route_id"], ["routes.id"]),
UniqueConstraint("route_id", "lat", "lon"),
)
__table_args__ = (ForeignKeyConstraint(["route_id"], ["routes.id"]),)
id: Mapped[int] = mapped_column(
primary_key=True, autoincrement=True, nullable=False
)
Expand Down
Loading