3

I am QGIS user but beginner in Python. I figured out how to transliterate Georgian script into Latin using this module (https://pypi.org/project/transliterate/).

from transliterate import translit, get_available_language_codes

text= 'საქართველო'

from transliterate.discover import autodiscover
autodiscover()

from transliterate.base import TranslitLanguagePack, registry

class MyGeorgianLanguagePack(TranslitLanguagePack):
    language_code = "my-ka"
    language_name = "MyGeorgian"
    mapping = (
    "აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰ", # Source script
    "abgdevztiklmnopzhrstupkghkshchtsdztschkhjh", # Target script
    )
    pre_processor_mapping = {
        "ჟ":  "zh",
        "რ":  "r",
        "ს":  "s",
        "ტ":  "t",
        "უ":  "u",    
        "ფ":  "p",
        "ქ":  "k",
        "ღ":  "gh",
        "ყ":  "k",
        "შ":  "sh",
        "ჩ":  "ch",
        "ც":  "ts",
        "ძ":  "dz",
        "წ":  "ts",
        "ჭ":  "ch",
        "ხ":  "kh",
        "ჯ":  "j",
        "ჰ":  "h"
    }

registry.register(MyGeorgianLanguagePack)

print((translit(text, 'my-ka')).title())

# Sakartvelo

But now I am interested, how can I incorporate this script in QGIS to automatically transliterate Georgian names in Latin script from one field to another?

1 Answer 1

2

Okay, strong efforts do its job.

Here is the answer:

from transliterate import translit, get_available_language_codes

from transliterate.discover import autodiscover
autodiscover()

from transliterate.base import TranslitLanguagePack, registry

class MyGeorgianLanguagePack(TranslitLanguagePack):
    language_code = "my-ka"
    language_name = "MyGeorgian"
    mapping = (
    "აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰ", # Source script
    "abgdevztiklmnopzhrstupkghkshchtsdztschkhjh", # Target script
    )
    pre_processor_mapping = {
        "ჟ":  "zh",
        "რ":  "r",
        "ს":  "s",
        "ტ":  "t",
        "უ":  "u",    
        "ფ":  "p",
        "ქ":  "k",
        "ღ":  "gh",
        "ყ":  "k",
        "შ":  "sh",
        "ჩ":  "ch",
        "ც":  "ts",
        "ძ":  "dz",
        "წ":  "ts",
        "ჭ":  "ch",
        "ხ":  "kh",
        "ჯ":  "j",
        "ჰ":  "h"
    }

registry.register(MyGeorgianLanguagePack)

# pre-pyqgis code above

# pyqgis code below

layer=QgsVectorLayer(uri.uri(False), "any_layer_name_here", "postgres")
features=layer.getFeatures()

layer_provider=layer.dataProvider()

layer.startEditing()
for f in features:
    id=f.id()
    text=f.attributes()[1]
    transl=(translit(text, 'my-ka')).title()
    attr_value={2:transl}
    layer_provider.changeAttributeValues({id:attr_value})
layer.commitChanges()

# field under index [1] will be transliterated into field under index [2]

UPDATE

If your field contains NULL values, modify the part of the script like below.

    for f in features:
        id=f.id()
        text=f.attributes()[1]
        if f.attributes()[1] == None:
            nodata = NULL
            attr_value1 = {2:nodata}
            layer_provider.changeAttributeValues({id:attr_value1})
        else:
            transl=(translit(text, 'my-ka')).title()
            attr_value2={2:transl}
            layer_provider.changeAttributeValues({id:attr_value2})
    layer.commitChanges()

# field under index [1] will be transliterated into field under index [2], NULL values will stay as NULL.

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.