Skip to content

Commit 81f28ac

Browse files
flatten geo data for geojson data display in table renderer (#37)
enable json parse error logging for now
1 parent 5aa2bc4 commit 81f28ac

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/renderer/outputLoader.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class OutputLoader {
6969
const textData: string = this.patchJson(data);
7070
const objectData: any = JSON.parse(textData);
7171
if (Array.isArray(objectData)) {
72-
console.log('data.table:format: JSON');
72+
console.log('data.table:format: JSON array');
7373
return objectData;
7474
}
7575
}
@@ -82,19 +82,24 @@ export class OutputLoader {
8282
jsonData = jsonData.data;
8383
}
8484

85+
if (jsonData.features) {
86+
console.log('data.table:format: GeoJSON');
87+
jsonData = this.flattenGeoData(jsonData);
88+
}
89+
8590
if (Array.isArray(jsonData)) {
86-
console.log('data.table:format: JSON');
91+
console.log('data.table:format: JSON array');
8792
return jsonData;
8893
}
89-
94+
9095
if (typeof jsonData === 'string' && this.isCsv(jsonData)) {
9196
// parse CSV data for JSON response from REST Book
9297
// see: https://github.com/tanhakabir/rest-book/issues/114
9398
return csvParse(jsonData);
9499
}
95100
}
96101
catch (error) {
97-
// console.log('data.table: JSON.parse error:\n', error.message);
102+
console.log('data.table: JSON.parse error:\n', error.message);
98103
}
99104
return undefined;
100105
}
@@ -120,6 +125,33 @@ export class OutputLoader {
120125
return textData;
121126
}
122127

128+
/**
129+
* Flattens geo data for tabular data display.
130+
* @param data Geojson or topojson data object.
131+
*/
132+
flattenGeoData(data: any): any {
133+
if (data.features) {
134+
const features = data.features.map((feature: any) => {
135+
let geometry = {} as Record<string, any>;
136+
Object.keys(feature?.geometry).forEach(key => {
137+
geometry[`geometry.${key}`] = feature.geometry[key];
138+
});
139+
let properties = {} as Record<string, any>;
140+
Object.keys(feature?.properties).forEach(key => {
141+
properties[`properties.${key}`] = feature.properties[key];
142+
});
143+
const {geometry: g, properties: p, ...restOfKeys} = feature;
144+
return {...restOfKeys, ...geometry, ...properties};
145+
});
146+
147+
// make features the first key of the object
148+
const {features: f, ...restOfData } = data;
149+
data = {features, ...restOfData };
150+
// console.log('data.table:geoData:', JSON.stringify(data, null, 2));
151+
return data.features;
152+
}
153+
return data;
154+
}
123155

124156
/**
125157
* Checks if text content is in CSV format.

0 commit comments

Comments
 (0)