Skip to content

Instantly share code, notes, and snippets.

@bikeoid
Last active December 9, 2024 17:25
Show Gist options
  • Save bikeoid/cba84543b6c5b6ad72d44d7bb6dcb072 to your computer and use it in GitHub Desktop.
Save bikeoid/cba84543b6c5b6ad72d44d7bb6dcb072 to your computer and use it in GitHub Desktop.
JOSM Script to modify tags
# Python script for JOSM (jython engine)
# Purpose: Tag manipulation on currently selected single way for road review in specific US county
# Remove tiger:reviewed
# If no existing surface, add surface=asphalt
# Swap name and name_1 if it appears that name contains a ref pattern
# if name_1 pattern matches S-nn-nn , change to reg_ref
# or if name_1 pattern matches x-x-x , change to loc_ref
#
# Not touched:
# State Highway nnn or Highway nnn : this is ambiguous
# name_1= C-167 : This is an ambiguous transcription error. Should be C-1-67 but could be C-16-7 . Just delete it
# name_2=*, unless it contains an SC road designation S-nn-nn
#
import re
from javax.swing import JOptionPane
from org.openstreetmap.josm.gui import MainApplication
from org.openstreetmap.josm.data import UndoRedoHandler
from org.openstreetmap.josm.plugins.scripting.ui.console import ScriptingConsole
import org.openstreetmap.josm.command as Command
import org.openstreetmap.josm.data.osm.Way as Way
def log(msg):
log_writer = ScriptingConsole.getInstance().getScriptLog().getLogWriter()
log_writer.println(msg)
editLayer = MainApplication.getLayerManager().getEditLayer()
if editLayer and editLayer.data:
selected_ways = editLayer.data.getSelectedWays()
log( "---")
if not(selected_ways) or len(selected_ways)>1:
JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
"Please select a single highway.")
else:
for way in selected_ways:
if not (way.get("name")) or not (way.get("highway")):
JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
"Please select a highway.")
else:
changed = False
roadName = way.get('name')
log( "name=" + roadName)
modified_way = Way(way)
if not (modified_way.get("surface")):
modified_way.put('surface', 'asphalt')
log( 'added surface')
changed = True
if modified_way.get("tiger:reviewed"):
modified_way.remove("tiger:reviewed")
log( 'removed tiger:reviewed')
changed = True
changeSource = modified_way.get("source")
if changeSource is not None:
if changeSource.startswith("tiger2010_import"):
modified_way.remove("source")
log( 'removed source ="tiger2010_import*..."')
changed = True
roadRef = modified_way.get("ref")
if roadRef is not None:
if re.search(r"^S-\d*-\d*$", roadRef):
modified_way.remove("ref")
modified_way.put("reg_ref", roadRef)
log( 'Changed ref -> reg_ref')
changed = True
if modified_way.get("tiger:mtfcc"):
modified_way.remove("tiger:mtfcc")
log( 'removed tiger:mtfcc')
changed = True
if modified_way.get("name_1"):
name1=modified_way.get("name_1")
# look for name_1 and name reversed
if re.search(r"^\S*-\S*-\S*$", roadName, re.IGNORECASE):
# Swap before more checks
tempName=roadName # Likely loc_name
roadName=name1
name1=tempName
modified_way.put("name", roadName)
modified_way.put("name_1", name1)
log("Swap name, name_1")
stateRoad = re.search(r"^State Road (S-\d*-\d*$)", name1)
if re.search(r"^S-\d*-\d*$", name1, re.IGNORECASE):
# Successful match for state road ref designation
# Change name_1 to reg_ref
modified_way.remove("name_1")
modified_way.put("reg_ref", name1)
log( 'Changed name_1 -> reg_ref')
changed = True
elif stateRoad is not None:
# Successful match for state road ref designation with "State Road
# Change name_1 to reg_ref
modified_way.remove("name_1")
modified_way.put("reg_ref", stateRoad.group(1))
log( 'Changed State Road name_1 -> reg_ref')
changed = True
elif re.search(r"^.*-.*-.*$", name1, re.IGNORECASE):
# Successful match for county road ref
# Change name_1 to loc_ref
modified_way.remove("name_1")
modified_way.put("loc_ref", name1)
log( 'Changed name_1 -> loc_ref')
changed = True
elif re.search(r"^\d*$", name1):
# Successful match for county road ref
# Change name_1 to loc_ref
modified_way.remove("name_1")
modified_way.put("reg_ref", "S-4-" + name1)
log( 'Changed name_1 -> loc_ref')
changed = True
if modified_way.get("name_2"):
name2=modified_way.get("name_2")
stateRoad = re.search(r"^State Road (S-\d*-\d*$)", name2)
if stateRoad is not None:
# Successful match for state road ref designation with "State Road
# Change name_2 to reg_ref
modified_way.remove("name_2")
modified_way.put("reg_ref", stateRoad.group(1))
log( 'Changed State Road name_2 -> reg_ref')
changed = True
if changed:
log( "committing change")
commandsList = []
commandsList.append(Command.ChangeCommand(way, modified_way))
UndoRedoHandler.getInstance().add(Command.SequenceCommand(
"Modify reviewed highway tags", commandsList), True)
commandsList = []
else:
log ("no changes")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment