我正在使用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}}编辑:我也没有足够的分数来注册topojson标签,所以在创建该标签之前,我会将其列为D3。
发布于 2017-03-16 19:28:32
topojson中的这个函数现在已经移到了geo2topo中,不再提供编辑原始属性的方法。
输入要素对象的任何属性和标识符都将传播到输出。如果要转换或过滤属性,请尝试ndjson-cli,如命令行制图中所示。
我发现使用ndjson-cli在命令行上编辑所有属性比使用write my own script更容易。
/**
* 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!");
});
}发布于 2013-01-04 23:10:19
您是否在使用-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
发布于 2018-09-06 00:13:23
正如@ow3n所说,geo2topo和不再提供编辑原件属性的方法。因此,@james246伟大的答案不再适用于最新的包。
但我最终明白了如何使用ndjson-cli来做这件事。多亏了Mike Bostock自己在github issue thread中的回答,这几乎是一个复制粘贴,所以在网上犹豫是否要看一下原件。
首先安装新的软件包:
npm i -g shapefile ndjson-cli topojson-client topojson-server topojson-simplify然后分三步走:
步骤1:将Shapefile转换为换行符分隔的GeoJSON功能。
shp2json -n original.shp > myfile.ndjson步骤2:重新定义GeoJSON属性,您也可以重命名它们。
ndjson-map 'd.properties = {prop1: d.properties.prop1, p2: d.properties.prop2}, d' \
< myfile.ndjson \
> myfile-filtered.ndjson步骤3:将换行符分隔的GeoJSON转换为TopoJSON。
geo2topo -n myfile-filtered.ndjson > myfile-topo.json便笺
如果不再具有原始.shp文件,则可以使用ndjson-split将实际的.json文件转换为换行符分隔的GeoJSON要素
ndjson-split 'd.features' \
< myfile.json \
> myfile.ndjson然后按照步骤2中的说明进行操作。
https://stackoverflow.com/questions/14095316
复制相似问题