在一个文件中创建具有不同图层的shapefile格式的全新世界

时间:2015-01-20 13:46:04

标签: d3.js geojson shapefile qgis topojson

我想创建一个新的世界地图,并使用 D3.js topojson ,如果可能的话 QGIS 。所以最后我需要一个包含国家,河流等的 shapefile 。创建所有这些图层后,我希望该文件由 mapshaper 转换,以便结果为 topojson 。到目前为止一切都很好。

我用一个简单的多边形尝试了它,所以它只是一层。我创建了shapefile,然后将其转换为topojson。现在我想将该文件与d3一起使用:

{"type":"Topology","transform":{"scale":[0.8423423423423423,0.808252427184466],"translate":[1491.6423611111095,-3184.7916666666715]},"objects":{"country":{"type":"GeometryCollection","geometries":[{"type":"LineString","arcs":[0]}]}},"arcs":[[[437,17],[-7,87],[69,64],[23,74],[-31,80],[-14,54],[19,69],[-9,18],[-45,-35],[-53,-32],[-33,-12],[-26,-38],[-26,-27],[-29,-15],[-54,-12],[-29,22],[-40,27],[-31,10],[-36,35],[-2,30],[-17,57],[-14,19],[-19,28],[-31,62],[-2,47],[7,39],[17,40],[28,57],[119,37],[180,22],[247,-2],[105,-109],[78,-122],[52,-96],[55,-176],[-12,-109],[-47,-69],[-60,-77],[-104,-57],[-181,-7],[-49,30]]]}

这只是一个多边形,这是正确的。这是我的d3代码:

var width = 960,
    height = 500;

var path = d3.geo.path();

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.json("file.json", function(error, topology) {

  console.clear();

  var featureCollection = topojson.feature(topology, topology.objects.country);
  var bounds = d3.geo.bounds(featureCollection);

  var centerX = d3.sum(bounds, function(d) {return d[0];}) / 2,
      centerY = d3.sum(bounds, function(d) {return d[1];}) / 2;

  var projection = d3.geo.mercator()
    .scale(3000)
    .center([centerX, centerY]);

  path.projection(projection);

  svg.selectAll("path")
      .data(featureCollection.features)
      .enter().append("path")
      .attr("d", path);
});

但我总是得到“TypeError:t is undefined”。但是,如果我使用其他人的json文件,它正在工作。那么如何让这个小例子运行呢?我的QGIS是否有问题,或者我使用它错了?感谢。

或者你知道更好的解决方法吗?

1 个答案:

答案 0 :(得分:0)

只需删除

.scale(3000)
.center([centerX, centerY])

它应该有效。或者将其编辑为正确的值。