Full code cleanup

This commit is contained in:
Pieter Vander Vennet 2021-11-07 16:34:51 +01:00
parent 8e6ee8c87f
commit bd21212eba
246 changed files with 19418 additions and 11729 deletions

View file

@ -10,6 +10,7 @@ export interface RelationSplitInput {
originalNodes: number[],
allWaysNodesInOrder: number[][]
}
abstract class AbstractRelationSplitHandler extends OsmChangeAction {
protected readonly _input: RelationSplitInput;
protected readonly _theme: string;
@ -57,11 +58,11 @@ export default class RelationSplitHandler extends AbstractRelationSplitHandler {
}
async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> {
if(this._input.relation.tags["type"] === "restriction"){
if (this._input.relation.tags["type"] === "restriction") {
// This is a turn restriction
return new TurnRestrictionRSH(this._input, this._theme).CreateChangeDescriptions(changes)
}
return new InPlaceReplacedmentRTSH(this._input, this._theme).CreateChangeDescriptions(changes)
return new InPlaceReplacedmentRTSH(this._input, this._theme).CreateChangeDescriptions(changes)
}
@ -72,68 +73,71 @@ export class TurnRestrictionRSH extends AbstractRelationSplitHandler {
constructor(input: RelationSplitInput, theme: string) {
super(input, theme);
}
public async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> {
const relation = this._input.relation
const members = relation.members
const selfMembers = members.filter(m => m.type === "way" && m.ref === this._input.originalWayId)
if(selfMembers.length > 1){
if (selfMembers.length > 1) {
console.warn("Detected a turn restriction where this way has multiple occurances. This is an error")
}
const selfMember = selfMembers[0]
if(selfMember.role === "via"){
if (selfMember.role === "via") {
// A via way can be replaced in place
return new InPlaceReplacedmentRTSH(this._input, this._theme).CreateChangeDescriptions(changes);
}
// We have to keep only the way with a common point with the rest of the relation
// Let's figure out which member is neighbouring our way
let commonStartPoint : number = await this.targetNodeAt(members.indexOf(selfMember), true)
let commonEndPoint : number = await this.targetNodeAt(members.indexOf(selfMember), false)
let commonStartPoint: number = await this.targetNodeAt(members.indexOf(selfMember), true)
let commonEndPoint: number = await this.targetNodeAt(members.indexOf(selfMember), false)
// In normal circumstances, only one of those should be defined
let commonPoint = commonStartPoint ?? commonEndPoint
// Let's select the way to keep
const idToKeep : {id: number} = this._input.allWaysNodesInOrder.map((nodes, i) => ({nodes: nodes, id: this._input.allWayIdsInOrder[i]}))
const idToKeep: { id: number } = this._input.allWaysNodesInOrder.map((nodes, i) => ({
nodes: nodes,
id: this._input.allWayIdsInOrder[i]
}))
.filter(nodesId => {
const nds = nodesId.nodes
return nds[0] == commonPoint || nds[nds.length - 1] == commonPoint
return nds[0] == commonPoint || nds[nds.length - 1] == commonPoint
})[0]
if(idToKeep === undefined){
if (idToKeep === undefined) {
console.error("No common point found, this was a broken turn restriction!", relation.id)
return []
}
const originalWayId = this._input.originalWayId
if(idToKeep.id === originalWayId){
if (idToKeep.id === originalWayId) {
console.log("Turn_restriction fixer: the original ID can be kept, nothing to do")
return []
}
const newMembers : {
ref:number,
type:"way" | "node" | "relation",
role:string
const newMembers: {
ref: number,
type: "way" | "node" | "relation",
role: string
} [] = relation.members.map(m => {
if(m.type === "way" && m.ref === originalWayId){
if (m.type === "way" && m.ref === originalWayId) {
return {
ref: idToKeep.id,
type:"way",
type: "way",
role: m.role
}
}
return m
})
return [
{
type: "relation",
@ -148,7 +152,7 @@ export class TurnRestrictionRSH extends AbstractRelationSplitHandler {
}
];
}
}
/**
@ -184,8 +188,8 @@ export class InPlaceReplacedmentRTSH extends AbstractRelationSplitHandler {
const nodeIdBefore = await this.targetNodeAt(i - 1, false)
const nodeIdAfter = await this.targetNodeAt(i + 1, true)
const firstNodeMatches = nodeIdBefore === undefined || nodeIdBefore === firstNode
const lastNodeMatches =nodeIdAfter === undefined || nodeIdAfter === lastNode
const firstNodeMatches = nodeIdBefore === undefined || nodeIdBefore === firstNode
const lastNodeMatches = nodeIdAfter === undefined || nodeIdAfter === lastNode
if (firstNodeMatches && lastNodeMatches) {
// We have a classic situation, forward situation
@ -200,10 +204,10 @@ export class InPlaceReplacedmentRTSH extends AbstractRelationSplitHandler {
}
const firstNodeMatchesRev = nodeIdBefore === undefined || nodeIdBefore === lastNode
const lastNodeMatchesRev =nodeIdAfter === undefined || nodeIdAfter === firstNode
const lastNodeMatchesRev = nodeIdAfter === undefined || nodeIdAfter === firstNode
if (firstNodeMatchesRev || lastNodeMatchesRev) {
// We (probably) have a reversed situation, backward situation
for (let i1 = this._input.allWayIdsInOrder.length - 1; i1 >= 0; i1--){
for (let i1 = this._input.allWayIdsInOrder.length - 1; i1 >= 0; i1--) {
// Iterate BACKWARDS
const wId = this._input.allWayIdsInOrder[i1];
newMembers.push({
@ -214,7 +218,7 @@ export class InPlaceReplacedmentRTSH extends AbstractRelationSplitHandler {
}
continue;
}
// Euhm, allright... Something weird is going on, but let's not care too much
// Lets pretend this is forward going
for (const wId of this._input.allWayIdsInOrder) {
@ -231,7 +235,7 @@ export class InPlaceReplacedmentRTSH extends AbstractRelationSplitHandler {
id: relation.id,
type: "relation",
changes: {members: newMembers},
meta:{
meta: {
changeType: "relation-fix",
theme: this._theme
}