Skip to content

Commit 0a13f19

Browse files
show text output for data without features and geometry to display on the map (#35)
1 parent 32509f5 commit 0a13f19

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

src/renderer/geoConverter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export class GeoConverter {
5858
if (Array.isArray(objects)) {
5959
// create geo features collection
6060
geoJson = {type: 'FeatureCollection', features: []};
61-
objects.forEach(item => geoJson.features.push(this.getFeature(item, settings)));
61+
objects.forEach(item => {
62+
const feature: any = this.getFeature(item, settings);
63+
if (feature.geometry?.type !== undefined) { // has geometry type and coordinates
64+
geoJson.features.push(feature);
65+
}
66+
});
6267
this.addOptionalProperties(geoJson, settings);
6368
}
6469
else {

src/renderer/outputLoader.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ export class OutputLoader {
249249
catch(error: any) {
250250
console.log('leaflet.map:data: GeoJSON parse error:\n', error);
251251
}
252+
if (!geoData.features || geoData.features.length <= 0) {
253+
// console.log(`leaflet.map:data:features: ${JSON.stringify(geoData.features, null, 2)}`);
254+
// use parsed data input instead
255+
return data;
256+
}
252257
return geoData;
253258
}
254259

src/renderer/renderer.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,22 @@ const leafletMap = require('./leafletMap.js');
3030
console.log(`leaflet.map:data:mimeType: ${output.mimeType}`);
3131
const outputLoader: OutputLoader = new OutputLoader(output.value, output.mimeType);
3232
let data: any = outputLoader.getData();
33-
if (data.features) { // has geometry features collection
33+
if (data.features && data.features.length > 0) { // has geometry features to display
3434
// create leaflet map and add it to notebook cell output display
3535
const mapContainer: HTMLDivElement = document.createElement('div');
3636
mapContainer.className = 'map-container';
3737
output.container.appendChild(mapContainer);
38-
const map = leafletMap.createMap(data, mapContainer);
39-
}
40-
else {
41-
// create text output display nodes
42-
const pre = document.createElement('pre');
43-
pre.className = 'text-output';
44-
const code = document.createElement('code');
45-
if (typeof data !== 'string') {
46-
// stringify json data
47-
code.textContent = JSON.stringify(data, null, 2);
38+
try {
39+
// try to load geo data into leaflet map
40+
const map = leafletMap.createMap(data, mapContainer);
4841
}
49-
else {
50-
// show cell output text
51-
code.textContent = output.value.text();
42+
catch(error: any) {
43+
console.error('leaflet.map:data: GeoJSON parse error:\n', error);
44+
showTextData(data, output);
5245
}
53-
pre.appendChild(code);
54-
output.container.appendChild(pre);
46+
}
47+
else {
48+
showTextData(data, output);
5549
}
5650
}
5751

@@ -60,3 +54,23 @@ if (module.hot) {
6054
// cleanup or stash any state on renderer dispose
6155
});
6256
}
57+
58+
/**
59+
* Displays text data.
60+
*/
61+
function showTextData(data: any, output: IRenderInfo): void {
62+
// create text output display nodes
63+
const pre = document.createElement('pre');
64+
pre.className = 'text-output';
65+
const code = document.createElement('code');
66+
if (typeof data !== 'string') {
67+
// stringify json data
68+
code.textContent = JSON.stringify(data, null, 2);
69+
}
70+
else {
71+
// show cell output text
72+
code.textContent = output.value.text();
73+
}
74+
pre.appendChild(code);
75+
output.container.appendChild(pre);
76+
}

0 commit comments

Comments
 (0)