Topojson添加外部属性而没有任何明显的映射上下文

问题描述 投票:1回答:1

目标是将外部属性添加到topojson。我成功地将各种国家的http://www.diva-gis.org/gdata的shapefile转换为json文件。我还成功地将这些文件下采样到更前端友好的文件大小。像这样:

shp2json CHN_adm0.shp --out CHN_adm0.json
geo2topo CHN_adm0.json > china.json
shp2json CHN_adm1.shp --out CHN_adm1.json
geo2topo CHN_adm1.json > china.json
toposimplify -s 1e-9 -f < china.json > china-topo.json
toposimplify -s 1e-9 -f < china1.json > china1-topo.json

然后我将国家级多边形与省级多边形合并。如果this是正确的,也许这就是我可能附加外部属性的地方,但是你会看到我可能还有另外一个障碍:

geo2topo regions=CHN_adm1.json country=CHN_adm0.json > china-regions-topo.json
toposimplify -s 1e-9 -f < china-regions-topo.json > china-regions.json

在这个时刻我不确定的是如何将属性附加到diva-gis的数据中。这是dict / object的一个示例切片以及我的china-regions.json最终结果的相关键(注意:我正在使用python来探索json文件,但我不会真正使用python进行任何操作否则有问题):

len(json['objects']['regions']['geometries'])
>> 31 ## 31 provinces in China

json_data['objects']['regions'].keys()

>> dict_keys(['type', 'bbox', 'geometries']) ## seems like there is nothing to map to, like an id or province name

如何将外部属性添加到我从diva-gis的shapefile转换的topojsons?似乎没有什么可以映射到。在探索数据后,我甚至无法确定哪个省是哪个省;它只是一个弧形列表 - 它在地图上绘制得足够好,但是没有什么直觉可以让人类辨别出哪个省。

进一步澄清:

  • 外部属性格式是任意的;想象一下省名和数字的csv,比如省的人口。
  • 不反对投入一些肘部油脂,但我想尽可能避免反复试验
  • json文件中区域的顺序未知。不确定diva-gis是否会遵守任何特定的约定(字母顺序等)
json topojson
1个回答
0
投票

您即将找到一个标识符 - 它们通常是每个几何体的大孩子:

topojson.objects.regions[n].geometry.properties.property // where property is a column in the original shapefile.

使用shp2json,您不能使用--geometry或--ignore-properties,否则您将忽略dbf文件。 dbf文件包含每个功能的属性

这些属性是shapefile的属性表的列。如果此处没有任何内容,则表示您未将包含此数据的dbf文件作为转换过程的一部分。

只需使用javascript,我就可以获得每个几何的属性列表:

    // Get the geometries:
    var regions = data.objects.regions.geometries;

    // List of properties present (in first element anyways):
    console.log(Object.keys(regions[0].properties));

要使用javascript和d3的map方法添加新属性,我可能会使用:

    // Create a map to look up regions:
    var map = d3.map(regions, function(d) { return d.properties.NAME_1; });

    // Get by Name_1:
    console.log(map.get("ExampleName"));        

    // New data to join to topojson:
    var dataToAttach = [
        { region: "A", capital: "X" },
        { region: "B", capital: "Y" },
        { region: "C", capital: "Z"}  //...
    ]

    // Add new property:
    dataToAttach.forEach(function(d) {
      map.get(d.region).capital = d.capital;
    })

这里最常见的问题是标识符与您要加入的表格数据不完全匹配或根本不匹配,这通常需要修改。

Here's上面的一个实现(在客户端虽然),使用和添加数据到topajson创建从diva gis数据。

© www.soinside.com 2019 - 2024. All rights reserved.