Skip to content

Instantly share code, notes, and snippets.

@sjwhitworth
Last active December 21, 2015 06:09
Show Gist options
  • Select an option

  • Save sjwhitworth/6262467 to your computer and use it in GitHub Desktop.

Select an option

Save sjwhitworth/6262467 to your computer and use it in GitHub Desktop.
<script>
var width = 1500;
var height = 1500;
var color = d3.scale.linear()
.domain([0, 350000])
.range(["white", "#3498db"]);
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0.8);
var format = d3.format(".0%")
//Create an SVG element for us to use.
var svg = d3.select("body").append("svg").attr("width", width).attr("height", height);
d3.json("statgeo.json", function(uk){
//Gets the coordinate data from our dataset.
var subunits = topojson.feature(uk, uk.objects.thenewestdict1);
//Allows us to check the validity of our data in the console.
console.log(subunits);
//Creates a projection which we can apply to the path.
var projection = d3.geo.mercator()
.center([-0.10,51.5171])
.rotate([0,0])
.scale(95000)
.translate([width / 2, height / 3]);
var path = d3.geo.path().projection(projection);
svg.append("g")
.attr("class", "boroughs")
.selectAll("path")
.data(topojson.feature(uk, uk.objects.thenewestdict1).features)
.enter().append("path")
.attr("d", path)
.attr("id", function(d){return d.properties.Name})
.attr("fill", function(d){return color(d.properties.population)})
.attr("stroke", "black")
.attr("stroke-width", "0.35");
svg.selectAll("path")
.data(topojson.feature(uk, uk.objects.thenewestdict1).features)
.on("mouseover", function(d){div.transition().duration(200);
div.html("<b>" + d.properties.Name + "</b><br>"
+ "Population: " + d.properties.population + "<br>" +
"Average house price: £" + d.properties.avghouseprice + "<br>"
+ "Percent renting from landlord: " + d.properties.percentrent + "<br>"
+ "New homes built 2011/2012: " + d.properties.numnewhomes + "<br>" +
"People per new home built: " + d3.round(d.properties.peoplepernewhome))});
});
</script>
import csv, json, os
boroughmetrics = {}
geojson = json.load(open('clean.json'))
csvdata = csv.reader(open('london-borough-profiles.csv', 'U'))
csvdata.next() #Skip headers.
for line in csvdata:
tempdict = {}
csvname = line[0]
tempdict = {'population': int(line[1]),
'avghouseprice': int(line[2]),
'numnewhomes': int(line[3]),
'percentrent': float(line[4]),
'peoplepernewhome': float(line[1]) / int(line[3])}
for borough in geojson['objects']['thenewestdict1']['geometries']:
boroughname = borough['properties']['Name']
if csvname == boroughname:
borough['properties'].update(tempdict)
with open('statgeo.json', 'w') as outfile:
json.dump(geojson, outfile)
os.system("topojson -o stattopo.json statgeo.json -p")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment