forked from opengeos/GeoLibre
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisibility.ts
More file actions
76 lines (66 loc) · 2.14 KB
/
Copy pathvisibility.ts
File metadata and controls
76 lines (66 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { FeatureCollection } from "geojson";
import type { GeoLibreProject, FieldVisibility } from "./types";
/**
* Returns a new FeatureCollection with properties marked as "excluded" removed.
*/
export function excludeHiddenFieldsFromGeojson(
geojson: FeatureCollection,
fieldVisibility?: Record<string, FieldVisibility>,
): FeatureCollection {
const excludedKeys = new Set(
Object.entries(fieldVisibility || {})
.filter(([_, visibility]) => visibility === "excluded")
.map(([key]) => key),
);
if (excludedKeys.size === 0) {
return geojson;
}
// Deep clone to avoid mutating the live store state
const stripped: FeatureCollection = {
...geojson,
features: geojson.features.map((feature) => {
const properties = { ...feature.properties };
for (const key of excludedKeys) {
delete properties[key];
}
return { ...feature, properties };
}),
};
return stripped;
}
/**
* Returns a new GeoLibreProject where all layers have their excluded fields
* physically removed from their inline GeoJSON.
*/
export function excludeHiddenFieldsFromProject(project: GeoLibreProject): GeoLibreProject {
let changed = false;
const layers = project.layers.map((layer) => {
if (!layer.fieldVisibility) return layer;
let updatedLayer = layer;
if (layer.geojson) {
const strippedGeojson = excludeHiddenFieldsFromGeojson(layer.geojson, layer.fieldVisibility);
if (strippedGeojson !== layer.geojson) {
changed = true;
updatedLayer = { ...updatedLayer, geojson: strippedGeojson };
}
}
if (layer.metadata?.embeddedGeoJSON) {
const strippedEmbedded = excludeHiddenFieldsFromGeojson(
layer.metadata.embeddedGeoJSON as FeatureCollection,
layer.fieldVisibility,
);
if (strippedEmbedded !== layer.metadata.embeddedGeoJSON) {
changed = true;
updatedLayer = {
...updatedLayer,
metadata: {
...updatedLayer.metadata,
embeddedGeoJSON: strippedEmbedded,
},
};
}
}
return updatedLayer;
});
return changed ? { ...project, layers } : project;
}