forked from MapComplete/MapComplete
		
	Remove runningFromConsole from scripts and tests, add routeablePointScript for postal codes
This commit is contained in:
		
							parent
							
								
									5b513b1248
								
							
						
					
					
						commit
						480bb4902b
					
				
					 15 changed files with 99 additions and 42 deletions
				
			
		|  | @ -5,8 +5,6 @@ import * as https from "https"; | ||||||
| import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | ||||||
| import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson"; | import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true |  | ||||||
| 
 |  | ||||||
| export default class ScriptUtils { | export default class ScriptUtils { | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -53,7 +51,7 @@ export default class ScriptUtils { | ||||||
|             try { |             try { | ||||||
|                 headers = headers ?? {} |                 headers = headers ?? {} | ||||||
|                 headers.accept = "application/json" |                 headers.accept = "application/json" | ||||||
|                 console.log("ScriptUtils.DownloadJson(", url.substring(0, 40), url.length > 40 ? "..." : "", ")") |                 console.log(" > ScriptUtils.DownloadJson(", url, ")") | ||||||
|                 const urlObj = new URL(url) |                 const urlObj = new URL(url) | ||||||
|                 https.get({ |                 https.get({ | ||||||
|                     host: urlObj.host, |                     host: urlObj.host, | ||||||
|  |  | ||||||
|  | @ -9,9 +9,6 @@ import AllImageProviders from "../Logic/ImageProviders/AllImageProviders"; | ||||||
| import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | ||||||
| import LayerConfig from "../Models/ThemeConfig/LayerConfig"; | import LayerConfig from "../Models/ThemeConfig/LayerConfig"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| ScriptUtils.fixUtils() | ScriptUtils.fixUtils() | ||||||
| 
 | 
 | ||||||
| if (process.argv.length == 2) { | if (process.argv.length == 2) { | ||||||
|  |  | ||||||
|  | @ -26,7 +26,6 @@ import FilteringFeatureSource from "../Logic/FeatureSource/Sources/FilteringFeat | ||||||
| import Loc from "../Models/Loc"; | import Loc from "../Models/Loc"; | ||||||
| ScriptUtils.fixUtils() | ScriptUtils.fixUtils() | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| function createOverpassObject(theme: LayoutConfig, relationTracker: RelationsTracker, backend: string) { | function createOverpassObject(theme: LayoutConfig, relationTracker: RelationsTracker, backend: string) { | ||||||
|     let filters: TagsFilter[] = []; |     let filters: TagsFilter[] = []; | ||||||
|     let extraScripts: string[] = []; |     let extraScripts: string[] = []; | ||||||
|  | @ -167,6 +166,7 @@ function loadAllTiles(targetdir: string, r: TileRange, theme: LayoutConfig, extr | ||||||
| 
 | 
 | ||||||
|             // Create and save the geojson file - which is the main chunk of the data
 |             // Create and save the geojson file - which is the main chunk of the data
 | ||||||
|             const geojson = OsmToGeoJson.default(rawOsm); |             const geojson = OsmToGeoJson.default(rawOsm); | ||||||
|  |             console.log(" which as",geojson.features.length, "features") | ||||||
| 
 | 
 | ||||||
|             allFeatures.push(...geojson.features) |             allFeatures.push(...geojson.features) | ||||||
|         } |         } | ||||||
|  | @ -203,6 +203,7 @@ function sliceToTiles(allFeatures: FeatureSource, theme: LayoutConfig, relations | ||||||
| 
 | 
 | ||||||
|         const layerId = layer.id |         const layerId = layer.id | ||||||
|         if (layer.source.isOsmCacheLayer !== true) { |         if (layer.source.isOsmCacheLayer !== true) { | ||||||
|  |             console.log("Skipping layer ", layerId, ": not a caching layer") | ||||||
|             skippedLayers.add(layer.id) |             skippedLayers.add(layer.id) | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  | @ -241,6 +242,7 @@ function sliceToTiles(allFeatures: FeatureSource, theme: LayoutConfig, relations | ||||||
|             maxFeatureCount: undefined, |             maxFeatureCount: undefined, | ||||||
|             registerTile: tile => { |             registerTile: tile => { | ||||||
|                 const tileIndex = tile.tileIndex; |                 const tileIndex = tile.tileIndex; | ||||||
|  |                 console.log("Got tile:", tileIndex, tile.layer.layerDef.id) | ||||||
|                 if (tile.features.data.length === 0) { |                 if (tile.features.data.length === 0) { | ||||||
|                     return |                     return | ||||||
|                 } |                 } | ||||||
|  | @ -365,7 +367,7 @@ async function main(args: string[]) { | ||||||
|     const targetdir = args[2] + "/" + themeName |     const targetdir = args[2] + "/" + themeName | ||||||
|     if (!existsSync(args[2])) { |     if (!existsSync(args[2])) { | ||||||
|         console.log("Directory not found") |         console.log("Directory not found") | ||||||
|         throw "The directory " + args[2] + "does not exist" |         throw `The directory ${args[2]} does not exist` | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const lat0 = Number(args[3]) |     const lat0 = Number(args[3]) | ||||||
|  |  | ||||||
|  | @ -1,4 +1,3 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import SpecialVisualizations from "../UI/SpecialVisualizations"; | import SpecialVisualizations from "../UI/SpecialVisualizations"; | ||||||
| import SimpleMetaTagger from "../Logic/SimpleMetaTagger"; | import SimpleMetaTagger from "../Logic/SimpleMetaTagger"; | ||||||
| import Combine from "../UI/Base/Combine"; | import Combine from "../UI/Base/Combine"; | ||||||
|  | @ -16,9 +15,6 @@ import TableOfContents from "../UI/Base/TableOfContents"; | ||||||
| import Title from "../UI/Base/Title"; | import Title from "../UI/Base/Title"; | ||||||
| import QueryParameterDocumentation from "../UI/QueryParameterDocumentation"; | import QueryParameterDocumentation from "../UI/QueryParameterDocumentation"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| function WriteFile(filename, html: BaseUIElement, autogenSource: string[]): void { | function WriteFile(filename, html: BaseUIElement, autogenSource: string[]): void { | ||||||
| 
 | 
 | ||||||
|     if (html instanceof Combine) { |     if (html instanceof Combine) { | ||||||
|  |  | ||||||
|  | @ -1,4 +1,3 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import {existsSync, mkdirSync, readFileSync, writeFile, writeFileSync} from "fs"; | import {existsSync, mkdirSync, readFileSync, writeFile, writeFileSync} from "fs"; | ||||||
| import Locale from "../UI/i18n/Locale"; | import Locale from "../UI/i18n/Locale"; | ||||||
| import Translations from "../UI/i18n/Translations"; | import Translations from "../UI/i18n/Translations"; | ||||||
|  | @ -7,9 +6,6 @@ import Constants from "../Models/Constants"; | ||||||
| import * as all_known_layouts from "../assets/generated/known_layers_and_themes.json" | import * as all_known_layouts from "../assets/generated/known_layers_and_themes.json" | ||||||
| import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | ||||||
| import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | ||||||
| // We HAVE to mark this while importing
 |  | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| const sharp = require('sharp'); | const sharp = require('sharp'); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,10 +1,7 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import {existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync} from "fs"; | import {existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync} from "fs"; | ||||||
| import SmallLicense from "../Models/smallLicense"; | import SmallLicense from "../Models/smallLicense"; | ||||||
| import ScriptUtils from "./ScriptUtils"; | import ScriptUtils from "./ScriptUtils"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * Sweeps the entire 'assets/' (except assets/generated) directory for image files and any 'license_info.json'-file. |  * Sweeps the entire 'assets/' (except assets/generated) directory for image files and any 'license_info.json'-file. | ||||||
|  * Checks that the license info is included for each of them and generates a compiles license_info.json for those |  * Checks that the license info is included for each of them and generates a compiles license_info.json for those | ||||||
|  |  | ||||||
|  | @ -1,4 +1,3 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import {AllKnownLayouts} from "../Customizations/AllKnownLayouts"; | import {AllKnownLayouts} from "../Customizations/AllKnownLayouts"; | ||||||
| import Locale from "../UI/i18n/Locale"; | import Locale from "../UI/i18n/Locale"; | ||||||
| import {Translation} from "../UI/i18n/Translation"; | import {Translation} from "../UI/i18n/Translation"; | ||||||
|  | @ -6,9 +5,6 @@ import {readFileSync, writeFileSync} from "fs"; | ||||||
| import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | ||||||
| import LayerConfig from "../Models/ThemeConfig/LayerConfig"; | import LayerConfig from "../Models/ThemeConfig/LayerConfig"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * Generates all the files in "Docs/TagInfo". These are picked up by the taginfo project, showing a link to the mapcomplete theme if the key is used |  * Generates all the files in "Docs/TagInfo". These are picked up by the taginfo project, showing a link to the mapcomplete theme if the key is used | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | @ -1,12 +1,8 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import {writeFile} from "fs"; | import {writeFile} from "fs"; | ||||||
| import Translations from "../UI/i18n/Translations"; | import Translations from "../UI/i18n/Translations"; | ||||||
| import {AllKnownLayouts} from "../Customizations/AllKnownLayouts"; | import {AllKnownLayouts} from "../Customizations/AllKnownLayouts"; | ||||||
| import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| function generateWikiEntry(layout: LayoutConfig) { | function generateWikiEntry(layout: LayoutConfig) { | ||||||
|     if (layout.hideFromOverview) { |     if (layout.hideFromOverview) { | ||||||
|         return ""; |         return ""; | ||||||
|  |  | ||||||
							
								
								
									
										92
									
								
								scripts/postal_code_tools/createRoutablePoint.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								scripts/postal_code_tools/createRoutablePoint.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,92 @@ | ||||||
|  | import {appendFileSync, existsSync, readFileSync, writeFileSync} from "fs"; | ||||||
|  | import {GeoOperations} from "../../Logic/GeoOperations"; | ||||||
|  | import ScriptUtils from "../ScriptUtils"; | ||||||
|  | import {Utils} from "../../Utils"; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | async function main(args: string[]) { | ||||||
|  | 
 | ||||||
|  |     const pointCandidates = JSON.parse(readFileSync(args[0], "utf8")) | ||||||
|  |     const postcodes = JSON.parse(readFileSync(args[1], "utf8")) | ||||||
|  |     const output = args[2] ?? "centralCoordinates.csv" | ||||||
|  | 
 | ||||||
|  |     const perPostCode = new Map<string, any[]>() | ||||||
|  |      | ||||||
|  |     const alreadyLoaded = new Set<number>() | ||||||
|  |     if(existsSync(output)){ | ||||||
|  |       const lines =  readFileSync(output, "UTF8").split("\n") | ||||||
|  |         lines.shift() | ||||||
|  |         lines.forEach(line => { | ||||||
|  |           const postalCode = Number( line.split(",")[0]) | ||||||
|  |             alreadyLoaded.add(postalCode) | ||||||
|  |         }) | ||||||
|  |     }else{ | ||||||
|  |         writeFileSync(output,"postal_code,lon,lat\n","UTF-8") | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     for (const boundary of postcodes.features) { | ||||||
|  |         const postcode = boundary.properties.nouveau_PO | ||||||
|  |         if(alreadyLoaded.has(Number(postcode))){ | ||||||
|  |             console.log("Skipping ", postcode, "as already loaded") | ||||||
|  |             continue | ||||||
|  |         } | ||||||
|  |         if (perPostCode.has(postcode)) { | ||||||
|  |             perPostCode.get(postcode).push(boundary) | ||||||
|  |         } else { | ||||||
|  |             perPostCode.set(postcode, [boundary]) | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     for (const postcode of Array.from(perPostCode.keys())) { | ||||||
|  |         const boundaries = perPostCode.get(postcode) | ||||||
|  |         const candidates = [] | ||||||
|  |         for (const boundary of boundaries) { | ||||||
|  |             for (const candidate of pointCandidates.features) { | ||||||
|  |                 if (!GeoOperations.inside(candidate, boundary)) { | ||||||
|  |                     // console.log(JSON.stringify(candidate))
 | ||||||
|  |                     continue | ||||||
|  |                 } | ||||||
|  |                 candidates.push(candidate.geometry.coordinates) | ||||||
|  | 
 | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         if (candidates.length === 0) { | ||||||
|  |             console.log("Postcode ", postcode, "has", candidates.length, "candidates, using centerpoint instead") | ||||||
|  |             candidates.push(...boundaries.map(boundary => GeoOperations.centerpointCoordinates(boundary))) | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         const url = "https://staging.anyways.eu/routing-api/v1/routes?access_token=postal_code_script&turn_by_turn=false&format=geojson&language=en" | ||||||
|  |         const depPoints :[number,number][] = Utils.NoNull( await Promise.all(candidates.map(async candidate => { | ||||||
|  |             try{ | ||||||
|  | 
 | ||||||
|  |                 const result = await ScriptUtils.DownloadJSON(url + "&loc=" + candidate.join("%2C") + "&loc=3.22000%2C51.21577&profile=car.short") | ||||||
|  |                 const depPoint = result.features.filter(f => f.geometry.type === "LineString")[0].geometry.coordinates[0] | ||||||
|  |                 return <[number,number]>[depPoint[0], depPoint[1]] // Drop elevation
 | ||||||
|  |             }catch(e){ | ||||||
|  |                 console.error("No result or could not calculate a route") | ||||||
|  |             } | ||||||
|  |         }))) | ||||||
|  |          | ||||||
|  |         const centers = boundaries.map(b => GeoOperations.centerpointCoordinates(b)) | ||||||
|  |         const center = GeoOperations.centerpointCoordinates({ | ||||||
|  |             type:"Feature", | ||||||
|  |             geometry:{ | ||||||
|  |                 type:"LineString", | ||||||
|  |                 coordinates: centers | ||||||
|  |             } | ||||||
|  |         }) | ||||||
|  |          | ||||||
|  |         depPoints.sort((c0, c1) => GeoOperations.distanceBetween(c0, center) - GeoOperations.distanceBetween(c1, center)) | ||||||
|  |         console.log("Sorted departure point candidates for ",postcode," are ", JSON.stringify(depPoints)) | ||||||
|  |         appendFileSync(output,[postcode, ...depPoints[0]].join(", ")+"\n","UTF-8") | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | let args = [...process.argv] | ||||||
|  | args.splice(0, 2) | ||||||
|  | main(args).then(_ => console.log("Done!")) | ||||||
							
								
								
									
										3
									
								
								scripts/postal_code_tools/genPostal.sh
									
										
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										3
									
								
								scripts/postal_code_tools/genPostal.sh
									
										
									
									
									
										
										
										Normal file → Executable file
									
								
							|  | @ -1,4 +1,5 @@ | ||||||
| #! /bin/bash | #! /bin/bash | ||||||
| 
 | 
 | ||||||
| npm run generate:layeroverview | # npm run generate:layeroverview | ||||||
|  | cd ../.. | ||||||
| ts-node scripts/generateCache.ts postal_codes 8 /home/pietervdvn/Downloads/postal_codes 49.69606181911566 2.373046875 51.754240074033525 6.459960937499999 --generate-point-overview '*' --force-zoom-level 1 | ts-node scripts/generateCache.ts postal_codes 8 /home/pietervdvn/Downloads/postal_codes 49.69606181911566 2.373046875 51.754240074033525 6.459960937499999 --generate-point-overview '*' --force-zoom-level 1 | ||||||
|  | @ -1,6 +1,3 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import Constants from "../Models/Constants"; | import Constants from "../Models/Constants"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| console.log("git tag -a", Constants.vNumber, `-m "Deployed on ${new Date()}"`) | console.log("git tag -a", Constants.vNumber, `-m "Deployed on ${new Date()}"`) | ||||||
|  | @ -1,12 +1,9 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import * as Assert from "assert"; | import * as Assert from "assert"; | ||||||
| import {equal} from "assert"; | import {equal} from "assert"; | ||||||
| import T from "./TestHelper"; | import T from "./TestHelper"; | ||||||
| import {GeoOperations} from "../Logic/GeoOperations"; | import {GeoOperations} from "../Logic/GeoOperations"; | ||||||
| import {BBox} from "../Logic/BBox"; | import {BBox} from "../Logic/BBox"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| export default class GeoOperationsSpec extends T { | export default class GeoOperationsSpec extends T { | ||||||
| 
 | 
 | ||||||
|     private static polygon = { |     private static polygon = { | ||||||
|  |  | ||||||
|  | @ -1,12 +1,9 @@ | ||||||
| import {Utils} from "../Utils"; |  | ||||||
| import {equal} from "assert"; | import {equal} from "assert"; | ||||||
| import T from "./TestHelper"; | import T from "./TestHelper"; | ||||||
| import {Translation} from "../UI/i18n/Translation"; | import {Translation} from "../UI/i18n/Translation"; | ||||||
| import * as bike_repair_station from "../assets/layers/bike_repair_station/bike_repair_station.json" | import * as bike_repair_station from "../assets/layers/bike_repair_station/bike_repair_station.json" | ||||||
| import LayerConfig from "../Models/ThemeConfig/LayerConfig"; | import LayerConfig from "../Models/ThemeConfig/LayerConfig"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| export default class ImageAttributionSpec extends T { | export default class ImageAttributionSpec extends T { | ||||||
|     constructor() { |     constructor() { | ||||||
|         super( |         super( | ||||||
|  |  | ||||||
|  | @ -10,9 +10,6 @@ import {And} from "../Logic/Tags/And"; | ||||||
| import {TagUtils} from "../Logic/Tags/TagUtils"; | import {TagUtils} from "../Logic/Tags/TagUtils"; | ||||||
| import TagRenderingConfig from "../Models/ThemeConfig/TagRenderingConfig"; | import TagRenderingConfig from "../Models/ThemeConfig/TagRenderingConfig"; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| export default class TagSpec extends T { | export default class TagSpec extends T { | ||||||
| 
 | 
 | ||||||
|     constructor() { |     constructor() { | ||||||
|  |  | ||||||
|  | @ -4,8 +4,6 @@ import * as assert from "assert"; | ||||||
| import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | ||||||
| import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"; | ||||||
| 
 | 
 | ||||||
| Utils.runningFromConsole = true; |  | ||||||
| 
 |  | ||||||
| export default class ThemeSpec extends T { | export default class ThemeSpec extends T { | ||||||
|     constructor() { |     constructor() { | ||||||
|         super("theme", |         super("theme", | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue