In QGIS I need to do a string conversion, from serbian latin to serbian cyrilic.
In field calculator, i have only this inelegant solution :
replace(replace(replace(replace( etc... ))))
It's long to type all translitterations (about 30), is there a better way to do this ?
Inpired by given links, i did this :
from qgis.core import *
from qgis.gui import *
import string
@qgsfunction(args='auto', group='Custom')
def serbian_latin_to_cyrillic(n):
capital_letters = {
u'Џ': u'DŽ',
u'Њ': u'NJ',
u'Љ': u'LJ',
u'А': u'A',
u'Б': u'B',
u'В': u'V',
u'Г': u'G',
u'Д': u'D',
u'Ђ': u'Ð',
u'Е': u'E',
u'Ж': u'Ž',
u'З': u'Z',
u'И': u'I',
u'Ј': u'J',
u'К': u'K',
u'Л': u'L',
u'М': u'M',
u'Н': u'N',
u'О': u'O',
u'П': u'P',
u'Р': u'R',
u'С': u'S',
u'Т': u'T',
u'Ћ': u'Ć',
u'У': u'U',
u'Ф': u'F',
u'Х': u'H',
u'Ц': u'C',
u'Ч': u'Č',
u'Ш': u'Š'
}
lower_case_letters = {
u'dž': u'џ',
u'nj': u'њ',
u'lj': u'љ',
u'a': u'а',
u'b': u'б',
u'v': u'в',
u'g': u'г',
u'd': u'д',
u'ð': u'ђ',
u'e': u'е',
u'ž': u'ж',
u'z': u'з',
u'i': u'и',
u'j': u'ј',
u'k': u'к',
u'l': u'л',
u'm': u'м',
u'n': u'н',
u'o': u'о',
u'p': u'п',
u'r': u'р',
u's': u'с',
u't': u'т',
u'ć': u'ћ',
u'u': u'у',
u'f': u'ф',
u'h': u'х',
u'c': u'ц',
u'č': u'ч',
u'š': u'ш'
}
translit_string = ""
for index, char in enumerate(string):
if char in lower_case_letters.keys():
char = lower_case_letters[char]
elif char in capital_letters.keys():
char = capital_letters[char]
if len(string) > index+1:
if string[index+1] not in lower_case_letters.keys():
char = char.upper()
else:
char = char.upper()
translit_string += char
return translit_string
But when i use the function, and put a field form the table in the parameter of the function, it says the function use only one paramater and it says i put 3 value. Why that ?
I try the second solution, and it gives me some strange result. Beside this, i cannot get the value of a field transmitted in function paramater.
@qgsfunction(args='auto', group='Python')
def serbian_latin_to_cyrillic(field, feature, parent):
sr_latin = u"Bačka Palanka"
symbols = (u"džnjljabvgdðežzijklmnoprstćufhcčšDŽNJLJABVGDÐEŽZIJKLMNOPRSTĆUFHCČŠ",
u"џњљабвгдђежзијклмнопрстћуфхцчшЏЊЉАБВГДЂЕЖЗИЈКЛМНОПРСТЋУФХЦЧШ")
"""tr = {ord(a):ord(b) for a, b in zip(*symbols)}"""
tr = dict( [ (ord(a), ord(b)) for (a, b) in zip(*symbols) ] )
return sr_latin.translate(tr)
It gives me this : ÐнµÐ ÐÐÐÐеÐ
If i disable the transliteration function, it gives me this : BaÄka Palanka Maybe problem with character encoding ?