Skip to content

Instantly share code, notes, and snippets.

@zdila
Created November 29, 2022 17:42
Show Gist options
  • Save zdila/0b5f711c156f1a5f35fb86aa51a07460 to your computer and use it in GitHub Desktop.
Save zdila/0b5f711c156f1a5f35fb86aa51a07460 to your computer and use it in GitHub Desktop.
add waterways to relation using scripting plugin
import josm from "josm";
import { RelationBuilder } from "josm/builder";
const byName = {};
const ds = josm.layers.activeLayer.getDataSet();
const ways = ds.getWays();
for (const way of ways) {
if (way.hasKey("waterway")) {
const name = way.get("name");
let ways = byName[name];
if (!ways) {
ways = byName[name] = new Set();
}
ways.add(way);
}
}
for (const name in byName) {
josm.console.println(name);
const ways = byName[name];
while (ways.size) {
const [way] = ways;
const collected = [way];
ways.delete(way);
collect(collected, ways, way);
if (collected.length > 1) {
const tags = { type: "waterway" };
for (const w of collected) {
const waterway = w.get("waterway");
if (
(tags.waterway !== "stream" && tags.waterway !== "river") ||
waterway == "river"
) {
tags.waterway = waterway;
}
for (const tagname of w.keySet()) {
if (
tagname.includes("_name") ||
tagname == "name" ||
tagname.startsWith("name:")
) {
tags[tagname] = w.get(tagname);
}
}
}
josm.console.println(Object.keys(tags));
const rb = new RelationBuilder(ds);
rb.withTags(tags).withMembers(collected).create();
}
}
}
function collect(collected, ways, way1) {
const newCollected = [];
for (const way2 of ways) {
const nodeIds = new Set(way2.getNodeIds());
if ([...way1.getNodeIds()].some((n) => nodeIds.has(n))) {
newCollected.push(way2);
}
}
for (const w of newCollected) {
ways.delete(w);
}
for (const w of newCollected) {
collect(collected, ways, w);
}
collected.push(...newCollected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment