From 4984741dbcd655966c1d475370326987843d0732 Mon Sep 17 00:00:00 2001 From: Felix Horvat Date: Wed, 4 Dec 2024 16:09:52 +0100 Subject: [PATCH] fixed issue with Color being mutable * improved README * Color is now frozen, so mutable issues with dataclass are avoided --- README.md | 11 +++++------ mapsy/color.py | 19 ++++++------------- mapsy/layer/symbol_layer.py | 4 ++-- pyproject.toml | 2 +- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 94bc3f5..bb42cb9 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,11 @@ The Mapsy library is designed to be simple to use. The following sections provid ### Creating a simple Map -Here is an example of how to create a simple map with a filled polygon: +Here is an example of how to create a simple map with a tiled raster layer: ```python import mapsy + my_map = mapsy.Map() tile_layer = mapsy.TiledRasterLayer( [ @@ -52,20 +53,18 @@ tile_layer = mapsy.TiledRasterLayer( ] ) my_map.add_layer(tile_layer) -my_map.add_layer(Attribution("© OpenStreetMap contributors")) +my_map.add_layer(mapsy.Attribution("© OpenStreetMap contributors")) surf = my_map.render( mapsy.FixedScreenSize( - Box.from_lng_lat(5.988, 47.302, 15.016, 54.983), mapsy.ScreenSize(512, 512) + mapsy.Box.from_lng_lat(5.988, 47.302, 15.016, 54.983), mapsy.ScreenSize(512, 512) ) ) surf.write_to_png("my_map.png") ``` -If you want to use the map on a public place be sure to include proper attribution. This code will generate a map with a filled polygon and save it as `simple_map.png`. - - +### Layers #### Background Layer A background layer provides a solid color background for the map. diff --git a/mapsy/color.py b/mapsy/color.py index 09ab96f..42ad58e 100644 --- a/mapsy/color.py +++ b/mapsy/color.py @@ -1,8 +1,9 @@ import colorsys from dataclasses import dataclass +from functools import cached_property -@dataclass +@dataclass(frozen=True) class Color: r: float g: float @@ -11,27 +12,19 @@ class Color: @property def h(self) -> float: - self._ensure_hsv_exists() return self.hsv[0] @property def s(self) -> float: - self._ensure_hsv_exists() - return self.hsv[2] + return self.hsv[1] @property def v(self) -> float: - self._ensure_hsv_exists() - return self.hsv[3] + return self.hsv[2] - @property + @cached_property def hsv(self) -> tuple[float, float, float]: - self._ensure_hsv_exists() - return self.hsv - - def _ensure_hsv_exists(self): - if self._hsv is None: - self._hsv = colorsys.rgb_to_hsv(self.r, self.g, self.b) + return colorsys.rgb_to_hsv(self.r, self.g, self.b) def float_rgb(self) -> tuple[float, float, float]: return self.r, self.g, self.b diff --git a/mapsy/layer/symbol_layer.py b/mapsy/layer/symbol_layer.py index 81a0c51..cb81049 100644 --- a/mapsy/layer/symbol_layer.py +++ b/mapsy/layer/symbol_layer.py @@ -2,7 +2,7 @@ from affine import Affine from shapely import MultiPoint, Point, affinity -from mapsy.color import Color +from mapsy.color import Color, Colors from mapsy.common import FontSlant, FontWeight, TextAnchor from mapsy.icon.icon import Icon from mapsy.layer.layer import Layer @@ -22,7 +22,7 @@ class SymbolItem: text_offset: tuple[float, float] = (0, 0) text_font: str = "arial" text_size: float = 12 - text_color: Color | None = Color(0, 0, 0) + text_color: Color | None = Colors.BLACK text_slant: FontSlant | None = None text_weight: FontWeight | None = None text_anchor: TextAnchor | None = None diff --git a/pyproject.toml b/pyproject.toml index b6fba4f..aecefa2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "mapsy" -version = "0.2.1" +version = "0.3.0" description = "Mapsy is a Python library designed easily render static maps in python." authors = ["Felix Horvat "] readme = "README.md"