forked from MapComplete/MapComplete
		
	Add createNewWay-action, more work on GRB import theme, add import button
This commit is contained in:
		
							parent
							
								
									e4cd93ffb0
								
							
						
					
					
						commit
						da65bbbc86
					
				
					 9 changed files with 341 additions and 100 deletions
				
			
		|  | @ -8,20 +8,29 @@ import {GeoOperations} from "../../GeoOperations"; | |||
| 
 | ||||
| export default class CreateNewNodeAction extends OsmChangeAction { | ||||
| 
 | ||||
|     /** | ||||
|      * Maps previously created points onto their assigned ID, to reuse the point if uplaoded | ||||
|      * "lat,lon" --> id | ||||
|      */ | ||||
|     private static readonly previouslyCreatedPoints = new Map<string, number>() | ||||
|     public newElementId: string = undefined | ||||
|     public newElementIdNumber: number = undefined | ||||
|     private readonly _basicTags: Tag[]; | ||||
|     private readonly _lat: number; | ||||
|     private readonly _lon: number; | ||||
|     private readonly _snapOnto: OsmWay; | ||||
|     private readonly _reusePointDistance: number; | ||||
|     private meta: { changeType: "create" | "import"; theme: string }; | ||||
|     private readonly _reusePreviouslyCreatedPoint: boolean; | ||||
| 
 | ||||
|     constructor(basicTags: Tag[], | ||||
|                 lat: number, lon: number, | ||||
|                 options: { | ||||
|                     snapOnto?: OsmWay,  | ||||
|                     reusePointWithinMeters?: number,  | ||||
|                     theme: string, changeType: "create" | "import" }) { | ||||
|                     allowReuseOfPreviouslyCreatedPoints?: boolean, | ||||
|                     snapOnto?: OsmWay, | ||||
|                     reusePointWithinMeters?: number, | ||||
|                     theme: string, changeType: "create" | "import" | null | ||||
|                 }) { | ||||
|         super() | ||||
|         this._basicTags = basicTags; | ||||
|         this._lat = lat; | ||||
|  | @ -31,18 +40,47 @@ export default class CreateNewNodeAction extends OsmChangeAction { | |||
|         } | ||||
|         this._snapOnto = options?.snapOnto; | ||||
|         this._reusePointDistance = options?.reusePointWithinMeters ?? 1 | ||||
|         this._reusePreviouslyCreatedPoint = options?.allowReuseOfPreviouslyCreatedPoints ?? (basicTags.length === 0) | ||||
|         this.meta = { | ||||
|             theme: options.theme, | ||||
|             changeType: options.changeType | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static registerIdRewrites(mappings: Map<string, string>) { | ||||
|         const toAdd: [string, number][] = [] | ||||
| 
 | ||||
|         this.previouslyCreatedPoints.forEach((oldId, key) => { | ||||
|             if (!mappings.has("node/" + oldId)) { | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|             const newId = Number(mappings.get("node/" + oldId).substr("node/".length)) | ||||
|             toAdd.push([key, newId]) | ||||
|         }) | ||||
|         for (const [key, newId] of toAdd) { | ||||
|             CreateNewNodeAction.previouslyCreatedPoints.set(key, newId) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> { | ||||
|         if (this._reusePreviouslyCreatedPoint) { | ||||
| 
 | ||||
|             const key = this._lat + "," + this._lon | ||||
|             const prev = CreateNewNodeAction.previouslyCreatedPoints | ||||
|             if (prev.has(key)) { | ||||
|                 this.newElementIdNumber = prev.get(key) | ||||
|                 this.newElementId = "node/" + this.newElementIdNumber | ||||
|                 return [] | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         const id = changes.getNewID() | ||||
|         const properties = { | ||||
|             id: "node/" + id | ||||
|         } | ||||
|         this.newElementId = "node/" + id | ||||
|         this.setElementId(id) | ||||
|         for (const kv of this._basicTags) { | ||||
|             if (typeof kv.value !== "string") { | ||||
|                 throw "Invalid value: don't use a regex in a preset" | ||||
|  | @ -84,8 +122,7 @@ export default class CreateNewNodeAction extends OsmChangeAction { | |||
|         } | ||||
|         if (reusedPointId !== undefined) { | ||||
|             console.log("Reusing an existing point:", reusedPointId) | ||||
|             this.newElementId = "node/" + reusedPointId | ||||
| 
 | ||||
|             this.setElementId(reusedPointId) | ||||
|             return [{ | ||||
|                 tags: new And(this._basicTags).asChange(properties), | ||||
|                 type: "node", | ||||
|  | @ -112,10 +149,20 @@ export default class CreateNewNodeAction extends OsmChangeAction { | |||
|                     coordinates: locations, | ||||
|                     nodes: ids | ||||
|                 }, | ||||
|                 meta:this.meta | ||||
|                 meta: this.meta | ||||
|             } | ||||
|         ] | ||||
|     } | ||||
| 
 | ||||
|     private setElementId(id: number) { | ||||
|         this.newElementIdNumber = id; | ||||
|         this.newElementId = "node/"+id | ||||
|         if (!this._reusePreviouslyCreatedPoint) { | ||||
|             return | ||||
|         } | ||||
|         const key = this._lat + "," + this._lon | ||||
|         CreateNewNodeAction.previouslyCreatedPoints.set(key, id) | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue