Skip to content

Commit 2587d79

Browse files
committed
fix:layers
1 parent 9741ec6 commit 2587d79

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

src/vendors/helpers/Layers.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export function createLayer(layerConfig, map) {
104104
}),
105105
});
106106
case 'geojson':
107+
console.log(JSON.stringify(layerConfig.source?.data || {}))
107108
return new VectorLayer({
108109
name: layerConfig.label,
109110
title: layerConfig.title || layerConfig.name,
@@ -118,7 +119,7 @@ export function createLayer(layerConfig, map) {
118119
splitscreen: layerConfig.splitscreen,
119120
displayInLayerSwitcher: layerConfig.userVisible,
120121
source: new VectorSource({
121-
features: new GeoJSON().readFeatures(JSON.stringify(layerConfig.source.data, null), {
122+
features: new GeoJSON().readFeatures(JSON.stringify(layerConfig.source?.data || {}), {
122123
// Ensure the features are read with the correct projection
123124
dataProjection: layerConfig.source.projection || 'EPSG:4326', // Assuming the GeoJSON is in WGS 84
124125
featureProjection: map.getView().getProjection() || 'EPSG:3857' // Assuming the map projection
@@ -254,51 +255,49 @@ export function findLayer(map, name) {
254255
}
255256

256257
export function setFeatures(map, data, name, clear) {
257-
return new Promise((resolve, reject) => {
258-
const layer = findLayer(map, name);
259-
if (layer) {
260-
const source = layer.getSource()
261-
//Check if there is a undo stack connected to this source, if so clear and disable
262-
var undos = []
263-
//Disable all undo stacks
264-
map.getControls().forEach((c) => {
265-
if (c instanceof UndoRedo && c.getActive()) {
266-
undos.push(c)
267-
c.setActive(false)
268-
}
269-
})
270-
271-
//Check if we should clear te source
272-
if (clear) {
273-
source.clear()
274-
undos.forEach((c) => { c.clear() })
258+
const layer = findLayer(map, name);
259+
if (layer) {
260+
const source = layer.getSource()
261+
//Check if there is a undo stack connected to this source, if so clear and disable
262+
var undos = []
263+
//Disable all undo stacks
264+
map.getControls().forEach((c) => {
265+
if (c instanceof UndoRedo && c.getActive()) {
266+
undos.push(c)
267+
c.setActive(false)
275268
}
276-
const reader = (source.getFormat ? source.getFormat() : null) || new GeoJSON()
277-
if (reader && data) {
278-
//Now add the features based on types
279-
if (Array.isArray(data)) {
280-
data.forEach((rec) => {
281-
if (source.setFeatures) {
282-
source.setFeatures(reader.readFeatures(rec))
283-
} else {
284-
source.addFeatures(reader.readFeatures(rec))
285-
}
286-
})
287-
} else {
269+
})
270+
271+
//Check if we should clear te source
272+
if (clear) {
273+
source.clear()
274+
undos.forEach((c) => { c.clear() })
275+
}
276+
const reader = (source.getFormat ? source.getFormat() : null) || new GeoJSON()
277+
if (reader && data) {
278+
//Now add the features based on types
279+
if (Array.isArray(data)) {
280+
data.forEach((rec) => {
288281
if (source.setFeatures) {
289-
source.setFeatures(reader.readFeatures(data))
282+
source.setFeatures(reader.readFeatures(rec))
290283
} else {
291-
source.addFeatures(reader.readFeatures(data))
284+
source.addFeatures(reader.readFeatures(rec))
292285
}
286+
})
287+
} else {
288+
if (source.setFeatures) {
289+
source.setFeatures(reader.readFeatures(data))
290+
} else {
291+
source.addFeatures(reader.readFeatures(data))
293292
}
294293
}
295-
//Enable the undo stack
296-
undos.forEach((c) => { c.setActive(true) })
297-
//Enable the connected undo check
298-
return resolve()//Exit, work is done
299294
}
300-
return reject()
301-
})
295+
//Enable the undo stack
296+
undos.forEach((c) => { c.setActive(true) })
297+
//Enable the connected undo check
298+
return true//Exit, work is done
299+
}
300+
return false
302301
}
303302

304303
//Read feature of map
@@ -309,15 +308,15 @@ export function getFeatures(map, name) {
309308
//Check if there is a undo stack connected to this source, if so clear and disable
310309
return new GeoJSON().writeFeaturesObject(source.getFeatures())
311310
}
312-
return Promise.reject()
311+
return false
313312
}
314313

315314
//Clear feature of map
316315
export function clearFeatures(map, name) {
317316
const layer = findLayer(map, name);
318317
if (layer) {
319318
layer.getSource().clear()
320-
return Promise.resolve()
319+
return true
321320
}
322-
return Promise.reject()
321+
return false
323322
}

0 commit comments

Comments
 (0)