| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { TagsFilter } from "../../Logic/Tags/TagsFilter" | 
					
						
							|  |  |  | import { RegexTag } from "../../Logic/Tags/RegexTag" | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class SourceConfig { | 
					
						
							| 
									
										
										
										
											2022-09-14 14:43:14 +02:00
										 |  |  |     public osmTags?: TagsFilter | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public readonly overpassScript?: string | 
					
						
							|  |  |  |     public geojsonSource?: string | 
					
						
							|  |  |  |     public geojsonZoomLevel?: number | 
					
						
							|  |  |  |     public isOsmCacheLayer: boolean | 
					
						
							|  |  |  |     public readonly mercatorCrs: boolean | 
					
						
							|  |  |  |     public readonly idKey: string | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         params: { | 
					
						
							|  |  |  |             mercatorCrs?: boolean | 
					
						
							|  |  |  |             osmTags?: TagsFilter | 
					
						
							|  |  |  |             overpassScript?: string | 
					
						
							|  |  |  |             geojsonSource?: string | 
					
						
							|  |  |  |             isOsmCache?: boolean | 
					
						
							|  |  |  |             geojsonSourceLevel?: number | 
					
						
							|  |  |  |             idKey?: string | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         isSpecialLayer: boolean, | 
					
						
							|  |  |  |         context?: string | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         let defined = 0 | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |         if (params.osmTags) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             defined++ | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (params.overpassScript) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             defined++ | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (params.geojsonSource) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             defined++ | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (defined == 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             throw `Source: nothing correct defined in the source (in ${context}) (the params are ${JSON.stringify( | 
					
						
							|  |  |  |                 params | 
					
						
							|  |  |  |             )})`
 | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |         if (params.isOsmCache && params.geojsonSource == undefined) { | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |             console.error(params) | 
					
						
							|  |  |  |             throw `Source said it is a OSM-cached layer, but didn't define the actual source of the cache (in context ${context})` | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (params.geojsonSource !== undefined && params.geojsonSourceLevel !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if ( | 
					
						
							|  |  |  |                 !["x", "y", "x_min", "x_max", "y_min", "Y_max"].some( | 
					
						
							|  |  |  |                     (toSearch) => params.geojsonSource.indexOf(toSearch) > 0 | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ) { | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  |                 throw `Source defines a geojson-zoomLevel, but does not specify {x} nor {y} (or equivalent), this is probably a bug (in context ${context})` | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         if (params.osmTags !== undefined && !isSpecialLayer) { | 
					
						
							| 
									
										
										
										
											2022-04-14 00:53:38 +02:00
										 |  |  |             const optimized = params.osmTags.optimize() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (optimized === false) { | 
					
						
							|  |  |  |                 throw ( | 
					
						
							|  |  |  |                     "Error at " + | 
					
						
							|  |  |  |                     context + | 
					
						
							|  |  |  |                     ": the specified tags are conflicting with each other: they will never match anything at all" | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2022-04-14 00:53:38 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (optimized === true) { | 
					
						
							|  |  |  |                 throw ( | 
					
						
							|  |  |  |                     "Error at " + | 
					
						
							|  |  |  |                     context + | 
					
						
							|  |  |  |                     ": the specified tags are very wide: they will always match everything" | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2022-04-14 00:53:38 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.osmTags = params.osmTags ?? new RegexTag("id", /.*/) | 
					
						
							|  |  |  |         this.overpassScript = params.overpassScript | 
					
						
							|  |  |  |         this.geojsonSource = params.geojsonSource | 
					
						
							|  |  |  |         this.geojsonZoomLevel = params.geojsonSourceLevel | 
					
						
							|  |  |  |         this.isOsmCacheLayer = params.isOsmCache ?? false | 
					
						
							|  |  |  |         this.mercatorCrs = params.mercatorCrs ?? false | 
					
						
							|  |  |  |         this.idKey = params.idKey | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |