| 
									
										
										
										
											2021-04-18 14:24:30 +02:00
										 |  |  | import * as turf from '@turf/turf' | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  | import {BBox} from "./BBox"; | 
					
						
							| 
									
										
										
										
											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-07-23 17:18:30 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |      * Converts a GeoJson feature to a point GeoJson feature | 
					
						
							| 
									
										
										
										
											2021-07-23 17:18:30 +02:00
										 |  |  |      * @param feature | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |         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-06-21 03:12:18 +02:00
										 |  |  |                 if (feature.id !== undefined && feature.id === otherFeature.id) { | 
					
						
							| 
									
										
										
										
											2021-06-20 03:07:58 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-06-20 03:07:58 +02:00
										 |  |  |                 if (GeoOperations.inside(coor, otherFeature)) { | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |                     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) { | 
					
						
							| 
									
										
										
										
											2021-06-20 03:07:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  |                 if (feature.id !== undefined && feature.id === otherFeature.id) { | 
					
						
							| 
									
										
										
										
											2021-06-20 03:07:58 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  |                 const intersection = this.calculateInstersection(feature, otherFeature, featureBBox) | 
					
						
							|  |  |  |                 if (intersection === null) { | 
					
						
							|  |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  |                 result.push({feat: otherFeature, overlap: intersection}) | 
					
						
							| 
									
										
										
										
											2021-05-14 02:25:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (feature.id === otherFeature.id) { | 
					
						
							| 
									
										
										
										
											2021-06-20 03:07:58 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (otherFeature.geometry.type === "Point") { | 
					
						
							| 
									
										
										
										
											2021-06-20 03:07:58 +02:00
										 |  |  |                     if (this.inside(otherFeature, feature)) { | 
					
						
							|  |  |  |                         result.push({feat: otherFeature, overlap: undefined}) | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 23:33:48 +02:00
										 |  |  |                 // Calculate the surface area of the intersection
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  |                 const intersection = this.calculateInstersection(feature, otherFeature, featureBBox) | 
					
						
							|  |  |  |                 if (intersection === null) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  |                 result.push({feat: otherFeature, overlap: intersection}) | 
					
						
							| 
									
										
										
										
											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-06-21 03:12:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (pointCoordinate.geometry !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-06-20 03:07:58 +02:00
										 |  |  |             pointCoordinate = pointCoordinate.geometry.coordinates | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 13:50:35 +02:00
										 |  |  |         for (let i = 0; i < feature.geometry.coordinates.length; i++) { | 
					
						
							|  |  |  |             let poly = feature.geometry.coordinates[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             let inside = false; | 
					
						
							|  |  |  |             for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) { | 
					
						
							|  |  |  |                 const coori = poly[i]; | 
					
						
							|  |  |  |                 const coorj = poly[j]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const xi = coori[0]; | 
					
						
							|  |  |  |                 const yi = coori[1]; | 
					
						
							|  |  |  |                 const xj = coorj[0]; | 
					
						
							|  |  |  |                 const yj = coorj[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const intersect = ((yi > y) != (yj > y)) | 
					
						
							|  |  |  |                     && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); | 
					
						
							|  |  |  |                 if (intersect) { | 
					
						
							|  |  |  |                     inside = !inside; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (inside) { | 
					
						
							|  |  |  |                 return true; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 13:50:35 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-18 14:24:30 +02:00
										 |  |  |     static lengthInMeters(feature: any) { | 
					
						
							|  |  |  |         return turf.length(feature) * 1000 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static buffer(feature: any, bufferSizeInMeter: number) { | 
					
						
							|  |  |  |         return turf.buffer(feature, bufferSizeInMeter / 1000, { | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |             units: 'kilometers' | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static bbox(feature: any) { | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         const [lon, lat, lon0, lat0] = turf.bbox(feature) | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             "type": "Feature", | 
					
						
							|  |  |  |             "geometry": { | 
					
						
							|  |  |  |                 "type": "LineString", | 
					
						
							|  |  |  |                 "coordinates": [ | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         lon, | 
					
						
							|  |  |  |                         lat | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         lon0, | 
					
						
							|  |  |  |                         lat | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         lon0, | 
					
						
							|  |  |  |                         lat0 | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         lon, | 
					
						
							|  |  |  |                         lat0 | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         lon, | 
					
						
							|  |  |  |                         lat | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Generates the closest point on a way from a given point | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |      *  | 
					
						
							|  |  |  |      *  The properties object will contain three values: | 
					
						
							|  |  |  |      // - `index`: closest point was found on nth line part,
 | 
					
						
							|  |  |  |      // - `dist`: distance between pt and the closest point (in kilometer),
 | 
					
						
							| 
									
										
										
										
											2021-10-16 02:54:22 +02:00
										 |  |  |      // `location`: distance along the line between start (of the line) and the closest point.
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |      * @param way The road on which you want to find a point | 
					
						
							|  |  |  |      * @param point Point defined as [lon, lat] | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static nearestPoint(way, point: [number, number]) { | 
					
						
							|  |  |  |         return turf.nearestPointOnLine(way, point, {units: "kilometers"}); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static toCSV(features: any[]): string { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const headerValuesSeen = new Set<string>(); | 
					
						
							|  |  |  |         const headerValuesOrdered: string[] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function addH(key) { | 
					
						
							|  |  |  |             if (!headerValuesSeen.has(key)) { | 
					
						
							|  |  |  |                 headerValuesSeen.add(key) | 
					
						
							|  |  |  |                 headerValuesOrdered.push(key) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         addH("_lat") | 
					
						
							|  |  |  |         addH("_lon") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const lines: string[] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const feature of features) { | 
					
						
							|  |  |  |             const properties = feature.properties; | 
					
						
							|  |  |  |             for (const key in properties) { | 
					
						
							|  |  |  |                 if (!properties.hasOwnProperty(key)) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 addH(key) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         headerValuesOrdered.sort() | 
					
						
							|  |  |  |         for (const feature of features) { | 
					
						
							|  |  |  |             const properties = feature.properties; | 
					
						
							|  |  |  |             let line = "" | 
					
						
							|  |  |  |             for (const key of headerValuesOrdered) { | 
					
						
							|  |  |  |                 const value = properties[key] | 
					
						
							|  |  |  |                 if (value === undefined) { | 
					
						
							|  |  |  |                     line += "," | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     line += JSON.stringify(value) + "," | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             lines.push(line) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return headerValuesOrdered.map(v => JSON.stringify(v)).join(",") + "\n" + lines.join("\n") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:18 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the intersection between two features. | 
					
						
							|  |  |  |      * Returns the length if intersecting a linestring and a (multi)polygon (in meters), returns a surface area (in m²) if intersecting two (multi)polygons | 
					
						
							|  |  |  |      * Returns 0 if both are linestrings | 
					
						
							|  |  |  |      * Returns null if the features are not intersecting | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private static calculateInstersection(feature, otherFeature, featureBBox: BBox, otherFeatureBBox?: BBox): number { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             if (feature.geometry.type === "LineString") { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 otherFeatureBBox = otherFeatureBBox ?? BBox.get(otherFeature); | 
					
						
							|  |  |  |                 const overlaps = featureBBox.overlapsWith(otherFeatureBBox) | 
					
						
							|  |  |  |                 if (!overlaps) { | 
					
						
							|  |  |  |                     return null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Calculate the length of the intersection
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 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)) { | 
					
						
							|  |  |  |                         return this.lengthInMeters(feature) | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     return null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 let intersectionPointsArray = intersectionPoints.features.map(d => { | 
					
						
							|  |  |  |                     return d.geometry.coordinates | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (otherFeature.geometry.type === "LineString") { | 
					
						
							|  |  |  |                     if (intersectionPointsArray.length > 0) { | 
					
						
							|  |  |  |                         return 0 | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     return null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 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) { | 
					
						
							|  |  |  |                     return null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const intersectionSize = turf.length(intersection); // in km
 | 
					
						
							|  |  |  |                 return intersectionSize * 1000 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (feature.geometry.type === "Polygon" || feature.geometry.type === "MultiPolygon") { | 
					
						
							|  |  |  |                 const otherFeatureBBox = BBox.get(otherFeature); | 
					
						
							|  |  |  |                 const overlaps = featureBBox.overlapsWith(otherFeatureBBox) | 
					
						
							|  |  |  |                 if (!overlaps) { | 
					
						
							|  |  |  |                     return null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (otherFeature.geometry.type === "LineString") { | 
					
						
							|  |  |  |                     return this.calculateInstersection(otherFeature, feature, otherFeatureBBox, featureBBox) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const intersection = turf.intersect(feature, otherFeature); | 
					
						
							|  |  |  |                 if (intersection == null) { | 
					
						
							|  |  |  |                     return null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return turf.area(intersection); // in m²
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (exception) { | 
					
						
							|  |  |  |             console.warn("EXCEPTION CAUGHT WHILE INTERSECTING: ", exception); | 
					
						
							|  |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |