Skip to content

Commit

Permalink
Merge pull request #54 from wmo-raf/develop
Browse files Browse the repository at this point in the history
Implement adding stations to geomanager datasets api list
  • Loading branch information
erick-otenyo authored Nov 10, 2023
2 parents 664a195 + 0905b2c commit 60bcd16
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2.3 on 2023-11-10 09:27

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('geomanager', '0030_delete_stationsettings'),
('stations', '0002_stationsettings'),
]

operations = [
migrations.AddField(
model_name='stationsettings',
name='geomanager_layer_metadata',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='geomanager.metadata', verbose_name='Stations Layer Metadata'),
),
migrations.AddField(
model_name='stationsettings',
name='geomanager_subcategory',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='geomanager.subcategory', verbose_name='Stations Layer SubCategory'),
),
migrations.AddField(
model_name='stationsettings',
name='layer_title',
field=models.CharField(blank=True, default='Stations', max_length=100, null=True, verbose_name='Stations Layer Title'),
),
migrations.AddField(
model_name='stationsettings',
name='show_on_mapviewer',
field=models.BooleanField(default=False, help_text='Check to show stations data on Mapviewer', verbose_name='Show on Mapviewer'),
),
]
26 changes: 26 additions & 0 deletions pages/stations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from django_tables2 import tables, LazyPaginator, TemplateColumn
from geomanager.fields import ListField
from geomanager.models import SubCategory, Metadata
from geomanager.utils.vector_utils import get_model_field
from wagtail.admin.panels import FieldPanel
from wagtail.contrib.routable_page.models import path, RoutablePageMixin
from wagtail.contrib.settings.models import BaseSiteSetting
from wagtail.contrib.settings.registry import register_setting
from wagtail.models import Page

from base.mixins import MetadataPageMixin


@register_setting(name="station-settings")
class StationSettings(BaseSiteSetting):
stations_table_name = "stations_station"
db_schema = "public"
Expand All @@ -21,10 +26,31 @@ class StationSettings(BaseSiteSetting):
bounds = ListField(max_length=256, blank=True, null=True)
name_column = models.CharField(max_length=100, blank=True, null=True)

show_on_mapviewer = models.BooleanField(default=False, verbose_name=_("Show on Mapviewer"),
help_text=_("Check to show stations data on Mapviewer"))
layer_title = models.CharField(max_length=100, blank=True, null=True, default="Stations",
verbose_name=_("Stations Layer Title"))
geomanager_subcategory = models.ForeignKey(SubCategory, null=True, blank=True,
verbose_name=_("Stations Layer SubCategory"),
on_delete=models.SET_NULL)
geomanager_layer_metadata = models.ForeignKey(Metadata, on_delete=models.SET_NULL, blank=True, null=True,
verbose_name=_("Stations Layer Metadata"))

panels = [
FieldPanel("show_on_mapviewer"),
FieldPanel("layer_title"),
FieldPanel("geomanager_subcategory"),
FieldPanel("geomanager_layer_metadata"),
]

@cached_property
def full_table_name(self):
return f"{self.db_schema}.{self.stations_table_name}"

@property
def admin_url(self):
return reverse("wagtailsettings:edit", args=[self._meta.app_label, self._meta.model_name, ])

@cached_property
def stations_vector_tiles_url(self):
base_url = reverse("station_tiles", args=(0, 0, 0)).replace("/0/0/0", r"/{z}/{x}/{y}")
Expand Down
74 changes: 74 additions & 0 deletions pages/stations/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
def create_stations_geomanager_dataset(station_settings, request=None):
sub_category = station_settings.geomanager_subcategory

if not sub_category:
return None

title = station_settings.layer_title
metadata = station_settings.geomanager_layer_metadata
popup_fields = station_settings.station_popup_columns_list

dataset_id = "country_stations"

dataset = {
"id": dataset_id,
"dataset": dataset_id,
"name": title,
"layer": dataset_id,
"category": sub_category.category.pk,
"sub_category": sub_category.pk,
"public": True,
"layers": [

]
}
if metadata:
dataset.update({"metadata": metadata.pk})

station_tiles_url = station_settings.stations_vector_tiles_url

if request:
station_tiles_url = request.scheme + '://' + request.get_host() + station_tiles_url

layer = {
"id": dataset_id,
"name": title,
"layerConfig": {
"type": "vector",
"source": {
"type": "vector",
"tiles": [station_tiles_url],
},
"render": {
"layers": [
{
"type": "circle",
"source-layer": "default",
'paint': {
"circle-color": "#adefd1",
"circle-radius": 8,
"circle-stroke-width": 4,
"circle-stroke-color": "#00203F",
}}
]
}
},
"legendConfig": {}
}

if popup_fields:
interactionConfig = {
"output": []
}
for field in popup_fields:
interactionConfig["output"].append({
"column": field.get("name"),
"property": field.get("label"),
"type": "string"
})

layer.update({"interactionConfig": interactionConfig})

dataset["layers"].append(layer)

return dataset
30 changes: 29 additions & 1 deletion pages/stations/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from wagtail import hooks
from wagtail.admin.menu import MenuItem

from .models import StationSettings
from .utils import create_stations_geomanager_dataset
from .views import load_stations, preview_stations


Expand All @@ -14,7 +16,33 @@ def urlconf_stations():
]


@hooks.register('construct_settings_menu')
def hide_settings_menu_item(request, menu_items):
hidden_settings = ["station-settings"]
menu_items[:] = [item for item in menu_items if item.name not in hidden_settings]


@hooks.register('register_geo_manager_menu_item')
def add_stations_to_geomanager():
def add_stations_loader_to_geomanager():
stations_data = MenuItem(label=_("Stations Data"), url=reverse("preview_stations"), icon_name="map")
return stations_data


@hooks.register('register_geo_manager_menu_item')
def add_stations_settings_to_geomanager():
settings_url = reverse("wagtailsettings:edit",
args=[StationSettings._meta.app_label, StationSettings._meta.model_name, ], )
station_settings_menu = MenuItem(label=_("Station Settings"), url=settings_url, icon_name="cog")
return station_settings_menu


@hooks.register('register_geomanager_datasets')
def add_geomanager_datasets(request):
datasets = []
station_settings = StationSettings.for_request(request)
if station_settings.show_on_mapviewer and station_settings.geomanager_subcategory:
dataset = create_stations_geomanager_dataset(station_settings, request)
if dataset:
datasets.append(dataset)

return datasets
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fonttools==4.39.4
forecastmanager==0.2.0
frozenlist==1.3.3
future==0.18.3
geomanager==0.3.2
geomanager==0.3.3
geopandas==0.13.0
google-api-core==2.11.0
google-api-python-client==2.79.0
Expand Down

0 comments on commit 60bcd16

Please sign in to comment.