So I’m trying to use the JOSM Scripting plugin to programmatically Split Ways IDs through my .osm.pbf file (city-scale filesize) via the Rhino Javascript.
Does anyone have any sample code or example code of how to get a split to work in the scripter?
Here is as close as I’ve come but I get an error very early in the script. I’ve looked the few examples I can find. I think my format is correct. In fact I am currently wondering if there is a bug triggered by the
var actions = require("josm/actions");
line of code.
function current_layer() {
var layers = require("josm/layers");
return layers.activeLayer;
}
//Define the function
function SplitWaysAndAssignNewIDs(){
var util = require("josm/util");
var command = require("josm/command");
var console = require("josm/scriptingconsole");
var actions = require("josm/actions");
var layer = current_layer();
if (layer == null) return;
var dataset = layer.data;
//Select my Node of Interest which is a member of a Given Way.
//I will eventually write this into a For Loop to iterate through several ways of a given highway type
var sampleNodeInWay= dataset.query("id:175494043");
//Run the Split command - This code is wrong, and I don't know how to fix it!
//I'm sorry but I cannot understand the documentation about declaring java classes in javascript
split.runOn(sampleNodeInWay);
var waySplitInHalf = dataset.query("type:way")
//Assign a New OSM ID to each half of the Way I just split
for (j = 0; j < waySplitInHalf.length; j++) {
// waySplitInHalf[j].setOsmId(999000000+j,1);
}
}
//Run the function
SplitWaysAndAssignNewIDs();
PS. Don’t worry I am NOT uploading “My Split Dataset” to OSM. This is just for a personal offline project.
Thank you kindly for your time, and thank you in advance for your reply! Best wishes.
plamen
(Plamen)
2
If somebody is still interested in 2023.
This code works with JOSM Scripting Plugin 0.2.7 using the latest API V3.
It is a rough replacement of the JOSM tool “split way” (keyboard shortcut “P”)
Code is not fully tested, use with care.
import josm from 'josm';
import { DataSet, DataSetUtil } from 'josm/ds'
import * as util from 'josm/util';
import * as command from 'josm/command'
import * as console from 'josm/scriptingconsole'
const layer = josm.layers.activeLayer;
var ds = layer.data;
var dsutil = new DataSetUtil(ds);
const sel = ds.selection;
const selection = dsutil.query("selected");
const nodes = dsutil.query("type:node selected");
const ways = dsutil.query("type:way selected");
const myWay = ways[0];
const SWC = Java.type('org.openstreetmap.josm.command.SplitWayCommand');
const chunks = SWC.buildSplitChunks(myWay, nodes);
josm.console.println("chunks: " + chunks.length);
var ArrayList = Java.type('java.util.ArrayList');
var list = new ArrayList();
list.add(selection);
var result = SWC.splitWay(myWay, chunks, list , SWC.Strategy.keepLongestChunk());
const UndoRedoHandler = Java.type('org.openstreetmap.josm.data.UndoRedoHandler')
const split = UndoRedoHandler.getInstance().add(result);
josm.console.println("Split done.");