Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import / Identification - Erreur encodage Python sur QGIS 2.18.10 #106

Closed
mdouchin opened this issue Jul 5, 2017 · 15 comments
Closed

Import / Identification - Erreur encodage Python sur QGIS 2.18.10 #106

mdouchin opened this issue Jul 5, 2017 · 15 comments
Assignees
Labels
bug à fermer ? Python remonte une erreur pur Python

Comments

@mdouchin
Copy link
Collaborator

mdouchin commented Jul 5, 2017

Constaté avec une récente version de QGIS 2.18.10 (et peut-être antérieure ?).

Suite au commit suivant qgis/QGIS@14ab5eb

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 431: ordinal not in range(128) 
Traceback (most recent call last):
  File "/home/mdouchin/.qgis2/python/plugins/cadastre/cadastre_menu.py", line 361, in getParcelleInfo
    self.cadastre_search_dialog
  File "/home/mdouchin/.qgis2/python/plugins/cadastre/cadastre_dialogs.py", line 2386, in __init__
    self.setProprietairesContent()
  File "/home/mdouchin/.qgis2/python/plugins/cadastre/cadastre_dialogs.py", line 2493, in setProprietairesContent
    [header, data, rowCount, ok] = cadastre_common.fetchDataFromSqlQuery(self.connector, sql)
  File "/home/mdouchin/.qgis2/python/plugins/cadastre/cadastre_dialogs.py", line 461, in fetchDataFromSqlQuery
    c = connector._execute(None,unicode(sql))
  File "/usr/share/qgis/python/plugins/db_manager/db_plugins/connector.py", line 81, in _execute
    cursor.execute(str(sql))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 431: ordinal not in range(128)
@mdouchin mdouchin added the bug label Jul 5, 2017
@mdouchin mdouchin self-assigned this Jul 5, 2017
mdouchin referenced this issue in qgis/QGIS Jul 5, 2017
@mdouchin
Copy link
Collaborator Author

mdouchin commented Jul 5, 2017

It seems I can find a workaround by replacing in my code L 461 of cadastre_dialogs.py
c = connector._execute(None,unicode(sql))
by
c = connector._execute(None,unicode(sql).encode('utf-8'))

@jusabatier Can you confirm this workaround allows you to import data ( AND have good accentuated chars visibles in tables and in the plugin dialogs ) ?

@mdouchin mdouchin changed the title Outil d'identification de parcelle - Erreur Python sur QGIS 2.18 Import / Identification - Erreur encodage Python sur QGIS 2.18.10 Jul 5, 2017
@jusabatier
Copy link

Nope, I replaced this line with your code in ~/.qgis2/python/plugins/cadastre/cadastre_dialogs.py and restarted my QGis, but still have the same error when importing in a PostGIS database.

@jusabatier
Copy link

jusabatier commented Jul 5, 2017

Moreover, it seems like your stacktrace is different, the one I have is :

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 23239: ordinal not in range(128) 
Traceback (most recent call last):
  File "/home/jusabatier/.qgis2/python/plugins/cadastre/cadastre_dialogs.py", line 1083, in processImport
    qi.installCadastreStructure()
  File "/home/jusabatier/.qgis2/python/plugins/cadastre/cadastre_import.py", line 251, in installCadastreStructure
    self.executeSqlScript(s, item.has_key('constraints'))
  File "/home/jusabatier/.qgis2/python/plugins/cadastre/cadastre_import.py", line 1064, in executeSqlScript
    self.executeSqlQuery(sql, ignoreError)
  File "/home/jusabatier/.qgis2/python/plugins/cadastre/cadastre_import.py", line 1115, in executeSqlQuery
    c = self.connector._execute_and_commit(sql)
  File "/usr/share/qgis/python/plugins/db_manager/db_plugins/connector.py", line 95, in _execute_and_commit
    self._execute(None, sql)
  File "/usr/share/qgis/python/plugins/db_manager/db_plugins/connector.py", line 81, in _execute
    cursor.execute(str(sql))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 23239: ordinal not in range(128)

The problem seems to be in cadastre_import.py

@mdouchin
Copy link
Collaborator Author

mdouchin commented Jul 5, 2017

yep, I will try to adapt the line "/home/jusabatier/.qgis2/python/plugins/cadastre/cadastre_import.py", line 1115
There are probably more locations (but hopefully not, and if so, I would need to refactor the code...)

I would find a way to try catch and use one or other method to avoid bugs in versions prior and after the QGIS commit

@jusabatier
Copy link

I confirm that replace cadastre_import.py line 1115 :
c = self.connector._execute_and_commit(sql)
with
c = self.connector._execute_and_commit(unicode(sql).encode('utf-8'))

solved the problem and allow to perform a successful import in PostGIS database

@mdouchin
Copy link
Collaborator Author

mdouchin commented Jul 5, 2017

Ok. I will try to provide updated code as a workaround

@guillaumelaurant
Copy link

Bonjour,

J'ai essayé de modifié cadastre_dialogs.py comme demandé mais ça ne fonctionne toujours pas, sous QGIS 2.18 comme 2.14...

@jusabatier
Copy link

Il y a probablement 2 fichiers à modifier :

  • cadastre_dialogs.py ligne 461 :
    c = connector._execute(None,unicode(sql).encode('utf-8'))

  • cadastre_import.py ligne 1115 :
    c = self.connector._execute_and_commit(unicode(sql).encode('utf-8'))

Et bien penser à relancer QGis après, pour moi ça a résolu le problème et l'import dans PostGIS s'est fait correctement en 2.18.10

@guillaumelaurant
Copy link

Ça ne fonctionne pas, voici le message d'erreur :

Traceback (most recent call last):
File "C:/Users/g.laurant/.qgis2/python/plugins\cadastre\cadastre_menu.py", line 361, in getParcelleInfo
self.cadastre_search_dialog
File "C:/Users/g.laurant/.qgis2/python/plugins\cadastre\cadastre_dialogs.py", line 2371, in init
self.setProprietairesContent()
File "C:/Users/g.laurant/.qgis2/python/plugins\cadastre\cadastre_dialogs.py", line 2477, in setProprietairesContent
[header, data, rowCount, ok] = cadastre_common.fetchDataFromSqlQuery(self.connector, sql)
File "C:/Users/g.laurant/.qgis2/python/plugins\cadastre\cadastre_dialogs.py", line 460, in fetchDataFromSqlQuery
c = connector._execute(None,unicode(sql).encode('utf-8'))
File "C:/PROGRA1/QGIS21.18/apps/qgis/./python/plugins\db_manager\db_plugins\connector.py", line 89, in _execute
raise DbError(e, sql)
File "C:/PROGRA1/QGIS21.18/apps/qgis/./python/plugins\db_manager\db_plugins\plugin.py", line 66, in init
self.query = unicode(query) if query is not None else None
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 424: ordinal not in range(128)

Faut-il que je recharge la base cadastre du plugin ?

Merci par avance,

@jusabatier
Copy link

Après avoir fait les modification de mon dernier post, j'ai relancé QGis puis réimporté les données dans le schema PostGIS (eventuellement le supprimer puis recréer).

Par la suite celle-cis se chargent sans soucis.

@mdouchin
Copy link
Collaborator Author

mdouchin commented Jul 6, 2017

Le commit 58beda1 doit régler le souci, et permettre au plugin Cadastre de fonctionner avec les version de QGIS avant et après le commit qgis/QGIS@14ab5eb

Merci de télécharger la dernière version "master" du plugin depuis ce site et de vérifier si c'est bon

@mdouchin
Copy link
Collaborator Author

Bug QGIS référencé par d'autres ici:
https://issues.qgis.org/issues/16833

@MaelREBOUX MaelREBOUX added the Python remonte une erreur pur Python label Aug 19, 2019
@MaelREBOUX
Copy link
Collaborator

Vu que cela concernait la version 2.18 de QGIS : doit-on fermer ce ticket @mdouchin ?

@mdouchin
Copy link
Collaborator Author

mdouchin commented Oct 8, 2019

on peut fermer. Le ticket n'est pas récent, un fix a été proposé (try catch si je me souviens...). On peut fermer, merci

@mdouchin mdouchin closed this as completed Oct 8, 2019
@boillodmanuel
Copy link

Bonjour,
J'ai rencontré un problème similaire : UnicodeDecodeError: 'ascii' codec can't decode
Mon env :

  • QGIS : 3.18
  • Plugin Cadastre :1.10.2
  • OS : macos bigsur 11.2.3

J'ai fini par trouver comment corriger mon problème (la résolution de celui-ci est peut-être similaire)

Dans le fichier cadastre_import.py, fonction replaceParametersInScript, il manque l'encoding sur cette ligne :

with open(scriptPath, 'w') as fout:
fout.write(data)

Voici le fix:

with open(scriptPath, 'w', encoding='utf-8') as fout:
    fout.write(data)

La locale système ne doit pas être configuré comme il faut dans QGIS.
Pour le vérifier, j'ai ouvert la console python (Plugins > Console Python) et exécuté le code suivant :

# Explicitly open with ascii encoding:
# ❌ Error:
#   UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in position 0: ordinal not in range(128)
with open('/tmp/test', 'w', encoding='ascii') as fout:
    fout.write(u"\u2122")

# Open without encoding specified:
# ❌ Error or ✅ Works depending of system locale :
#   UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in position 0: ordinal not in range(128)
with open('/tmp/test', 'w') as fout:
    fout.write(u"\u2122")
    
# Explicitly open with utf-8 encoding:
# ✅ Works
with open('/tmp/test', 'w', encoding='utf-8') as fout:
    fout.write(u"\u2122")

Pour forcer la locale dans QGIS, j'ai ajouté la variable d'environnement LC_ALL dans les préférences (interface en anglais) :.

  • Open QGIS > Preferences > System
  • Check "Use custom variable"
  • Add LC_ALL = en_US.UTF-8 or whatever you want.

Je ne suis pas sûr qu'il faille garder ce paramétrage mais ca aide pour débugger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug à fermer ? Python remonte une erreur pur Python
Projects
None yet
Development

No branches or pull requests

5 participants