| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  | import Script from "./Script" | 
					
						
							| 
									
										
										
										
											2023-07-15 18:04:30 +02:00
										 |  |  | import { Utils } from "../src/Utils" | 
					
						
							| 
									
										
										
										
											2023-09-02 23:24:01 +02:00
										 |  |  | import { Eli, EliEntry } from "./@types/eli" | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  | import fs from "fs" | 
					
						
							| 
									
										
										
										
											2024-01-28 05:10:13 +01:00
										 |  |  | import { BingRasterLayer } from "../src/UI/Map/BingRasterLayer" | 
					
						
							| 
									
										
										
										
											2025-05-03 15:23:17 +02:00
										 |  |  | import ScriptUtils from "./ScriptUtils" | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DownloadEli extends Script { | 
					
						
							|  |  |  |     constructor() { | 
					
						
							|  |  |  |         super("Downloads a fresh copy of the editor layer index, removes all unnecessary data.") | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-01-28 05:10:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |     async main(args: string[]): Promise<void> { | 
					
						
							|  |  |  |         const url = "https://osmlab.github.io/editor-layer-index/imagery.geojson" | 
					
						
							|  |  |  |         // Target should use '.json' instead of '.geojson', as the latter cannot be imported by the build systems
 | 
					
						
							| 
									
										
										
										
											2024-08-14 11:41:57 +02:00
										 |  |  |         const target = args[0] ?? "public/assets/data/editor-layer-index.json" | 
					
						
							| 
									
										
										
										
											2024-10-13 21:37:04 +02:00
										 |  |  |         const targetGlobal = args[1] ?? "src/assets/generated/editor-layer-index-global.json" | 
					
						
							| 
									
										
										
										
											2024-08-14 11:41:57 +02:00
										 |  |  |         const targetBing = args[0] ?? "src/assets/bing.json" | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 23:24:01 +02:00
										 |  |  |         const eli: Eli = await Utils.downloadJson(url) | 
					
						
							|  |  |  |         const keptLayers: EliEntry[] = [] | 
					
						
							| 
									
										
										
										
											2024-10-13 21:37:04 +02:00
										 |  |  |         const keptGlobalLayers: EliEntry[] = [] | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |         console.log("Got", eli.features.length, "ELI-entries") | 
					
						
							|  |  |  |         for (let layer of eli.features) { | 
					
						
							|  |  |  |             const props = layer.properties | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.type === "bing") { | 
					
						
							| 
									
										
										
										
											2025-04-07 19:10:11 +02:00
										 |  |  |                 // A lot of work to implement - see https://source.mapcomplete.org/MapComplete/MapComplete/issues/648
 | 
					
						
							| 
									
										
										
										
											2024-01-28 05:10:13 +01:00
										 |  |  |                 try { | 
					
						
							|  |  |  |                     const bing = await BingRasterLayer.get() | 
					
						
							|  |  |  |                     if (bing === "error") { | 
					
						
							|  |  |  |                         continue | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     delete props.default | 
					
						
							|  |  |  |                     props.category = "photo" | 
					
						
							|  |  |  |                     props.url = bing.properties.url.replace("%7Bquadkey%7D", "{quadkey}") | 
					
						
							|  |  |  |                 } catch (e) { | 
					
						
							|  |  |  |                     console.error("Could not fetch URL for bing", e) | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.id === "MAPNIK") { | 
					
						
							|  |  |  |                 // Already added by default
 | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.overlay) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:23:42 +01:00
										 |  |  |             if (props.id === "Mapbox") { | 
					
						
							| 
									
										
										
										
											2023-03-19 13:37:28 +01:00
										 |  |  |                 /** | 
					
						
							|  |  |  |                  * This token is managed by Martin Reifer on the 'OpenStreetMap'-account on MapBox | 
					
						
							|  |  |  |                  */ | 
					
						
							| 
									
										
										
										
											2023-03-24 12:23:42 +01:00
										 |  |  |                 const token = | 
					
						
							|  |  |  |                     "pk.eyJ1Ijoib3BlbnN0cmVldG1hcCIsImEiOiJjbGZkempiNDkyandvM3lwY3M4MndpdWdzIn0.QnvRv52n3qffVEKmQa9vJA" | 
					
						
							| 
									
										
										
										
											2023-03-19 13:37:28 +01:00
										 |  |  |                 props.url = props.url.replace("{apikey}", token) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |             if (props.url.toLowerCase().indexOf("apikey") > 0) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 23:24:01 +02:00
										 |  |  |             if (props.permission_osm === "no") { | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 23:24:01 +02:00
										 |  |  |             if (props.max_zoom && props.max_zoom < 19) { | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |                 // We want users to zoom to level 19 when adding a point
 | 
					
						
							|  |  |  |                 // If they are on a layer which hasn't enough precision, they can not zoom far enough. This is confusing, so we don't use this layer
 | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.name === undefined) { | 
					
						
							|  |  |  |                 console.warn("Editor layer index: name not defined on ", props) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 23:24:01 +02:00
										 |  |  |             if (props.url.startsWith("http://")) { | 
					
						
							|  |  |  |                 // Mixed content will not work properly, so we don't use this layer
 | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Override the layer, so it contains only the properties we need
 | 
					
						
							|  |  |  |             layer.properties = { | 
					
						
							|  |  |  |                 name: props.name, | 
					
						
							|  |  |  |                 id: props.id, | 
					
						
							|  |  |  |                 url: props.url, | 
					
						
							|  |  |  |                 attribution: props.attribution, | 
					
						
							|  |  |  |                 type: props.type, | 
					
						
							|  |  |  |                 category: props.category, | 
					
						
							|  |  |  |                 min_zoom: props.min_zoom, | 
					
						
							|  |  |  |                 max_zoom: props.max_zoom, | 
					
						
							|  |  |  |                 best: props.best ? true : undefined, | 
					
						
							|  |  |  |                 default: props.default ? true : undefined, | 
					
						
							|  |  |  |                 "tile-size": props["tile-size"], | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             layer = { properties: layer.properties, type: layer.type, geometry: layer.geometry } | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |             if (layer.geometry === null) { | 
					
						
							| 
									
										
										
										
											2024-10-13 21:37:04 +02:00
										 |  |  |                 keptGlobalLayers.push(layer) | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2024-10-13 21:37:04 +02:00
										 |  |  |                 keptLayers.push(layer) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const contents = | 
					
						
							|  |  |  |             '{"type":"FeatureCollection",\n  "features": [\n' + | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |             keptLayers.map((l) => JSON.stringify(l)).join(",\n") + | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |             "\n]}" | 
					
						
							| 
									
										
										
										
											2024-08-11 16:27:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |         const contentsGlobal = keptGlobalLayers | 
					
						
							|  |  |  |             .filter((l) => l.properties.id !== "Bing") | 
					
						
							|  |  |  |             .map((l) => l.properties) | 
					
						
							| 
									
										
										
										
											2024-10-13 21:37:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const bing = keptGlobalLayers.find((l) => l.properties.id === "Bing") | 
					
						
							| 
									
										
										
										
											2024-08-14 13:53:56 +02:00
										 |  |  |         if (bing) { | 
					
						
							| 
									
										
										
										
											2024-08-14 11:41:57 +02:00
										 |  |  |             fs.writeFileSync(targetBing, JSON.stringify(bing), { encoding: "utf8" }) | 
					
						
							|  |  |  |             console.log("Written", targetBing) | 
					
						
							| 
									
										
										
										
											2024-08-14 13:53:56 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2024-08-11 16:27:00 +02:00
										 |  |  |             console.log("No bing entry found") | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-03 15:23:17 +02:00
										 |  |  |         ScriptUtils.createParentDir(target) | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |         fs.writeFileSync(target, contents, { encoding: "utf8" }) | 
					
						
							|  |  |  |         console.log("Written", keptLayers.length + ", entries to the ELI") | 
					
						
							| 
									
										
										
										
											2025-05-03 15:23:17 +02:00
										 |  |  |         ScriptUtils.createParentDir(targetGlobal) | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |         fs.writeFileSync(targetGlobal, JSON.stringify(contentsGlobal, null, "  "), { | 
					
						
							|  |  |  |             encoding: "utf8", | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2025-05-03 23:48:35 +02:00
										 |  |  |         console.log( | 
					
						
							|  |  |  |             "Written", | 
					
						
							|  |  |  |             keptGlobalLayers.length + ", entries to the global ELI (" + targetGlobal + ")" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-03-10 19:41:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | new DownloadEli().run() |