From ce4cd331392a2aa3b440bdba6d8925159acd45da Mon Sep 17 00:00:00 2001 From: SNoiraud Date: Sun, 7 Jul 2019 19:24:44 +0200 Subject: [PATCH] Narrative web: Add Stamen map Fixes #05984 --- gramps/plugins/webreport/common.py | 50 +++++++++++++++++++++++- gramps/plugins/webreport/narrativeweb.py | 20 ++++++++++ gramps/plugins/webreport/person.py | 31 ++++++++++++--- gramps/plugins/webreport/place.py | 25 ++++++++---- 4 files changed, 111 insertions(+), 15 deletions(-) diff --git a/gramps/plugins/webreport/common.py b/gramps/plugins/webreport/common.py index 3d47d92ed..1f733f940 100644 --- a/gramps/plugins/webreport/common.py +++ b/gramps/plugins/webreport/common.py @@ -171,6 +171,10 @@ MARKERS = """ }""" # javascript for OpenStreetMap's markers... +""" +https://openlayers.org/en/latest/examples/ +""" + OSM_MARKERS = """ function initialize(){ var map; @@ -198,12 +202,56 @@ OSM_MARKERS = """ style: iconStyle }); var centerCoord = new ol.proj.transform([%s, %s], 'EPSG:4326', 'EPSG:3857'); - map= new ol.Map({ + map = new ol.Map({ target: 'map_canvas', layers: [new ol.layer.Tile({ source: new ol.source.OSM() }), markerLayer], view: new ol.View({ center: centerCoord, zoom: %d }) }); +""" + +STAMEN_MARKERS = """ + function initialize(){ + var map; + var tracelife = %s; + var layer = '%s'; + var iconStyle = new ol.style.Style({ + image: new ol.style.Icon(({ + //anchor: [0.5, 46], + anchorXUnits: 'fraction', + anchorYUnits: 'pixels', + opacity: 1.0, + src: marker_png + })) + }); + var markerSource = new ol.Collection(); + for (var i = 0; i < tracelife.length; i++) { + var loc = tracelife[i]; + var iconFeature = new ol.Feature({ + geometry: new ol.geom.Point(ol.proj.transform([loc[0], loc[1]], + 'EPSG:4326', 'EPSG:3857')), + name: loc[2], + }); + iconFeature.setStyle(iconStyle); + markerSource.push(iconFeature); + } + var centerCoord = new ol.proj.transform([%s, %s], 'EPSG:4326', 'EPSG:3857'); + map = new ol.Map({ + target: 'map_canvas', + layers: [ + new ol.layer.Tile({ source: new ol.source.Stamen({ + layer: layer + }) + }), + new ol.layer.Vector({ source: new ol.source.Vector({ + features: markerSource }) + }) + ], + view: new ol.View({ center: centerCoord, zoom: %d }) + }); +""" + +OPENLAYER = """ var element = document.getElementById('popup'); var tooltip = new ol.Overlay({ element: element, diff --git a/gramps/plugins/webreport/narrativeweb.py b/gramps/plugins/webreport/narrativeweb.py index c1e1441a4..a44c0df77 100644 --- a/gramps/plugins/webreport/narrativeweb.py +++ b/gramps/plugins/webreport/narrativeweb.py @@ -234,6 +234,7 @@ class NavWebReport(Report): self.mapservice = self.options['mapservice'] self.googleopts = self.options['googleopts'] self.googlemapkey = self.options['googlemapkey'] + self.stamenopts = self.options['stamenopts'] self.reference_sort = self.options['reference_sort'] if self.use_home: @@ -2035,6 +2036,7 @@ class NavWebOptions(MenuReportOptions): mapopts = [ [_("OpenStreetMap"), "OpenStreetMap"], + [_("StamenMap"), "StamenMap"], [_("Google"), "Google"]] self.__mapservice = EnumeratedListOption(_("Map Service"), mapopts[0][1]) @@ -2081,6 +2083,19 @@ class NavWebOptions(MenuReportOptions): self.__googlemapkey.set_help(_("The API key used for the Google maps")) addopt("googlemapkey", self.__googlemapkey) + stamenopts = [ + (_("Toner"), "toner"), + (_("Terrain"), "terrain"), + (_("WaterColor"), "watercolor")] + self.__stamenopts = EnumeratedListOption(_("Stamen Option"), + stamenopts[0][1]) + for trans, opt in stamenopts: + self.__stamenopts.add_item(opt, trans) + self.__stamenopts.set_help( + _("Select which option that you would like " + "to have for the Stamenmap Map pages...")) + addopt("stamenopts", self.__stamenopts) + self.__placemap_options() def __add_others_options(self, menu): @@ -2266,6 +2281,11 @@ class NavWebOptions(MenuReportOptions): else: self.__mapservice.set_available(False) + if mapservice_opts == "StamenMap": + self.__stamenopts.set_available(True) + else: + self.__stamenopts.set_available(False) + if family_active and mapservice_opts == "Google": self.__googleopts.set_available(True) else: diff --git a/gramps/plugins/webreport/person.py b/gramps/plugins/webreport/person.py index 5e2ff5ed9..450d0afc2 100644 --- a/gramps/plugins/webreport/person.py +++ b/gramps/plugins/webreport/person.py @@ -74,7 +74,8 @@ from gramps.plugins.webreport.common import (get_first_letters, _KEYPERSON, get_index_letter, add_birthdate, primary_difference, FULLCLEAR, _find_birth_date, _find_death_date, - MARKER_PATH, OSM_MARKERS, + MARKER_PATH, OPENLAYER, + OSM_MARKERS, STAMEN_MARKERS, GOOGLE_MAPS, MARKERS, html_escape, DROPMASTERS, FAMILYLINKS) from gramps.plugins.webreport.layout import LayoutTree @@ -455,6 +456,7 @@ class PersonPages(BasePage): self.mapservice = self.report.options['mapservice'] self.googleopts = self.report.options['googleopts'] self.googlemapkey = self.report.options['googlemapkey'] + self.stamenopts = self.report.options['stamenopts'] # decide if we will sort the birth order of siblings... self.birthorder = self.report.options['birthorder'] @@ -702,7 +704,7 @@ class PersonPages(BasePage): src_js += "&key=" + self.googlemapkey head += Html("script", type="text/javascript", src=src_js, inline=True) - else: + else: # OpenStreetMap, Stamen... url = self.secure_mode url += ("maxcdn.bootstrapcdn.com/bootstrap/3.3.7/" "css/bootstrap.min.css") @@ -777,8 +779,8 @@ class PersonPages(BasePage): ['%s', %s, %s, %d] ];""" % (placetitle.replace("'", "\\'"), latitude, longitude, seq_) - # are we using OpenStreetMap? - elif self.mapservice == "OpenStreetMap": + # we are using OpenStreetMap, Stamen... + else: tracelife += """ [%f, %f, \'%s\'] ];""" % (float(longitude), float(latitude), placetitle.replace("'", "\\'")) @@ -852,8 +854,8 @@ class PersonPages(BasePage): jsc += MARKERS % (tracelife, midx_, midy_, zoomlevel) - # we are using OpenStreetMap... - else: + # we are using OpenStreetMap + elif self.mapservice == "OpenStreetMap": if midy_ is None: jsc += OSM_MARKERS % (tracelife, longitude, @@ -861,6 +863,23 @@ class PersonPages(BasePage): else: jsc += OSM_MARKERS % (tracelife, midy_, midx_, zoomlevel) + jsc += OPENLAYER + # we are using StamenMap + elif self.mapservice == "StamenMap": + if midy_ is None: + jsc += STAMEN_MARKERS % (tracelife, + self.stamenopts, + longitude, + latitude, + 10, + ) + else: + jsc += STAMEN_MARKERS % (tracelife, + self.stamenopts, + midy_, midx_, + zoomlevel, + ) + jsc += OPENLAYER # if Google and Drop Markers are selected, # then add "Drop Markers" button? diff --git a/gramps/plugins/webreport/place.py b/gramps/plugins/webreport/place.py index ea8be3a24..a95e63632 100644 --- a/gramps/plugins/webreport/place.py +++ b/gramps/plugins/webreport/place.py @@ -63,8 +63,9 @@ from gramps.plugins.webreport.common import (get_first_letters, first_letter, alphabet_navigation, GOOGLE_MAPS, primary_difference, _KEYPLACE, get_index_letter, FULLCLEAR, - MARKER_PATH, OSM_MARKERS, MARKERS, - html_escape) + MARKER_PATH, OPENLAYER, + OSM_MARKERS, STAMEN_MARKERS, + MARKERS, html_escape) _ = glocale.translation.sgettext LOG = logging.getLogger(".NarrativeWeb") @@ -310,6 +311,7 @@ class PlacePages(BasePage): self.placemappages = self.report.options['placemappages'] self.mapservice = self.report.options['mapservice'] self.googlemapkey = self.report.options['googlemapkey'] + self.stamenopts = self.report.options['stamenopts'] # begin PlaceDetail Division with Html("div", class_="content", id="PlaceDetail") as placedetail: @@ -379,7 +381,7 @@ class PlacePages(BasePage): src_js += "&key=" + self.googlemapkey head += Html("script", type="text/javascript", src=src_js, inline=True) - else: + else: # OpenStreetMap, Stamen... url = self.secure_mode url += ("maxcdn.bootstrapcdn.com/bootstrap/3.3.7/" "css/bootstrap.min.css") @@ -428,18 +430,25 @@ class PlacePages(BasePage): latitude, longitude, 10) - else: - # OpenStreetMap (OSM) adds Longitude/ Latitude - # to its maps, and needs a country code in - # lowercase letters... + elif self.mapservice == "OpenStreetMap": with Html("script", type="text/javascript") as jsc: canvas += jsc - #param1 = xml_lang()[3:5].lower() jsc += MARKER_PATH % marker_path jsc += OSM_MARKERS % ([[float(longitude), float(latitude), placetitle]], longitude, latitude, 10) + jsc += OPENLAYER + else: # STAMEN + with Html("script", type="text/javascript") as jsc: + canvas += jsc + jsc += MARKER_PATH % marker_path + jsc += STAMEN_MARKERS % ([[float(longitude), + float(latitude), + placetitle]], + self.stamenopts, + longitude, latitude, 10) + jsc += OPENLAYER # add javascript function call to body element body.attr += ' onload = "initialize();" '