Public
Edited
Jan 2, 2023
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function circleFill(lat, lng, searchRadiusKm, res, pad = 0) {
const origin = h3.geoToH3(lat, lng, res);
const e = h3.edgeLength(res, h3.UNITS.km);
// Transform the radius from km to grid distance
const radius = (((searchRadiusKm - e)/e)) * (2/3) + pad;

let k = 1;
let foundh3Inside = true;
const h3s = [origin];
while (foundh3Inside) {
foundh3Inside = false;
const newH3s = h3.hexRing(origin, k);
newH3s.forEach(newH3 => {
const h3Center = h3.h3ToGeo(newH3);
if(h3.pointDist([lat, lng], h3Center, h3.UNITS.km) < (searchRadiusKm + e)) {
foundh3Inside = true;
h3s.push(newH3);
}
});
k++;
}

return h3s;
}
Insert cell
function featureFill(geoJson, res, expandOutward = false) {
let polygons;
if (geoJson.geometry.type === "Polygon") {
polygons = [geoJson.geometry.coordinates];
} else if (geoJson.geometry.type === "MultiPolygon") {
polygons = geoJson.geometry.coordinates;
} else {
throw new Error("Unable to fill geoJSON of type " + geoJson.type)
}
return [...new Set(polygons.map(polygon =>
[... new Set(h3.polyfill(polygon, res, true)
.flatMap(h3Index => {
if (expandOutward) {
return h3.kRing(h3Index, 1);
} else {
return [h3Index];
}
}))]
.filter(h3Index => {
if (expandOutward) {
const h3AsGeoJson = {
type: "Feature",
geometry: { type: "Polygon", coordinates: [h3.h3ToGeoBoundary(h3Index, true)] } };
const polygonAsSingleFeature = {
type: "Feature",
geometry: { type: "Polygon", coordinates: polygon } };
console.log(polygonAsSingleFeature)
return intersects(polygonAsSingleFeature, h3AsGeoJson)
}
return true;
})).flat())];
}
Insert cell
Insert cell
function closestGeoHashPrecision(h3Resolution, lat, lng) {
const targetArea = h3.cellArea(h3.geoToH3(lat, lng, h3Resolution), h3.UNITS.m2);
let lastGeoHashArea;
let geoHashArea;
let precision = 0;
while (lastGeoHashArea === undefined || Math.abs(geoHashArea - targetArea) < Math.abs(lastGeoHashArea - targetArea)) {
precision += 1;
lastGeoHashArea = geoHashArea;
const geoHashBbox = geoHash.decode_bbox(geoHash.encode(lat, lng, precision));
geoHashArea = area(bboxPolygon(geoHashBbox));
console.log("precision: "+ precision)
console.log("geoHashArea: "+ geoHashArea)
}
return precision - 1;
}
Insert cell
function polyfillGeohashes(geoJson, precision) {
const geoJsonBbox = bbox(geoJson);
const geoHashesForBbox = geoHash.bboxes(...geoJsonBbox, precision);
return geoHashesForBbox.filter(geoHashForBbox => {
const geoHashBbox = geoHash.decode_bbox(geoHashForBbox);
return intersects(bboxPolygon(geoHashBbox), geoJson);
});
}
Insert cell
function geoHashSetToMultiPolygonFeature(geoHashes) {
return {
type: "FeatureCollection",
features: geoHashes.map(singleGeoHash => bboxPolygon(geoHash.decode_bbox(singleGeoHash)))
};
}
Insert cell
precisionTest = closestGeoHashPrecision(14, 0, 0)
Insert cell
Insert cell
Insert cell
geoHash = {
const module = await import('https://cdn.skypack.dev/ngeohash@0.6.3?min');
return module.default;
}
Insert cell
Insert cell
intersects = {
const module = await import('https://cdn.skypack.dev/@turf/boolean-intersects@6.5.0?min');
return module.default;
}
Insert cell
Insert cell
Insert cell
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