Public
Edited
Apr 18, 2023
1 star
Insert cell
Insert cell
// It was determined that TopoJSON conversion is the best simplifications method for preserving the 'look'
Insert cell
// https://observablehq.com/@coachman/week-11-intro-to-d3-js-mapping-data-with-d3
// https://observablehq.com/@mbostock/lets-make-a-map-part-1
Insert cell
Insert cell
Insert cell
/*
viewof file = fileInput({
initialValue: FileAttachment(
"example.geojson"
).blob(),
accept: ".json,.geojson"
})
*/
Insert cell
// geo = uploaded_file === false ? await d3.json(geo_url) : uploaded_file
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
v0 = initSVGObject(data, object.label, object.convex, 600, width)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
v1 = initSVGObject(data1, object.label, object.convex, 600, width)
Insert cell
Insert cell
Insert cell
// https://observablehq.com/@john-guerra/topojson-simplify-and-filter-attributes
// https://observablehq.com/@mkfreeman/topojson-simplifier
// https://observablehq.com/@lemonnish/minify-topojson-in-the-browser
Insert cell
topojson = require("topojson-client", "topojson-simplify", "topojson-server")
Insert cell
Insert cell
topoJSON = {
let t = data
if (data.type === "Topology") { return t; }
else {
// Here we convert the file to geoJSON
return topojson.topology({ name: t });
}
}
Insert cell
Insert cell
Insert cell
topodata = simplify(topoJSON, quantile_value2)
Insert cell
Insert cell
Insert cell
v2 = {
let projection = projectionInput({
value: "Patterson cylindrical",
name: "projection"
}).value
// Create projection that fits to the screen
let proj = d3.geoProjection(projection).fitExtent( [[10, 10], [width, 600]],
topojson.feature(topodata, topodata.objects[feature]) )

// Function to draw the path
let path = d3.geoPath().projection(proj)
let merged = topojson.mesh( topodata ) // Merge into a single path (faster to draw // no overlapping borders)
// Display the (merged) path
let prepped_map = html`<svg height = ${600} width = ${width}>
<path id="path" style="fill:none; stroke:black" d = ${path(merged)}></path>
</svg>`
return prepped_map
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data2 = '' // topojson.feature(topodata, topodata.objects[feature])
Insert cell
v3 = '' //initSVGObject(data2, object.label, object.convex, 600, width)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
DOM.download(
new Blob([simplifiedAsStr], { type: "text/json" }), "topojson.json"
)
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const loader = new THREE.SVGLoader();
let paths = loader.parse( v2.outerHTML ).paths
return paths
}
Insert cell
simplifiedPaths = {
let clone = v0.cloneNode(true);
let v2children = Array.from(clone.children)
for (let i=0; i < v2children.length; i++) {
let bbox = Array.from(v0.children)[i].getBBox()
let path1 = v2children[i].getAttribute('d')
let path2 = await simplifyPath(path1)
v2children[i].setAttribute("d", path2 )
v2children[i].setAttribute('transform',`translate(${bbox.x}, ${bbox.y})`)
}
clone.innerHTML = '';
return clone
}
Insert cell
v0.children
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