Monday, 29 August 2016

pyqgis - Non-ASCII characters not displaying correctly after Python manipulation


I'm using QGIS 2.18 on a Mac (Sierra).


I'm doing some pretty extensive data manipulation using a virtual column, and using the function editor (so Python) to do so: one of my functions, for example, is to change street-name abbreviations to long-format (e.g.: All => Allée), and in French.


The result is encoded in UTF-8, and I can't seem to find a means to translate it into whatever encoding is needed to display it correctly on a map... The result of any string containing an accented character is 'null' in the attributes table.


Yet in another 'street name' layer, pulled directly from a PostGIS database, even accented labels (street names) display correctly.


Is this a common problem, and is there any way around this?


#!/usr/bin/env python

# -*- coding: utf-8 -*-


from qgis.core import *
from qgis.gui import *
from qgis.utils import qgsfunction
import re

@qgsfunction(args='auto', group='Custom')
def streettype_format(input, feature, parent):

rep = {
"CITE":"Cité",
"ALL":"Allée",
"RUE":"Rue",
"RPT":"Rond-point",
"SENT":"Sentier",
"VLA":"Villa",
"TERR":"Terrace",
"CHEM":"Chemin",
"CAR":"Carrefour",

"HAM":"Hameau",
"BD":"Boulevard",
"AV":"Avenue",
"CRS":"Cours",
"VOIE":"Voie",
"CHAU":"Chaussée",
"ARC":"Arcade",
"GAL":"Galérie"
}
rep = dict((re.escape(k), v) for k, v in rep.iteritems())

pattern = re.compile("|".join(rep.keys()))
input = pattern.sub(lambda m: rep[re.escape(m.group(0))], input)
return input

Also: from some d*cking around in the Python console:


>>>> import sys
>>>>sys.getdefaultencoding()
ascii
>>>>u"Héy"
u'H\xe9y'

>>>>"Héy"
'H\xc3\xa9y'


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...