|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'package:turf/turf.dart'; |
| 4 | + |
| 5 | +void main() async { |
| 6 | + // Process each input file |
| 7 | + await processFile('polygon_feature.geojson'); |
| 8 | + await processFile('polygon.geojson'); |
| 9 | + await processFile('point.geojson'); |
| 10 | + await processFile('linestring.geojson'); |
| 11 | + await processFile('multipolygon.geojson'); |
| 12 | + await processFile('featurecollection.geojson'); |
| 13 | + |
| 14 | + print('All files processed successfully!'); |
| 15 | +} |
| 16 | + |
| 17 | +Future<void> processFile(String filename) async { |
| 18 | + try { |
| 19 | + final inputPath = 'test/examples/point_on_feature/in/$filename'; |
| 20 | + final outputPath = 'test/examples/point_on_feature/out/$filename'; |
| 21 | + |
| 22 | + print('Processing $inputPath'); |
| 23 | + |
| 24 | + // Read the input file |
| 25 | + final file = File(inputPath); |
| 26 | + final jsonString = await file.readAsString(); |
| 27 | + final geojson = jsonDecode(jsonString); |
| 28 | + |
| 29 | + // Parse the GeoJSON and create appropriate object based on type |
| 30 | + dynamic featureInput; |
| 31 | + |
| 32 | + if (geojson['type'] == 'Feature') { |
| 33 | + featureInput = Feature.fromJson(geojson); |
| 34 | + } else if (geojson['type'] == 'FeatureCollection') { |
| 35 | + featureInput = FeatureCollection.fromJson(geojson); |
| 36 | + } else { |
| 37 | + // For raw geometry objects, create a Feature with the geometry |
| 38 | + final geometry = parseGeometry(geojson); |
| 39 | + if (geometry != null) { |
| 40 | + featureInput = Feature(geometry: geometry); |
| 41 | + } else { |
| 42 | + print(' Unsupported geometry type: ${geojson['type']}'); |
| 43 | + return; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Apply point_on_feature function |
| 48 | + final pointResult = pointOnFeature(featureInput); |
| 49 | + |
| 50 | + // Generate output - wrap in a FeatureCollection for better compatibility |
| 51 | + Map<String, dynamic> outputJson; |
| 52 | + |
| 53 | + if (pointResult != null) { |
| 54 | + final features = <Feature>[]; |
| 55 | + |
| 56 | + // Create a new feature based on the input geometry |
| 57 | + GeometryObject? inputGeometry; |
| 58 | + if (featureInput is Feature) { |
| 59 | + inputGeometry = featureInput.geometry; |
| 60 | + } else if (featureInput is FeatureCollection && featureInput.features.isNotEmpty) { |
| 61 | + inputGeometry = featureInput.features[0].geometry; |
| 62 | + } else { |
| 63 | + inputGeometry = parseGeometry(geojson); |
| 64 | + } |
| 65 | + |
| 66 | + if (inputGeometry != null) { |
| 67 | + // Create a new feature with the input geometry |
| 68 | + final styledInputFeature = Feature( |
| 69 | + geometry: inputGeometry, |
| 70 | + properties: { |
| 71 | + 'name': 'Input Geometry', |
| 72 | + 'description': 'Original geometry from $filename' |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + // Add styling based on geometry type |
| 77 | + if (inputGeometry is Polygon || inputGeometry is MultiPolygon) { |
| 78 | + styledInputFeature.properties!['stroke'] = '#0000FF'; |
| 79 | + styledInputFeature.properties!['stroke-width'] = 2; |
| 80 | + styledInputFeature.properties!['fill'] = '#0000FF'; |
| 81 | + styledInputFeature.properties!['fill-opacity'] = 0.2; |
| 82 | + } else if (inputGeometry is LineString || inputGeometry is MultiLineString) { |
| 83 | + styledInputFeature.properties!['stroke'] = '#0000FF'; |
| 84 | + styledInputFeature.properties!['stroke-width'] = 2; |
| 85 | + } else if (inputGeometry is Point) { |
| 86 | + styledInputFeature.properties!['marker-color'] = '#0000FF'; |
| 87 | + } |
| 88 | + |
| 89 | + features.add(styledInputFeature); |
| 90 | + } |
| 91 | + |
| 92 | + // Create a new feature for the point result to avoid modifying unmodifiable maps |
| 93 | + final styledPointResult = Feature( |
| 94 | + geometry: pointResult.geometry, |
| 95 | + properties: { |
| 96 | + 'marker-color': '#FF0000', |
| 97 | + 'marker-size': 'medium', |
| 98 | + 'marker-symbol': 'star', |
| 99 | + 'name': 'Point on Feature Result', |
| 100 | + 'description': 'Point generated by pointOnFeature function' |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + features.add(styledPointResult); |
| 105 | + |
| 106 | + outputJson = FeatureCollection(features: features).toJson(); |
| 107 | + print(' Found point at coordinates: ${pointResult.geometry?.coordinates}'); |
| 108 | + } else { |
| 109 | + // Create an empty FeatureCollection with error info in properties |
| 110 | + outputJson = { |
| 111 | + 'type': 'FeatureCollection', |
| 112 | + 'features': [ |
| 113 | + { |
| 114 | + 'type': 'Feature', |
| 115 | + 'properties': { |
| 116 | + 'error': 'Could not generate point for this input', |
| 117 | + 'name': 'Error', |
| 118 | + 'description': 'pointOnFeature function could not generate a point' |
| 119 | + }, |
| 120 | + 'geometry': null |
| 121 | + } |
| 122 | + ] |
| 123 | + }; |
| 124 | + print(' Could not generate point for this input'); |
| 125 | + } |
| 126 | + |
| 127 | + // Write to output file with pretty formatting |
| 128 | + final outputFile = File(outputPath); |
| 129 | + await outputFile.writeAsString(JsonEncoder.withIndent(' ').convert(outputJson)); |
| 130 | + print(' Saved result to $outputPath'); |
| 131 | + } catch (e) { |
| 132 | + print('Error processing $filename: $e'); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +GeometryObject? parseGeometry(Map<String, dynamic> json) { |
| 137 | + final type = json['type']; |
| 138 | + |
| 139 | + switch (type) { |
| 140 | + case 'Point': |
| 141 | + return Point.fromJson(json); |
| 142 | + case 'LineString': |
| 143 | + return LineString.fromJson(json); |
| 144 | + case 'Polygon': |
| 145 | + return Polygon.fromJson(json); |
| 146 | + case 'MultiPoint': |
| 147 | + return MultiPoint.fromJson(json); |
| 148 | + case 'MultiLineString': |
| 149 | + return MultiLineString.fromJson(json); |
| 150 | + case 'MultiPolygon': |
| 151 | + return MultiPolygon.fromJson(json); |
| 152 | + case 'GeometryCollection': |
| 153 | + return GeometryCollection.fromJson(json); |
| 154 | + default: |
| 155 | + return null; |
| 156 | + } |
| 157 | +} |
0 commit comments