问题
GIS to Topojson:
I'm using DIVA-gis.org for my data source. I've downloaded the data for Armenias administrative areas. This dowload produces a .shp file. I've tried to pass it directly to topoJSON and I get a json file as follows
Object {
type: "Topology",
objects: Object,
arcs: Array[1],
transform: Object
}
Topojson to SVG:
I'm pointing d3 to objects.armenia which contains my geometries property. The problem is that that geometries property contains an array of arcs which is 0. Now within my topoJSON of armenia I do see an arcs property array that contains quite a big array of values... I've tried pointing my d3 code to that element but I still get nothing.
Aside from that I've also used QGIS to convert my .shp file into geoJSON and them use topoJSON to go from geoJSON from topoJSON... but still nothing.
My d3 code is as follows
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.scale(1000)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var mapContainer = d3.select("#armenia").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("/ARM_adm/output.json", function(error, country) {
console.log("country",country)
mapContainer.insert("path", ".graticule")
.datum(topojson.feature(country, country.armenia))
.attr("class", "land")
.attr("d", path);
});
I simply get an empty path... any help would be much apreciated.
回答1:
Being a windows user I don't use topojson all that often and when I do I use a virtual machine, so I won't comment on the topojson process. What I tend to do instead is to use some online topojson converters for quick conversion. There are a number services available such as:
- Shape Escape;
- GeoJson-TopoJson;
- MapShaper; and
- The Distillery.
All of which have the positives and negatives.
I downloaded the data you mentioned above from DIVA and used Shape Escape to translate into Topojson. One thing to note with Shape Escape is that it can give you some silly names for the objects. Just open the downloaded topojson file and change the names to something useful.
I then used your code to map it, the first thing was to check the data which was all fine but no map was being displayed. There were two reasons for this. The first was the unpacking of the topojson in the
.datum(topojson.feature(country, country.arcs[0]))
line. You were asking D3 to map the arcs not the geometry. If you have a look at the console.log and dig down into the objects you'll see the geometry there. So if you point topojson at the geometry D3 will have something to work. So the datum line becomes:
.datum(topojson.feature(country, country.objects.states))
You were also using an Albers projection, this tends to work best with US data, so I changed that to Mercator and played about with the scale and center and everything was fine. The Projection ended up being:
var projection = d3.geo.mercator()
.scale(5000)
.center([45.55, 40.5])
.translate([width / 2, height / 2]);
来源:https://stackoverflow.com/questions/20029434/convert-shp-to-topojson-failing