From 212d8906f77e6532c23bc472edd477f2d1d80582 Mon Sep 17 00:00:00 2001 From: Will Kocur Date: Tue, 8 Jun 2021 11:23:52 +0100 Subject: [PATCH 1/3] fallback to solid fill if layer image not found --- .../components/map/mixins/mixin-add-layers.js | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/app/javascript/components/map/mixins/mixin-add-layers.js b/app/javascript/components/map/mixins/mixin-add-layers.js index 18cca8ec..905eff59 100644 --- a/app/javascript/components/map/mixins/mixin-add-layers.js +++ b/app/javascript/components/map/mixins/mixin-add-layers.js @@ -1,21 +1,29 @@ import { getFirstDataLayerId } from '../helpers/map-helpers' -const addImagePaintOptions = (map, options, layer) => { - addFillPaintOptions(options, layer) - const patternId = 'pattern' + layer.id - - options['paint']['fill-pattern'] = patternId - +const addImageLayer = (map, options, layer, nextLayer) => { map.loadImage( layer.image, (err, image) => { - if (err) { throw err } - - map.addImage(patternId, image) + if (err) { + console.log(`Image ${layer.image} not found. Falling back to solid fill.`) + + delete options['paint']['fill-pattern'] + + map.addLayer(options, nextLayer) + } else { + map.addImage(getPatternId(layer), image) + map.addLayer(options, nextLayer) + } } ) } +const addImagePaintOptions = (options, layer) => { + addFillPaintOptions(options, layer) + + options['paint']['fill-pattern'] = getPatternId(layer) +} + const addFillPaintOptions = (options, layer) => { options['type'] = 'fill' options['paint'] = { @@ -54,6 +62,8 @@ const addLinePaintOptions = (options, layer) => { } } +const getPatternId = layer => 'pattern' + layer.id + const getOpacity = opacity => opacity || 1 export default { @@ -74,14 +84,19 @@ export default { } else if (layer.type === 'line') { addLinePaintOptions(options, layer) } else if (layer.image) { - addImagePaintOptions(this.map, options, layer) + addImagePaintOptions(options, layer) } else { addFillPaintOptions(options, layer) } const nextLayer = layer.addUnderneath ? getFirstDataLayerId(this.map) : this.firstForegroundLayerId - this.map.addLayer(options, nextLayer) + if (layer.image) { + // Need to wait until image is loaded in case falling back to solid fill + addImageLayer(this.map, options, layer, nextLayer) + } else { + this.map.addLayer(options, nextLayer) + } } } } From 9bddf622543f1d312f8951323fe06839f1d630e4 Mon Sep 17 00:00:00 2001 From: Will Kocur Date: Tue, 8 Jun 2021 11:37:09 +0100 Subject: [PATCH 2/3] use console warn --- app/javascript/components/map/mixins/mixin-add-layers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/javascript/components/map/mixins/mixin-add-layers.js b/app/javascript/components/map/mixins/mixin-add-layers.js index 905eff59..d2e745f8 100644 --- a/app/javascript/components/map/mixins/mixin-add-layers.js +++ b/app/javascript/components/map/mixins/mixin-add-layers.js @@ -5,7 +5,7 @@ const addImageLayer = (map, options, layer, nextLayer) => { layer.image, (err, image) => { if (err) { - console.log(`Image ${layer.image} not found. Falling back to solid fill.`) + console.warn(`Image ${layer.image} not found. Falling back to solid fill.`) delete options['paint']['fill-pattern'] From 68a93f50cea866dd3a2a7fa67214a87fe5dd91aa Mon Sep 17 00:00:00 2001 From: Will Kocur Date: Tue, 8 Jun 2021 11:37:57 +0100 Subject: [PATCH 3/3] attempted fix for missing images for map --- .../serializers/map_datasets_serializer.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/serializers/serializers/map_datasets_serializer.rb b/app/serializers/serializers/map_datasets_serializer.rb index 959d536b..506bb918 100644 --- a/app/serializers/serializers/map_datasets_serializer.rb +++ b/app/serializers/serializers/map_datasets_serializer.rb @@ -111,7 +111,7 @@ addUnderneath: true, name: I18n.t('global.map.wdpa_title'), disabled: false, - image: ActionController::Base.helpers.image_url('map/stripes-wdpa.png') + image: 'map/stripes-wdpa.png' }, { id: 'oecm', @@ -131,7 +131,7 @@ addUnderneath: true, name: I18n.t('global.map.oecm_title'), disabled: false, - image: ActionController::Base.helpers.image_url('map/stripes-oecm.png') + image: 'map/stripes-oecm.png' } ].freeze @@ -163,11 +163,19 @@ def serialize() dataset end - habitat_datasets + WDPA_DATASETS + habitat_datasets + wdpa_datasets end private + def wdpa_datasets + WDPA_DATASETS.map do |ds| + dataset = ds.dup + dataset[:image] = ActionController::Base.helpers.image_url(dataset[:image]) + + dataset + end + end def habitat_presence_status dataset @habitat_presence_statuses[dataset[:id]]