Skip to content

Commit

Permalink
Add bounds property to layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Jul 22, 2024
1 parent fb97161 commit 4a0fa33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
33 changes: 30 additions & 3 deletions _experimental/simple_layers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from __future__ import annotations

from typing import TypeVar

from maplibre import Layer, LayerType
from maplibre._utils import fix_keys
from maplibre.sources import GeoDataFrame, Source

MLSource = TypeVar("MLSource", Source, dict, GeoDataFrame)

DEFAULT_COLOR = "darkred"

Expand Down Expand Up @@ -34,16 +39,17 @@ class Line(SimpleLayer):
def __init__(
self,
line_color: str | list = None,
line_width: int | float = None,
line_width: int | float = 1,
line_opacity: float = 1.0,
source: MLSource = None,
id: str = None,
**kwargs,
):
line_color = line_color or DEFAULT_COLOR
paint = dict(
line_color=line_color, line_width=line_width, line_opacity=line_opacity
)
super().__init__(LayerType.LINE, paint, id, **kwargs)
super().__init__(LayerType.LINE, paint, id, source=source, **kwargs)


class Fill(SimpleLayer):
Expand All @@ -61,10 +67,31 @@ def __init__(


class Circle(SimpleLayer):
pass
def __init__(self, circle_radius: int | float = 1.0, id: str = None, **kwargs):
pass


class TestLayer(Layer):
def set_paint_properties(self, **props) -> TestLayer:
self.paint.update(fix_keys(props))
return self

def set_paint_properties_(
self,
line_color: str | list = None,
line_opacity: int | float = 1.0,
line_width: int | float | list = None,
fill_color: str | list = None,
fill_opacity: int | float = 1.0,
fill_outline_color: str | list = None,
**kwargs,
) -> TestLayer:
props = dict(
filter(lambda item: item[0].startswith(self.type), locals().items())
)
if self.paint is None:
self.paint = fix_keys(props)
else:
self.paint.update(props)

return self
11 changes: 11 additions & 0 deletions maplibre/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ._core import MapLibreBaseModel
from ._utils import fix_keys
from .sources import SimpleFeatures, Source
from .utils import get_bounds

try:
from geopandas import GeoDataFrame
Expand Down Expand Up @@ -94,3 +95,13 @@ def fix_paint(cls, v):
return fix_keys(v)

return v

@property
def bounds(self) -> tuple | None:
try:
bounds = get_bounds(self.source["data"])
except Exception as e:
# print(e)
bounds = None

return bounds
4 changes: 2 additions & 2 deletions maplibre/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def df_to_geojson(
return geojson


def get_bounds(geojson: dict) -> list:
def get_bounds(geojson: dict) -> tuple | None:
try:
import shapely
except ImportError as e:
print(e)
return

return list(shapely.bounds(shapely.from_geojson(json.dumps(geojson))))
return tuple(shapely.bounds(shapely.from_geojson(json.dumps(geojson))))

0 comments on commit 4a0fa33

Please sign in to comment.