问题
I have US state topojson and Canada state topojson I want to merge them into single file. Can some one tell me how to merge two files into single topojson file. I'm using mercator projection while creating map
回答1:
I was facing a similar issue, and ended up converting topojson files to geojson, merging them with geojson-merge, finally converting to topojson.
As Canada and the US share some boundaries (arcs
in topojson), it should reduce the total file size.
for item in "us" "ca"
do
topo2geo tracts=$item-geo.json < $item-topo.json
done
geojson-merge *-geo.json > combined-geo.json
geo2topo tracts=combined-geo.json > combined-topo.json
回答2:
This doesn't address merging topojson files, but addresses the core problem of the question
A little late to the party here, but I was just trying to do the same thing. I can easily find us topo and Canada topo, but none together. I then somehow googled the right thing and stumbled upon a Merging TopoJSON States tutorial on blocks. Turns out, the tutorial uses a map of the US and Canada. Perfect! That's all I needed! If you look at the network traffic, you can get the URL to the topo json. Unfortunately, CORS isn't set up so you'll have to download the file and make your own copy to use it. But, it works perfectly:
Here's the JavaScript (the /topo
endpoint is just calling a python endpoint that returns the file I copied and pasted from the block)
function generateMap() {
return new Promise((resolve, reject) => {
d3.json('/topo').then(_topo => {
console.log(_topo);
const height = 1500;
const width = 960;
const path = d3.geoPath().projection(null);
const svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
svg.append('path')
.datum(topojson.feature(_topo, _topo.objects.usa_canada))
.attr('class', 'state')
.attr('d', path);
svg.append('path')
.datum(topojson.mesh(_topo, _topo.objects.usa_canada, (a, b) => a !== b))
.attr('class', 'state-boundary')
.attr('d', path);
resolve({ path: path, svg: svg });
});
});
来源:https://stackoverflow.com/questions/33390422/how-can-i-merge-multiple-topojson-files-into-single-topojson-file