Showing only part of a topoison map using D3.js

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.

+7
javascript windows topojson geojson
source share
2 answers

You are very close. Without checking the code, I see one important problem. The second parameter of your JSON callback is mt_topo , which you use when defining

var states = topojson.feature(mt_topo, mt_topo.objects.states)

However, later you use us as your callback object, presumably because this is what Mike Bostock used in your example. Instead, it should be as follows:

 svg.append("path") .datum(topojson.mesh(mt_topo, mt_topo.objects.states,function(a, b) {return a !== b;})) .attr("d", path); 

However, your question really depends on whether the census maps have a β€œstate” function. I assume that any geometry you use does not have a state function, and that is why you get an error. When using the topojson command-line tool, the function name (i.e. Data.objects.x) is usually independent of the input file name, so if your file was US_Census_2010.shp , you need to define the states as

var states = topojson.feature(mt_topo, mt_topo.objects.US_Census_2010)

Open your mt_geo.json file and see what names you named. Hope this helps!

+1
source share

A GeoJSON file representing a map usually has one or more functions. Each function may have zero or more properties. Typically, properties are used to store metadata for places (state, county name, etc.). You can read your GeoJSON file and see what properties it has and use these properties to show or hide functions.

When converting a file from GeoJSON to TopoJSON, you can save or delete the original properties . For example, the functions and points in the uk.json file from the tutorial Let Make a Map have the name property.

0
source share

All Articles