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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more