Refactor tree drawing.

This commit is contained in:
Sergey Vartanov 2022-01-26 09:44:08 +03:00
parent 60836bf2f7
commit 710e3b0980
3 changed files with 37 additions and 36 deletions

View file

@ -17,8 +17,8 @@ from map_machine.feature.direction import DirectionSector
from map_machine.feature.road import Road, Roads from map_machine.feature.road import Road, Roads
from map_machine.figure import ( from map_machine.figure import (
StyledFigure, StyledFigure,
Tree,
) )
from map_machine.feature.tree import Tree
from map_machine.geometry.flinger import Flinger from map_machine.geometry.flinger import Flinger
from map_machine.map_configuration import DrawingMode, MapConfiguration from map_machine.map_configuration import DrawingMode, MapConfiguration
from map_machine.osm.osm_reader import ( from map_machine.osm.osm_reader import (

View file

@ -0,0 +1,35 @@
import numpy as np
from colour import Color
from svgwrite import Drawing
from map_machine.geometry.flinger import Flinger
from map_machine.osm.osm_reader import Tagged
from map_machine.scheme import Scheme
class Tree(Tagged):
"""Tree on the map."""
def __init__(
self, tags: dict[str, str], coordinates: np.ndarray, point: np.ndarray
) -> None:
super().__init__(tags)
self.coordinates: np.ndarray = coordinates
self.point: np.ndarray = point
def draw(self, svg: Drawing, flinger: Flinger, scheme: Scheme) -> None:
"""Draw crown and trunk."""
scale: float = flinger.get_scale(self.coordinates)
radius: float
if diameter_crown := self.get_float("diameter_crown") is not None:
radius = diameter_crown / 2.0
else:
radius = 2.0
color: Color = scheme.get_color("evergreen_color")
svg.add(svg.circle(self.point, radius * scale, fill=color, opacity=0.3))
if (circumference := self.get_float("circumference")) is not None:
radius: float = circumference / 2.0 / np.pi
svg.add(svg.circle(self.point, radius * scale, fill="#B89A74"))

View file

@ -1,15 +1,11 @@
""" """
Figures displayed on the map. Figures displayed on the map.
""" """
from typing import Optional
import numpy as np import numpy as np
from colour import Color
from svgwrite import Drawing
from map_machine.geometry.flinger import Flinger from map_machine.geometry.flinger import Flinger
from map_machine.osm.osm_reader import OSMNode, Tagged from map_machine.osm.osm_reader import OSMNode, Tagged
from map_machine.scheme import LineStyle, Scheme from map_machine.scheme import LineStyle
__author__ = "Sergey Vartanov" __author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru" __email__ = "me@enzet.ru"
@ -67,36 +63,6 @@ class StyledFigure(Figure):
self.line_style: LineStyle = line_style self.line_style: LineStyle = line_style
class Tree(Tagged):
"""Tree on the map."""
def __init__(
self, tags: dict[str, str], coordinates: np.ndarray, point: np.ndarray
) -> None:
super().__init__(tags)
self.coordinates: np.ndarray = coordinates
self.point: np.ndarray = point
def draw(self, svg: Drawing, flinger: Flinger, scheme: Scheme) -> None:
"""Draw crown and trunk."""
scale: float = flinger.get_scale(self.coordinates)
diameter_crown: Optional[float] = self.get_float("diameter_crown")
radius: float
if diameter_crown is not None:
radius = float(self.tags["diameter_crown"]) / 2.0
else:
radius = 2.0
color: Color = scheme.get_color("evergreen_color")
svg.add(svg.circle(self.point, radius * scale, fill=color, opacity=0.3))
circumference: Optional[float] = self.get_float("circumference")
if circumference is not None:
radius: float = circumference / 2.0 / np.pi
svg.add(svg.circle(self.point, radius * scale, fill="#B89A74"))
def is_clockwise(polygon: list[OSMNode]) -> bool: def is_clockwise(polygon: list[OSMNode]) -> bool:
""" """
Return true if polygon nodes are in clockwise order. Return true if polygon nodes are in clockwise order.