Skip to content

Commit fc24d29

Browse files
zrichardetZachery Thomas Richardetm-mohr
authored
Issue 200 Update s2 cloudless basemap to correspond with the 'year' in the time tab (#225)
* setup layer switching on year change but need to finalize the layer appearing * layer changes based on year selection * Restrict years to the available years --------- Co-authored-by: Zachery Thomas Richardet <zachery@Mac.lan> Co-authored-by: Matthias Mohr <webmaster@mamo-net.de>
1 parent 9e1e639 commit fc24d29

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/components/MapComponent.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import ProcessingResults from './ProcessingResults.vue'
66
import PropertiesDisplay from './PropertiesDisplay.vue'
77
import createLabelLayer from '../layers/Label-Layer'
88
import createS2GridLayer from '../layers/S2-Grid-Layer'
9-
import createCloudlessLayer from '../layers/S2-Cloudless-Layer'
109
import { generateJWT } from '../functions/generate-jwt'
1110
import useAreaOfInterest from '../composables/useAreaOfInterest'
1211
import usePermalink from '../composables/usePermalink'
@@ -22,6 +21,7 @@ const {
2221
originalClickPosition,
2322
hidePropertiesBox,
2423
geoJsonResults,
24+
initCloudlessLayer,
2525
} = useMap()
2626
2727
const { addMapClickHandler } = useAreaOfInterest()
@@ -39,7 +39,7 @@ const critical = ref<string | null>(null)
3939
onMounted(async () => {
4040
map.value = new Map({
4141
target: 'map',
42-
layers: [createCloudlessLayer(), createLabelLayer()],
42+
layers: [createLabelLayer()],
4343
view: new View({
4444
center: [0, 0],
4545
zoom: 2,
@@ -80,10 +80,12 @@ onMounted(async () => {
8080
8181
// Add S2 Grid layer after map is initialized
8282
if (map.value) {
83+
// Initialize the cloudless base layer
84+
initCloudlessLayer()
85+
8386
const s2GridLayer = createS2GridLayer()
8487
addMapClickHandler(map.value as Map, areaValues.value)
8588
map.value.addLayer(s2GridLayer)
86-
8789
// Setup permalink functionality
8890
setupPermalink(map)
8991
}

src/composables/useMap.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { ref, shallowRef, watch } from 'vue'
22
import type Map from 'ol/Map'
33
import VectorSource from 'ol/source/Vector'
44
import VectorLayer from 'ol/layer/Vector'
5+
import TileLayer from 'ol/layer/Tile'
6+
import type XYZ from 'ol/source/XYZ'
57
import GeoJSON from 'ol/format/GeoJSON'
68
import { transformExtent } from 'ol/proj'
79
import type { Extent } from 'ol/extent'
810
import { type FeatureCollection } from 'geojson'
911
import useNotifier from './useNotifier'
12+
import useSettings from './useSettings'
13+
import createCloudlessLayer from '../layers/S2-Cloudless-Layer'
1014
import { Fill, Stroke, Style } from 'ol/style'
1115
import { type FeatureLike } from 'ol/Feature'
1216

@@ -35,6 +39,38 @@ const showPropertiesBox = ref(false)
3539

3640
const geoJsonResults = shallowRef<any[]>([])
3741

42+
// Cloudless layer management
43+
const cloudlessLayer = shallowRef<TileLayer<XYZ> | null>(null)
44+
const { settings } = useSettings()
45+
46+
// Watch for year changes and update the cloudless layer
47+
watch(
48+
() => settings.value.year,
49+
(newYear) => {
50+
if (!map.value) {
51+
return
52+
}
53+
54+
// Remove the old cloudless layer if it exists
55+
if (cloudlessLayer.value) {
56+
map.value.removeLayer(cloudlessLayer.value)
57+
}
58+
59+
// Create and add the new cloudless layer with the updated year
60+
cloudlessLayer.value = createCloudlessLayer(newYear)
61+
// Insert at index 0 to keep it as the base layer
62+
map.value.getLayers().insertAt(0, cloudlessLayer.value)
63+
},
64+
)
65+
66+
const initCloudlessLayer = () => {
67+
if (!map.value) {
68+
return
69+
}
70+
cloudlessLayer.value = createCloudlessLayer(settings.value.year)
71+
map.value.getLayers().insertAt(0, cloudlessLayer.value)
72+
}
73+
3874
const featureStyle = new Style({
3975
fill: new Fill({
4076
color: 'rgba(255, 255, 0, 0.1)',
@@ -221,5 +257,6 @@ export default function useMap() {
221257
fitMapToBbox,
222258
displayGeoJSON,
223259
geoJsonResults,
260+
initCloudlessLayer,
224261
}
225262
}

src/layers/S2-Cloudless-Layer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import TileLayer from 'ol/layer/Tile'
22
import { XYZ } from 'ol/source'
33

4-
export default function createCloudlessLayer() {
4+
export default function createCloudlessLayer(year: number) {
5+
if (year < 2016) {
6+
year = 2016
7+
} else if (year > 2024) {
8+
year = 2024
9+
}
510
return new TileLayer({
611
source: new XYZ({
7-
url: 'https://tiles.maps.eox.at/wmts?layer=s2cloudless-2024_3857&style=default&tilematrixset=GoogleMapsCompatible&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fjpeg&TileMatrix={z}&TileCol={x}&TileRow={y}',
12+
url: `https://tiles.maps.eox.at/wmts?layer=s2cloudless${year === 2016 ? '' : '-' + year}_3857&style=default&tilematrixset=GoogleMapsCompatible&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fjpeg&TileMatrix={z}&TileCol={x}&TileRow={y}`,
813
maxZoom: 18,
914
tileSize: 256,
1015
crossOrigin: 'anonymous',

0 commit comments

Comments
 (0)