Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port coordAll function and test, also: refactor coordEach callback #64

Merged
merged 9 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Any new benchmarks must be named `*_benchmark.dart` and reside in the
- [ ] clustersKmeans

### META
- [ ] coordAll
- [x] coordAll
- [x] coordEach
- [ ] coordReduce
- [x] [featureEach](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta.dart#L157)
Expand Down
49 changes: 39 additions & 10 deletions lib/src/meta.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import 'geojson.dart';

typedef CoordEachCallback = dynamic Function(
CoordinateType? currentCoord,
Position? currentCoord,
int? coordIndex,
int? featureIndex,
int? multiFeatureIndex,
int? geometryIndex,
);

///
/// Iterate over coordinates in any [geoJSON] object, similar to Array.forEach()
/// Iterate over coordinates in any [geoJSON] object, similar to [Iterable.forEach()]
///
/// For example:
///
/// ```dart
/// // TODO add example
/// //TODO add example
/// ```
void coordEach(GeoJSONObject geoJSON, CoordEachCallback callback,
[bool excludeWrapCoord = false]) {
Expand Down Expand Up @@ -55,9 +55,9 @@ void _forEachCoordInGeometryObject(
: 0;
indexCounter.multiFeatureIndex = 0;

dynamic coords = geometry.coordinates as Iterable;
var coords = geometry.coordinates;
if (geomType == GeoJSONObjectType.point) {
_forEachCoordInPoint(coords as CoordinateType, callback, indexCounter);
_forEachCoordInPoint(coords, callback, indexCounter);
} else if (geomType == GeoJSONObjectType.lineString ||
geomType == GeoJSONObjectType.multiPoint) {
_forEachCoordInCollection(coords, geomType, callback, indexCounter);
Expand Down Expand Up @@ -141,8 +141,8 @@ void _forEachCoordInCollection(coords, GeoJSONObjectType geomType,
}
}

void _forEachCoordInPoint(CoordinateType coords, CoordEachCallback callback,
_IndexCounter indexCounter) {
void _forEachCoordInPoint(
Position coords, CoordEachCallback callback, _IndexCounter indexCounter) {
if (callback(coords, indexCounter.coordIndex, indexCounter.featureIndex,
indexCounter.multiFeatureIndex, indexCounter.geometryIndex) ==
false) {
Expand Down Expand Up @@ -265,7 +265,7 @@ typedef PropEachCallback = dynamic Function(
Map<String, dynamic>? currentProperties, num featureIndex);

/// Iterate over properties in any [geoJSON] object, calling [callback] on each
/// iteration. Similar to Array.forEach()
/// iteration. Similar to [Iterable.forEach()]
///
/// For example:
///
Expand Down Expand Up @@ -298,7 +298,7 @@ typedef FeatureEachCallback = dynamic Function(
Feature currentFeature, num featureIndex);

/// Iterate over features in any [geoJSON] object, calling [callback] on each
/// iteration. Similar to Array.forEach.
/// iteration. Similar to [Iterable.forEach()].
///
/// For example:
///
Expand Down Expand Up @@ -331,7 +331,7 @@ typedef FlattenEachCallback = dynamic Function(
Feature currentFeature, int featureIndex, int multiFeatureIndex);

/// Iterate over flattened features in any [geoJSON] object, similar to
/// Array.forEach, calling [callback] on each flattened feature
/// [Iterable.forEach()], calling [callback] on each flattened feature

///
/// flattenEach(featureCollection, (currentFeature, featureIndex, multiFeatureIndex) {
Expand Down Expand Up @@ -403,3 +403,32 @@ void _callFlattenEachCallback(
throw _ShortCircuit();
}
}

/// Gets all coordinates from any [GeoJSONObject].
/// Receives any [GeoJSONObject]
/// Returns [List<Position>]
/// For example:
///
/// ```dart
/// var featureColl = FeatureCollection(features:
/// [Feature(geometry: Point(coordinates: Position(13,15)))
/// ,Feature(geometry: LineString(coordinates: [Position(1, 2),
/// Position(67, 50)]))]);
///
/// var coords = coordAll(features);
/// //= [Position(13,15), Position(1, 2), Position(67, 50)]
///
List<Position?> coordAll(GeoJSONObject geojson) {
List<Position?> coords = [];
coordEach(geojson, (
Position? currentCoord,
int? coordIndex,
int? featureIndex,
int? multiFeatureIndex,
int? geometryIndex,
) {
coords.add(currentCoord);
return true;
});
return coords;
}
35 changes: 35 additions & 0 deletions test/components/meta_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,39 @@ main() {
expect(lastProperties, geomCollection.properties);
});
});

test('meta -- coordAll', () {
FeatureCollection<LineString> lines = FeatureCollection<LineString>(
features: [
Feature<LineString>(
geometry: LineString.fromJson({
'coordinates': [
[10, 10],
[50, 30],
[30, 40]
]
}),
),
Feature<LineString>(
geometry: LineString.fromJson({
'coordinates': [
[-10, -10],
[-50, -30],
[-30, -40]
]
}),
),
],
);

List<Position?> results = coordAll(lines);
expect(results, [
Position.of([10, 10]),
Position.of([50, 30]),
Position.of([30, 40]),
Position.of([-10, -10]),
Position.of([-50, -30]),
Position.of([-30, -40]),
]);
});
}