| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { ChangeDescription } from "./ChangeDescription" | 
					
						
							|  |  |  | import { OsmCreateAction } from "./OsmChangeAction" | 
					
						
							|  |  |  | import { Changes } from "../Changes" | 
					
						
							|  |  |  | import { Tag } from "../../Tags/Tag" | 
					
						
							|  |  |  | import CreateNewNodeAction from "./CreateNewNodeAction" | 
					
						
							|  |  |  | import { And } from "../../Tags/And" | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 04:00:02 +01:00
										 |  |  | export default class CreateNewWayAction extends OsmCreateAction { | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |     public newElementId: string = undefined | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public newElementIdNumber: number = 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: { | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |         theme: string | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*** | 
					
						
							|  |  |  |      * Creates a new way to upload to OSM | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |      * @param tags: the tags to apply to the way | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |      * @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
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         tags: Tag[], | 
					
						
							|  |  |  |         coordinates: { nodeId?: number; lat: number; lon: number }[], | 
					
						
							|  |  |  |         options: { | 
					
						
							|  |  |  |             theme: string | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |         super(null, true) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.coordinates = [] | 
					
						
							| 
									
										
										
										
											2022-02-11 02:55:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (const coordinate of coordinates) { | 
					
						
							|  |  |  |             /* The 'PointReuseAction' is a bit buggy and might generate duplicate ids. | 
					
						
							|  |  |  |                 We filter those here, as the CreateWayWithPointReuseAction delegates the actual creation to here. | 
					
						
							|  |  |  |                 Filtering here also prevents similar bugs in other actions | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if ( | 
					
						
							|  |  |  |                 this.coordinates.length > 0 && | 
					
						
							|  |  |  |                 coordinate.nodeId !== undefined && | 
					
						
							|  |  |  |                 this.coordinates[this.coordinates.length - 1].nodeId === coordinate.nodeId | 
					
						
							|  |  |  |             ) { | 
					
						
							| 
									
										
										
										
											2022-02-11 02:55:42 +01:00
										 |  |  |                 // This is a duplicate id
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 console.warn( | 
					
						
							|  |  |  |                     "Skipping a node in createWay to avoid a duplicate node:", | 
					
						
							|  |  |  |                     coordinate, | 
					
						
							|  |  |  |                     "\nThe previous coordinates are: ", | 
					
						
							|  |  |  |                     this.coordinates | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2022-02-11 02:55:42 +01:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 02:55:42 +01:00
										 |  |  |             this.coordinates.push(coordinate) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.tags = tags | 
					
						
							|  |  |  |         this._options = options | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-09 01:49:07 +01:00
										 |  |  |     public async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> { | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |         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, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 theme: this._options.theme, | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             newElements.push(...(await newPoint.CreateChangeDescriptions(changes))) | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |             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() | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |         this.newElementIdNumber = id | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 changeType: "import", | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             tags: new And(this.tags).asChange({}), | 
					
						
							|  |  |  |             changes: { | 
					
						
							|  |  |  |                 nodes: pointIds, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 coordinates: this.coordinates.map((c) => [c.lon, c.lat]), | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2021-10-29 16:38:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |