Skip to content

Commit 38a710f

Browse files
author
Amelia Wattenbeger
committed
spread geojson geo & props to show fields as columns
1 parent dcf79fd commit 38a710f

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/api/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,26 @@ export function fetchDataFile(params: FileParamsWithSHA) {
145145
try {
146146
if (fileType === "csv") {
147147
data = csvParse(res);
148-
} else if (fileType === "json" || fileType === "geojson" || fileType === "topojson") {
148+
} else if (["geojson", "topojson"].includes(fileType) || filename.endsWith(".geo.json")) {
149+
data = JSON.parse(res);
150+
if (data.features) {
151+
const features = data.features.map((feature: any) => {
152+
let geometry = {} as Record<string, any>
153+
Object.keys(feature?.geometry).forEach(key => {
154+
geometry[`geometry.${key}`] = feature.geometry[key];
155+
})
156+
let properties = {} as Record<string, any>
157+
Object.keys(feature?.properties).forEach(key => {
158+
properties[`properties.${key}`] = feature.properties[key];
159+
})
160+
const {geometry: g, properties: p, ...restOfKeys} = feature;
161+
return {...restOfKeys, ...geometry, ...properties};
162+
})
163+
// make features the first key of the object
164+
const { features: f, ...restOfData } = data
165+
data = { features, ...restOfData }
166+
}
167+
} else if (fileType === "json") {
149168
data = JSON.parse(res);
150169
} else if (fileType === "tsv") {
151170
data = tsvParse(res);

0 commit comments

Comments
 (0)