|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:test/test.dart'; |
| 5 | +import 'package:turf/helpers.dart'; |
| 6 | +import 'package:turf/src/point_to_line_distance.dart'; |
| 7 | + |
| 8 | +final distances = { |
| 9 | + "city-line1.geojson": 1.0299686758, |
| 10 | + "city-line2.geojson": 3.6186172981, |
| 11 | + "city-segment-inside1.geojson": 1.1489389115, |
| 12 | + "city-segment-inside2.geojson": 1.0280898152, |
| 13 | + "city-segment-inside3.geojson": 3.5335695907, |
| 14 | + "city-segment-obtuse1.geojson": 2.8573246363, |
| 15 | + "city-segment-obtuse2.geojson": 3.3538913334, |
| 16 | + "city-segment-projected1.geojson": 3.5886611693, |
| 17 | + "city-segment-projected2.geojson": 4.163469898, |
| 18 | + "issue-1156.geojson": 189.6618028794, |
| 19 | + "line-fiji.geojson": 27.1266612008, |
| 20 | + "line-resolute-bay.geojson": 425.0745081528, |
| 21 | + "line1.geojson": 23.4224834672, |
| 22 | + "line2.geojson": 188.015686924, |
| 23 | + "segment-fiji.geojson": 27.6668301762, |
| 24 | + "segment1.geojson": 69.0934195756, |
| 25 | + "segment1a.geojson": 69.0934195756, |
| 26 | + "segment2.geojson": 69.0934195756, |
| 27 | + "segment3.geojson": 69.0828960461, |
| 28 | + "segment4.geojson": 332.8803863574 |
| 29 | +}; |
| 30 | + |
| 31 | +void main() { |
| 32 | + group('pointToLineDistance', () { |
| 33 | + group('in == out', () { |
| 34 | + final inDir = Directory('./test/examples/point_to_line_distance/in'); |
| 35 | + |
| 36 | + for (final file in inDir.listSync(recursive: true)) { |
| 37 | + if (file is File && file.path.endsWith('.geojson')) { |
| 38 | + testFile(file); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + group('unit tests', () { |
| 44 | + testPlanarGeodesic(); |
| 45 | + }); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +void testFile(File file) { |
| 50 | + test(file.path, () { |
| 51 | + final inSource = file.readAsStringSync(); |
| 52 | + final collection = FeatureCollection.fromJson(jsonDecode(inSource)); |
| 53 | + |
| 54 | + final rawPoint = collection.features[0]; |
| 55 | + final rawLine = collection.features[1]; |
| 56 | + |
| 57 | + final point = Feature<Point>.fromJson(rawPoint.toJson()); |
| 58 | + final line = Feature<LineString>.fromJson(rawLine.toJson()); |
| 59 | + |
| 60 | + final properties = rawPoint.properties ?? {}; |
| 61 | + final unitRaw = properties["units"] as String?; |
| 62 | + |
| 63 | + var unit = Unit.kilometers; |
| 64 | + if (unitRaw == 'meters') { |
| 65 | + unit = Unit.meters; |
| 66 | + } else if (unitRaw == 'miles') { |
| 67 | + unit = Unit.miles; |
| 68 | + } else { |
| 69 | + expect(unitRaw, null, reason: '"units" was given but not handled.'); |
| 70 | + } |
| 71 | + |
| 72 | + final distance = |
| 73 | + pointToLineDistance(point.geometry!, line.geometry!, unit: unit); |
| 74 | + |
| 75 | + final name = file.path.substring(file.path.lastIndexOf('/') + 1); |
| 76 | + |
| 77 | + expect(distance, closeTo(distances[name]!, 0.01)); |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +void testPlanarGeodesic() { |
| 82 | + test('Check planar and geodesic results are different', () { |
| 83 | + final pt = Point(coordinates: Position(0, 0)); |
| 84 | + final line = LineString(coordinates: [ |
| 85 | + Position(10, 10), |
| 86 | + Position(-1, 1), |
| 87 | + ]); |
| 88 | + |
| 89 | + final geoOut = |
| 90 | + pointToLineDistance(pt, line, method: DistanceGeometry.geodesic); |
| 91 | + final planarOut = |
| 92 | + pointToLineDistance(pt, line, method: DistanceGeometry.planar); |
| 93 | + |
| 94 | + expect(geoOut, isNot(equals(planarOut))); |
| 95 | + }); |
| 96 | +} |
0 commit comments