10

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.

10

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
8

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
  • 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 May 10 at 18:02

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.