|
| 1 | +import 'package:turf/turf.dart'; |
| 2 | +import 'package:turf/bbox.dart' as b; |
| 3 | +import 'package:turf/nearest_point.dart' as np; |
| 4 | + |
| 5 | +/// Finds the tangents of a [Polygon] or [MultiPolygon] from a [Point]. |
| 6 | +/// |
| 7 | +/// This function calculates the two tangent points on the boundary of the given |
| 8 | +/// polygon (or multipolygon) starting from the external [point]. If the point |
| 9 | +/// lies within the polygon's bounding box, the nearest vertex is used as a |
| 10 | +/// reference to determine the tangents. |
| 11 | +/// |
| 12 | +/// Returns a [FeatureCollection] containing two [Point] features: |
| 13 | +/// - The right tangent point. |
| 14 | +/// - The left tangent point. |
| 15 | +/// |
| 16 | +/// Example: |
| 17 | +/// |
| 18 | +/// ```dart |
| 19 | +/// // Create a polygon |
| 20 | +/// final polygon = Feature<Polygon>( |
| 21 | +/// geometry: Polygon(coordinates: [ |
| 22 | +/// [ |
| 23 | +/// Position.of([11, 0]), |
| 24 | +/// Position.of([22, 4]), |
| 25 | +/// Position.of([31, 0]), |
| 26 | +/// Position.of([31, 11]), |
| 27 | +/// Position.of([21, 15]), |
| 28 | +/// Position.of([11, 11]), |
| 29 | +/// Position.of([11, 0]) |
| 30 | +/// ] |
| 31 | +/// ]), |
| 32 | +/// properties: {}, |
| 33 | +/// ); |
| 34 | +/// |
| 35 | +/// // Create a point |
| 36 | +/// final point = Point(coordinates: Position.of([61, 5])); |
| 37 | +/// |
| 38 | +/// // Calculate tangents |
| 39 | +/// final tangents = polygonTangents(point, polygon); |
| 40 | +/// |
| 41 | +/// // The FeatureCollection 'tangents' now contains the two tangent points. |
| 42 | +/// ``` |
| 43 | +
|
| 44 | +FeatureCollection<Point> polygonTangents(Point point, GeoJSONObject inputPolys) { |
| 45 | + |
| 46 | + if (inputPolys is! Feature<Polygon> && inputPolys is! Feature<MultiPolygon>) { |
| 47 | + throw Exception("Input must be a Polygon or MultiPolygon feature."); |
| 48 | + } |
| 49 | + |
| 50 | + final pointCoords = getCoord(point); |
| 51 | + final polyCoords = getCoords(inputPolys); |
| 52 | + |
| 53 | + Position rtan = Position.of([0, 0]); |
| 54 | + Position ltan = Position.of([0, 0]); |
| 55 | + double eprev = 0; |
| 56 | + final bbox = b.bbox(inputPolys); |
| 57 | + int nearestPtIndex = 0; |
| 58 | + Feature<Point>? nearest; |
| 59 | + |
| 60 | + // If the external point lies within the polygon's bounding box, find the nearest vertex. |
| 61 | + if (pointCoords[0]! > bbox[0]! && |
| 62 | + pointCoords[0]! < bbox[2]! && |
| 63 | + pointCoords[1]! > bbox[1]! && |
| 64 | + pointCoords[1]! < bbox[3]!) { |
| 65 | + final nearestFeature = |
| 66 | + np.nearestPoint(Feature<Point>(geometry: point), explode(inputPolys)); |
| 67 | + nearest = nearestFeature; |
| 68 | + nearestPtIndex = nearest.properties!['featureIndex'] as int; |
| 69 | + } |
| 70 | + |
| 71 | + geomEach(inputPolys, (GeometryType? geom, featureIndex, featureProperties, |
| 72 | + featureBBox, featureId) { |
| 73 | + switch (geom?.type) { |
| 74 | + case GeoJSONObjectType.polygon: |
| 75 | + rtan = polyCoords[0][nearestPtIndex]; |
| 76 | + ltan = polyCoords[0][0]; |
| 77 | + if (nearest != null) { |
| 78 | + if (nearest.geometry!.coordinates[1]! < pointCoords[1]!) { |
| 79 | + ltan = polyCoords[0][nearestPtIndex]; |
| 80 | + } |
| 81 | + } |
| 82 | + eprev = isLeft( |
| 83 | + polyCoords[0][0], |
| 84 | + polyCoords[0][polyCoords[0].length - 1], |
| 85 | + pointCoords, |
| 86 | + ).toDouble(); |
| 87 | + final processed = processPolygon( |
| 88 | + polyCoords[0], |
| 89 | + pointCoords, |
| 90 | + eprev, |
| 91 | + rtan, |
| 92 | + ltan, |
| 93 | + ); |
| 94 | + rtan = processed[0]; |
| 95 | + ltan = processed[1]; |
| 96 | + break; |
| 97 | + case GeoJSONObjectType.multiPolygon: |
| 98 | + var closestFeature = 0; |
| 99 | + var closestVertex = 0; |
| 100 | + var verticesCounted = 0; |
| 101 | + for (int i = 0; i < polyCoords[0].length; i++) { |
| 102 | + closestFeature = i; |
| 103 | + var verticeFound = false; |
| 104 | + for (var j = 0; j < polyCoords[0][i].length; j++) { |
| 105 | + closestVertex = j; |
| 106 | + if (verticesCounted == nearestPtIndex) { |
| 107 | + verticeFound = true; |
| 108 | + break; |
| 109 | + } |
| 110 | + verticesCounted++; |
| 111 | + } |
| 112 | + if (verticeFound) break; |
| 113 | + } |
| 114 | + rtan = polyCoords[0][closestFeature][closestVertex]; |
| 115 | + ltan = polyCoords[0][closestFeature][closestVertex]; |
| 116 | + eprev = isLeft( |
| 117 | + polyCoords[0][0][0], |
| 118 | + polyCoords[0][0][polyCoords[0][0].length - 1], |
| 119 | + pointCoords, |
| 120 | + ).toDouble(); |
| 121 | + polyCoords[0].forEach((polygon) { |
| 122 | + final processed = processPolygon( |
| 123 | + polygon, |
| 124 | + pointCoords, |
| 125 | + eprev, |
| 126 | + rtan, |
| 127 | + ltan, |
| 128 | + ); |
| 129 | + rtan = processed[0]; |
| 130 | + ltan = processed[1]; |
| 131 | + }); |
| 132 | + break; |
| 133 | + default: |
| 134 | + throw Exception("Unsupported geometry type: ${geom?.type}"); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + return FeatureCollection(features: [ |
| 139 | + Feature<Point>(geometry: Point(coordinates: rtan)), |
| 140 | + Feature<Point>(geometry: Point(coordinates: ltan)), |
| 141 | + ]); |
| 142 | +} |
| 143 | + |
| 144 | +/// Processes a polygon to determine the right and left tangents. |
| 145 | +List<Position> processPolygon(List<Position> polygonCoords, |
| 146 | + Position pointCoords, double eprev, Position rtan, Position ltan) { |
| 147 | + for (int i = 0; i < polygonCoords.length; i++) { |
| 148 | + final currentCoords = polygonCoords[i]; |
| 149 | + var nextCoords = polygonCoords[(i + 1) % polygonCoords.length]; |
| 150 | + final enext = isLeft(currentCoords, nextCoords, pointCoords); |
| 151 | + if (eprev <= 0 && enext > 0) { |
| 152 | + if (!isBelow(pointCoords, currentCoords, rtan)) { |
| 153 | + rtan = currentCoords; |
| 154 | + } |
| 155 | + } else if (eprev > 0 && enext <= 0) { |
| 156 | + if (!isAbove(pointCoords, currentCoords, ltan)) { |
| 157 | + ltan = currentCoords; |
| 158 | + } |
| 159 | + } else if (eprev > 0 && enext <= 0) { |
| 160 | + if (!isAbove(pointCoords, currentCoords, ltan)) { |
| 161 | + ltan = currentCoords; |
| 162 | + } |
| 163 | + } |
| 164 | + eprev = enext.toDouble(); |
| 165 | + } |
| 166 | + return [rtan, ltan]; |
| 167 | +} |
| 168 | + |
| 169 | +/// Returns a positive value if [p3] is to the left of the line from [p1] to [p2], |
| 170 | +/// negative if to the right, and 0 if collinear. |
| 171 | +num isLeft(Position p1, Position p2, Position p3) { |
| 172 | + return ((p2[0]! - p1[0]!) * (p3[1]! - p1[1]!) - |
| 173 | + (p3[0]! - p1[0]!) * (p2[1]! - p1[1]!)); |
| 174 | +} |
| 175 | + |
| 176 | +/// Returns true if [p3] is above the line from [p1] to [p2]. |
| 177 | +bool isAbove(Position p1, Position p2, Position p3) { |
| 178 | + return isLeft(p1, p2, p3) > 0; |
| 179 | +} |
| 180 | + |
| 181 | +/// Returns true if [p3] is below the line from [p1] to [p2]. |
| 182 | +bool isBelow(Position p1, Position p2, Position p3) { |
| 183 | + return isLeft(p1, p2, p3) < 0; |
| 184 | +} |
0 commit comments