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

Feature collection from wkt #29

Merged
merged 3 commits into from
Apr 3, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ but might extended to include additional properties in the future.
**Constructors**

* `FeatureCollection.fromJson(Map<String, dynamic> json)` - Creates a [`FeatureCollection`](#Feature-Collection) from a JSON object. Automatically converts features from GeoJSON to their respective types.
* `FeatureCollection.fromWKT(String wkt)` - Creates a [`FeatureCollection`](#Feature-Collection) from a Well-Known Text string.

**Methods**

Expand Down
39 changes: 39 additions & 0 deletions lib/src/featureTypes/feature_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,45 @@ class FeatureCollection {
);
}

/// Creates a FeatureCollection from a WKT string.
///
/// Example:
/// ```dart
/// FeatureCollection.fromWKT('GEOMETRYCOLLECTION(POINT(1 2), POINT(3 4))'); // FeatureCollection([Point(Coordinate(1, 2)), Point(Coordinate(3, 4))])
/// ```
factory FeatureCollection.fromWKT(String wkt) {
final regexBody = RegExp(r'GEOMETRYCOLLECTION\w?\((.*)\)', multiLine: true);
final match = regexBody.firstMatch(wkt);
if (match == null) {
throw ArgumentError('Invalid WKT');
}

final geometryString = match.group(1);
final regexGeometry = RegExp(
r'(\),[\s|\n]{0,2})(?=(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING|POLYGON|MULTIPOLYGON))');
final geometries = geometryString?.split(regexGeometry) ?? [];

return FeatureCollection(
geometries.map((g) {
if (g.startsWith('POINT')) {
return Point.fromWKT(g);
} else if (g.startsWith('MULTIPOINT')) {
return MultiPoint.fromWKT(g);
} else if (g.startsWith('LINESTRING')) {
return LineString.fromWKT(g);
} else if (g.startsWith('MULTILINESTRING')) {
return MultiLineString.fromWKT(g);
} else if (g.startsWith('POLYGON')) {
return Polygon.fromWKT(g);
} else if (g.startsWith('MULTIPOLYGON')) {
return MultiPolygon.fromWKT(g);
} else {
throw ArgumentError('Invalid geometry type');
}
}).toList(),
);
}

/// Creates a [FeatureCollection] of [Point]s from the geometries of the [Feature]s contained within.
///
/// Example:
Expand Down
9 changes: 9 additions & 0 deletions test/featureTypes/feature_collection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ void main() {

expect(hydroelectricLinesCanada, featureCollectionJson);
});

test('from wkt', () {
FeatureCollection featureCollection = FeatureCollection.fromWKT(
'GEOMETRYCOLLECTION(POINT(1 2), LINESTRING(1 2, 3 4))');
expect(featureCollection.features.length, 2);
expect(featureCollection.features[0] is Point, true);
expect(featureCollection.features[1] is LineString, true);
expect(featureCollection.features[0], Point.fromWKT('POINT(1 2)'));
});
});
}
Loading