2021-09-22 05:02:09 +02:00
|
|
|
import {OsmObject, OsmWay} from "../OsmObject";
|
2021-07-15 20:47:28 +02:00
|
|
|
import {Changes} from "../Changes";
|
|
|
|
import {GeoOperations} from "../../GeoOperations";
|
|
|
|
import OsmChangeAction from "./OsmChangeAction";
|
|
|
|
import {ChangeDescription} from "./ChangeDescription";
|
2021-09-22 05:02:09 +02:00
|
|
|
import RelationSplitHandler from "./RelationSplitHandler";
|
2021-07-15 20:47:28 +02:00
|
|
|
|
|
|
|
interface SplitInfo {
|
|
|
|
originalIndex?: number, // or negative for new elements
|
|
|
|
lngLat: [number, number],
|
|
|
|
doSplit: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class SplitAction extends OsmChangeAction {
|
2021-09-22 05:02:09 +02:00
|
|
|
private readonly wayId: string;
|
|
|
|
private readonly _splitPointsCoordinates: [number, number] []// lon, lat
|
2021-10-04 03:12:42 +02:00
|
|
|
private _meta: { theme: string, changeType: "split" };
|
2021-10-16 02:54:22 +02:00
|
|
|
private _toleranceInMeters: number;
|
2021-07-15 20:47:28 +02:00
|
|
|
|
2021-09-22 16:07:56 +02:00
|
|
|
/**
|
2021-10-16 02:54:22 +02:00
|
|
|
* Create a changedescription for splitting a point.
|
|
|
|
* Will attempt to reuse existing points
|
2021-09-22 16:07:56 +02:00
|
|
|
* @param wayId
|
|
|
|
* @param splitPointCoordinates: lon, lat
|
2021-10-04 03:12:42 +02:00
|
|
|
* @param meta
|
2021-10-16 02:54:22 +02:00
|
|
|
* @param toleranceInMeters: if a splitpoint closer then this amount of meters to an existing point, the existing point will be used to split the line instead of a new point
|
2021-09-22 16:07:56 +02:00
|
|
|
*/
|
2021-11-07 16:34:51 +01:00
|
|
|
constructor(wayId: string, splitPointCoordinates: [number, number][], meta: { theme: string }, toleranceInMeters = 5) {
|
2022-01-26 21:40:38 +01:00
|
|
|
super(wayId, true)
|
2021-09-22 05:02:09 +02:00
|
|
|
this.wayId = wayId;
|
|
|
|
this._splitPointsCoordinates = splitPointCoordinates
|
2021-10-16 02:54:22 +02:00
|
|
|
this._toleranceInMeters = toleranceInMeters;
|
2021-10-04 03:12:42 +02:00
|
|
|
this._meta = {...meta, changeType: "split"};
|
2021-07-15 20:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static SegmentSplitInfo(splitInfo: SplitInfo[]): SplitInfo[][] {
|
|
|
|
const wayParts = []
|
|
|
|
let currentPart = []
|
|
|
|
for (const splitInfoElement of splitInfo) {
|
|
|
|
currentPart.push(splitInfoElement)
|
|
|
|
|
|
|
|
if (splitInfoElement.doSplit) {
|
|
|
|
// We have to do a split!
|
|
|
|
// We add the current index to the currentParts, flush it and add it again
|
|
|
|
wayParts.push(currentPart)
|
|
|
|
currentPart = [splitInfoElement]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wayParts.push(currentPart)
|
|
|
|
return wayParts.filter(wp => wp.length > 0)
|
|
|
|
}
|
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> {
|
2021-11-07 16:34:51 +01:00
|
|
|
const originalElement = <OsmWay>await OsmObject.DownloadObjectAsync(this.wayId)
|
2021-09-22 05:02:09 +02:00
|
|
|
const originalNodes = originalElement.nodes;
|
2021-07-15 20:47:28 +02:00
|
|
|
|
|
|
|
// First, calculate splitpoints and remove points close to one another
|
2021-10-16 02:54:22 +02:00
|
|
|
const splitInfo = this.CalculateSplitCoordinates(originalElement, this._toleranceInMeters)
|
2021-07-15 20:47:28 +02:00
|
|
|
// Now we have a list with e.g.
|
|
|
|
// [ { originalIndex: 0}, {originalIndex: 1, doSplit: true}, {originalIndex: 2}, {originalIndex: undefined, doSplit: true}, {originalIndex: 3}]
|
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
// Lets change 'originalIndex' to the actual node id first (or assign a new id if needed):
|
2021-07-15 20:47:28 +02:00
|
|
|
for (const element of splitInfo) {
|
|
|
|
if (element.originalIndex >= 0) {
|
|
|
|
element.originalIndex = originalElement.nodes[element.originalIndex]
|
|
|
|
} else {
|
|
|
|
element.originalIndex = changes.getNewID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next up is creating actual parts from this
|
|
|
|
const wayParts: SplitInfo[][] = SplitAction.SegmentSplitInfo(splitInfo);
|
|
|
|
// Allright! At this point, we have our new ways!
|
|
|
|
// Which one is the longest of them (and can keep the id)?
|
|
|
|
|
|
|
|
let longest = undefined;
|
|
|
|
for (const wayPart of wayParts) {
|
|
|
|
if (longest === undefined) {
|
|
|
|
longest = wayPart;
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (wayPart.length > longest.length) {
|
|
|
|
longest = wayPart
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const changeDescription: ChangeDescription[] = []
|
|
|
|
// Let's create the new points as needed
|
|
|
|
for (const element of splitInfo) {
|
|
|
|
if (element.originalIndex >= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
changeDescription.push({
|
|
|
|
type: "node",
|
|
|
|
id: element.originalIndex,
|
2021-07-18 21:37:14 +02:00
|
|
|
changes: {
|
|
|
|
lon: element.lngLat[0],
|
2021-07-15 20:47:28 +02:00
|
|
|
lat: element.lngLat[1]
|
2021-10-04 03:12:42 +02:00
|
|
|
},
|
|
|
|
meta: this._meta
|
2021-07-15 20:47:28 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
// The ids of all the ways (including the original)
|
|
|
|
const allWayIdsInOrder: number[] = []
|
|
|
|
|
|
|
|
const allWaysNodesInOrder: number[][] = []
|
2021-07-15 20:47:28 +02:00
|
|
|
// Lets create OsmWays based on them
|
|
|
|
for (const wayPart of wayParts) {
|
|
|
|
|
|
|
|
let isOriginal = wayPart === longest
|
|
|
|
if (isOriginal) {
|
|
|
|
// We change the actual element!
|
2021-09-22 05:02:09 +02:00
|
|
|
const nodeIds = wayPart.map(p => p.originalIndex)
|
2021-07-15 20:47:28 +02:00
|
|
|
changeDescription.push({
|
2021-07-18 21:37:14 +02:00
|
|
|
type: "way",
|
2021-07-15 20:47:28 +02:00
|
|
|
id: originalElement.id,
|
2021-07-18 21:37:14 +02:00
|
|
|
changes: {
|
2021-09-22 05:02:09 +02:00
|
|
|
coordinates: wayPart.map(p => p.lngLat),
|
|
|
|
nodes: nodeIds
|
2021-10-04 03:12:42 +02:00
|
|
|
},
|
|
|
|
meta: this._meta
|
2021-07-15 20:47:28 +02:00
|
|
|
})
|
2021-09-22 05:02:09 +02:00
|
|
|
allWayIdsInOrder.push(originalElement.id)
|
|
|
|
allWaysNodesInOrder.push(nodeIds)
|
2021-07-15 20:47:28 +02:00
|
|
|
} else {
|
|
|
|
let id = changes.getNewID();
|
2021-09-22 05:02:09 +02:00
|
|
|
// Copy the tags from the original object onto the new
|
2021-07-15 20:47:28 +02:00
|
|
|
const kv = []
|
|
|
|
for (const k in originalElement.tags) {
|
2021-07-18 21:37:14 +02:00
|
|
|
if (!originalElement.tags.hasOwnProperty(k)) {
|
2021-07-15 20:47:28 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-07-18 21:37:14 +02:00
|
|
|
if (k.startsWith("_") || k === "id") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
kv.push({k: k, v: originalElement.tags[k]})
|
2021-07-15 20:47:28 +02:00
|
|
|
}
|
2021-09-22 05:02:09 +02:00
|
|
|
const nodeIds = wayPart.map(p => p.originalIndex)
|
2021-07-15 20:47:28 +02:00
|
|
|
changeDescription.push({
|
2021-07-18 21:37:14 +02:00
|
|
|
type: "way",
|
|
|
|
id: id,
|
2021-07-15 20:47:28 +02:00
|
|
|
tags: kv,
|
2021-07-18 21:37:14 +02:00
|
|
|
changes: {
|
2021-09-22 05:02:09 +02:00
|
|
|
coordinates: wayPart.map(p => p.lngLat),
|
|
|
|
nodes: nodeIds
|
2021-10-04 03:12:42 +02:00
|
|
|
},
|
|
|
|
meta: this._meta
|
2021-07-15 20:47:28 +02:00
|
|
|
})
|
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
allWayIdsInOrder.push(id)
|
|
|
|
allWaysNodesInOrder.push(nodeIds)
|
|
|
|
}
|
2021-07-15 20:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// At last, we still have to check that we aren't part of a relation...
|
|
|
|
// At least, the order of the ways is identical, so we can keep the same roles
|
2021-09-22 05:02:09 +02:00
|
|
|
const relations = await OsmObject.DownloadReferencingRelations(this.wayId)
|
|
|
|
for (const relation of relations) {
|
|
|
|
const changDescrs = await new RelationSplitHandler({
|
|
|
|
relation: relation,
|
|
|
|
allWayIdsInOrder: allWayIdsInOrder,
|
|
|
|
originalNodes: originalNodes,
|
|
|
|
allWaysNodesInOrder: allWaysNodesInOrder,
|
2021-10-04 03:12:42 +02:00
|
|
|
originalWayId: originalElement.id,
|
|
|
|
}, this._meta.theme).CreateChangeDescriptions(changes)
|
2021-09-22 05:02:09 +02:00
|
|
|
changeDescription.push(...changDescrs)
|
|
|
|
}
|
2021-07-15 20:47:28 +02:00
|
|
|
|
|
|
|
// And we have our objects!
|
|
|
|
// Time to upload
|
|
|
|
|
2021-07-18 21:37:14 +02:00
|
|
|
return changeDescription
|
2021-07-15 20:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the actual points to split
|
|
|
|
* If another point is closer then ~5m, we reuse that point
|
|
|
|
*/
|
2021-09-22 05:02:09 +02:00
|
|
|
private CalculateSplitCoordinates(osmWay: OsmWay, toleranceInM = 5): SplitInfo[] {
|
|
|
|
const wayGeoJson = osmWay.asGeoJson()
|
|
|
|
// Should be [lon, lat][]
|
2021-11-07 16:34:51 +01:00
|
|
|
const originalPoints: [number, number][] = osmWay.coordinates.map(c => [c[1], c[0]])
|
2021-09-22 05:02:09 +02:00
|
|
|
const allPoints: {
|
2021-09-22 16:07:56 +02:00
|
|
|
// lon, lat
|
2021-09-22 05:02:09 +02:00
|
|
|
coordinates: [number, number],
|
|
|
|
isSplitPoint: boolean,
|
|
|
|
originalIndex?: number, // Original index
|
|
|
|
dist: number, // Distance from the nearest point on the original line
|
|
|
|
location: number // Distance from the start of the way
|
|
|
|
}[] = this._splitPointsCoordinates.map(c => {
|
|
|
|
// From the turf.js docs:
|
|
|
|
// The properties object will contain three values:
|
|
|
|
// - `index`: closest point was found on nth line part,
|
|
|
|
// - `dist`: distance between pt and the closest point,
|
|
|
|
// `location`: distance along the line between start and the closest point.
|
|
|
|
let projected = GeoOperations.nearestPoint(wayGeoJson, c)
|
2021-09-22 16:07:56 +02:00
|
|
|
// c is lon lat
|
2021-09-22 05:02:09 +02:00
|
|
|
return ({
|
|
|
|
coordinates: c,
|
|
|
|
isSplitPoint: true,
|
|
|
|
dist: projected.properties.dist,
|
|
|
|
location: projected.properties.location
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
// We have a bunch of coordinates here: [ [lon, lon], [lat, lon], ...] ...
|
|
|
|
// We project them onto the line (which should yield pretty much the same point and add them to allPoints
|
2021-07-15 20:47:28 +02:00
|
|
|
for (let i = 0; i < originalPoints.length; i++) {
|
|
|
|
let originalPoint = originalPoints[i];
|
2021-09-22 05:02:09 +02:00
|
|
|
let projected = GeoOperations.nearestPoint(wayGeoJson, originalPoint)
|
|
|
|
allPoints.push({
|
|
|
|
coordinates: originalPoint,
|
|
|
|
isSplitPoint: false,
|
|
|
|
location: projected.properties.location,
|
|
|
|
originalIndex: i,
|
|
|
|
dist: projected.properties.dist
|
|
|
|
})
|
2021-07-15 20:47:28 +02:00
|
|
|
}
|
|
|
|
// At this point, we have a list of both the split point and the old points, with some properties to discriminate between them
|
|
|
|
// We sort this list so that the new points are at the same location
|
2021-09-22 05:02:09 +02:00
|
|
|
allPoints.sort((a, b) => a.location - b.location)
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = allPoints.length - 2; i >= 1; i--) {
|
|
|
|
// We 'merge' points with already existing nodes if they are close enough to avoid closeby elements
|
|
|
|
|
|
|
|
// Note the loop bounds: we skip the first two and last two elements:
|
|
|
|
// The first and last element are always part of the original way and should be kept
|
|
|
|
// Furthermore, we run in reverse order as we'll delete elements on the go
|
|
|
|
|
|
|
|
const point = allPoints[i]
|
|
|
|
if (point.originalIndex !== undefined) {
|
|
|
|
// We keep the original points
|
|
|
|
continue
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
// At this point, 'dist' told us the point is pretty close to an already existing point.
|
|
|
|
// Lets see which (already existing) point is closer and mark it as splitpoint
|
|
|
|
const nextPoint = allPoints[i + 1]
|
|
|
|
const prevPoint = allPoints[i - 1]
|
|
|
|
const distToNext = nextPoint.location - point.location
|
2021-10-16 02:54:22 +02:00
|
|
|
const distToPrev = point.location - prevPoint.location
|
2021-11-07 16:34:51 +01:00
|
|
|
|
|
|
|
if (distToNext * 1000 > toleranceInM && distToPrev * 1000 > toleranceInM) {
|
2021-10-16 02:54:22 +02:00
|
|
|
// Both are too far away to mark them as the split point
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
let closest = nextPoint
|
|
|
|
if (distToNext > distToPrev) {
|
|
|
|
closest = prevPoint
|
|
|
|
}
|
|
|
|
// Ok, we have a closest point!
|
2021-11-07 16:34:51 +01:00
|
|
|
if (closest.originalIndex === 0 || closest.originalIndex === originalPoints.length) {
|
2021-09-22 16:07:56 +02:00
|
|
|
// We can not split on the first or last points...
|
|
|
|
continue
|
|
|
|
}
|
2021-09-22 05:02:09 +02:00
|
|
|
closest.isSplitPoint = true;
|
|
|
|
allPoints.splice(i, 1)
|
|
|
|
|
|
|
|
}
|
2021-07-15 20:47:28 +02:00
|
|
|
|
|
|
|
const splitInfo: SplitInfo[] = []
|
2021-09-22 05:02:09 +02:00
|
|
|
let nextId = -1 // Note: these IDs are overwritten later on, no need to use a global counter here
|
2021-07-15 20:47:28 +02:00
|
|
|
|
|
|
|
for (const p of allPoints) {
|
2021-09-22 05:02:09 +02:00
|
|
|
let index = p.originalIndex
|
2021-07-15 20:47:28 +02:00
|
|
|
if (index === undefined) {
|
|
|
|
index = nextId;
|
|
|
|
nextId--;
|
|
|
|
}
|
|
|
|
const splitInfoElement = {
|
|
|
|
originalIndex: index,
|
2021-09-22 05:02:09 +02:00
|
|
|
lngLat: p.coordinates,
|
|
|
|
doSplit: p.isSplitPoint
|
2021-07-15 20:47:28 +02:00
|
|
|
}
|
|
|
|
splitInfo.push(splitInfoElement)
|
|
|
|
}
|
|
|
|
|
|
|
|
return splitInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|