Skip to content

Commit a926fb6

Browse files
committed
Further refactor pointOnFeature to use library functions for area and length calculations
1 parent 1f9fcef commit a926fb6

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

lib/src/point_on_feature.dart

+8-32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'package:geotypes/geotypes.dart';
33
import 'package:turf/centroid.dart';
44
import 'package:turf/boolean.dart';
55
import 'package:turf/midpoint.dart';
6+
import 'package:turf/area.dart';
7+
import 'package:turf/length.dart';
68

79
/// Returns a [Feature<Point>] that represents a point guaranteed to be on the feature.
810
///
@@ -111,40 +113,14 @@ double _calculateFeatureSize(Feature feature) {
111113
return 0; // Points have zero area
112114
} else if (geometry is LineString) {
113115
// For LineString, use the length as a proxy for size
114-
double totalLength = 0;
115-
final coords = geometry.coordinates;
116-
for (int i = 0; i < coords.length - 1; i++) {
117-
final start = coords[i];
118-
final end = coords[i + 1];
119-
final dx = (end[0] ?? 0.0) - (start[0] ?? 0.0);
120-
final dy = (end[1] ?? 0.0) - (start[1] ?? 0.0);
121-
totalLength += math.sqrt(dx * dx + dy * dy); // Simple Euclidean distance
122-
}
123-
return totalLength;
116+
// Use the built-in length function that accounts for geodesic distance
117+
return length(Feature<LineString>(geometry: geometry)).toDouble();
124118
} else if (geometry is Polygon) {
125-
// For Polygon, use area of the outer ring as a simple approximation
126-
double area = 0;
127-
final outerRing = geometry.coordinates.first;
128-
for (int i = 0; i < outerRing.length - 1; i++) {
129-
area += ((outerRing[i][0] ?? 0.0) * (outerRing[i + 1][1] ?? 0.0)) -
130-
((outerRing[i + 1][0] ?? 0.0) * (outerRing[i][1] ?? 0.0));
131-
}
132-
return area.abs() / 2;
119+
// For Polygon, use the built-in area function that uses proper spherical calculations
120+
return (area(Feature<Polygon>(geometry: geometry)) ?? 0.0).toDouble();
133121
} else if (geometry is MultiPolygon) {
134-
// For MultiPolygon, sum the areas of all polygons
135-
double totalArea = 0;
136-
for (final polyCoords in geometry.coordinates) {
137-
if (polyCoords.isNotEmpty) {
138-
final outerRing = polyCoords.first;
139-
double area = 0;
140-
for (int i = 0; i < outerRing.length - 1; i++) {
141-
area += ((outerRing[i][0] ?? 0.0) * (outerRing[i + 1][1] ?? 0.0)) -
142-
((outerRing[i + 1][0] ?? 0.0) * (outerRing[i][1] ?? 0.0));
143-
}
144-
totalArea += area.abs() / 2;
145-
}
146-
}
147-
return totalArea;
122+
// For MultiPolygon, use the built-in area function that handles all polygons
123+
return (area(Feature<MultiPolygon>(geometry: geometry)) ?? 0.0).toDouble();
148124
}
149125

150126
return 0; // Default for unsupported geometry types

0 commit comments

Comments
 (0)