Published
Edited
Jul 6, 2019
1 fork
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
features = {
// build an array of features as they are parsed from the stream
const f = [];
for await (const newFeatures of streamGeoJSONL(url)) {
f.push(...newFeatures);
yield f;
}
}
Insert cell
async function* streamGeoJSONL(url) {
const response = await fetch(url);
const reader = response.body.getReader(); // stream reader
const decoder = new TextDecoder('utf-8'); // for decoding binary stream data into strings

try {
let lineFragment = ''; // incomplete line fragment from previous stream chunk

while (true) {
// get next stream chunk
const { done, value } = await reader.read();

if (!done) {
// decode buffer into string, and split into lines
const lines = decoder.decode(value).split('\n');

// prepend last line fragment from previous to the first new line
lines[0] = lineFragment + lines[0];

// save any line fragment from this chunk for next one
lineFragment = lines.pop();

// parse and return new features
yield lines.map(JSON.parse);
}
else {
break; // done processing stream
}
}
}
finally {
reader.releaseLock(); // release lock on stream
}
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more