Skip to content

Commit

Permalink
Merge pull request #19 from airof98/master
Browse files Browse the repository at this point in the history
Serialize point properties
  • Loading branch information
synw committed Mar 22, 2020
2 parents ae20588 + 4fccb3a commit 5bcc656
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/src/models.dart
Expand Up @@ -76,7 +76,7 @@ class GeoJsonFeature<T> {
switch (type) {
case GeoJsonFeatureType.point:
final geom = geometry as GeoJsonPoint;
featStr = geom.serializeFeature();
featStr = geom.serializeFeature(properties);
break;
case GeoJsonFeatureType.multipoint:
final geom = geometry as GeoJsonMultiPoint;
Expand Down Expand Up @@ -175,7 +175,12 @@ class GeoJsonPoint {
String name;

/// Serialize to a geojson feature string
String serializeFeature() => geoPoint.toGeoJsonFeatureString();
String serializeFeature(Map properties) {
final p = properties ?? <String, dynamic>{};
return '{"type":"Feature","properties":${jsonEncode(p)},'
'"geometry":{"type":"Point",'
'"coordinates":' + geoPoint.toGeoJsonCoordinatesString() + '}}';
}
}

/// Multiple points
Expand Down
17 changes: 17 additions & 0 deletions test/geojson_test.dart
@@ -1,6 +1,7 @@
import 'dart:io';

import "package:geojson/geojson.dart";
import 'package:geopoint/geopoint.dart';
import "package:test/test.dart";

import 'data.dart';
Expand Down Expand Up @@ -96,4 +97,20 @@ void main() {
expect(e.message, "The feature Unknown is not supported");
}
});*/

test("point properties", () async {
final gfc = GeoJsonFeatureCollection()..name="mapmatch";
final lprops = Map<String, dynamic>();
lprops["point_color"] = "#000";
lprops["point_size"] = "7";
final lp = GeoJsonFeature<GeoJsonPoint>()
..type=GeoJsonFeatureType.point
..properties=lprops
..geometry=GeoJsonPoint(
geoPoint: GeoPoint(latitude: 37.111, longitude: 126.000), name: "a");
gfc.collection.add(lp);
final s = gfc.serialize();
expect(s.contains("\"point_size\":\"7\""), true);
expect(s.contains("\"point_color\":\"#000\""), true);
});
}

0 comments on commit 5bcc656

Please sign in to comment.