Skip to content

Commit

Permalink
Add extrusion layer to express
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 2, 2024
1 parent 1f91856 commit 08ea1c9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
20 changes: 20 additions & 0 deletions examples/maplibre_express_fill_extrusion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from maplibre import express as mx
from maplibre.expressions import get_column, interpolate
from maplibre.settings import settings

settings.fallback_color = "#ffffff"
settings.fill_extrusion_color = "yellow"
settings.fill_extrusion_opacity = 0.5
settings.cmap = "YlOrRd"

# data = "https://docs.maptiler.com/sdk-js/assets/Mean_age_of_women_at_first_marriage_in_2019.geojson"
data = "https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_urban_areas.geojson"
# data = "https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_admin_1_states_provinces_scale_rank.geojson"
# data = "https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_1_states_provinces.geojson"
# data = "https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_railroads_north_america.geojson"

mx.fill_extrusion(data, fill_extrusion_height=get_column("area_sqkm")).color_category(
"scalerank"
).to_map(hash=True, pitch=45, center=(7.94, 51.539), zoom=5, fit_bounds=False).save(
"/tmp/py-maplibre-express.html"
)
28 changes: 24 additions & 4 deletions maplibre/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
color_quantile_step_expr,
color_step_expr,
geometry_type_filter,
get_column,
interpolate,
)
from maplibre.layer import Layer, LayerType
Expand Down Expand Up @@ -133,10 +134,13 @@ def to_map(
map_options: MapOptions = MapOptions(),
controls: list = None,
tooltip: bool = True,
fit_bounds: bool = True,
**kwargs,
) -> Map:
controls = controls or [NavigationControl()]
map_options.bounds = self.sf.bounds
if fit_bounds:
map_options.bounds = self.sf.bounds

m = Map(map_options, layers=[self], controls=controls, **kwargs)
if tooltip:
m.add_tooltip(self.id)
Expand Down Expand Up @@ -178,11 +182,27 @@ def line(data: gpd.GeoDataFrame | str, **kwargs) -> SimpleLayer:
)


def fill_extrusion(data: gpd.GeoDataFrame | str, **kwargs) -> SimpleLayer:
pass
def fill_extrusion(
data: gpd.GeoDataFrame | str,
fill_extrusion_base: int | float | list = None,
fill_extrusion_height: int | float | list = None,
**kwargs,
) -> SimpleLayer:
if "paint" not in kwargs:
kwargs["paint"] = settings.paint_props[
LayerType.FILL_EXTRUSION.value.replace("-", "_")
]

if fill_extrusion_base is not None:
kwargs["paint"]["fill-extrusion-base"] = fill_extrusion_base

if fill_extrusion_height is not None:
kwargs["paint"]["fill-extrusion-height"] = fill_extrusion_height

return SimpleLayer(type=LayerType.FILL_EXTRUSION, sf=data, **kwargs)


# TODO: Add default layers to settings
# TODO: Use default layers from settings
def fill_line_circle(source_id: str, colors: list = None) -> list:
if colors is not None:
assert len(colors) == 3
Expand Down
7 changes: 7 additions & 0 deletions maplibre/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Settings(BaseModel):
fill_opacity: Optional[float] = 0.5
fill_outline_color: Optional[str] = "#FFFFFF"

fill_extrusion_color: Optional[str] = "#3B9AB2"
fill_extrusion_opacity: Optional[float] = 1.0

line_color: Optional[str] = "#F21A00"
line_opacity: Optional[float] = 1.0
line_width: Optional[float] = 1.0
Expand Down Expand Up @@ -39,6 +42,10 @@ def paint_props(self) -> dict:
"fill-opacity": self.fill_opacity,
"fill-outline-color": self.fill_outline_color,
},
fill_extrusion={
"fill-extrusion-color": self.fill_extrusion_color,
"fill-extrusion-opacity": self.fill_extrusion_opacity,
},
line={
"line-color": self.line_color,
"line-opacity": self.line_opacity,
Expand Down

0 comments on commit 08ea1c9

Please sign in to comment.