|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | +import 'package:turf/turf.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group('Square BBox Transformation', () { |
| 8 | + // Unit tests for specific scenarios |
| 9 | + test('Square a horizontal rectangle', () { |
| 10 | + // Input: Rectangle wider than tall |
| 11 | + final bbox = BBox.named(lng1: 0, lat1: 0, lng2: 10, lat2: 5); |
| 12 | + |
| 13 | + final squaredBBox = square(bbox); |
| 14 | + |
| 15 | + // Verify square dimensions |
| 16 | + expect(squaredBBox.lng2 - squaredBBox.lng1, |
| 17 | + squaredBBox.lat2 - squaredBBox.lat1, |
| 18 | + reason: 'BBox should be a perfect square'); |
| 19 | + |
| 20 | + // Verify center point remains the same |
| 21 | + expect((squaredBBox.lng1 + squaredBBox.lng2) / 2, |
| 22 | + (bbox.lng1 + bbox.lng2) / 2, |
| 23 | + reason: 'Center longitude should remain the same'); |
| 24 | + expect((squaredBBox.lat1 + squaredBBox.lat2) / 2, |
| 25 | + (bbox.lat1 + bbox.lat2) / 2, |
| 26 | + reason: 'Center latitude should remain the same'); |
| 27 | + }); |
| 28 | + |
| 29 | + test('Square a vertical rectangle', () { |
| 30 | + // Input: Rectangle taller than wide |
| 31 | + final bbox = BBox.named(lng1: 0, lat1: 0, lng2: 5, lat2: 10); |
| 32 | + |
| 33 | + final squaredBBox = square(bbox); |
| 34 | + |
| 35 | + // Verify square dimensions |
| 36 | + expect(squaredBBox.lng2 - squaredBBox.lng1, |
| 37 | + squaredBBox.lat2 - squaredBBox.lat1, |
| 38 | + reason: 'BBox should be a perfect square'); |
| 39 | + |
| 40 | + // Verify center point remains the same |
| 41 | + expect((squaredBBox.lng1 + squaredBBox.lng2) / 2, |
| 42 | + (bbox.lng1 + bbox.lng2) / 2, |
| 43 | + reason: 'Center longitude should remain the same'); |
| 44 | + expect((squaredBBox.lat1 + squaredBBox.lat2) / 2, |
| 45 | + (bbox.lat1 + bbox.lat2) / 2, |
| 46 | + reason: 'Center latitude should remain the same'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Square an already square BBox', () { |
| 50 | + // Input: Already square BBox |
| 51 | + final bbox = BBox.named(lng1: 0, lat1: 0, lng2: 10, lat2: 10); |
| 52 | + |
| 53 | + final squaredBBox = square(bbox); |
| 54 | + |
| 55 | + // Verify dimensions remain the same |
| 56 | + expect(squaredBBox.lng2 - squaredBBox.lng1, |
| 57 | + squaredBBox.lat2 - squaredBBox.lat1, |
| 58 | + reason: 'BBox should remain a square'); |
| 59 | + |
| 60 | + expect(squaredBBox.lng1, bbox.lng1); |
| 61 | + expect(squaredBBox.lat1, bbox.lat1); |
| 62 | + expect(squaredBBox.lng2, bbox.lng2); |
| 63 | + expect(squaredBBox.lat2, bbox.lat2); |
| 64 | + }); |
| 65 | + |
| 66 | + test('Square a BBox with negative coordinates', () { |
| 67 | + // Input: BBox with negative coordinates |
| 68 | + final bbox = BBox.named(lng1: -10, lat1: -5, lng2: 0, lat2: 5); |
| 69 | + |
| 70 | + final squaredBBox = square(bbox); |
| 71 | + |
| 72 | + // Verify square dimensions |
| 73 | + expect(squaredBBox.lng2 - squaredBBox.lng1, |
| 74 | + squaredBBox.lat2 - squaredBBox.lat1, |
| 75 | + reason: 'BBox should be a perfect square'); |
| 76 | + |
| 77 | + // Verify center point remains the same |
| 78 | + expect((squaredBBox.lng1 + squaredBBox.lng2) / 2, |
| 79 | + (bbox.lng1 + bbox.lng2) / 2, |
| 80 | + reason: 'Center longitude should remain the same'); |
| 81 | + expect((squaredBBox.lat1 + squaredBBox.lat2) / 2, |
| 82 | + (bbox.lat1 + bbox.lat2) / 2, |
| 83 | + reason: 'Center latitude should remain the same'); |
| 84 | + }); |
| 85 | + |
| 86 | + // File-based test for real-world scenarios |
| 87 | + // File-based test for real-world scenarios |
| 88 | + group('File-based Tests', () { |
| 89 | + var inDir = Directory('./test/examples/square/in'); |
| 90 | + if (inDir.existsSync()) { |
| 91 | + for (var file in inDir.listSync(recursive: true)) { |
| 92 | + if (file is File && file.path.endsWith('.geojson')) { |
| 93 | + test(file.path, () { |
| 94 | + var inSource = file.readAsStringSync(); |
| 95 | + var inJson = jsonDecode(inSource); |
| 96 | + |
| 97 | + // Get the first feature |
| 98 | + var feature = inJson['features'][0]; |
| 99 | + if (feature['geometry']['bbox'] == null) { |
| 100 | + throw Exception("Missing 'bbox' in GeoJSON feature geometry"); |
| 101 | + } |
| 102 | + |
| 103 | + // Extract bbox from the geometry |
| 104 | + var bbox = feature['geometry']['bbox']; |
| 105 | + |
| 106 | + // Create BBox instance |
| 107 | + var geoBbox = BBox.named( |
| 108 | + lng1: bbox[0], // min longitude |
| 109 | + lat1: bbox[1], // min latitude |
| 110 | + lng2: bbox[2], // max longitude |
| 111 | + lat2: bbox[3], // max latitude |
| 112 | + ); |
| 113 | + |
| 114 | + // Perform square operation (this doesn't change geometry type) |
| 115 | + BBox squaredBBox = square(geoBbox); |
| 116 | + |
| 117 | + // Create updated feature with new bbox but original geometry |
| 118 | + var updatedFeature = Map<String, dynamic>.from(feature); |
| 119 | + updatedFeature['geometry'] = |
| 120 | + Map<String, dynamic>.from(feature['geometry']); |
| 121 | + updatedFeature['geometry']['bbox'] = [ |
| 122 | + squaredBBox.lng1, |
| 123 | + squaredBBox.lat1, |
| 124 | + squaredBBox.lng2, |
| 125 | + squaredBBox.lat2, |
| 126 | + ]; |
| 127 | + |
| 128 | + // Prepare output path |
| 129 | + var outPath = file.path.replaceAll('/in', '/out'); |
| 130 | + var outFile = File(outPath); |
| 131 | + if (!outFile.existsSync()) { |
| 132 | + print('Warning: Output file not found at $outPath'); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + var outSource = outFile.readAsStringSync(); |
| 137 | + var expectedOutput = jsonDecode(outSource); |
| 138 | + var expectedFeature = expectedOutput['features'][0]; |
| 139 | + |
| 140 | + // Verify feature collection structure |
| 141 | + expect(expectedOutput['type'], 'FeatureCollection'); |
| 142 | + |
| 143 | + // Compare geometry types (should remain unchanged) |
| 144 | + expect( |
| 145 | + updatedFeature['geometry']['type'], |
| 146 | + expectedFeature['geometry']['type'], |
| 147 | + reason: 'Geometry type should remain the same', |
| 148 | + ); |
| 149 | + |
| 150 | + // Compare coordinates (should remain unchanged) |
| 151 | + expect( |
| 152 | + updatedFeature['geometry']['coordinates'], |
| 153 | + expectedFeature['geometry']['coordinates'], |
| 154 | + reason: 'Coordinates should remain unchanged', |
| 155 | + ); |
| 156 | + |
| 157 | + // Compare the updated bbox values |
| 158 | + expect( |
| 159 | + updatedFeature['geometry']['bbox'], |
| 160 | + expectedFeature['geometry']['bbox'], |
| 161 | + reason: 'BBox should match expected squared version', |
| 162 | + ); |
| 163 | + }); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + }); |
| 168 | + }); |
| 169 | +} |
0 commit comments