Clean up leaflet polygon API surface.

This commit is contained in:
Dan Albert
2022-03-06 23:25:25 -08:00
parent 15176223fa
commit 6ee235545f
10 changed files with 53 additions and 65 deletions

View File

@@ -8,7 +8,7 @@ from pydantic import BaseModel
from game.server.combat.models import FrozenCombatJs
from game.server.flights.models import FlightJs
from game.server.frontlines.models import FrontLineJs
from game.server.leaflet import LeafletLatLon
from game.server.leaflet import LeafletPoint
if TYPE_CHECKING:
from game import Game
@@ -16,7 +16,7 @@ if TYPE_CHECKING:
class GameUpdateEventsJs(BaseModel):
updated_flight_positions: dict[UUID, LeafletLatLon]
updated_flight_positions: dict[UUID, LeafletPoint]
new_combats: list[FrozenCombatJs]
updated_combats: list[FrozenCombatJs]
ended_combats: list[UUID]
@@ -33,7 +33,7 @@ class GameUpdateEventsJs(BaseModel):
deleted_front_lines: set[UUID]
updated_tgos: set[UUID]
updated_control_points: set[int]
reset_on_map_center: LeafletLatLon | None
reset_on_map_center: LeafletPoint | None
game_unloaded: bool
new_turn: bool
@@ -55,14 +55,9 @@ class GameUpdateEventsJs(BaseModel):
for c in events.updated_combats
]
recenter_map = None
if events.reset_on_map_center is not None:
recenter_map = events.reset_on_map_center.as_list()
return GameUpdateEventsJs(
updated_flight_positions={
f[0].id: f[1].latlng().as_list()
for f in events.updated_flight_positions
f[0].id: f[1].latlng() for f in events.updated_flight_positions
},
new_combats=new_combats,
updated_combats=updated_combats,
@@ -84,7 +79,7 @@ class GameUpdateEventsJs(BaseModel):
deleted_front_lines=events.deleted_front_lines,
updated_tgos=events.updated_tgos,
updated_control_points=events.updated_control_points,
reset_on_map_center=recenter_map,
reset_on_map_center=events.reset_on_map_center,
game_unloaded=events.game_unloaded,
new_turn=events.new_turn,
)

View File

@@ -3,14 +3,12 @@ from __future__ import annotations
from typing import Union
from dcs import Point
from dcs.mapping import LatLng
from pydantic import BaseModel
from shapely.geometry import LineString, MultiLineString, MultiPolygon, Polygon
from game.theater import ConflictTheater
LeafletLatLon = list[float]
LeafletPoly = list[LeafletLatLon]
class LeafletPoint(BaseModel):
lat: float
@@ -22,13 +20,20 @@ class LeafletPoint(BaseModel):
title = "LatLng"
LeafletPoly = list[LeafletPoint]
class ShapelyUtil:
@staticmethod
def poly_to_leaflet(poly: Polygon, theater: ConflictTheater) -> LeafletPoly:
def latlng_to_leaflet(latlng: LatLng) -> LeafletPoint:
return LeafletPoint(lat=latlng.lat, lng=latlng.lng)
@classmethod
def poly_to_leaflet(cls, poly: Polygon, theater: ConflictTheater) -> LeafletPoly:
if poly.is_empty:
return []
return [
Point(x, y, theater.terrain).latlng().as_list()
cls.latlng_to_leaflet(Point(x, y, theater.terrain).latlng())
for x, y in poly.exterior.coords
]
@@ -43,15 +48,13 @@ class ShapelyUtil:
return [cls.poly_to_leaflet(poly, theater) for poly in polys]
@staticmethod
def line_to_leaflet(
line: LineString, theater: ConflictTheater
) -> list[LeafletLatLon]:
return [Point(x, y, theater.terrain).latlng().as_list() for x, y in line.coords]
def line_to_leaflet(line: LineString, theater: ConflictTheater) -> list[LatLng]:
return [Point(x, y, theater.terrain).latlng() for x, y in line.coords]
@classmethod
def lines_to_leaflet(
cls, line_string: MultiLineString | LineString, theater: ConflictTheater
) -> list[list[LeafletLatLon]]:
) -> list[list[LatLng]]:
if isinstance(line_string, MultiLineString):
lines = line_string.geoms
else: