Shapefile到Topojson的转换

GPi*_*rre 3 shapefile geojson topojson

我正在尝试转换可在此处找到的加纳admin1 shapefile .我的最终目标是获得此问题中描述的TopoJSON .我在Linux机器上使用过这个命令:

ogr2ogr -f GeoJSON GHA_adm1.json GHA_adm1.shp
Run Code Online (Sandbox Code Playgroud)

它返回:

Unable to open datasource `GHA_adm1.shp' with the following drivers.
... here goes a long list of drivers...
Run Code Online (Sandbox Code Playgroud)

我做错了什么?我应该安装其他"司机"吗?但是怎么样?谢谢.

Hug*_*lpz 6

下载GIS源

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

GADM.org> 下载页面:选择您所在的国家/地区并格式化"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 ./
Run Code Online (Sandbox Code Playgroud)

shp to topojson

使用topojson命令行,它更直接.如果您想保留所有属性:

 topojson -q 1e4 \
          -o out.json \
          -- in1.shp in2.shp
Run Code Online (Sandbox Code Playgroud)

您还可以从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
Run Code Online (Sandbox Code Playgroud)

对于GHA,.shp中没有适合良好ID的属性.NAME_1有空格会id在HTML中无效.

在此输入图像描述

检查json

使用http://jsoneditoronline.org.检查你的JSON会给你的线索,什么数据是可用的,并且在那里(点符号路径).该topojson酒厂帮助预览任何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>
Run Code Online (Sandbox Code Playgroud)

焦点

正确的自动对焦将需要的一小段代码迈克Bostocks,例如在这里:

// 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);
Run Code Online (Sandbox Code Playgroud)

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