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

@@ -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: