14

I have some GML data that has some unneeded columns in it. I'd like to remove these columns in order to reduce the file size. (To clarify: I mean that the file has many points in it, and each of these points has some metadata attached, and I don't need all of the metadata.)

I'd also like to do this from the command line, as I have many files.

I can convert the GML files into different formats using ogr2ogr, so my question is this: what's the easiest way to remove a column from a geographic data file, from the command line?

I'm a Python programmer so my instinct would be to convert the file to GeoJSON, then use Python's JSON-parsing methods to read and rewrite the JSON file.

But perhaps there's a more direct way, using a GDAL tool or similar.

2 Answers 2

13

Shapefile as a temporary file format is not necessary and it may also alter the GML data (shorten attribute names, cut strings to max 255 characters, change DATETIME to DATE etc.)

I would use ogr2ogr not by dropping unwanted attributes but by selecting those which are wanted. The SQLite SQL dialect is more standard than the defauld OGR dialect, that's why I use that even it is not necessary for this query.

ogr2ogr -f GML -dialect SQLite -sql "SELECT attr1, attr2, attr3,... FROM layer" output.gml input.gml
1
  • 1
    good idea, but what if there are 1000 columns/fields. You'd have to explicitly write out all column names using your method.
    – Theo F
    Commented Jul 4, 2022 at 21:01
12

You can delete a field using OGR SQL and choosing an OGR format which supports the field deletion. Unfortunately GML doesn't support it, so you have to pass through another format, e.g. SHP:

ogr2ogr temp.shp input.gml
ogrinfo temp.shp -sql "ALTER TABLE temp DROP COLUMN field_to_drop"
ogr2ogr -f GML output.gml temp.shp
2
  • 6
    I was able to do a simpler, similar thing with GeoJSON driver; ogrinfo input.geojson -sql "ALTER TABLE input DROP COLUMN field_to_drop" Note the use of the file name as table to ALTER.
    – vpipkt
    Commented May 10, 2019 at 18:02
  • 2
    @vpipkt so concise, and works well Commented Nov 2, 2021 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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