3

Let's say I have:

$ ogrinfo ...
OGRFeature(SELECT):0
  name (Integer) = 29670000
  lon (Real) = 120.870582715925
  lat (Real) = 24.183864790114

How do I get an additional:

POINT (120.870582715925 24.183864790114)

And this is my command:

$ ogrinfo qingfujie_roads.geojson -dialect SQLite -sql \
'SELECT land_id AS name, xcenter AS lon, ycenter AS lat FROM "qingfujie_roads"'
4
  • What do you mean with "random" in <<random data fields>> Commented Nov 30 at 11:51
  • Well like xcenter. Just one of the many fields present in my data. Lumped along with zip_code, etc. Commented Nov 30 at 13:08
  • 1
    Ah... OK. So, they are specific data fields, not random. Commented Dec 1 at 8:08
  • Now fixed. Thanks! Commented Dec 1 at 8:13

2 Answers 2

8

Given my dummy data:

$ cat qingfujie_roads.geojson
{
    "type": "FeatureCollection",
    "name": "qingfujie_roads",
    "features": [
        { "type": "Feature", "properties": { "land_id": "123", "xcenter": 120.870582715925, "ycenter": 24.183864790114}}
    ]
}

Output in one go using OGR Spatialite SQL functions ST_Point (create point geometry from x,y) and ST_AsText (create the WKT from geometry):

$ ogr2ogr -f CSV /vsistdout/ qingfujie_roads.geojson -dialect SQLite \
  -sql 'SELECT land_id AS name, ST_AsText(ST_Point(xcenter, ycenter)) as WKT FROM "qingfujie_roads"'
name,WKT
"123",POINT(120.870583 24.183865)

Or

$ ogrinfo qingfujie_roads.geojson -dialect SQLite \
  -sql 'SELECT land_id AS name, ST_AsText(ST_Point(xcenter, ycenter)) as WKT FROM "qingfujie_roads"'
INFO: Open of `qingfujie_roads.geojson'
      using driver `GeoJSON' successful.

Layer name: SELECT
Geometry: None
Feature Count: 1
Layer SRS WKT:
(unknown)
name: String (0.0)
WKT: String (0.0)
OGRFeature(SELECT):0
  name (String) = 123
  WKT (String) = POINT(120.870583 24.183865)

Documentation on OGR SQL if built with spatialite support:

0
3

Alas,
https://gdal.org/en/latest/drivers/vector/geojson.html
doesn't say what to do. Yes one surely could do it in one step, but how to has never been documented.

However
https://gdal.org/en/latest/drivers/vector/csv.html#reading-csv-containing-spatial-information
certainly does say what to do. Therefore consider converting your GeoJSON into CSV first. Let's try that two step approach:

Step 1:

$ ogr2ogr qingfujie_roads.csv qingfujie_roads.geojson -dialect SQLite -sql \
'SELECT land_id AS name, xcenter AS lon, ycenter AS lat FROM "qingfujie_roads"'
$ cat qingfujie_roads.csv
name,lon,lat
29670000,120.870582715925,24.183864790114
29680000,120.870047099875,24.183650358631
...

Step 2:

$ ogrinfo -ro -al qingfujie_roads.csv -oo KEEP_GEOM_COLUMNS=NO \
-oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat

Layer name: qingfujie_roads
OGRFeature(qingfujie_roads):1
  name (String) = 29670000
  POINT (120.870582715925 24.183864790114)

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.