Skip to content

Commit

Permalink
fixed issue with Color being mutable
Browse files Browse the repository at this point in the history
 * improved README
 * Color is now frozen, so mutable issues with
 dataclass are avoided
  • Loading branch information
felix-ht committed Dec 4, 2024
1 parent eb1f1c4 commit 4984741
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,30 @@ 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(
[
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
]
)
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.
Expand Down
19 changes: 6 additions & 13 deletions mapsy/color.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mapsy/layer/symbol_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 4984741

Please sign in to comment.