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)
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Apr 28, 2019 at 19:47
4
  • Which fields GDAL finds with ogrinfo -al -so path_to_my_map.geojson? Commented Apr 28, 2019 at 19:48
  • Just updated the question with the output of above command, thank you. Commented Apr 28, 2019 at 19:57
  • 2
    don't enclose the column list with ' Commented Apr 28, 2019 at 21:30
  • Care to post your comment as solution? it worked :) Commented Apr 28, 2019 at 21:46

1 Answer 1

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
answered Apr 29, 2019 at 9:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.