【问题标题】:topoJSON on D3: map doesn't show (but it works on www.mapshaper.org)D3 上的 topoJSON:地图不显示(但它适用于 www.mapshaper.org)
【发布时间】:2014-09-05 21:27:14
【问题描述】:

我正在尝试使用英国县地图复制 http://bl.ocks.org/mbostock/4060606

我遵循了以下步骤 - 几乎是 http://bost.ocks.org/mike/map 上的建议: 1)我从军械测量局下载了shapefile并使用qGIS提取了一些数据 2) 准备好后,我使用 ogr2ogr 将 shapefile 转换为 GeoJSON 3) 我将 GeoJSON 转换为 topoJSON,确保保留了 ID

我几乎从 mbostock 复制了 choropleth 的原始示例。然而,我得到的不是一张漂亮的地图,而是一个……圆圈。我想知道我的投影是否有一些错误?

为了完整起见,这是页面的 javascript 部分:

var width = 960,
    height = 600;

var rateById = d3.map();

var quantize = d3.scale.quantize()
    .domain([0, .15])
    .range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));

var projection = d3.geo.albers()
          .center([0, 55.4])
          .rotate([4.4, 0])
          .parallels([50, 60])
          .scale(50)
          .translate([width / 2, height / 2]);

var path = d3.geo.path()
    .projection(projection);


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

queue()
    .defer(d3.json, "uk.topo.json")
    .defer(d3.tsv, "unemployment.tsv", function(d) { rateById.set(d.id, +d.rate); })
    .await(ready);

function ready(error, uk) {
    svg.append("g")
     .attr("class", "counties")
     .selectAll("path")
     .data(topojson.feature(uk, uk.objects.counties).features)
     .enter().append("path")
//     .attr("class", function(d) { return quantize(rateById.get(d.id)); })
     .attr("class", "q5-9" )
     .attr("d", path);

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

d3.select(self.frameElement).style("height", height + "px");

各县的topoJSON太大,这里贴不上来,大致是这样的:

{"type":"Topology","objects":{"counties":{"type":"GeometryCollection","bbox":[220956.7,35190.3,655967.1,586683],"geometries":[{"type":"Polygon","properties":{"name":"Buckinghamshire"},"id":11901,"arcs":[[-1,-2,-3,-4,-5,-6]]},{"type":"Polygon","properties":{"name":"Cambridgeshire"},"id":1386,"arcs":[[-7,-8,-9,-10,-11,-12,-13,-14]]},{"type":"Polygon","properties":{"name":"Cumbria"},"id":13244,"arcs":[[-15,-16,-17]]},{"type":"Polygon","properties":{"name":"Derbs"},"id":13688,"arcs":[[-18,-19,-20,-21],[-22]]},...},"arcs":[[[5876,2688],[-67,53],[-21,101],[7,65],[96,66],[-7,66],[-78,69],[-234,12],[-5,42],...},"transform":{"scale":[43.5053905390539,55.15478547854785],"translate":[220956.7,35190.3]}}

我在这里不是一个很好的专家,所以我可能会做一些根本错误的事情。不过,我有一个确定:

有什么想法吗?如果需要,我很乐意粘贴完整的文件。

谢谢!

【问题讨论】:

  • 不要粘贴它们,将它们添加到 jsfiddle 以便我们可以摆弄
  • 我没听说过mapshaper,谢谢提醒。
  • 谢谢@albert。如果您想看看,这里是 topoJSON:Counties
  • 哈哈。我的意思是你所有的代码。
  • 嗯,除了包含,d3部分的代码都在那里。无论如何,我已经接受@herrstucki 的回复为有效。

标签: javascript d3.js gis geojson topojson


【解决方案1】:

坐标似乎已经投影(即笛卡尔坐标)。

在这种情况下你应该使用

d3.geo.path().projection(null);

但请确保首先将您的 topojson 缩放到所需的大小

topojson --width=960 --height=500 --margin=10 --cartesian -o out.json -- in.shp

或者首先使用ogr2ogr重新投影shapefile

ogr2ogr -t_srs EPSG:4326 out.shp in.shp

【讨论】:

  • 谢谢@herrstucki。我会试试这个。您是否建议直接在 ShapeFile 上使用 topojson?我不知道 topojson 命令可以直接在 shapefile 上工作,谢谢!我会在我尝试后报告。
  • 只是一个后续问题,@herrstucki。如果我有 2 个 shapefile 需要显示怎么办?我正在使用军械测量地图。我的目标是有国家大纲和县。但是,如果我在两个 shapefile 上运行该 topojson 命令,我会发现两个地图都缩放到 960x500 - 这意味着它们的比例不同(县仅适用于英格兰,所以我以 960x560 得到英格兰,而使用第二个 shapefile整个国家被缩放到 960x560)。有没有一种通用的方法可以做到这一点,还是我采用了错误的方法?
  • 只需将两个 shapfile 都提供给一个 topojson 命令:topojson [your options] -- a.shp b.shp。或者,您可以显式命名生成的 topojson 对象:topojson [your options] -- foo=a.shp bar=b.shp
  • 我认为你的问题是坐标系,EPSG:4326。我有同样的问题。军械调查有其自己的格式。您必须使用诸如 ogr2ogr 或 QGIS 之类的工具将其转换为 WSG 84,就像您这个答案一样。从教程等中看不出这是一个重要的步骤。
猜你喜欢
  • 2015-02-21
  • 1970-01-01
  • 2013-10-01
  • 2016-08-04
  • 2014-09-23
  • 2014-01-24
  • 2014-06-18
  • 2016-11-11
  • 1970-01-01
相关资源
最近更新 更多