I use the D3.js library to create maps from US Census shapefiles. I am looking to create an entire map of the USA that is not a problem, and maps for each state.
My workflow uses census data, which is changed by ogr2ogr on the command line as necessary, and then converted to topojson or geojson on shpescape.com due to errors in loading the topojson node.js module (see below for an edited solution to this problem) .
My question is more about a PRACTICAL question than about anything else - when presenting this code (with modeling http://bl.ocks.org/mbostock/4707858 ):
var width = 640, height = 500; var projection = d3.geo.albers(); var path = d3.geo.path() .projection(projection); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); d3.json("mt_geo.json", function(error, mt_topo) { var states = topojson.feature(mt_topo, mt_topo.objects.states), state = states.features.filter(function(d) { return d.id === 34; })[0]; projection .scale(1) .translate([0,0]); var b = path.bounds(state), s = .95 / Math.max ((b[1][0]-b[0][0])/width, (b[1][1]-b[0][1])/height), t = [(width-s*(b[1][0]+b[0][0]))/2, (height-s*(b[1][1]+b[0][1]))/2]; projection .scale(s) .translate(t); svg.append("path") .datum(states) .attr("class", "feature") .attr("d", path); svg.append("path") .datum(topojson.mesh(us, us.objects.states,function(a, b) {return a !== b;})) .attr("d", path); svg.append("path") .datum(state) .attr("class", "outline") .attr("d", path);
It not only throws an error in the string "var states" that says "cannot read the property type undefined", but I also donβt know that I should go into an anonymous function, or what mt_topo.objects.states should refer to. There is no good documentation on this kind of GIS. Do all census maps have "state" functions? Do you lose this information when compressing .shp in topojson?
Simply, if d3.json accepts (object, function (error, json)), what would an example of what actually work look like?
EDIT: HISTORY AND WINDOW 7 IDYNOSYNCRAZRA -----
Most tutorials suggest you use the module from node.js, but I'm on Windows7, and the canonical command line "npm install -g topojson" fails "on context". The creator sent me a link to get around this problem.
This is important because there is a flag on the command line for topojson in which you can package existing functions in geojson into an accessible object in topojson. For example, the above code uses βstatesβ in topojson - something meaningless and inaccessible if you do not use the following command:
topojson -o us.topojson -- states=us_states.json
The space between double hyphens and states is important. You can then access the states through us.objects.states, as shown in the source code above.