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")
Check it: cat example.geojson
:
{
"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 would like each feature to have an id
. I achieve that via ogr2ogr
& the ID_GENERATE
layer creation option (see docs):
ogr2ogr example_withIDs.geojson example.geojson -lco ID_GENERATE="YES"
Check the result via cat example_withIDs.geojson
:
{
"type": "FeatureCollection",
"name": "example",
"features": [
{ "type": "Feature", "id": 0, "properties": { "data": "a" }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } },
{ "type": "Feature", "id": 1, "properties": { "data": "b" }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } }
]
}
as expected. However, I would need all my id
s to be positive.
How can I generate unique positive integers as id
s for each feature using ogr2ogr
?
Below I present attempts/clarifications, but no need to read them if one understands the question above.
Attempt 1
I tried to use the ID_FIELD
layer creation option too:
ogr2ogr -sql "SELECT *, 12345 as generated_id FROM example" -dialect sqlite example_withIDs.geojson example.geojson -lco ID_FIELD='generated_id'
Check the result via cat example_withIDs.geojson
:
{
"type": "FeatureCollection",
"name": "SELECT",
"features": [
{ "type": "Feature", "id": 12345, "properties": { "data": "a" }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } },
{ "type": "Feature", "id": 12345, "properties": { "data": "b" }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } }
]
}
All id
s now are 12345
. If I could somehow enumerate rows starting from 1
, instead of having 12345 as generated_id
in the SQL
statement, that would probably work.
A note on id
s
Generating an id
or ID
, or similar field to be a member of properties
is not what I am trying to achieve. A feature in a GeoJSON file looks like this:
{
"type": "Feature",
"id": 12345, // <- I am talking about this id
"geometry": {...},
"properties": { "id": 12345, "ID": 12345 } // <- not about these
}
I am talking about the id
above which is on the same level as type
, geometry
and properties
. Not about one of the properties
elements. This is in line with section 3.2 of the GeoJSON specification:
If a Feature has a commonly used identifier, that identifier SHOULD be included as a member of the Feature object with the name "id", and the value of this member is either a JSON string or a number.
This is a separate thing from properties
, which is:
A Feature object has a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).