Skip to content

Instantly share code, notes, and snippets.

@nsivertsen
Last active March 10, 2017 19:33
Show Gist options
  • Save nsivertsen/119a7bf7f11d2384b0084125fe6e0b0e to your computer and use it in GitHub Desktop.
Save nsivertsen/119a7bf7f11d2384b0084125fe6e0b0e to your computer and use it in GitHub Desktop.
Berlin Population Density 2015
license: mit
height: 860
border: no
.DS_Store
node_modules
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.block {
/* stroke: none;*/
}
.borders {
fill: none;
opacity: .6;
stroke: #000;
stroke-width: .5;
stroke-linejoin: round;
stroke-linecap: round;
}
</style>
<body>
<svg width="960" height="860"></svg>
<script src="//d3js.org/d3.v4.min.js" charset="utf-8"></script>
<script src="//d3js.org/topojson.v2.min.js"></script>
<script src="//d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var path = d3.geoPath()
.projection(null);
var threshold = d3.scaleThreshold()
.domain([1, 10, 50, 200, 500, 1000, 1300])
.range(d3.schemeOrRd[7]);
var formatNumber = d3.format(".0f");
var x = d3.scaleLinear()
.domain([0, 1300])
.range([0, 920]);
var xAxis = d3.axisBottom(x)
.tickSize(13)
.tickValues(threshold.domain())
.tickFormat(function(d) { return d === 1 ? '' : formatNumber(d); });
var legend = svg.append("g").call(xAxis);
legend.attr("transform", "translate(20,820)");
legend.select(".domain")
.remove();
legend.selectAll("rect")
.data(threshold.range().map(function(color) {
var d = threshold.invertExtent(color);
if (d[0] == null) d[0] = x.domain()[0];
if (d[1] == null) d[1] = x.domain()[1];
return d;
}))
.enter().insert("rect", ".tick")
.attr("height", 8)
.attr("x", function(d) { return x(d[0]); })
.attr("width", function(d) { return x(d[1]) - x(d[0]); })
.attr("fill", function(d) { return threshold(d[0]); });
legend.append("text")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.attr("y", -6)
.text("Inhabitants / ha");
d3.json("population.json", function(error, einwohnerdichte) {
if (error) return console.error(error);
svg.append("g")
.attr("class", "blocks")
.selectAll("path")
.data(topojson.feature(einwohnerdichte, einwohnerdichte.objects.blocks).features)
.enter().append("path")
.attr("class", "block")
.attr("fill", function(d) { return d3.schemeOrRd[7][d.properties.density]; })
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(einwohnerdichte, einwohnerdichte.objects.blocks), function(a, b) { return a !== b; })
.attr("class", "borders")
.attr("d", path);
});
</script>
population.json: population.xml
ogr2ogr -f GeoJSON /vsistdout/ $< \
-s_srs http://spatialreference.org/ref/epsg/25833/ \
-t_srs http://spatialreference.org/ref/epsg/4326/ \
| ./node_modules/.bin/ndjson-cat \
| ./node_modules/.bin/geoproject \
'd3.geoConicEqualArea().parallels([50, 55]).rotate([0, 0]).fitSize([960, 800], d)' \
| ./node_modules/.bin/ndjson-split 'd.features' \
| ./node_modules/.bin/ndjson-map -r d3 \
'{ \
type: d.type, \
properties: { \
density: d3.bisect([1, 10, 50, 200, 500, 1000, 1300], (+d.properties.EW_HA2015 || 0)), \
},\
geometry: d.geometry, \
id: d.properties.spatial_name \
}' \
| ./node_modules/.bin/ndjson-reduce \
| ./node_modules/.bin/ndjson-map \
'{type: "FeatureCollection", features: d}' \
| ./node_modules/.bin/geo2topo \
blocks=- \
| ./node_modules/.bin/topomerge \
-k \
'd.properties.density' \
blocks=blocks \
| ./node_modules/.bin/toposimplify \
-p 1 -f \
| ./node_modules/.bin/topo2geo \
blocks=- \
| ./node_modules/.bin/geo2topo \
blocks=- \
| ./node_modules/.bin/topoquantize \
1e4 \
> $@
population.xml:
curl -o $@ 'http://fbinter.stadt-berlin.de/fb/wfs/geometry/senstadt/re_einwohnerdichte2015/?service=wfs&request=GetFeature&version=2.0.0&typeNames=fis:re_einwohnerdichte2015&outputFormat=gml2'
.PHONY : clean
clean :
-rm population.xml \
population.json
{
"name": "population-density-berlin",
"version": "1.0.0",
"main": "index.js",
"author": "Nikolai Sivertsen",
"license": "MIT",
"dependencies": {
"d3": "^4.7.2",
"d3-geo-projection": "^1.2.1",
"ndjson-cli": "^0.3.0",
"topojson": "^2.2.0"
}
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment