Last active
September 28, 2023 19:04
-
-
Save zyphlar/42ff9a8db4492c8a9d26714aba950a28 to your computer and use it in GitHub Desktop.
QGIS user expression to separate and format streets from addresses (updated for latest Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qgis.core | |
import qgis.gui | |
import re | |
# | |
# This will keep street names like SR 574A as SR 574A however | |
# will lowercase other number-digit suffixes with <2 or >4 numbers | |
# or >1 suffix-letters, like 12th Street or 243rd Ave. | |
# | |
@qgsfunction(args='auto', group='Custom', referenced_columns=[]) | |
def getstreetfromaddress(value1, feature, parent): | |
parts = value1.split() | |
parts.pop(0) # Ignore the first bit (i.e. "123" in "123 N MAIN ST") | |
parts = map(formatstreetname, parts) | |
return " ".join(parts) | |
@qgsfunction(args='auto', group='Custom', referenced_columns=[]) | |
def formatstreet(value1, feature, parent): | |
parts = value1.split() | |
parts = map(formatstreetname, parts) | |
return " ".join(parts) | |
# Internal function | |
def formatstreetname(name): | |
# Acryonyms | |
if name == "CR": | |
return "County Road" | |
if name == "SR": | |
return "SR" # State Route | |
if name == "NFS": | |
return "NFS" # National Forest Service? | |
if name == "US": | |
return "US" | |
# Directions | |
if name == "N": | |
return "North" | |
if name == "NE": | |
return "Northeast" | |
if name == "E": | |
return "East" | |
if name == "SE": | |
return "Southeast" | |
if name == "S": | |
return "South" | |
if name == "SW": | |
return "Southwest" | |
if name == "W": | |
return "West" | |
if name == "NW": | |
return "Northwest" | |
# Suffixes | |
if name == "AVE": | |
return "Avenue" | |
if name == "BLVD": | |
return "Boulevard" | |
if name == "BND": | |
return "Bend" | |
if name == "CIR": | |
return "Circle" | |
if name == "CR": | |
return "Circle" | |
if name == "CT": | |
return "Court" | |
if name == "DR": | |
return "Drive" | |
if name == "FLDS": | |
return "Fields" | |
if name == "GRV": | |
return "Grove" | |
if name == "HL": | |
return "Hill" | |
if name == "HOLW": | |
return "Hollow" | |
if name == "HWY": | |
return "Highway" | |
if name == "LN": | |
return "Lane" | |
if name == "LOOP": | |
return "Loop" | |
if name == "LP": | |
return "Loop" | |
if name == "PATH": | |
return "Path" | |
if name == "PL": | |
return "Place" | |
if name == "RD": | |
return "Road" | |
if name == "RUN": | |
return "Run" | |
if name == "SQ": | |
return "Square" | |
if name == "ST": | |
return "Street" | |
if name == "TER": | |
return "Terrace" | |
if name == "TRL": | |
return "Trail" | |
if name == "VW": | |
return "View" | |
if name == "WAY": | |
return "Way" | |
if name == "WY": | |
return "Way" | |
if name == "XING": | |
return "Crossing" | |
if re.match('^[0-9]{2,4}[A-Za-z]$', name) != None: | |
return name | |
return name.capitalize() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment