12

I'm using GDAL/OGR's ogr2ogr command line tool to export data from a PostGIS-enabled PostgreSQL database to various GIS file formats, Shapefile amongst them. To create a shapefile with the default encoding (ISO8859-1 a.k.a. latin1, see Which character encoding is used by the DBF file in shapefiles?), I'm using a command line like this:

ogr2ogr \
    -f 'Esri Shapefile' \
    $OUTPUT_PATH \
    -t_srs $OUT_SRS \
    "PG:dbname=${DB_NAME} user=${DB_USERNAME} password=${DB_PASSWORD} schemas=${SCHEMA_NAME}"

But in my data, there may be features with arbitrary languages and scripts in the attribute values. I'd like to preserve these values and thus think that the *.dbf file of the exported Shapefile should be UTF-8 encoded. (The attribute names are guaranteed to be within 7-bit ASCII.)

How can I get ogr2ogr to write a UTF-8 encoded *.dbf file when exporting to Shapefile? The documentation of the GDAL/OGR "ESRI Shapefile / DBF" driver is explicitly ambigous (sic!) about the ENCODING option in the "Layer Creation Options":

The default value is "LDID/87". It is not clear what other values may be appropriate.

And will the used encoding be indicated in the *.dbf file itself or in an accompanying *.cpg file? (I guess the latter, as UTF-8 probably isn't per se a valid (dBASE DBMS) DBF encoding.) If the latter, will ogr2ogr create the *.cpg file or do I have to create it manually?

2
  • 4
    If this works ogr2ogr output.shp input -lco ENCODING=UTF-8 then your question is a duplicate of gis.stackexchange.com/questions/15912/….
    – user30184
    Commented Nov 4, 2016 at 11:15
  • 1
    That works. Thanks, @user30184! But while the answer seems to be the same and the questions certainly overlap, I don't agree that the questions are duplicates of each other. (Just from the question title, mine is a bit more general: gis.stackexchange.com/q/15912/51574 just asks for a Shapefile-to-Shapefile conversion, while my question doesn't care (too much) about the input source and focuses on the output.)
    – das-g
    Commented Nov 7, 2016 at 16:15

1 Answer 1

19

How can I get ogr2ogr to write a UTF-8 encoded *.dbf file when exporting to Shapefile?

Similar to How to encode shapefiles from LATIN1 to UTF-8?, this is possible with -lco ENCODING=UTF-8. So for my case

ogr2ogr \
    -f 'Esri Shapefile' \
    $OUTPUT_PATH \
    -t_srs $OUT_SRS \
    "PG:dbname=${DB_NAME} user=${DB_USERNAME} password=${DB_PASSWORD} schemas=${SCHEMA_NAME}" \
    -lco ENCODING=UTF-8

And will the used encoding be indicated in the *.dbf file itself or in an accompanying *.cpg file? (I guess the latter, as UTF-8 probably isn't per se a valid (dBASE DBMS) DBF encoding.)

The latter, indeed: In *.cpg files (one per database table), each just having the content

UTF-8

If the latter, will ogr2ogr create the *.cpg file or do I have to create it manually?

ogr2ogr will create the *.cpg files for you.

5
  • FYI, -lco ENCODING=UTF-8 with still throw a ERROR during the COPY phase if the source data contains an invalid character.
    – Theo F
    Commented Jan 26, 2023 at 16:25
  • 1
    @TheoF, an invalid character in which codec? Commented Jun 8, 2023 at 14:41
  • Option -oo ISO8859-1 or whatever the input file encoding is might be also necessary. Commented Oct 15 at 16:22
  • @DimaChubarov While I don't quite remember what I was doing 8 years ago, I assume the input encoding was UTF-8 in my case.
    – das-g
    Commented Oct 15 at 23:32
  • Sure. I came across this post while trying to convert from 8-bit encoding to UTF-8 and just specifying -lco was not sufficient, but after specifying the correct input encoding in -oo I managed to get an output with strings in UTF-8. And, it should be -oo ENCODING=ISO8859-1. My previous comment was incorrect. Commented Oct 16 at 11:13

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.