| 
									
										
										
										
											2021-04-18 14:24:30 +02:00
										 |  |  | import * as turf from '@turf/turf' | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  | export class GeoOperations { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |     static surfaceAreaInSqMeters(feature: any) { | 
					
						
							|  |  |  |         return turf.area(feature); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |     static centerpoint(feature: any) { | 
					
						
							|  |  |  |         const newFeature = turf.center(feature); | 
					
						
							| 
									
										
										
										
											2020-07-22 00:50:30 +02:00
										 |  |  |         newFeature.properties = feature.properties; | 
					
						
							|  |  |  |         newFeature.id = feature.id; | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 00:50:30 +02:00
										 |  |  |         return newFeature; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static centerpointCoordinates(feature: any): [number, number] { | 
					
						
							| 
									
										
										
										
											2021-04-18 18:23:59 +02:00
										 |  |  |         // @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2021-03-29 02:05:11 +02:00
										 |  |  |         return turf.center(feature).geometry.coordinates; | 
					
						
							| 
									
										
										
										
											2021-02-14 20:36:20 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-22 00:50:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-24 01:25:57 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns the distance between the two points in kilometers | 
					
						
							|  |  |  |      * @param lonlat0 | 
					
						
							|  |  |  |      * @param lonlat1 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |     static distanceBetween(lonlat0: [number, number], lonlat1: [number, number]) { | 
					
						
							| 
									
										
										
										
											2021-03-24 01:25:57 +01:00
										 |  |  |         return turf.distance(lonlat0, lonlat1) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the overlap of 'feature' with every other specified feature. | 
					
						
							|  |  |  |      * The features with which 'feature' overlaps, are returned together with their overlap area in m² | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * If 'feature' is a LineString, the features in which this feature is (partly) embedded is returned, the overlap length in meter is given | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |      * If 'feature' is a point, it will return every feature the point is embedded in. Overlap will be undefined | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |     static calculateOverlap(feature: any, otherFeatures: any[]): { feat: any, overlap: number }[] { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |         const featureBBox = BBox.get(feature); | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |         const result: { feat: any, overlap: number }[] = []; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         if (feature.geometry.type === "Point") { | 
					
						
							|  |  |  |             const coor = feature.geometry.coordinates; | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |             for (const otherFeature of otherFeatures) { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |                 if (otherFeature.geometry === undefined) { | 
					
						
							|  |  |  |                     console.error("No geometry for feature ", feature) | 
					
						
							|  |  |  |                     throw "List of other features contains a feature without geometry an undefined" | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |                 let otherFeatureBBox = BBox.get(otherFeature); | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |                 if (!featureBBox.overlapsWith(otherFeatureBBox)) { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |                 if (this.inside(coor, otherFeature)) { | 
					
						
							|  |  |  |                     result.push({feat: otherFeature, overlap: undefined}) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |             return result; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |         if (feature.geometry.type === "LineString") { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const otherFeature of otherFeatures) { | 
					
						
							|  |  |  |                 const otherFeatureBBox = BBox.get(otherFeature); | 
					
						
							|  |  |  |                 const overlaps = featureBBox.overlapsWith(otherFeatureBBox) | 
					
						
							|  |  |  |                 if (!overlaps) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Calculate the length of the intersection
 | 
					
						
							|  |  |  |                 try { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     let intersectionPoints = turf.lineIntersect(feature, otherFeature); | 
					
						
							|  |  |  |                     if (intersectionPoints.features.length == 0) { | 
					
						
							|  |  |  |                         // No intersections.
 | 
					
						
							|  |  |  |                         // If one point is inside of the polygon, all points are
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         const coors = feature.geometry.coordinates; | 
					
						
							|  |  |  |                         const startCoor = coors[0] | 
					
						
							|  |  |  |                         if (this.inside(startCoor, otherFeature)) { | 
					
						
							|  |  |  |                             result.push({feat: otherFeature, overlap: this.lengthInMeters(feature)}) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     let intersectionPointsArray = intersectionPoints.features.map(d => { | 
					
						
							|  |  |  |                         return d.geometry.coordinates | 
					
						
							|  |  |  |                     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (intersectionPointsArray.length == 1) { | 
					
						
							|  |  |  |                         // We need to add the start- or endpoint of the current feature, depending on which one is embedded
 | 
					
						
							|  |  |  |                         const coors = feature.geometry.coordinates; | 
					
						
							|  |  |  |                         const startCoor = coors[0] | 
					
						
							|  |  |  |                         if (this.inside(startCoor, otherFeature)) { | 
					
						
							|  |  |  |                             // The startpoint is embedded
 | 
					
						
							|  |  |  |                             intersectionPointsArray.push(startCoor) | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             intersectionPointsArray.push(coors[coors.length - 1]) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     let intersection = turf.lineSlice(turf.point(intersectionPointsArray[0]), turf.point(intersectionPointsArray[1]), feature); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (intersection == null) { | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     const intersectionSize = turf.length(intersection); // in km
 | 
					
						
							|  |  |  |                     result.push({feat: otherFeature, overlap: intersectionSize * 1000}) | 
					
						
							|  |  |  |                 } catch (exception) { | 
					
						
							|  |  |  |                     console.warn("EXCEPTION CAUGHT WHILE INTERSECTING: ", exception); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |         if (feature.geometry.type === "Polygon" || feature.geometry.type === "MultiPolygon") { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |             for (const otherFeature of otherFeatures) { | 
					
						
							|  |  |  |                 const otherFeatureBBox = BBox.get(otherFeature); | 
					
						
							|  |  |  |                 const overlaps = featureBBox.overlapsWith(otherFeatureBBox) | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |                 if (!overlaps) { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |                 // Calculate the surface area of the intersection
 | 
					
						
							|  |  |  |                 try { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |                     const intersection = turf.intersect(feature, otherFeature); | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |                     if (intersection == null) { | 
					
						
							|  |  |  |                         continue; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |                     const intersectionSize = turf.area(intersection); // in m²
 | 
					
						
							|  |  |  |                     result.push({feat: otherFeature, overlap: intersectionSize}) | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |                 } catch (exception) { | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |                     console.warn("EXCEPTION CAUGHT WHILE INTERSECTING: ", exception); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |             return result; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |         console.error("Could not correctly calculate the overlap of ", feature, ": unsupported type") | 
					
						
							|  |  |  |         return result; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 21:58:29 +02:00
										 |  |  |     public static inside(pointCoordinate, feature): boolean { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         // ray-casting algorithm based on
 | 
					
						
							|  |  |  |         // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (feature.geometry.type === "Point") { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |         if (feature.geometry.type === "MultiPolygon") { | 
					
						
							|  |  |  |             const coordinates = feature.geometry.coordinates[0]; | 
					
						
							|  |  |  |             const outerPolygon = coordinates[0]; | 
					
						
							|  |  |  |             const inside = GeoOperations.inside(pointCoordinate, { | 
					
						
							|  |  |  |                 geometry: { | 
					
						
							|  |  |  |                     type: 'Polygon', | 
					
						
							|  |  |  |                     coordinates: [outerPolygon] | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             if (!inside) { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             for (let i = 1; i < coordinates.length; i++) { | 
					
						
							|  |  |  |                 const inHole = GeoOperations.inside(pointCoordinate, { | 
					
						
							|  |  |  |                     geometry: { | 
					
						
							|  |  |  |                         type: 'Polygon', | 
					
						
							|  |  |  |                         coordinates: [coordinates[i]] | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |                 if (inHole) { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const x: number = pointCoordinate[0]; | 
					
						
							|  |  |  |         const y: number = pointCoordinate[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let poly = feature.geometry.coordinates[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var inside = false; | 
					
						
							| 
									
										
										
										
											2020-08-30 01:13:18 +02:00
										 |  |  |         for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             const coori = poly[i]; | 
					
						
							|  |  |  |             const coorj = poly[j]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const xi = coori[0]; | 
					
						
							|  |  |  |             const yi = coori[1]; | 
					
						
							|  |  |  |             const xj = coorj[0]; | 
					
						
							|  |  |  |             const yj = coorj[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-30 01:13:18 +02:00
										 |  |  |             const intersect = ((yi > y) != (yj > y)) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                 && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); | 
					
						
							|  |  |  |             if (intersect) { | 
					
						
							|  |  |  |                 inside = !inside; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return inside; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-18 14:24:30 +02:00
										 |  |  |     static lengthInMeters(feature: any) { | 
					
						
							|  |  |  |         return turf.length(feature) * 1000 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  | class BBox { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     readonly maxLat: number; | 
					
						
							|  |  |  |     readonly maxLon: number; | 
					
						
							|  |  |  |     readonly minLat: number; | 
					
						
							|  |  |  |     readonly minLon: number; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(coordinates) { | 
					
						
							|  |  |  |         this.maxLat = Number.MIN_VALUE; | 
					
						
							|  |  |  |         this.maxLon = Number.MIN_VALUE; | 
					
						
							|  |  |  |         this.minLat = Number.MAX_VALUE; | 
					
						
							|  |  |  |         this.minLon = Number.MAX_VALUE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const coordinate of coordinates) { | 
					
						
							|  |  |  |             this.maxLon = Math.max(this.maxLon, coordinate[0]); | 
					
						
							|  |  |  |             this.maxLat = Math.max(this.maxLat, coordinate[1]); | 
					
						
							|  |  |  |             this.minLon = Math.min(this.minLon, coordinate[0]); | 
					
						
							|  |  |  |             this.minLat = Math.min(this.minLat, coordinate[1]); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |         this.check(); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static get(feature) { | 
					
						
							| 
									
										
										
										
											2021-01-15 01:57:46 +01:00
										 |  |  |         if (feature.bbox?.overlapsWith === undefined) { | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (feature.geometry.type === "MultiPolygon") { | 
					
						
							|  |  |  |                 let coordinates = []; | 
					
						
							|  |  |  |                 for (const coorlist of feature.geometry.coordinates) { | 
					
						
							|  |  |  |                     coordinates = coordinates.concat(coorlist[0]); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 feature.bbox = new BBox(coordinates); | 
					
						
							|  |  |  |             } else if (feature.geometry.type === "Polygon") { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                 feature.bbox = new BBox(feature.geometry.coordinates[0]); | 
					
						
							|  |  |  |             } else if (feature.geometry.type === "LineString") { | 
					
						
							|  |  |  |                 feature.bbox = new BBox(feature.geometry.coordinates); | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |             } else if (feature.geometry.type === "Point") { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                 // Point
 | 
					
						
							|  |  |  |                 feature.bbox = new BBox([feature.geometry.coordinates]); | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 throw "Cannot calculate bbox, unknown type " + feature.geometry.type; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return feature.bbox; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |     public overlapsWith(other: BBox) { | 
					
						
							|  |  |  |         this.check(); | 
					
						
							|  |  |  |         other.check(); | 
					
						
							|  |  |  |         if (this.maxLon < other.minLon) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (this.maxLat < other.minLat) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (this.minLon > other.maxLon) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return this.minLat <= other.maxLat; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private check() { | 
					
						
							|  |  |  |         if (isNaN(this.maxLon) || isNaN(this.maxLat) || isNaN(this.minLon) || isNaN(this.minLat)) { | 
					
						
							|  |  |  |             console.log(this); | 
					
						
							|  |  |  |             throw  "BBOX has NAN"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | } |