Search code examples
postgisgdalogr2ogrosm2pgsql

Query attributes with colon : inbetween with ogr2ogr and PostGIS


I'm running a PostGIS Database filled by osm2pgsql. The address-keys are stored for example as

"addr:street"

The objective is to store those geometries as GeoPackage by ogr2ogr. The script looks like this

ogr2ogr -f "GPKG" address_point.gpkg PG:"host=**** dbname=**** user=**** password=****" -sql "select addr:housenumber","addr:housename","addr:flats","addr:conscriptionnumber","addr:street","addr:place","addr:postcode","addr:city","addr:country","addr:full","addr:hamlet","addr:suburb","addr:subdistrict","addr:district","addr:province","addr:state","addr:interpolation","addr:interpolation","addr:inclusion","addr:door","addr:unit","addr:floor","addr:block",way from planet_osm_point"

ogr2ogr returns an error because of the colon in each attribute. I think, the query fails because of " which is to suppose to wrap the query for ogr2ogr.

When I use ' as following:

ogr2ogr -f "GPKG" address_point.gpkg PG:"host=**** dbname=**** user=**** password=****" -sql "select 'addr:housenumber','addr:housename','addr:flats','addr:conscriptionnumber','addr:street','addr:place','addr:postcode','addr:city','addr:country','addr:full','addr:hamlet','addr:suburb','addr:subdistrict','addr:district','addr:province','addr:state','addr:interpolation','addr:interpolation','addr:inclusion','addr:door','addr:unit','addr:floor','addr:block',way from planet_osm_point"

the query fails, too.

Is there a way to solve this problem?


Solution

  • You need to escape the double quotes around the field names

    ogr2ogr [...] -sql "select \"addr:housenumber\",\"addr:housename\" from planet_osm_point"
    

    Or you can use single quotes for the entire SQL string

    ogr2ogr [...] -sql 'select "addr:housenumber","addr:housename" from planet_osm_point'