Published
Edited
Mar 14, 2019
Insert cell
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);
console.log(response.redirected)
const reader = response.body.getReader(); // stream reader
const decoder = new TextDecoder('utf-8'); // for decoding binary stream data into strings

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');

// if we have a line fragment from the previous chunk, prepend it to the first new line
if (lineFragment != '') {
lines[0] = lineFragment + lines[0];
lineFragment = '';
}

// if the last line isn't complete (doesn't end in a newline), save it as a fragment for next chunk
const lastLine = lines[lines.length-1];
if (lastLine[lastLine.length-1] !== '\n') {
lineFragment = lines.pop();
}

// parse and return new features
yield lines.map(JSON.parse);
}
else {
break; // done processing stream
}
}
}
Insert cell
Insert cell
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