Public
Edited
Apr 11, 2023
7 stars
Insert cell
Insert cell
viewof file = fileInput({
initialValue: FileAttachment(
"Barrios_del_municipio_de_Pereira.geojson"
).blob()
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
topoJSON = {
if (originalJSON.type === "Topology") {
// the file is already a topoJSON
return originalJSON;
} else {
// Here we convert the file to geoJSON
return topojson.topology({ name: originalJSON });
}
}
Insert cell
Insert cell
simplified = topojson.simplify(
topojson.presimplify(topoJSON),
Math.pow(10, -1 * minWeightExp)
) // minWeight has a default value here of 0.002, you can change it later in the notebook
Insert cell
Insert cell
simplifiedAsStr = JSON.stringify(simplified)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof selected = {
const detectedAttributes = Object.keys(originalJSON?.features[0]?.properties);
return Inputs.checkbox(detectedAttributes || defaultProperties, {
label: "Attributes to keep",
value: detectedAttributes || defaultProperties
});
}
Insert cell
lessProperties = {
const lessProperties = { ...simplified };
const copyOnlySelected = (d) => {
const dCopy = { ...d };
dCopy.properties = Object.fromEntries(
selected
.map((a) => [a, d.properties[a]])
.filter((d) => d[1] !== undefined)
);
return dCopy;
};
lessProperties.objects = { ...simplified.objects };
for (let obj in lessProperties.objects) {
lessProperties.objects[obj] = { ...simplified.objects[obj] };
lessProperties.objects[obj].geometries = simplified.objects[
obj
].geometries.map(copyOnlySelected);
}
return lessProperties;
}
Insert cell
lessPropertiesAsStr = JSON.stringify(lessProperties)
Insert cell
DOM.download(
new Blob([lessPropertiesAsStr], { type: "text/json" }),
file.name ? file.name.split(".")[0] + ".json" : "topojson.json",
"Download With Less Properties"
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
height = 400
Insert cell
fmt = d3.format("0.2f")
Insert cell
map = geoJSON => {
const ctx = DOM.context2d(width, height);

const path = d3
.geoPath()
.projection(d3.geoMercator().fitSize([width, height], geoJSON))
.context(ctx);

ctx.beginPath();
path(geoJSON);
ctx.lineWidth = 1;
ctx.strokeStyle = "#000";
ctx.stroke();
return ctx.canvas;

// SVG Version
// const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
// svg
// .selectAll("path")
// .data(geoJSON.features)
// .join("path")
// .style("stroke", "black")
// .style("fill", "none")
// .attr("d", path);

// return svg.node();
}
Insert cell
d3 = require("d3@6", "d3-geo-projection@3")
Insert cell
topojson = require("topojson-server@3", "topojson-simplify@3", "topojson-client@3")
Insert cell
import { select, checkbox, number } from "@jashkenas/inputs"
Insert cell
import { fileInput } from "@mbostock/file-input-with-initial-value"
Insert cell
import { rewind } from "@john-guerra/d3-black-box-map"
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