Skip to content

Commit

Permalink
Implement multiple basemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
henhuy committed Apr 24, 2024
1 parent 0a17efd commit ec0dd21
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 60 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and the versioning aim to respect [Semantic Versioning](http://semver.org/spec/v

Here is a template for new release sections

## [Unreleased]
### Added
- multiple basemaps possible

## [1.4.1] - 2024-04-08
### Added
- initial cluster docs
Expand Down
11 changes: 10 additions & 1 deletion django_mapengine/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from appconf import AppConf
from django.conf import settings

from . import choropleth
from . import choropleth, setup

env = environ.Env()

Expand Down Expand Up @@ -66,6 +66,15 @@ class MapEngineConf(AppConf):

# MVTS and CLUSTERS

BASEMAPS = [
setup.MaptilerBasemap(
"satellite",
source_id="satellite",
type="raster",
image="django_mapengine/images/layer_ctrl_satellite.svg",
description="Satellite basemap view",
)
]
API_MVTS = {}
API_CLUSTERS = []

Expand Down
27 changes: 27 additions & 0 deletions django_mapengine/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@
from django_mapengine import setup


@dataclass
class BasemapLayer:
"""Default map layer used in maplibre."""

# pylint:disable=C0103
id: str # noqa: A003
source: str
type: str # noqa: A003

def get_layer(self):
"""
Build dict from layer settings and style.
Returns
-------
dict
to be used as layer in maplibre.
"""
return {"id": self.id, "source": self.source, "type": self.type}


@dataclass
class MapLayer:
"""Default map layer used in maplibre."""
Expand Down Expand Up @@ -163,6 +184,12 @@ def get_map_layers(self) -> Iterable[MapLayer]:
)


def get_basemap_layers() -> Iterable[BasemapLayer]:
"""Return basemap layers"""
for basemap in settings.MAP_ENGINE_BASEMAPS:
yield BasemapLayer(id=basemap.layer_id, source=basemap.layer_id, type=basemap.type)


def get_region_layers() -> Iterable[MapLayer]:
"""
Return map layers for region-based models.
Expand Down
30 changes: 30 additions & 0 deletions django_mapengine/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@
Zoom = namedtuple("MinMax", ("min", "max"))


@dataclass
class MaptilerBasemap:
"""
Base class for a basemap
This is used to:
- prepare basemap layers
- prepare basemap sources
"""

layer_id: str
source_id: str
description: str
image: str
type: str = "vector" # noqa: A003
format: str = "jpg"

def as_dict(self):
"""Return maptilerBasemap as dict"""
return {
"layer_id": self.layer_id,
"source_id": self.source_id,
"description": self.description,
"image": self.image,
"type": self.type,
"format": self.format,
}


@dataclass
class ModelAPI:
"""
Expand Down Expand Up @@ -45,6 +74,7 @@ def model(self) -> "Model":
@dataclass
class ClusterAPI(ModelAPI):
"""Exists only to distinguish between "normal" and clustered API"""

properties: list = field(default_factory=lambda: [])


Expand Down
21 changes: 11 additions & 10 deletions django_mapengine/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def get_cluster_sources() -> Iterable[MapSource]:
)


def get_satellite_source() -> MapSource:
def get_satellite_sources() -> Iterable[MapSource]:
"""
Return source for satellite basemap
Expand All @@ -163,14 +163,15 @@ def get_satellite_source() -> MapSource:
MapSource
of satellite raster
"""
return MapSource(
"satellite",
type="raster",
tiles=[
"https://api.maptiler.com/tiles/satellite-v2/"
f"{{z}}/{{x}}/{{y}}.jpg?key={settings.MAP_ENGINE_TILING_SERVICE_TOKEN}",
],
)
for basemap in settings.MAP_ENGINE_BASEMAPS:
yield MapSource(
basemap.layer_id,
type=basemap.type,
tiles=[
f"https://api.maptiler.com/maps/{basemap.source_id}/"
f"{{z}}/{{x}}/{{y}}.{basemap.format}?key={settings.MAP_ENGINE_TILING_SERVICE_TOKEN}",
],
)


def get_all_sources() -> List[MapSource]:
Expand All @@ -183,7 +184,7 @@ def get_all_sources() -> List[MapSource]:
all map sources
"""
sources = list(get_region_sources())
sources.append(get_satellite_source())
sources.extend(get_satellite_sources())
sources.extend(get_static_sources())
sources.extend(get_cluster_sources())
return sources
115 changes: 115 additions & 0 deletions django_mapengine/static/django_mapengine/images/layer_ctrl_default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ec0dd21

Please sign in to comment.