wiki:Help/Plugin/Scripting

Version 9 (modified by skyper, 12 years ago) ( diff )

link update

Plugin -> Scripting

TOC(noheading,inline)

In a nutshell

The scripting plugins allows you to run scripts within JOSM.

Use it to automate small tasks for which no dedicated plugin is available, i.e.

  • additional quality tests for which no validator test cases are available
  • automatically entering data in very specific situations (i.e. sequences of house numbers)
  • importing from a custom file format not supported by JOSM
  • exporting to a custom file format not supported by JOSM

You can use any scripting language which provides a JSR-223 compatible scripting engine, in particular:

Configuring a Scripting Engine

The scripting plugin doesn't ship with a script engine. A standard Java 6 installation should already include a JavaScript engine (Rhino). If you want to use another scripting language, you have to configure the respective script engine first:

  1. Select the menu item Scripting -> Configure ...
  2. Check whether your preferred scripting engine is already configured. If not,
    • download the jar file providing the scripting engine
    • add the path to this jar file to the list of scripting engine jars

Running a Script

  1. Select the menu item Scripting -> Run...
  2. Enter the path to the script
  3. Press Run

Writing a script

The scripting plugin doesn't provide a development console for scripts (yet).

Use your preferred editor or development environment to write the script in your preferred language.

Here are three examples which display the number of layers in JOSM. If you want to write more complex scripts you will have to get familiar with the JOSM object model and in consequence with the JOSM code base, because there is neither an explicit scripting API nor any documentation outside of the JOSM code.

Groovy

/*
 * HelloWorld.groovy - displays the number of actually open layers 
 */
import javax.swing.JOptionPane;
import org.openstreetmap.josm.Main;

def numlayers = Main.main?.map?.mapView?.numLayers
if (numlayers == null) numlayers = 0

JOptionPane.showMessageDialog(Main.parent, "[Groovy] Hello World!\nYou have ${numlayers} layer(s).")

JavaScript

/*
* HelloWorld.js  -  displays the number of currently open layers 
*/
importClass(Packages.javax.swing.JOptionPane)
importClass(Packages.org.openstreetmap.josm.Main)

function getMapView() {
	if (Main.main == null) return null
	if (Main.main.map == null) return null
	return Main.main.map.mapView
}

var numlayers = 0
var mv = getMapView()
if (mv != null){
	numlayers = mv.getNumLayers()
} 
JOptionPane.showMessageDialog(Main.parent, "[JavaScript] Hello World! You have " + numlayers + " layer(s).")

Python

#
# HelloWorld.py  - displays the number of currently open layers
# 
from javax.swing import JOptionPane
from org.openstreetmap.josm import Main

def getMapView():
    if Main.main and Main.main.map:
        return Main.main.map.mapView
    else:
        return None

numlayers = 0
mv = getMapView()
if mv != None:
        numlayers = mv.getNumLayers()
        
JOptionPane.showMessageDialog(Main.parent, "[Python] Hello World! You have %s layer(s)." % numlayers)

Here are some more examples:

Help/Plugin/Scripting/Python

Various small scripts that are meant to illustrate how to do one thing.

Help/Plugin/Scripting/Python/SurfaceTypesOfRoutes (Interesting for cycle routes, both loops and linear ones)

Short piece of code, but illustrates some interesting concepts and reports on OSM data. Doesn't make changes to the data

Help/Plugin/Scripting/Python/RCN_Route_Validator

This is not a trivial script anymore. It does a lot in a complicated field (networks of cycle node routes with numbered nodes), makes changes to relations, writes to a file that can be pasted to the wiki. Analyzes routes, but also networks of routes or collections of networks of routes, depending on the selection in JOSM when the script was run.


Back to Plugin Help
Back to Main Help

Note: See TracWiki for help on using the wiki.