Skip to content

Commit

Permalink
Merge pull request #29 from divvitco/feature-collection-from-wkt
Browse files Browse the repository at this point in the history
Feature collection from wkt
  • Loading branch information
ntatko authored Apr 3, 2024
2 parents cbddb27 + 4c5608f commit eee9eb8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
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)'));
});
});
}

0 comments on commit eee9eb8

Please sign in to comment.