Skip to content

Commit 1a8d675

Browse files
committed
fix: json format for layer
1 parent 4c9c8e9 commit 1a8d675

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@
7171
- Implement timeline function and layer types
7272
- Hide timeline layers from layer menu, and group the in on layergroup
7373
- Check if is workable on mobile
74+
- Problem with changing drawlayer, will not setFeatures
75+
- Loading of JSON not working

src/vendors/helpers/Layers.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export function createLayer(layerConfig, map) {
118118
splitscreen: layerConfig.splitscreen,
119119
displayInLayerSwitcher: layerConfig.userVisible,
120120
source: new VectorSource({
121-
features: new GeoJSON().readFeatures(layerConfig.source.data, {
121+
features: new GeoJSON().readFeatures(JSON.stringify(layerConfig.source.data, null), {
122122
// Ensure the features are read with the correct projection
123123
dataProjection: layerConfig.source.projection || 'EPSG:4326', // Assuming the GeoJSON is in WGS 84
124124
featureProjection: map.getView().getProjection() || 'EPSG:3857' // Assuming the map projection
@@ -254,49 +254,51 @@ export function findLayer(map, name) {
254254
}
255255

256256
export function setFeatures(map, data, name, clear) {
257-
const layer = findLayer(map, name);
258-
if (layer) {
259-
const source = layer.getSource()
260-
//Check if there is a undo stack connected to this source, if so clear and disable
261-
var undos = []
262-
//Disable all undo stacks
263-
map.getControls().forEach((c) => {
264-
if (c instanceof UndoRedo && c.getActive()) {
265-
undos.push(c)
266-
c.setActive(false)
267-
}
268-
})
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+
})
269270

270-
//Check if we should clear te source
271-
if (clear) {
272-
source.clear()
273-
undos.forEach((c) => { c.clear() })
274-
}
275-
const reader = (source.getFormat ? source.getFormat() : null) || new GeoJSON()
276-
if (reader && data) {
277-
//Now add the features based on types
278-
if (Array.isArray(data)) {
279-
data.forEach((rec) => {
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) => {
281+
if (source.setFeatures) {
282+
source.setFeatures(reader.readFeatures(rec))
283+
} else {
284+
source.addFeatures(reader.readFeatures(rec))
285+
}
286+
})
287+
} else {
280288
if (source.setFeatures) {
281-
source.setFeatures(reader.readFeatures(rec))
289+
source.setFeatures(reader.readFeatures(data))
282290
} else {
283-
source.addFeatures(reader.readFeatures(rec))
291+
source.addFeatures(reader.readFeatures(data))
284292
}
285-
})
286-
} else {
287-
if (source.setFeatures) {
288-
source.setFeatures(reader.readFeatures(data))
289-
} else {
290-
source.addFeatures(reader.readFeatures(data))
291293
}
292294
}
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
293299
}
294-
//Enable the undo stack
295-
undos.forEach((c) => { c.setActive(true) })
296-
//Enable the connected undo check
297-
return true//Exit, work is done
298-
}
299-
return false
300+
return reject()
301+
})
300302
}
301303

302304
//Read feature of map
@@ -307,15 +309,15 @@ export function getFeatures(map, name) {
307309
//Check if there is a undo stack connected to this source, if so clear and disable
308310
return new GeoJSON().writeFeaturesObject(source.getFeatures())
309311
}
310-
return false
312+
return Promise.reject()
311313
}
312314

313315
//Clear feature of map
314316
export function clearFeatures(map, name) {
315317
const layer = findLayer(map, name);
316318
if (layer) {
317319
layer.getSource().clear()
318-
return true
320+
return Promise.resolve()
319321
}
320-
return false
322+
return Promise.reject()
321323
}

0 commit comments

Comments
 (0)