Published
Edited
Jul 5, 2019
Insert cell
md`# Forest Change Detection preprocessing`
Insert cell
import { fetchGeoJSON, showGeoJSON } from '@gampleman/fetch-geojson'
Insert cell
viewof data = fetchGeoJSON('https://gist.githack.com/gampleman/f8c1696292edc459f14e47ecbed3e18e/raw/522f54865cf31fe2ce88dc4e8612b9c52462eeeb/Hansen_GFC-2018-v1.6_lossyear_cropped_2016-2018.geojson')
Insert cell
turf = require('@turf/turf')
Insert cell
rbush = require('rbush')
Insert cell
withBBoxes = data.features.map(feat => {
const [minX, minY, maxX, maxY] = turf.bbox(feat);
return {...feat, minX, minY, maxX, maxY};
});
Insert cell
rtree = {
const tree = new rbush();
tree.load(withBBoxes);
return tree;
}
Insert cell
turfPointGrid = require('https://bundle.run/@turf/point-grid@6.0.1')
Insert cell
import {mapboxgl} from "@tmcw/using-mapbox-gl-js"
Insert cell
Insert cell
dataSize = memorySizeOf(data)
Insert cell
buffered = turf.buffer(data, 15, {units: 'meters'})
Insert cell
memorySizeOf(buffered)
Insert cell
simplified = turf.simplify(data, {tolerance: 0.0001, highQuality: true})
Insert cell
memorySizeOf(simplified)
Insert cell
viewof intersections = []//computeIntersections(simplified)
Insert cell
// viewof joined1 = showGeoJSON(turf.featureCollection(intersections.map(feats => {
// if (feats.length === 1) {
// return feats[0];
// } else {
// return turf.union(...feats);
// }
// })));
Insert cell
// memorySizeOf(joined1)
Insert cell
mutable processed = simplified
Insert cell
Insert cell
showGeoJSON(iteration)
Insert cell
bbox = turf.bbox(simplified).map(v => Math.ceil(v * 1000) / 1000)
Insert cell
combined = turf.combine(simplified)
Insert cell
Insert cell
grid = {
const [minX, minY, maxX, maxY] = bbox.map(i => Math.round(i * 1000));
const squareSize = 0.0005;
const results = {};
for(let latInt = minY; latInt <= maxY; latInt++) {
for(let lngInt = minX; lngInt <= maxX; lngInt++) {
const lat = latInt / 1000;
const lng = lngInt / 1000;
const queryBBox = {
minX: lng - squareSize,
minY: lat - squareSize,
maxX: lng + squareSize,
maxY: lat + squareSize
};
const candidates = rtree.search(queryBBox);
if (candidates.length > 0) {
const queryPoly = turf.bboxPolygon([queryBBox.minX, queryBBox.minY, queryBBox.maxX, queryBBox.maxY]);
if (candidates.some(cand => !turf.booleanDisjoint(queryPoly, cand))) {
if (!results[latInt]) {
results[latInt] = [];
}
results[latInt].push(lngInt)
}
}
}
}
return results;
}
Insert cell
JSON.stringify(compactGrid)
Insert cell
Insert cell
import {progress} from "@mootari/displaying-progress";
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