0

I downloaded sample OSM data of my area of interest via Overpass-turbo as my_map.geojson. I am trying to import this geojson file to my PostgreSQL 10 (Windows 8 x64 machine) database using ogr2ogr (QGIS OSGeo CMD tool). The following command imports entire my_map.geojson without errors.

ogr2ogr -f PostgreSQL PG:"host=localhost user=my_user dbname=my_db password=my_pw" -lco GEOMETRY_NAME=the_geom -lco FID=gid -nln my_tbl path_to_my_map.geojson

However, when I try to import selected attributes using following command:

ogr2ogr -f PostgreSQL PG:"host=localhost user=my_user dbname=my_db password=my_pw" -select 'name,highway,oneway,surface' -lco GEOMETRY_NAME=the_geom -lco FID=gid -nln my_tbl path_to_my_map.geojson

I get the error:

ERROR 1: Field ''name' not found in source layer.
ERROR 1: Terminating translation prematurely after failed
translation of layer map (use -skipfailures to skip errors)

Any idea why -select flag of ogr2ogr throws error?

Update:

The output of:

ogrinfo -al -so path_to_my_map.geojson

is,

Layer name: my_map
Geometry: Unknown (any)
Feature Count: 28
..
..
id: String (0.0)
@id: String (0.0)
highway: String (0.0)
name: String (0.0)
surface: String (0.0)
  • Which fields GDAL finds with ogrinfo -al -so path_to_my_map.geojson? – user30184 Apr 28 at 19:48
  • Just updated the question with the output of above command, thank you. – khajlk Apr 28 at 19:57
  • 2
    don't enclose the column list with ' – ThingumaBob Apr 28 at 21:30
  • Care to post your comment as solution? it worked :) – khajlk Apr 28 at 21:46
2

Parameters passed to ogr2ogr flags are themselves parsed as string literals (and/or parsed to numeric types if needed); they don't require string enclosing (' or "), rather, consider them always getting enclosed implicitly.

To explain the behavior:

  • -select col1, col2 implicitly translates to "col1", "col2"
    • thus -select 'col1', 'col2' will translate to "'col1'", "'col2'"
      or -select 'col1, col2' to "'col1", "col2'", as in your case
    • non-standard column names, however, need string enclosing, just as referring to them in a SQL client would: -select "c0l!", col2

Notes:

  • flags like -sql expect an arbitrary query string as parameter to be passed and interpreted to the SQL backend; thus you need to pass the parameter with string enclosing
    • -sql "SELECT * FROM my_table"
  • in e.g. the -where flag you need to add string enclosing if you want to filter e.g. a text column, like -where name='Peter', but -where number=1

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.