| 
									
										
										
										
											2022-11-02 14:44:06 +01:00
										 |  |  | import { Utils } from "../../Utils" | 
					
						
							|  |  |  | import { Feature } from "geojson" | 
					
						
							|  |  |  | import { Point } from "@turf/turf" | 
					
						
							| 
									
										
										
										
											2022-12-22 04:13:52 +01:00
										 |  |  | import { GeoLocationPointProperties } from "../../Logic/State/GeoLocationState" | 
					
						
							| 
									
										
										
										
											2022-11-02 14:44:06 +01:00
										 |  |  | import UploadTraceToOsmUI from "../BigComponents/UploadTraceToOsmUI" | 
					
						
							|  |  |  | import { SpecialVisualization } from "../SpecialVisualization" | 
					
						
							| 
									
										
										
										
											2022-10-28 04:33:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Wrapper  around 'UploadTraceToOsmUI' | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class UploadToOsmViz implements SpecialVisualization { | 
					
						
							|  |  |  |     funcName = "upload_to_osm" | 
					
						
							| 
									
										
										
										
											2022-11-02 14:44:06 +01:00
										 |  |  |     docs = | 
					
						
							|  |  |  |         "Uploads the GPS-history as GPX to OpenStreetMap.org; clears the history afterwards. The actual feature is ignored." | 
					
						
							|  |  |  |     args = [] | 
					
						
							| 
									
										
										
										
											2022-10-28 04:33:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constr(state, featureTags, args) { | 
					
						
							|  |  |  |         function getTrace(title: string) { | 
					
						
							|  |  |  |             title = title?.trim() | 
					
						
							|  |  |  |             if (title === undefined || title === "") { | 
					
						
							|  |  |  |                 title = "Uploaded with MapComplete" | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             title = Utils.EncodeXmlValue(title) | 
					
						
							| 
									
										
										
										
											2022-11-02 14:44:06 +01:00
										 |  |  |             const userLocations: Feature<Point, GeoLocationPointProperties>[] = | 
					
						
							|  |  |  |                 state.historicalUserLocations.features.data.map((f) => f.feature) | 
					
						
							| 
									
										
										
										
											2022-10-28 04:33:05 +02:00
										 |  |  |             const trackPoints: string[] = [] | 
					
						
							|  |  |  |             for (const l of userLocations) { | 
					
						
							|  |  |  |                 let trkpt = `    <trkpt lat="${l.geometry.coordinates[1]}" lon="${l.geometry.coordinates[0]}">` | 
					
						
							|  |  |  |                 trkpt += `        <time>${l.properties.date}</time>` | 
					
						
							|  |  |  |                 if (l.properties.altitude !== null && l.properties.altitude !== undefined) { | 
					
						
							|  |  |  |                     trkpt += `        <ele>${l.properties.altitude}</ele>` | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 trkpt += "    </trkpt>" | 
					
						
							|  |  |  |                 trackPoints.push(trkpt) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-11-02 14:44:06 +01:00
										 |  |  |             const header = | 
					
						
							|  |  |  |                 '<gpx version="1.1" creator="MapComplete track uploader" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">' | 
					
						
							|  |  |  |             return ( | 
					
						
							|  |  |  |                 header + | 
					
						
							|  |  |  |                 "\n<name>" + | 
					
						
							|  |  |  |                 title + | 
					
						
							|  |  |  |                 "</name>\n<trk><trkseg>\n" + | 
					
						
							|  |  |  |                 trackPoints.join("\n") + | 
					
						
							|  |  |  |                 "\n</trkseg></trk></gpx>" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-10-28 04:33:05 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new UploadTraceToOsmUI(getTrace, state, { | 
					
						
							|  |  |  |             whenUploaded: async () => { | 
					
						
							|  |  |  |                 state.historicalUserLocations.features.setData([]) | 
					
						
							| 
									
										
										
										
											2022-11-02 14:44:06 +01:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2022-10-28 04:33:05 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |