9

I tried to change the only the shapefile datasource encoding through properties and also How to encode shapefiles from LATIN1 to UTF-8? but it doesn't change it.

Without saving the QGIS project can't I directly change the shapefile datasource encoding?

I also tried this code, it shows change encoding but as I open the shapefile again it show the same old system encoding:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    layer.setProviderEncoding(u'UTF-8')
    layer.dataProvider().setEncoding(u'UTF-8')
    print layer.name(), layer.dataProvider().encoding()  

(I am not saving QGIS project, just accessing and saving shapefile directly. When I save the project and do this task then it work correctly)

  • You can take a look at the code in the plugin: shapefile_encoding_fixer – Jakob Nov 25 '16 at 10:15
  • can i save in shapefile through qgis without saving the project? – Muhammad Faizan Khan Nov 25 '16 at 10:20
  • Have you tried right-clicking your layer and using the Save As... option? – Joseph Nov 25 '16 at 11:49
  • @Joseph yes man off course, i already mention that answer in my post! I direct open shape file into qgis project and change its encoding through save as but as i open it again it remaing to system (unchanged). – Muhammad Faizan Khan Nov 25 '16 at 12:01
  • @Joseph i did not save qgis project, when i save qgis project and open shape file through it then it works correctly! – Muhammad Faizan Khan Nov 25 '16 at 12:03
13

From the menubar, go to:

Settings > Options > Data Sources > Data source handling

and uncheck the Ignore shapefile encoding declaration setting:

Encoding setting

This sets all layers loaded into QGIS to be default encoded to UTF-8

  • 1
    Thanks @Joseph additionally for future user this link also helpful hub.qgis.org/issues/13203. Actually Ignore shapefile encoding declaration checked option ignore the utf-8 settings. its ignore .cpg file. – Muhammad Faizan Khan Nov 28 '16 at 6:58
  • @MohammadFaizanKhan - Most welcome and thanks for the link :) – Joseph Nov 28 '16 at 14:01
6

To recapitulate the different location where you can set the encoding:

When you open a shapefile: Open shp as

When you open a CSV: Add CSV

When you save a vector file: Save vector file as

In the layer properties:
Layer properties

And, as mentioned by @Joseph, you should probably uncheck the Ignore shapefile encoding declaration setting.

3

When you use Save As ..., you have to select a different filename, and Add file to canvas.

As a consequence, you MUST save the project too to get the new shapefile in it. The unsaved project still includes the untouched shapefile, and the wrong encoding.

BTW the Data encoding in the Layer properties is part of the project file, not the shapefile. Your code example does not write anything inside the shapefile, it changes just the layer settings of the project.

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.