我正在使用topojson转换现有的GeoJSON数据集,而不是保留属性.它遵循标准的GeoJSON格式,并将属性放置在与几何(下面的代码片段)相同的"属性"对象中,但是当topojson成功完成时,我最终得到一个有效的topojson数据文件,我可以使用并显示在地图,但文件中没有任何属性.我没有指定属性,默认行为是在这种情况下保留所有属性,所以我很困惑.
{"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}}
Run Code Online (Sandbox Code Playgroud)
编辑:我也没有足够的积分来注册topojson标签,所以我会将其列为D3,直到创建该标签.
jam*_*246 21
你在用这个-p选项吗?
topojson in.json -o out.json - 删除所有属性
topojson in.json -o out.json -p - 保留所有属性
topojson in.json -o out.json -p prop1,prop2 - 只保留prop1和prop2
topojson 中的此函数现已移至geo2topo,并且不再提供编辑原始属性的方法。
输入要素对象的任何属性和标识符都会传播到输出。如果您想转换或过滤属性,请尝试使用 ndjson-cli,如命令行制图中所示。
我发现编写自己的脚本比使用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!");
});
}
Run Code Online (Sandbox Code Playgroud)