forked from MapComplete/MapComplete
		
	chore: refactoring, linting, removing obsolete files
This commit is contained in:
		
							parent
							
								
									b7a4930583
								
							
						
					
					
						commit
						2bc2a6cddf
					
				
					 54 changed files with 125 additions and 457 deletions
				
			
		|  | @ -46,7 +46,6 @@ export default class InsertPointIntoWayAction { | |||
|         console.log("Attempting to snap:", { geojson, projected, projectedCoor, index }) | ||||
|         // We check that it isn't close to an already existing point
 | ||||
|         let reusedPointId = undefined | ||||
|         let reusedPointCoordinates: [number, number] = undefined | ||||
|         let outerring: [number, number][] | ||||
| 
 | ||||
|         if (geojson.geometry.type === "LineString") { | ||||
|  | @ -62,7 +61,6 @@ export default class InsertPointIntoWayAction { | |||
|         ) { | ||||
|             // We reuse this point instead!
 | ||||
|             reusedPointId = this._snapOnto.nodes[index] | ||||
|             reusedPointCoordinates = this._snapOnto.coordinates[index] | ||||
|         } | ||||
|         const next = outerring[index + 1] | ||||
|         if ( | ||||
|  | @ -71,14 +69,13 @@ export default class InsertPointIntoWayAction { | |||
|         ) { | ||||
|             // We reuse this point instead!
 | ||||
|             reusedPointId = this._snapOnto.nodes[index + 1] | ||||
|             reusedPointCoordinates = this._snapOnto.coordinates[index + 1] | ||||
|         } | ||||
|         if (reusedPointId !== undefined) { | ||||
|             return undefined | ||||
|         } | ||||
| 
 | ||||
|         const locations = [ | ||||
|             ...this._snapOnto.coordinates?.map(([lat, lon]) => <[number, number]>[lon, lat]), | ||||
|             ...(this._snapOnto.coordinates ?? []).map(([lat, lon]) => <[number, number]>[lon, lat]), | ||||
|         ] | ||||
|         const ids = [...this._snapOnto.nodes] | ||||
| 
 | ||||
|  |  | |||
|  | @ -181,7 +181,7 @@ export class InPlaceReplacedmentRTSH extends AbstractRelationSplitHandler { | |||
|         super(input, theme, objectDownloader) | ||||
|     } | ||||
| 
 | ||||
|     async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> { | ||||
|     async CreateChangeDescriptions(): Promise<ChangeDescription[]> { | ||||
|         const wayId = this._input.originalWayId | ||||
|         const relation = this._input.relation | ||||
|         const members = relation.members | ||||
|  |  | |||
|  | @ -382,7 +382,7 @@ export default class ReplaceGeometryAction extends OsmChangeAction implements Pr | |||
|         >() | ||||
|         { | ||||
|             // Lets check the unused ids: can they be detached or do they signify some relation with the object?
 | ||||
|             unusedIds.forEach(({}, id) => { | ||||
|             unusedIds.forEach((_, id) => { | ||||
|                 const info = nodeInfo.get(id) | ||||
|                 if (!(info.hasTags || info.partOfWay)) { | ||||
|                     // Nothing special here, we detach
 | ||||
|  |  | |||
|  | @ -144,9 +144,6 @@ export default class SplitAction extends OsmChangeAction { | |||
|                 // Copy the tags from the original object onto the new
 | ||||
|                 const kv = [] | ||||
|                 for (const k in originalElement.tags) { | ||||
|                     if (!originalElement.tags.hasOwnProperty(k)) { | ||||
|                         continue | ||||
|                     } | ||||
|                     if (k.startsWith("_") || k === "id") { | ||||
|                         continue | ||||
|                     } | ||||
|  |  | |||
|  | @ -1,200 +0,0 @@ | |||
| export default class AspectedRouting { | ||||
|     public readonly name: string | ||||
|     public readonly description: string | ||||
|     public readonly units: string | ||||
|     public readonly program: any | ||||
| 
 | ||||
|     public constructor(program) { | ||||
|         this.name = program.name | ||||
|         this.description = program.description | ||||
|         this.units = program.unit | ||||
|         this.program = JSON.parse(JSON.stringify(program)) | ||||
|         delete this.program.name | ||||
|         delete this.program.description | ||||
|         delete this.program.unit | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Interprets the given Aspected-routing program for the given properties | ||||
|      */ | ||||
|     public static interpret(program: any, properties: any) { | ||||
|         if (typeof program !== "object") { | ||||
|             return program | ||||
|         } | ||||
| 
 | ||||
|         let functionName /*: string*/ = undefined | ||||
|         let functionArguments /*: any */ = undefined | ||||
|         const otherValues = {} | ||||
|         // @ts-ignore
 | ||||
|         Object.entries(program).forEach((tag) => { | ||||
|             const [key, value] = tag | ||||
|             if (key.startsWith("$")) { | ||||
|                 functionName = key | ||||
|                 functionArguments = value | ||||
|             } else { | ||||
|                 otherValues[key] = value | ||||
|             } | ||||
|         }) | ||||
| 
 | ||||
|         if (functionName === undefined) { | ||||
|             return AspectedRouting.interpretAsDictionary(program, properties) | ||||
|         } | ||||
| 
 | ||||
|         if (functionName === "$multiply") { | ||||
|             return AspectedRouting.multiplyScore(properties, functionArguments) | ||||
|         } else if (functionName === "$firstMatchOf") { | ||||
|             return AspectedRouting.getFirstMatchScore(properties, functionArguments) | ||||
|         } else if (functionName === "$min") { | ||||
|             return AspectedRouting.getMinValue(properties, functionArguments) | ||||
|         } else if (functionName === "$max") { | ||||
|             return AspectedRouting.getMaxValue(properties, functionArguments) | ||||
|         } else if (functionName === "$default") { | ||||
|             return AspectedRouting.defaultV(functionArguments, otherValues, properties) | ||||
|         } else { | ||||
|             console.error( | ||||
|                 `Error: Program ${functionName} is not implemented yet. ${JSON.stringify(program)}` | ||||
|             ) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Given a 'program' without function invocation, interprets it as a dictionary | ||||
|      * | ||||
|      * E.g., given the program | ||||
|      * | ||||
|      * { | ||||
|      *     highway: { | ||||
|      *         residential: 30, | ||||
|      *         living_street: 20 | ||||
|      *     }, | ||||
|      *     surface: { | ||||
|      *         sett : 0.9 | ||||
|      *     } | ||||
|      * | ||||
|      * } | ||||
|      * | ||||
|      * in combination with the tags {highway: residential}, | ||||
|      * | ||||
|      * the result should be [30, undefined]; | ||||
|      * | ||||
|      * For the tags {highway: residential, surface: sett} we should get [30, 0.9] | ||||
|      * | ||||
|      * | ||||
|      * @param program | ||||
|      * @param tags | ||||
|      * @return {(undefined|*)[]} | ||||
|      */ | ||||
|     private static interpretAsDictionary(program, tags) { | ||||
|         // @ts-ignore
 | ||||
|         return Object.entries(tags).map((tag) => { | ||||
|             const [key, value] = tag | ||||
|             const propertyValue = program[key] | ||||
|             if (propertyValue === undefined) { | ||||
|                 return undefined | ||||
|             } | ||||
|             if (typeof propertyValue !== "object") { | ||||
|                 return propertyValue | ||||
|             } | ||||
|             // @ts-ignore
 | ||||
|             return propertyValue[value] | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     private static defaultV(subProgram, otherArgs, tags) { | ||||
|         // @ts-ignore
 | ||||
|         const normalProgram = Object.entries(otherArgs)[0][1] | ||||
|         const value = AspectedRouting.interpret(normalProgram, tags) | ||||
|         if (value !== undefined) { | ||||
|             return value | ||||
|         } | ||||
|         return AspectedRouting.interpret(subProgram, tags) | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Multiplies the default score with the proper values | ||||
|      * @param tags {object} the active tags to check against | ||||
|      * @param subprograms which should generate a list of values | ||||
|      * @returns score after multiplication | ||||
|      */ | ||||
|     private static multiplyScore(tags, subprograms) { | ||||
|         let number = 1 | ||||
| 
 | ||||
|         let subResults: any[] | ||||
|         if (subprograms.length !== undefined) { | ||||
|             subResults = AspectedRouting.concatMap(subprograms, (subprogram) => | ||||
|                 AspectedRouting.interpret(subprogram, tags) | ||||
|             ) | ||||
|         } else { | ||||
|             subResults = AspectedRouting.interpret(subprograms, tags) | ||||
|         } | ||||
| 
 | ||||
|         subResults.filter((r) => r !== undefined).forEach((r) => (number *= parseFloat(r))) | ||||
|         return number.toFixed(2) | ||||
|     } | ||||
| 
 | ||||
|     private static getFirstMatchScore(tags, order: any) { | ||||
|         /*Order should be a list of arguments after evaluation*/ | ||||
|         order = <string[]>AspectedRouting.interpret(order, tags) | ||||
|         for (const key of order) { | ||||
|             // @ts-ignore
 | ||||
|             for (const entry of Object.entries(JSON.parse(tags))) { | ||||
|                 const [tagKey, value] = entry | ||||
|                 if (key === tagKey) { | ||||
|                     // We have a match... let's evaluate the subprogram
 | ||||
|                     const evaluated = AspectedRouting.interpret(value, tags) | ||||
|                     if (evaluated !== undefined) { | ||||
|                         return evaluated | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // Not a single match found...
 | ||||
|         return undefined | ||||
|     } | ||||
| 
 | ||||
|     private static getMinValue(tags, subprogram) { | ||||
|         const minArr = subprogram | ||||
|             .map((part) => { | ||||
|                 if (typeof part === "object") { | ||||
|                     const calculatedValue = this.interpret(part, tags) | ||||
|                     return parseFloat(calculatedValue) | ||||
|                 } else { | ||||
|                     return parseFloat(part) | ||||
|                 } | ||||
|             }) | ||||
|             .filter((v) => !isNaN(v)) | ||||
|         return Math.min(...minArr) | ||||
|     } | ||||
| 
 | ||||
|     private static getMaxValue(tags, subprogram) { | ||||
|         const maxArr = subprogram | ||||
|             .map((part) => { | ||||
|                 if (typeof part === "object") { | ||||
|                     return parseFloat(AspectedRouting.interpret(part, tags)) | ||||
|                 } else { | ||||
|                     return parseFloat(part) | ||||
|                 } | ||||
|             }) | ||||
|             .filter((v) => !isNaN(v)) | ||||
|         return Math.max(...maxArr) | ||||
|     } | ||||
| 
 | ||||
|     private static concatMap(list, f): any[] { | ||||
|         const result = [] | ||||
|         list = list.map(f) | ||||
|         for (const elem of list) { | ||||
|             if (elem.length !== undefined) { | ||||
|                 // This is a list
 | ||||
|                 result.push(...elem) | ||||
|             } else { | ||||
|                 result.push(elem) | ||||
|             } | ||||
|         } | ||||
|         return result | ||||
|     } | ||||
| 
 | ||||
|     public evaluate(properties) { | ||||
|         return AspectedRouting.interpret(this.program, properties) | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue