|
| 1 | +import 'package:test/test.dart'; |
| 2 | +import 'package:turf/helpers.dart'; |
| 3 | +import 'package:turf/flatten.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('flatten', () { |
| 7 | + test('Point geometry - should return a FeatureCollection with a single Point feature', () { |
| 8 | + var point = Point(coordinates: Position(1, 2)); |
| 9 | + var result = flatten(point); |
| 10 | + |
| 11 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 12 | + expect(result.features.length, 1); |
| 13 | + expect(result.features[0].geometry, isA<Point>()); |
| 14 | + expect((result.features[0].geometry as Point).coordinates, equals(Position(1, 2))); |
| 15 | + }); |
| 16 | + |
| 17 | + test('MultiPoint geometry - should return a FeatureCollection with multiple Point features', () { |
| 18 | + var multiPoint = MultiPoint(coordinates: [ |
| 19 | + Position(1, 2), |
| 20 | + Position(4, 5) |
| 21 | + ]); |
| 22 | + var result = flatten(multiPoint); |
| 23 | + |
| 24 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 25 | + expect(result.features.length, 2); |
| 26 | + expect(result.features[0].geometry, isA<Point>()); |
| 27 | + expect((result.features[0].geometry as Point).coordinates, equals(Position(1, 2))); |
| 28 | + expect(result.features[1].geometry, isA<Point>()); |
| 29 | + expect((result.features[1].geometry as Point).coordinates, equals(Position(4, 5))); |
| 30 | + }); |
| 31 | + |
| 32 | + test('LineString geometry - should return a FeatureCollection with a single LineString feature', () { |
| 33 | + var lineString = LineString(coordinates: [ |
| 34 | + Position(1, 2), |
| 35 | + Position(4, 5) |
| 36 | + ]); |
| 37 | + var result = flatten(lineString); |
| 38 | + |
| 39 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 40 | + expect(result.features.length, 1); |
| 41 | + expect(result.features[0].geometry, isA<LineString>()); |
| 42 | + var coords = (result.features[0].geometry as LineString).coordinates; |
| 43 | + expect(coords.length, 2); |
| 44 | + expect(coords[0], equals(Position(1, 2))); |
| 45 | + expect(coords[1], equals(Position(4, 5))); |
| 46 | + }); |
| 47 | + |
| 48 | + test('MultiLineString geometry - should return a FeatureCollection with multiple LineString features', () { |
| 49 | + var multiLineString = MultiLineString(coordinates: [ |
| 50 | + [Position(1, 2), Position(4, 5)], |
| 51 | + [Position(7, 8), Position(10, 11)] |
| 52 | + ]); |
| 53 | + var result = flatten(multiLineString); |
| 54 | + |
| 55 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 56 | + expect(result.features.length, 2); |
| 57 | + expect(result.features[0].geometry, isA<LineString>()); |
| 58 | + expect(result.features[1].geometry, isA<LineString>()); |
| 59 | + |
| 60 | + var coords1 = (result.features[0].geometry as LineString).coordinates; |
| 61 | + expect(coords1.length, 2); |
| 62 | + expect(coords1[0], equals(Position(1, 2))); |
| 63 | + expect(coords1[1], equals(Position(4, 5))); |
| 64 | + |
| 65 | + var coords2 = (result.features[1].geometry as LineString).coordinates; |
| 66 | + expect(coords2.length, 2); |
| 67 | + expect(coords2[0], equals(Position(7, 8))); |
| 68 | + expect(coords2[1], equals(Position(10, 11))); |
| 69 | + }); |
| 70 | + |
| 71 | + test('Polygon geometry - should return a FeatureCollection with a single Polygon feature', () { |
| 72 | + var polygon = Polygon(coordinates: [ |
| 73 | + [Position(0, 0), Position(1, 0), Position(1, 1), Position(0, 1), Position(0, 0)] |
| 74 | + ]); |
| 75 | + var result = flatten(polygon); |
| 76 | + |
| 77 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 78 | + expect(result.features.length, 1); |
| 79 | + expect(result.features[0].geometry, isA<Polygon>()); |
| 80 | + |
| 81 | + var coords = (result.features[0].geometry as Polygon).coordinates; |
| 82 | + expect(coords.length, 1); |
| 83 | + expect(coords[0].length, 5); |
| 84 | + }); |
| 85 | + |
| 86 | + test('MultiPolygon geometry - should return a FeatureCollection with multiple Polygon features', () { |
| 87 | + var multiPolygon = MultiPolygon(coordinates: [ |
| 88 | + [ |
| 89 | + [Position(0, 0), Position(1, 0), Position(1, 1), Position(0, 1), Position(0, 0)] |
| 90 | + ], |
| 91 | + [ |
| 92 | + [Position(10, 10), Position(11, 10), Position(11, 11), Position(10, 11), Position(10, 10)] |
| 93 | + ] |
| 94 | + ]); |
| 95 | + var result = flatten(multiPolygon); |
| 96 | + |
| 97 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 98 | + expect(result.features.length, 2); |
| 99 | + expect(result.features[0].geometry, isA<Polygon>()); |
| 100 | + expect(result.features[1].geometry, isA<Polygon>()); |
| 101 | + }); |
| 102 | + |
| 103 | + test('Feature with Point geometry - should preserve properties', () { |
| 104 | + var feature = Feature<Point>( |
| 105 | + geometry: Point(coordinates: Position(1, 2)), |
| 106 | + properties: {'name': 'Test Point', 'value': 42}, |
| 107 | + id: 'point1', |
| 108 | + bbox: BBox.fromJson([1, 2, 1, 2]) |
| 109 | + ); |
| 110 | + var result = flatten(feature); |
| 111 | + |
| 112 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 113 | + expect(result.features.length, 1); |
| 114 | + expect(result.features[0].geometry, isA<Point>()); |
| 115 | + expect(result.features[0].properties, equals({'name': 'Test Point', 'value': 42})); |
| 116 | + // ID might not be preserved in the geotypes library implementation |
| 117 | + // so we won't test for it explicitly |
| 118 | + // BBox might not be preserved as well |
| 119 | + // Skip this check |
| 120 | + }); |
| 121 | + |
| 122 | + test('Feature with MultiPoint geometry - should preserve properties in all output features', () { |
| 123 | + var feature = Feature<MultiPoint>( |
| 124 | + geometry: MultiPoint(coordinates: [ |
| 125 | + Position(1, 2), |
| 126 | + Position(4, 5) |
| 127 | + ]), |
| 128 | + properties: {'name': 'Test MultiPoint', 'value': 42}, |
| 129 | + id: 'multipoint1' |
| 130 | + ); |
| 131 | + var result = flatten(feature); |
| 132 | + |
| 133 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 134 | + expect(result.features.length, 2); |
| 135 | + expect(result.features[0].geometry, isA<Point>()); |
| 136 | + expect(result.features[1].geometry, isA<Point>()); |
| 137 | + |
| 138 | + for (var feat in result.features) { |
| 139 | + expect(feat.properties, equals({'name': 'Test MultiPoint', 'value': 42})); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + test('Altitude preservation - should retain altitude (z) values in coordinates', () { |
| 144 | + // Create a multipoint with altitude values |
| 145 | + var multiPoint = MultiPoint(coordinates: [ |
| 146 | + Position(1, 2, 30), // With altitude value |
| 147 | + Position(4, 5, 50) // With altitude value |
| 148 | + ]); |
| 149 | + |
| 150 | + var result = flatten(multiPoint); |
| 151 | + |
| 152 | + expect(result.features.length, 2); |
| 153 | + // Check if first point's altitude is preserved |
| 154 | + var firstPoint = result.features[0].geometry as Point; |
| 155 | + var firstPos = firstPoint.coordinates; |
| 156 | + expect(firstPos.length, 3); // Position with x, y, z |
| 157 | + expect(firstPos[2], 30); // z value preserved |
| 158 | + |
| 159 | + // Check if second point's altitude is preserved |
| 160 | + var secondPoint = result.features[1].geometry as Point; |
| 161 | + var secondPos = secondPoint.coordinates; |
| 162 | + expect(secondPos.length, 3); // Position with x, y, z |
| 163 | + expect(secondPos[2], 50); // z value preserved |
| 164 | + }); |
| 165 | + |
| 166 | + test('Comprehensive altitude preservation test', () { |
| 167 | + // Create more complex geometries with altitude values |
| 168 | + var multiLineString = MultiLineString(coordinates: [ |
| 169 | + [ |
| 170 | + Position(1, 2, 10), |
| 171 | + Position(3, 4, 20), |
| 172 | + Position(5, 6, 30) |
| 173 | + ], |
| 174 | + [ |
| 175 | + Position(7, 8, 40), |
| 176 | + Position(9, 10, 50) |
| 177 | + ] |
| 178 | + ]); |
| 179 | + |
| 180 | + var result = flatten(multiLineString); |
| 181 | + |
| 182 | + expect(result.features.length, 2); |
| 183 | + |
| 184 | + // Check first linestring's altitude values are preserved |
| 185 | + var firstLine = result.features[0].geometry as LineString; |
| 186 | + expect(firstLine.coordinates[0][2], 10); |
| 187 | + expect(firstLine.coordinates[1][2], 20); |
| 188 | + expect(firstLine.coordinates[2][2], 30); |
| 189 | + |
| 190 | + // Check second linestring's altitude values are preserved |
| 191 | + var secondLine = result.features[1].geometry as LineString; |
| 192 | + expect(secondLine.coordinates[0][2], 40); |
| 193 | + expect(secondLine.coordinates[1][2], 50); |
| 194 | + }); |
| 195 | + |
| 196 | + test('FeatureCollection with mixed geometries - should flatten all Multi* geometries', () { |
| 197 | + var featureCollection = FeatureCollection<GeometryObject>(features: [ |
| 198 | + Feature<Point>(geometry: Point(coordinates: Position(1, 2))), |
| 199 | + Feature<MultiPoint>(geometry: MultiPoint(coordinates: [ |
| 200 | + Position(4, 5), |
| 201 | + Position(7, 8) |
| 202 | + ])), |
| 203 | + Feature<LineString>(geometry: LineString(coordinates: [ |
| 204 | + Position(10, 11), |
| 205 | + Position(13, 14) |
| 206 | + ])), |
| 207 | + Feature<MultiPolygon>(geometry: MultiPolygon(coordinates: [ |
| 208 | + [ |
| 209 | + [Position(0, 0), Position(1, 0), Position(1, 1), Position(0, 1), Position(0, 0)] |
| 210 | + ], |
| 211 | + [ |
| 212 | + [Position(10, 10), Position(11, 10), Position(11, 11), Position(10, 11), Position(10, 10)] |
| 213 | + ] |
| 214 | + ])) |
| 215 | + ]); |
| 216 | + |
| 217 | + var result = flatten(featureCollection); |
| 218 | + |
| 219 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 220 | + // The implementation likely gives us 6 features: |
| 221 | + // 1 Point + 2 Points from MultiPoint + 1 LineString + 2 Polygons from MultiPolygon |
| 222 | + expect(result.features.length, 6); |
| 223 | + |
| 224 | + // Check the types of features in order |
| 225 | + expect(result.features[0].geometry, isA<Point>()); |
| 226 | + expect(result.features[1].geometry, isA<Point>()); |
| 227 | + expect(result.features[2].geometry, isA<Point>()); |
| 228 | + expect(result.features[3].geometry, isA<LineString>()); |
| 229 | + expect(result.features[4].geometry, isA<Polygon>()); |
| 230 | + }); |
| 231 | + |
| 232 | + test('Empty FeatureCollection - should return empty FeatureCollection', () { |
| 233 | + var emptyFC = FeatureCollection<GeometryObject>(features: []); |
| 234 | + var result = flatten(emptyFC); |
| 235 | + |
| 236 | + expect(result, isA<FeatureCollection<GeometryObject>>()); |
| 237 | + expect(result.features.length, 0); |
| 238 | + }); |
| 239 | + |
| 240 | + test('Feature with null geometry - should handle gracefully', () { |
| 241 | + // In this package, we can't have null geometry in a Feature |
| 242 | + // So we'll skip this particular test case |
| 243 | + // There seems to be a constraint where GeometryType can't be null |
| 244 | + }); |
| 245 | + |
| 246 | + test('GeometryCollection - should throw ArgumentError', () { |
| 247 | + var geometryCollection = GeometryCollection(geometries: [ |
| 248 | + Point(coordinates: Position(1, 2)), |
| 249 | + LineString(coordinates: [Position(4, 5), Position(7, 8)]) |
| 250 | + ]); |
| 251 | + |
| 252 | + expect(() => flatten(geometryCollection), throwsArgumentError); |
| 253 | + }); |
| 254 | + |
| 255 | + test('JSON serialization - should preserve integrity in roundtrip', () { |
| 256 | + var multiPoint = MultiPoint(coordinates: [ |
| 257 | + Position(1, 2), |
| 258 | + Position(4, 5) |
| 259 | + ]); |
| 260 | + var feature = Feature<MultiPoint>( |
| 261 | + geometry: multiPoint, |
| 262 | + properties: {'name': 'Test MultiPoint', 'value': 42} |
| 263 | + ); |
| 264 | + |
| 265 | + var result = flatten(feature); |
| 266 | + var json = result.toJson(); |
| 267 | + var deserialized = FeatureCollection.fromJson(json); |
| 268 | + |
| 269 | + expect(deserialized.features.length, 2); |
| 270 | + expect(deserialized.features[0].properties!['name'], 'Test MultiPoint'); |
| 271 | + expect(deserialized.features[0].properties!['value'], 42); |
| 272 | + }); |
| 273 | + }); |
| 274 | +} |
0 commit comments