3

I used the following command to reproject a GeoJSON from lat/lon to 3857:

ogr2ogr -f "GeoJSON" results_3857.json -t_srs "EPSG:3857" results.json

Unexpectedly, the resulting file was 5 times larger than the original.

Turns out the resulting file contains all properties for each feature, while the original contained only those properties that were not null.

For example the original might have had a properties objects that look like this:

..."properties": { a: 1, d: 2, f: 1 }....
..."properties": { b: 1, c: 2, d: 2 }...

while the newly reprojected file has converted them to look like this:

..."properties": { a: 1, b: null, c: null, d: 2, e: null, f: 1 }...
..."properties": { a: null, b: 1, c: 2, d: 2, e: null, f: null }...

I don't want these extra null entries in my reprojected GeoJSON.

And so my question is this:

How can I remove those properties that have null values from the GeoJSON?

Is there some flag I can use in the ogr2ogr command?
Maybe there is a subsequent gdal command I can use to remove those attributes that have a null value?

  • You could eliminate the "null" values with a regex and iterate through file. Keep in mind this will cause problems when when filtering, styling, or querying a file. Features should contain the same field names throughout. – geomajor56 Jan 29 '15 at 2:10
  • As far as I know there is no such flag for GeoJSON driver. It should be doable because GDAL does not write null attributes into GML. I recommend to file a GDAL feature request. – user30184 Jan 29 '15 at 5:54
  • This has been fixed in GDAL 2.2.0 – AndrewHarvey Jan 11 at 3:03
3

for a GeoJSON featureCollection, you could do something like this:

var myGeoJSON = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "hi",
        "num": 9,
        "other": null
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          23.203125,
          53.12040528310657
        ]
      }
    }
    //,....
  ]
}

for(var i=0; i<myGeoJSON.features.length; i++){
    var obj = myGeoJSON.features[i].properties;
    for(var key in obj){
        var attrName = key;
        var attrValue = obj[key];
        if (!attrValue){
          delete obj[key];
        }
    }
}

i posted a sample here

  • Thanks John. I was hoping for an ogr command line solution...but perhaps it doesn't exist... – sfletche Feb 2 '15 at 17:45
  • @sfletche It may be fixed in GDAL 2.2.0 – AndrewHarvey Jan 11 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.