3

I have a file places.csv with the following contents:

name,lat,lon,type,population Aalfang,48.8393882,15.0675236,village,410 Abern,48.0556773,13.1533381,village,203 Abersee,47.7266536,13.4169871,village,320 Abetzberg,48.0839721,14.76567,village,260 Abfaltersbach,46.7564042,12.5298515,village,616 Absam,47.2965003,11.5051409,village,6543 Abschlag,48.6241667,14.8638577,village,78 Absdorf,48.4001618,15.9803689,village,459 

and an accompanying file places.csvt to specify the column types:

"String","CoordY","CoordX","String","Integer" 

I am using ogr2ogr to filter the CSV file for entries in a spatial region with this command:

ogr2ogr -spat 12.0 47.0 15.0 49.0 -a_srs EPSG:4326 -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat places_filtered.csv places.csv 

In the resulting CSV file, all values in the column population are represented as Strings with double quotes:

name,lat,lon,type,population Abern,48.0556773,13.1533381,village,"203" Abersee,47.7266536,13.4169871,village,"320" Abetzberg,48.0839721,14.76567,village,"260" Abschlag,48.6241667,14.8638577,village,"78" 

How can I get ogr2ogr to output unquoted integer values for the integer field?

I tried many options, but no success. I haven't found a way to specify field/column types explicitly, besides supplying the .csvt file with equal name as the input file. Also the -oo AUTODETECT_TYPE=YES option gives the same result.

3 Answers 3

5

Read documentation from here https://gdal.org/drivers/vector/csv.html#layer-creation-options

STRING_QUOTING=[IF_NEEDED/IF_AMBIGUOUS/ALWAYS]: Defaults to IF_AMBIGUOUS. whether to double-quote strings. IF_AMBIGUOUS means that string values that look like numbers will be quoted (it also implies IF_NEEDED). Defaults to IF_AMBIGUOUS (behavior in older versions was IF_NEEDED)

Then do

ogr2ogr -spat 12.0 47.0 15.0 49.0 -a_srs EPSG:4326 -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat places_filtered2.csv places.csv -lco STRING_QUOTING=IF_NEEDED more places_filtered2.csv name,lat,lon,type,population Abern,48.0556773,13.1533381,village,203 Abersee,47.7266536,13.4169871,village,320 Abetzberg,48.0839721,14.76567,village,260 Abschlag,48.6241667,14.8638577,village,78 
1
  • Argh, I tried this, but used -oo instead of -lco so didn't work. Thanks, this is the way to go! Commented Nov 24, 2023 at 17:12
3

Will be fixed in GDAL 3.8.2 per https://github.com/OSGeo/gdal/pull/8813

2

Using a VRT file?

I wrote a tutorial some years ago to explain a full example. It's in French but should be understandable with an automated translation: https://geotribu.fr/articles/2021/2021-09-07_traiter_fichiers_adresse_gdal_csv_vrt/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.