| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import * as fs from "fs" | 
					
						
							|  |  |  | import { writeFileSync } from "fs" | 
					
						
							|  |  |  | import ScriptUtils from "../ScriptUtils" | 
					
						
							| 
									
										
										
										
											2021-12-09 18:01:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function handleFile(file: string, postalCode: number) { | 
					
						
							| 
									
										
										
										
											2023-01-15 23:28:02 +01:00
										 |  |  |     const geojson = JSON.parse(fs.readFileSync(file, { encoding: "utf8" })) | 
					
						
							| 
									
										
										
										
											2021-12-09 18:01:11 +01:00
										 |  |  |     geojson.properties = { | 
					
						
							|  |  |  |         type: "boundary", | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         boundary: "postal_code", | 
					
						
							|  |  |  |         postal_code: postalCode + "", | 
					
						
							| 
									
										
										
										
											2021-12-09 18:01:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return geojson | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getKnownPostalCodes(): number[] { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     return fs | 
					
						
							| 
									
										
										
										
											2023-01-15 23:28:02 +01:00
										 |  |  |         .readFileSync("./scripts/postal_code_tools/knownPostalCodes.csv", { encoding: "utf8" }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         .split("\n") | 
					
						
							|  |  |  |         .map((line) => Number(line.split(",")[1])) | 
					
						
							| 
									
										
										
										
											2021-12-09 18:01:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function main(args: string[]) { | 
					
						
							|  |  |  |     const dir = args[0] | 
					
						
							|  |  |  |     const knownPostals = new Set<number>(getKnownPostalCodes()) | 
					
						
							|  |  |  |     const files = ScriptUtils.readDirRecSync(dir, 1) | 
					
						
							|  |  |  |     const allFiles = [] | 
					
						
							|  |  |  |     const skipped = [] | 
					
						
							|  |  |  |     for (const file of files) { | 
					
						
							|  |  |  |         const nameParts = file.split("-") | 
					
						
							|  |  |  |         const postalCodeStr = nameParts[nameParts.length - 1] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const postalCode = Number( | 
					
						
							|  |  |  |             postalCodeStr.substr(0, postalCodeStr.length - ".geojson.convex.geojson".length) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-12-09 18:01:11 +01:00
										 |  |  |         if (isNaN(postalCode)) { | 
					
						
							|  |  |  |             console.error("Not a number: ", postalCodeStr) | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (knownPostals.has(postalCode)) { | 
					
						
							|  |  |  |             skipped.push(postalCode) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             ScriptUtils.erasableLog( | 
					
						
							|  |  |  |                 "Skipping boundary for ", | 
					
						
							|  |  |  |                 postalCode, | 
					
						
							|  |  |  |                 "as it is already known - skipped ", | 
					
						
							|  |  |  |                 skipped.length, | 
					
						
							|  |  |  |                 "already" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-12-09 18:01:11 +01:00
										 |  |  |             continue | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         allFiles.push(handleFile(file, postalCode)) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     writeFileSync( | 
					
						
							|  |  |  |         "all_postal_codes_filtered.geojson", | 
					
						
							|  |  |  |         JSON.stringify({ | 
					
						
							|  |  |  |             type: "FeatureCollection", | 
					
						
							|  |  |  |             features: allFiles, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-12-09 18:01:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let args = [...process.argv] | 
					
						
							|  |  |  | args.splice(0, 2) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | main(args) |