13

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.

Elijah
  • 4,609
  • 3
  • 28
  • 36
  • 1
    See also : [How to add properties to topojson file?](http://stackoverflow.com/questions/18444261/) – Hugolpz Nov 01 '13 at 13:59

4 Answers4

21

Are you using the -p option?

topojson in.json -o out.json - remove all properties

topojson in.json -o out.json -p - keep all properties

topojson in.json -o out.json -p prop1,prop2 - keep only prop1 & prop2

james246
  • 1,884
  • 1
  • 15
  • 28
  • No, I wasn't. My reading of the help text was that not using -p was default to keep all properties, which on reflection doesn't make sense. Thanks, it really was that simple. – Elijah Jan 04 '13 at 17:35
  • I made the same mistake on reading the `man` page. Very confusing if you're not reading carefully... – LondonRob Nov 02 '15 at 14:36
2

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.

Kruupös
  • 5,097
  • 3
  • 27
  • 43
1

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!");
    }); 
}
ow3n
  • 5,974
  • 4
  • 53
  • 51
  • Hey, thanks for this answer. It's quite unconvenient to lose this key feature. It was very helpful. http://bl.ocks.org/hugolpz/4f8d6eaeaf41c72e268b – Hugolpz Oct 08 '20 at 20:58
1

I hit this problem, too, but @james246's answer didn't work for me. However, I found an equally simple solution.

The properties data are deleted only if you remove the source .shp file from its folder of sibling files. Ensure that the .shp and .dbf files are in the same folder before you apply the shp2geo command. (The .dbf file contains the properties data.)

There is no need to apply a condition like -p or anything else; the default command retains the properties.

Markus
  • 479
  • 4
  • 17
  • Although it works, it is a bit long with massive `.dbf` files and not very scalable, I tried it at first but I got tired ^^' – Kruupös Sep 05 '18 at 16:30