1

I create an example.geojson:

import shapely.wkt
import geopandas as gpd

s0 = shapely.wkt.loads("POINT(0 0)")
s1 = shapely.wkt.loads("POINT(1 1)")
gdf = gpd.GeoDataFrame({'geometry':[s0, s1],'data':['a','b']})

gdf.to_file("example.geojson")

example.geojson becomes:

{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "data": "a" }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } },
{ "type": "Feature", "properties": { "data": "b" }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } }
]
}

I want each feature in example.geojson to have a new property, with key newcol, value val. I achieve this via ogr2ogr:

ogr2ogr -dialect sqlite -sql "SELECT *, 'val' AS newcol FROM example" example_with_new_column.geojson example.geojson
mv example_with_new_column.geojson example.geojson
rm example_with_new_column.geojson

This is somewhat cumbersome however: it requires creating example_with_new_column.geojson, and then using that to overwrite the original example.geojson. example.geojson becomes:

{
"type": "FeatureCollection",
"name": "SELECT",
"features": [
{ "type": "Feature", "properties": { "data": "a", "newcol": "val" }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } },
{ "type": "Feature", "properties": { "data": "b", "newcol": "val" }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } }
]
}

as expected.

Is there a way to achieve the above via ogr2ogr without creating a new file and then overwriting the original one?

1 Answer 1

2

With some formats it is possible to add columns with ogrinfo by using SQL ALTER TABLE https://gdal.org/user/ogr_sql_dialect.html#alter-table.

The following OGR SQL ALTER TABLE commands can be used.

-“ALTER TABLE tablename ADD [COLUMN] columnname columntype” to add a new field. Supported if the layer declares the OLCCreateField capability.

Unfortunately I do not know how to check if a layer declares OLCCreateField capability. Obviously shapefile layer has that capability because the following works and adds a new column:

ogrinfo -sql "alter table test add column newcol varchar" test.shp

ogrinfo test.shp -al -so

INFO: Open of `test.shp'
      using driver 'ESRI Shapefile' successful.

Layer name: test
Metadata:
  DBF_DATE_LAST_UPDATE=2022-12-15
Geometry: Point
Feature Count: 2
...
attr: String (80.0)
newcol: String (80.0)

The same command does not add new column into GeoJSON. It does not show any error or warning either which may be considered as a bug.

It could be more convenient to add a new column with ALTER TABLE and then update the values with UPDATE table set newcol=... but I am pretty sure that it would be slower. GeoJSON is text and new property and values must be added into each feature in the middle of the file. So if ALTER TABLE ADD COLUMN would work it would most probably write a new temporary file anyway, and then delete the original and rename the new one. But that is not all, for doing the subsequent UPDATE table... the same thing should be done again. So with your current workflow GDAL must create one new table, but with the hypothetical alternative way that should be done two times.

The default name of a layer that is created with -sql option is SELECT as can be seen, but it can be changed with -nln.

If you have a permanent need for editing schema and contents of vector data then I would consider to use some more suitable format like GeoPackage during that step of your workflow and export GeoJSON when you are satisfied with the contents.

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.