本文介绍了TopoJSON属性保留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用topojson转换现有的GeoJSON数据集,并且它不保留属性.它遵循标准的GeoJSON格式,并将属性放置在与几何体相同的级别的属性"对象中(下面的代码段),但是当topojson成功完成时,我得到一个有效的topojson数据文件,可以在其中使用和显示该文件地图,但是文件中的任何地方都没有属性.我没有指定属性,在这种情况下,默认行为是保留所有属性,所以我感到困惑.

I'm using topojson to convert an existing GeoJSON dataset and it isn't preserving the properties. It follows the standard GeoJSON format and places the properties in a "properties" object at the same level as the geometry (snippet below) but when the topojson successfully completes, I end up with a valid topojson data file that I can use and display on a map, but there are no properties anywhere in the file. I did not specify properties, and the default behavior is to preserve all properties in that case, so I'm baffled.

{"type": "Feature", "geometry": {"type":"MultiLineString","coordinates":[[[12.06,37.97],[12.064,37.991]],[[12.064,37.991],[28.985,41.018]]]}, "properties": {"pair": 50129,"direction": 0,"month": 12,"priority": 0,"expense": 4.854,"duration": 20.423,"length": 2950.524}}

我也没有足够的点来注册topojson标签,因此在创建该标签之前,我将其列为D3.

edit: I also don't have enough points to register the topojson tag, so I'll list this as D3 until that tag is created.

推荐答案

topojson中的此函数现已移至 geo2topo ,并且不再提供编辑原始属性的方法.

This function in topojson has now been moved to geo2topo and no longer provides a way to edit properties of the original.

我发现编写自己的脚本比使用以下命令在命令行上编辑所有属性要容易 ndjson-cli .

I found it was easier to write my own script than edit all properties on the command line using ndjson-cli.

/**
 *  Remove unwanted geojson feature properties
 */

var fs = require('fs');

var inputFile = 'input.geojson',
    outputFile = 'output.geojson',
    remove = ["properties","to","remove"];

function editFunct(feature){
    feature.TID = feature.properties.TID; // set the TID in the feature
    return feature;
}

removeGeojsonProps(inputFile,outputFile,remove,editFunct);

function removeGeojsonProps(inputFile,outputFile,remove,editFunct){

    // import geojson
    var geojson = JSON.parse(fs.readFileSync(inputFile, 'utf8'));

    // for each feature in geojson
    geojson.features.forEach(function(feature,i){

        // edit any properties
        feature = editFunct(feature);

        // remove any you don't want
        for (var key in feature.properties) {   

            // remove unwanted properties
            if ( remove.indexOf(key) !== -1 )
                delete feature.properties[key];
        }
    });

    // write file
    fs.writeFile(outputFile, JSON.stringify(geojson), function(err) {
        if(err) return console.log(err);
        console.log("The file was saved!");
    }); 
}

这篇关于TopoJSON属性保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 13:01