diff --git a/examples/maplibre_express_fill_extrusion.py b/examples/maplibre_express_fill_extrusion.py new file mode 100644 index 0000000..09ba5ee --- /dev/null +++ b/examples/maplibre_express_fill_extrusion.py @@ -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" +) diff --git a/maplibre/express.py b/maplibre/express.py index 43e5200..23add1c 100644 --- a/maplibre/express.py +++ b/maplibre/express.py @@ -13,6 +13,7 @@ color_quantile_step_expr, color_step_expr, geometry_type_filter, + get_column, interpolate, ) from maplibre.layer import Layer, LayerType @@ -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) @@ -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 diff --git a/maplibre/settings.py b/maplibre/settings.py index d044425..5b7f43b 100644 --- a/maplibre/settings.py +++ b/maplibre/settings.py @@ -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 @@ -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,