0

I have a FeatureCollection in GeoJSON. I want to delete all features that have an ID that doesn't start with "B".

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 0,
      "properties": {
        "map": "eu",
        "name": "Belgium" // don't delete
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": ...
      }
    },
    {
      "type": "Feature",
      "id": 2,
      "properties": {
        "map": "eu",
        "name": "CzechRepublic" // do delete this Feature
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [ ...

I can do this manually in Python, but is there a nicer way to do it, using something on the command line?

  • What do you mean by manually? Do you just need to convert your existing logic to a command line tool? If so, please provide your code. – BritishSteel Mar 6 '15 at 16:36
  • I mean I can load the JSON object into Python, iterate over it and selectively delete features. But I thought perhaps there is a gdal tool that could do this. – Richard Mar 6 '15 at 16:39
3

Theoretically you can do it with ogrinfo, SQLite SQL dialect and -sql parameter.

ogrinfo -dialect sqlite -sql "delete from OGRGeoJSON where name not like 'B%'" json.json

However, it does not work because for GDAL existing GeoJSON files are read-only. But try it with shapefile and you will see that it works.

ogrinfo -dialect sqlite -sql "delete from json where name not like 'B%'" json.shp

If you want to use some GDAL command line tool you must copy what you want to keep instead of deleting what you want to get rid of.

ogr2ogr -f geojson -dialect sqlite -sql "select * from OGRGeoJSON where name like 'B%'" selected_only.json json.json

Amazing, isn't it?

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.