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}}
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.
As @ow3n said, geo2topo and no longer provides a way to edit properties of the original. Thus @james246 great answer no longer works on up-to-date package.
But I finally understood how to do it using ndjson-cli. Thanks to Mike Bostock own answer in a github issue thread, this is almost a copy paste so do net hesitate to have a look at the original.
First install the new packages:
npm i -g shapefile ndjson-cli topojson-client topojson-server topojson-simplify
Then in three steps:
step 1: Convert Shapefile to newline-delimited GeoJSON features.
shp2json -n original.shp > myfile.ndjson
step 2: Redefine the GeoJSON properties, you can also rename them.
ndjson-map 'd.properties = {prop1: d.properties.prop1, p2: d.properties.prop2}, d' \
  < myfile.ndjson \
  > myfile-filtered.ndjson
step 3: Convert the newline-delimited GeoJSON to TopoJSON.
geo2topo -n myfile-filtered.ndjson > myfile-topo.json
Note:
If you don't have the original .shp file anymore, you can convert your actual .json file to newline-delimited GeoJSON features using ndjson-split:
 ndjson-split 'd.features' \
  < myfile.json \
  > myfile.ndjson
And then follow the instructions at step 2.
This function in topojson has now been moved to geo2topo and no longer provides a way to edit properties of the original.
Any properties and identifiers of input feature objects are propagated to the output. If you want to transform or filter properties, try ndjson-cli as demonstrated in Command-Line Cartography.
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!");
    }); 
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With