Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ASGS 2011 Mesh Block Statistics and Graphs
#!/usr/bin/perl -w
# Converts CSV data into JSON as required by NVD3.js
# This script is licensed CC0 by Andrew Harvey <andrew.harvey4@gmail.com>
#
# To the extent possible under law, the person who associated CC0
# with this work has waived all copyright and related or neighboring
# rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/
use strict;
use JSON;
# check usage
if (!defined $ARGV[0]) {
print STDERR "Usage: $0 <dataset_name>\n";
exit 1;
}
# make a new JSON OO object using the pretty printer
my $json = JSON->new->pretty;
# make a new JSON OO object without using the pretty printer
#my $json = JSON->new;
# list of rows (where rows are hashrefs)
my @rows;
while (<STDIN>) {
chomp;
my ($label, $value) = split /,/;
my $row_ref = { "label" => $label, "value" => $value };
push @rows, $row_ref;
}
my $hash_ref = { "key" => "$ARGV[0]", "values" => \@rows};
# convert from perl data structures to JSON
print $json->encode($hash_ref);
#!/bin/bash
# This script is licensed CC0 by Andrew Harvey <andrew.harvey4@gmail.com>
#
# To the extent possible under law, the person who associated CC0
# with this work has waived all copyright and related or neighboring
# rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/
# declare functions
csv () {
psql --output="csv/$1.csv" --no-align --field-separator=',' --tuples-only --command="$2"
}
mkdir -p csv
# create csv files
csv category_counts 'SELECT category, count(code) FROM asgs_2011.mb GROUP BY category ORDER BY count DESC;'
csv category_areas 'SELECT category, sum(ST_Area(geom)) FROM asgs_2011.mb GROUP BY category ORDER BY sum DESC;'
# ## Before you Run
# You need to have a PostgreSQL database up and running with data
# populated by asgs2pgsql - https://github.com/andrewharvey/asgs2pgsql
#
# You also need to ensure you PG* environment variables are set up.
# See http://www.postgresql.org/docs/current/static/libpq-envars.html
# ## License
# This script is licensed CC0 by Andrew Harvey <andrew.harvey4@gmail.com>
#
# To the extent possible under law, the person who associated CC0
# with this work has waived all copyright and related or neighboring
# rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/
all : csv json
csv :
./make-csv.sh
json :
mkdir -p json
./csv2json.pl "Mesh Block Counts by Category" < csv/category_counts.csv > json/asgs_2011-mb-counts-by-category.json
./csv2json.pl "Mesh Block Areas by Category" < csv/category_areas.csv > json/asgs_2011-mb-areas-by-category.json
clean :
rm -rf csv json
/*
The data following is derived from the Australian Bureau of Statistics <a href="http://abs.gov.au/websitedbs/D3310114.nsf/home/Australian+Statistical+Geography+Standard+(ASGS)">Australian Statistical Geography Standard 2011</a>, Mesh Block Structure, which is © Commonwealth of Australia and used under the terms of the <a href="http://creativecommons.org/licenses/by/2.5/au/">Creative Commons Attribution 2.5 Australia license</a>.
*/
// The JSON generated by https://gist.github.com/3192345
var mbCountsByCategory = [
{
"values" : [
{
"value" : "218268",
"label" : "Residential"
},
{
"value" : "63504",
"label" : "Parkland"
},
{
"value" : "25348",
"label" : "Agricultural"
},
{
"value" : "17007",
"label" : "Commercial"
},
{
"value" : "8217",
"label" : "Education"
},
{
"value" : "7636",
"label" : "Industrial"
},
{
"value" : "4963",
"label" : "Water"
},
{
"value" : "1587",
"label" : "Transport"
},
{
"value" : "780",
"label" : "Hospital/Medical"
},
{
"value" : "209",
"label" : "Other"
},
{
"value" : "78",
"label" : "SHIPPING"
},
{
"value" : "11",
"label" : "MIGRATORY"
},
{
"value" : "9",
"label" : "NOUSUALRESIDENCE"
},
{
"value" : "9",
"label" : "OFFSHORE"
},
{
"value" : "1",
"label" : "ANTARCTICA"
}
],
"key" : "Mesh Block Counts by Category"
}
];
var mbAreasByCategory = [
{
"values" : [
{
"value" : "",
"label" : "SHIPPING"
},
{
"value" : "",
"label" : "NOUSUALRESIDENCE"
},
{
"value" : "",
"label" : "MIGRATORY"
},
{
"value" : "",
"label" : "ANTARCTICA"
},
{
"value" : "",
"label" : "OFFSHORE"
},
{
"value" : "8735597731289.3",
"label" : "Agricultural"
},
{
"value" : "814972676479.451",
"label" : "Parkland"
},
{
"value" : "55298999431.0938",
"label" : "Water"
},
{
"value" : "37317863100.6699",
"label" : "Residential"
},
{
"value" : "3502212306.95312",
"label" : "Other"
},
{
"value" : "2821416542.81445",
"label" : "Industrial"
},
{
"value" : "1455170802.60547",
"label" : "Commercial"
},
{
"value" : "797830048.066406",
"label" : "Education"
},
{
"value" : "263896961.103516",
"label" : "Transport"
},
{
"value" : "74288617.5273438",
"label" : "Hospital/Medical"
}
],
"key" : "Mesh Block Areas by Category"
}
];
<!DOCTYPE html>
<html lang="en">
<!--
This file is licenced CC0 http://creativecommons.org/publicdomain/zero/1.0/
-->
<head>
<meta charset="utf-8">
<title>ABS ASGS 2011 Mesh Block Statistics</title>
<link href="/javascript/nvd3/nv.d3.css" rel="stylesheet" type="text/css">
<script src="/javascript/d3/d3.js"></script>
<script src="/javascript/nvd3/nv.d3.min.js"></script>
<script src="mesh-block-graphs-data.js"></script>
</head>
<body>
<h1>ABS ASGS 2011 Mesh Block Statistics</h1>
<h2 id="mbAreasByCategoryTitle">Mesh Blocks Total Area by Category</h2>
<svg id="mbAreasByCategory"></svg>
<h2 id="mbAreasByCategoryTitle">Mesh Block Counts by Category</h2>
<svg id="mbCountsByCategory"></svg>
<p><a href="/leaflet.html#lat=-28;lon=133;zoom=5;map=asgs-2011-mb">See here for a geographic representation of the mesh blocks.</a></p>
<footer>These graphs are derived from data obtained from the Australian Bureau of Statistics <a href="http://abs.gov.au/websitedbs/D3310114.nsf/home/Australian+Statistical+Geography+Standard+(ASGS)">Australian Statistical Geography Standard 2011</a>, Mesh Block Structure, which is © Commonwealth of Australia and used under the terms of the <a href="http://creativecommons.org/licenses/by/2.5/au/">Creative Commons Attribution 2.5 Australia license</a>.</footer>
<script>
nv.addGraph(function() {
var width = 500,
height = 500;
var chart = nv.models.pieChart()
.x(function(d) { return d.label })
.y(function(d) { return d.value })
.color(d3.scale.category10().range())
.valueFormat(d3.format(',f'))
.width(width)
.height(height);
d3.select("#mbCountsByCategory")
.datum(mbCountsByCategory)
.transition().duration(1200)
.attr('width', width)
.attr('height', height)
.call(chart);
return chart;
});
nv.addGraph(function() {
var width = 500,
height = 500;
var chart = nv.models.pieChart()
.x(function(d) { return d.label })
.y(function(d) { return d.value / Math.pow(10,6) }) // x 10^6 for km^2
.color(d3.scale.category10().range())
.valueFormat(d3.format(',f'))
.width(width)
.height(height);
// FIXME help how do I override this?
//chart.tooltipContent = function (key, y, e, graph) {
// return '<h3>' + key + '</h3>' +
// '<p>' + y + ' km<sup>2</sup></p>'
// };
d3.select("#mbAreasByCategory")
.datum(mbAreasByCategory)
.transition().duration(1200)
.attr('width', width)
.attr('height', height)
.call(chart);
return chart;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment