| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  | import {ChangeDescription} from "./ChangeDescription"; | 
					
						
							|  |  |  | import OsmChangeAction from "./OsmChangeAction"; | 
					
						
							|  |  |  | import {Changes} from "../Changes"; | 
					
						
							|  |  |  | import {Tag} from "../../Tags/Tag"; | 
					
						
							|  |  |  | import CreateNewNodeAction from "./CreateNewNodeAction"; | 
					
						
							|  |  |  | import {And} from "../../Tags/And"; | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  | import {TagsFilter} from "../../Tags/TagsFilter"; | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class CreateNewWayAction extends OsmChangeAction { | 
					
						
							|  |  |  |     public newElementId: string = undefined | 
					
						
							|  |  |  |     private readonly coordinates: ({ nodeId?: number, lat: number, lon: number })[]; | 
					
						
							|  |  |  |     private readonly tags: Tag[]; | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |     private readonly _options: { | 
					
						
							|  |  |  |         theme: string, existingPointHandling?: { | 
					
						
							|  |  |  |             withinRangeOfM: number, | 
					
						
							|  |  |  |             ifMatches?: TagsFilter, | 
					
						
							|  |  |  |             mode: "reuse_osm_point" | "move_osm_point" | 
					
						
							|  |  |  |         } [] | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*** | 
					
						
							|  |  |  |      * Creates a new way to upload to OSM | 
					
						
							|  |  |  |      * @param tags: the tags to apply to the wya | 
					
						
							|  |  |  |      * @param coordinates: the coordinates. Might have a nodeId, in this case, this node will be used | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |      * @param options | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |     constructor(tags: Tag[], coordinates: ({ nodeId?: number, lat: number, lon: number })[], | 
					
						
							|  |  |  |                 options: { | 
					
						
							|  |  |  |                     theme: string, | 
					
						
							|  |  |  |                     /** | 
					
						
							|  |  |  |                      * IF specified, an existing OSM-point within this range and satisfying the condition 'ifMatches' will be used instead of a new coordinate. | 
					
						
							|  |  |  |                      * If multiple points are possible, only the closest point is considered | 
					
						
							|  |  |  |                      */ | 
					
						
							|  |  |  |                     existingPointHandling?: { | 
					
						
							|  |  |  |                         withinRangeOfM: number, | 
					
						
							|  |  |  |                         ifMatches?: TagsFilter, | 
					
						
							|  |  |  |                         mode: "reuse_osm_point" | "move_osm_point" | 
					
						
							|  |  |  |                     } [] | 
					
						
							|  |  |  |                 }) { | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |         super() | 
					
						
							|  |  |  |         this.coordinates = coordinates; | 
					
						
							|  |  |  |         this.tags = tags; | 
					
						
							|  |  |  |         this._options = options; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const newElements: ChangeDescription[] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const pointIds: number[] = [] | 
					
						
							|  |  |  |         for (const coordinate of this.coordinates) { | 
					
						
							|  |  |  |             if (coordinate.nodeId !== undefined) { | 
					
						
							|  |  |  |                 pointIds.push(coordinate.nodeId) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const newPoint = new CreateNewNodeAction([], coordinate.lat, coordinate.lon, { | 
					
						
							| 
									
										
										
										
											2021-10-30 02:34:16 +02:00
										 |  |  |                 allowReuseOfPreviouslyCreatedPoints: true, | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |                 changeType: null, | 
					
						
							|  |  |  |                 theme: this._options.theme | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             await changes.applyAction(newPoint) | 
					
						
							|  |  |  |             pointIds.push(newPoint.newElementIdNumber) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // We have all created (or reused) all the points!
 | 
					
						
							|  |  |  |         // Time to create the actual way
 | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |         const id = changes.getNewID() | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const newWay = <ChangeDescription>{ | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |             id, | 
					
						
							|  |  |  |             type: "way", | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |             meta: { | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |                 theme: this._options.theme, | 
					
						
							|  |  |  |                 changeType: "import" | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             tags: new And(this.tags).asChange({}), | 
					
						
							|  |  |  |             changes: { | 
					
						
							|  |  |  |                 nodes: pointIds, | 
					
						
							|  |  |  |                 coordinates: this.coordinates.map(c => [c.lon, c.lat]) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         newElements.push(newWay) | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |         this.newElementId = "way/" + id | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |         return newElements | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |