Published
Edited
Mar 27, 2021
1 fork
Importers
2 stars
Insert cell
Insert cell
Insert cell
sampleFeatureCollection = topojson.feature(
sampleTopology,
sampleTopology.objects["two-squares"]
)
Insert cell
Object.keys(sampleTopology.objects).map(key =>
topojson.feature(sampleTopology, sampleTopology.objects[key])
)
Insert cell
topojson.merge(sampleTopology, Object.values(sampleTopology.objects))
Insert cell
sampleTopology = JSON.parse(sampleTopoJsonData)
Insert cell
sampleTopoJsonData = `
{
"type":"Topology",
"transform":{
"scale": [1,1],
"translate": [0,0]
},
"objects":{
"two-squares":{
"type": "GeometryCollection",
"geometries":[
{"type": "Polygon", "arcs":[[0,1]],"properties": {"name": "Left_Polygon" }},
{"type": "Polygon", "arcs":[[2,-1]],"properties": {"name": "Right_Polygon" }}
]
},
"one-line": {
"type":"GeometryCollection",
"geometries":[
{"type": "LineString", "arcs": [3],"properties":{"name":"Under_LineString"}}
]
},
"two-places":{
"type":"GeometryCollection",
"geometries":[
{"type":"Point","coordinates":[0,0],"properties":{"name":"Origine_Point"}},
{"type":"Point","coordinates":[0,-1],"properties":{"name":"Under_Point"}}
]
}
},
"arcs": [
[[1,2],[0,-2]],
[[1,0],[-1,0],[0,2],[1,0]],
[[1,2],[1,0],[0,-2],[-1,0]],
[[0,-1],[2,0]]
]
}`
Insert cell
Insert cell
usCounties = topojson.feature(usTopology, usTopology.objects.counties).features
Insert cell
usStates = topojson.feature(
usTopology,
usTopology.objects.states
).features
Insert cell
usLand = topojson.feature(usTopology, usTopology.objects.land)
Insert cell
usTopology = d3.json(
"https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/us.json"
)
Insert cell
Insert cell
Insert cell
{
const [svgNode, svg] = getSVG(width, height);

svg
.append("path")
.attr("d", geopathGenerator(stateExteriors))
.style("stroke", "black")
.style("stroke-width", 1)
.style("fill", "none");

svg
.append("path")
.attr("d", geopathGenerator(stateInteriors))
.style("stroke", "green")
.style("stroke-width", 1)
.style("fill", "none");

return svgNode;
}
Insert cell
stateInteriors = topojson.mesh(
usTopology,
usTopology.objects.states,
(a, b) => a !== b
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
stateByUSPS = Object.fromEntries(
stateNamesAndCodes.map(d => [
d[" stusps"].trim(),
{ ID: d[" st"], name: d.stname }
])
)
Insert cell
Insert cell
Insert cell
usStates
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
countyNeighbors = topojson.neighbors(usTopology.objects.counties.geometries)
Insert cell
stateColorIndex = {
const index = Array(usStates.length);
stateNeighbors.forEach(
(A, i) => (index[i] = (d3.max(A, j => index[j]) + 1) | 0)
);

return index;
}
Insert cell
countyColorIndex = {
const index = Array(usCounties.length);
countyNeighbors.forEach(
(A, i) => (index[i] = (d3.max(A, j => index[j]) + 1) | 0)
);

return index;
}
Insert cell
getSVG = (width, height, margin = 0, outline = false) => {
const svgElement = DOM.svg(width, height);

let svg = d3.select(svgElement);
if (outline)
svg
.append("rect")
.attr("width", width)
.attr("height", height)
.style("stroke", "black")
.style("fill", "none");
if (margin)
svg = svg.append("g").attr("transform", `translate(${margin},${margin})`);
return [svgElement, svg];
}
Insert cell
Insert cell
usStatesSimplifiedTopology = {
let us_simplified = topojson.presimplify(usTopology);
let min_weight = topojson.quantile(us_simplified, weight);
us_simplified = topojson.simplify(us_simplified, min_weight);
return us_simplified;
}
Insert cell
usStatesSimplified = topojson.feature(
usStatesSimplifiedTopology,
usStatesSimplifiedTopology.objects.states
).features
Insert cell
viewof weight = slider({ min: 0, max: .1, step: 0.0001, description: "" })
Insert cell
Insert cell
Insert cell
md`#### US states by Region
See: https://en.wikipedia.org/wiki/List_of_regions_of_the_United_States
`
Insert cell
Insert cell
Insert cell
{
const [svgNode, svg] = getSVG(width, height, 0, true);

const divisions = svg
.selectAll("g")
.data(usDivisionFeatures)
.join("g");
divisions
.append("path")
.attr("d", geopathGenerator)
.style("fill", (d, i) => d3.schemeCategory10[i])
.append("title")
.text((d, i) => usDivisions[i]);

return svgNode;
}
Insert cell
usRegionFeatures = Array.from(usStatesGroupedByRegion.keys()).map(region =>
topojson.merge(
usTopology,
usTopology.objects.states.geometries.filter(d => inRegion(region, d.id))
)
)
Insert cell
usDivisionFeatures = Array.from(usStatesGroupedByDivision.keys()).map(division =>
topojson.merge(
usTopology,
usTopology.objects.states.geometries.filter(d => inDivision(division, d.id))
)
)
Insert cell
inRegion = (region, id) => {
const stateUSPS = stateNameByCode.get(id);
if (stateUSPS) {
return usRegionByState[stateUSPS[0][" stusps"].trim()] === region;
} else return false;
}
Insert cell
inDivision = (div, id) => {
const stateUSPS = stateNameByCode.get(id);
if (stateUSPS) {
return usRegionByDivision[stateUSPS[0][" stusps"].trim()] === div;
} else return false;
}
Insert cell
usRegions = Array.from(usStatesGroupedByRegion.keys())
Insert cell
usStatesGroupedByRegion = d3.group(usStateRegions, d=>d.Region)
Insert cell
usDivisions = Array.from(usStatesGroupedByDivision.keys())
Insert cell
usStatesGroupedByDivision = d3.group(usStateRegions, d => d.Division)
Insert cell
usRegionByState = Object.fromEntries(
usStateRegions.map(d => [d["State Code"], d["Region"]])
)
Insert cell
usRegionByDivision = Object.fromEntries(
usStateRegions.map(d => [d["State Code"], d["Division"]])
)
Insert cell
usStateRegions = (await FileAttachment("states.csv")).csv()
Insert cell
height = 500
Insert cell
projection = d3.geoAlbersUsa().fitSize([width, height], usLand);

Insert cell
geopathGenerator = d3.geoPath().projection(projection)
Insert cell
md`### External Libraries and Imports`
Insert cell
import { slider } from "@jashkenas/inputs"
Insert cell
import { soFetch } from '@alecglassford/so-fetch'
Insert cell
topojson = require("topojson")
Insert cell
d3 = require("d3@6")
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