Skip to content

Instantly share code, notes, and snippets.

@GeoWill
Created September 3, 2018 10:08
Show Gist options
  • Save GeoWill/e4c96812e34a7c32a045142ae5a73dc1 to your computer and use it in GitHub Desktop.
Save GeoWill/e4c96812e34a7c32a045142ae5a73dc1 to your computer and use it in GitHub Desktop.
QGIS Function to make strings ascii only
"""
Make place names be lowercase ascii only with '-' instead of ' '.
"""
from qgis.core import *
from qgis.gui import *
ascii_dict = {ord(u'ß'): u'ss', ord(u'à'): u'a', ord(u'á'): u'a', ord(u'â'): u'a', ord(u'ã'): u'a', ord(u'ä'): u'a', ord(u'å'): u'a', ord(u'æ'): u'ae', ord(u'ç'): u'c', ord(u'è'): u'e', ord(u'é'): u'e', ord(u'ê'): u'e', ord(u'ë'): u'e', ord(u'ì'): u'i', ord(u'í'): u'i', ord(u'î'): u'i', ord(u'ï'): u'i', ord(u'ð'): u'd', ord(u'ñ'): u'n', ord(u'ò'): u'o', ord(u'ó'): u'o', ord(u'ô'): u'o', ord(u'õ'): u'o', ord(u'ö'): u'o', ord(u'÷'): u'/', ord(u'ø'): u'o', ord(u'ù'): u'u', ord(u'ú'): u'u', ord(u'û'): u'u', ord(u'ü'): u'u', ord(u'ý'): u'y', ord(u'þ'): u'p', ord(u'ÿ'): u'y', ord(u'’'):u"'"}
@qgsfunction(args='auto', group='Custom')
def to_ascii(non_ascii_string, feature, parent):
"""
Converts characters in string with accents,
tildes, circumflex, etc to ascii.
"""
return non_ascii_string.lower().replace(' ','-').translate(ascii_dict)
#return non_ascii_string.encode('ascii', 'asciify')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment