Dan Albert 350f08be2f Add FastAPI interface between the game and map.
A possible explanation for the infrequent CTDs we've been seeing since
adding fast forward is that QWebChannel doesn't keep a reference to the
python objects that it passes to js, so if the object is GC'd before the
front end is done with it, it crashes.

We don't really like QWebChannel anyway, so this begins replacing that
with FastAPI.
2022-02-15 18:14:34 -08:00

48 lines
1.4 KiB
Python

from __future__ import annotations
from typing import Union
from dcs import Point
from shapely.geometry import LineString, MultiLineString, MultiPolygon, Polygon
from game.theater import ConflictTheater
LeafletLatLon = list[float]
LeafletPoly = list[LeafletLatLon]
class ShapelyUtil:
@staticmethod
def poly_to_leaflet(poly: Polygon, theater: ConflictTheater) -> LeafletPoly:
if poly.is_empty:
return []
return [
theater.point_to_ll(Point(x, y)).as_list() for x, y in poly.exterior.coords
]
@classmethod
def polys_to_leaflet(
cls, poly: Union[Polygon, MultiPolygon], theater: ConflictTheater
) -> list[LeafletPoly]:
if isinstance(poly, MultiPolygon):
polys = poly.geoms
else:
polys = [poly]
return [cls.poly_to_leaflet(poly, theater) for poly in polys]
@staticmethod
def line_to_leaflet(
line: LineString, theater: ConflictTheater
) -> list[LeafletLatLon]:
return [theater.point_to_ll(Point(x, y)).as_list() for x, y in line.coords]
@classmethod
def lines_to_leaflet(
cls, line_string: MultiLineString | LineString, theater: ConflictTheater
) -> list[list[LeafletLatLon]]:
if isinstance(line_string, MultiLineString):
lines = line_string.geoms
else:
lines = [line_string]
return [cls.line_to_leaflet(line, theater) for line in lines]