【问题标题】:Shapefile to Topojson conversionShapefile 到 Topojson 的转换
【发布时间】:2015-02-17 07:11:04
【问题描述】:

我正在尝试转换可以在here 找到的 Ghana admin1 shapefile。我的最终目标是获得此question 中描述的TopoJSON。 我在 Linux 机器上使用过这个命令:

ogr2ogr -f GeoJSON GHA_adm1.json GHA_adm1.shp

它返回这个:

Unable to open datasource `GHA_adm1.shp' with the following drivers.
... here goes a long list of drivers...

我做错了什么?我应该安装其他“驱动程序”吗?但是怎么做?谢谢。

【问题讨论】:

  • 你使用nodejs,npm吗?如果是这样,你可以使用topojsonnpm包将shp转换成topojson。
  • 确实是@Hugolpz。在上面链接的问题中,该人首先将其文件转换为 GeoJson,然后再转到 TopoJson。而且我相信我确实使用了topojsonpackage。
  • 如果您的目标是转换为 topojson,则无论如何都使用 topojson 命令行工具。如果不需要任何高级功能(重投影、裁剪等),最好直接从 shp 转换为 topojson。

标签: geojson shapefile topojson


【解决方案1】:

下载 GIS 源码

GADM 是管理 GIS 文件的完美来源。

GADM.org > Download page :选择您的国家并格式化“shapefile” > 好的。

或通过终端(将新加纳代码GHA 替换为您的目标国家/地区的iso 代码):

# prepare folder
mkdir -p ./map; cd ./map
curl http://d3js.org/d3.v3.min.js -O -J
curl http://d3js.org/topojson.v1.min.js -O -J
# download data
curl \
    -L -C - 'http://biogeo.ucdavis.edu/data/gadm2/shp/GHA_adm.zip' \
    -o ./GHA_adm.zip
unzip -n ./GHA_adm.zip -d ./

shp 到 topojson

使用topojson command line,更直接。如果您想保留所有属性:

 topojson -q 1e4 \
          -o out.json \
          -- in1.shp in2.shp

您还可以从 shapfiles 中选择属性,然后随时重命名它们:

 topojson \
    --bbox \
    --id-property none \
    -p name=NAME_1 \
    -p code=ID_1 \
    -p L0=NAME_0 \
    -q 1e4 \
    --filter=small \
    -o GHA_adm_w.topo.json \
    -- admin_1=GHA_adm1.shp admin_2=GHA_adm2.shp

对于 GHA,.shp 中没有适合良好 ID 的属性。 NAME_1 有空格,这将在您的 HTML 中给出无效的 id。

检查json

使用 http://jsoneditoronline.org 。检查您的 json 将为您提供 what 数据可用以及 where(点符号路径)的线索。 topojson distillery 帮助预览任何 topojson 以及代码是否正确。

D3js 调用

<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
.L1 {
    fill: #E0E0E0;
    stroke: #FFF;
    stroke-width:1px;
}
</style>
<body>
<script src="./d3.v3.min.js"></script>
<script src="./topojson.v1.min.js"></script>
<script>
var mapIt = function(width, url){
    console.log("mapIt(): start");
    var height = width/960*500;
    var svg = d3.select('body').append('svg')
        .attr('width', width)
        .attr('height', height);
    var projection = d3.geo.mercator()
          .scale(1)
          .translate([0, 0]);

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

    d3.json(url, function (error, json) {
        var admin_1 = topojson.feature(json, json.objects.admin_1);

        /* Autofocus code comes here ! */

        svg.selectAll("path")
            .data(admin_1.features)
          .enter().append("path")
            .attr('d', path)
            .attr('class', 'L1');
    });    
};
mapIt(960,"http://somesite.org/data/NZL_adm.topo.json");
</script>
</body>
</html>

专注

正确的自动对焦需要来自Mike Bostocks 的一小段代码,例如here:

// Compute the bounds of a feature of interest, then derive scale & translate.
var b = path.bounds(admin_1),
    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];

// Update the projection to use computed scale & translate.
projection
    .scale(s)
    .translate(t);

编辑:现在应该可以工作了。现场演示:bl.ocks.org

【讨论】:

  • 谢谢!这很有帮助。我使用您发布的第二个命令将 .shp 转换为 topoJSON。但是,我不能让 html 工作。 (我使用了您的代码并调整了网址)。您能否给我您对the output file I got 的意见,以便我评估错误是否来自json、我的代码或其他原因。
  • jsoneditoronline.org 和 Distillery 的数据看起来都不错。这段代码我是凭记忆写的,需要检查一下。
  • 确实,我犯了一些小错误。工作演示现在在bl.ocks.org/hugolpz/4f8d6eaeaf41c72e268b
  • 出色的工作,对于像我这样的入门级用户来说非常有用。谢谢。
  • 我用你的问题写了一篇关于如何通过 gadm 和 D3js 制作地图的长期教程。
【解决方案2】:

在这里工作得很好:

GDAL 1.11.1,于 2014 年 9 月 24 日发布

也许您正在运行一个过时的 GDAL 版本?您可以使用以下方式检查您的版本:ogr2ogr --version

【讨论】:

  • 我的版本是GDAL 2.0.0dev, released 2014/04/16。
  • 那应该很棒:(我认为没有办法添加/删除驱动程序,我想最好尝试重新编译/安装。你可以随时尝试在@中问这个987654321@
  • 谢谢。我可能有两个并发的 GDAL 安装。我正在尝试查找文件。
猜你喜欢
  • 2013-05-31
  • 1970-01-01
  • 2015-03-20
  • 2017-08-24
  • 2015-03-28
  • 2014-05-12
  • 2017-10-31
  • 2014-09-02
  • 1970-01-01
相关资源
最近更新 更多