This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
deserializers.dart
132 lines (124 loc) · 4.26 KB
/
deserializers.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'package:geopoint/geopoint.dart';
import 'models.dart';
/// Get a point from coordinates and feature
GeoJsonPoint getPoint(
{List<dynamic> coordinates, GeoJsonFeature feature, String nameProperty}) {
final point = GeoJsonPoint();
final name = _getName(feature: feature, nameProperty: nameProperty);
final geoPoint = _getGeoPoints(<dynamic>[coordinates])[0]..name = name;
point
..name = name
..geoPoint = geoPoint;
return point;
}
/// Get multi points from coordinates and feature
GeoJsonMultiPoint getMultiPoint(
{List<dynamic> coordinates, GeoJsonFeature feature, String nameProperty}) {
final multiPoint = GeoJsonMultiPoint();
final name = _getName(feature: feature, nameProperty: nameProperty);
multiPoint.name = name;
final geoSerie = GeoSerie(
name: name,
type: GeoSerieType.group,
geoPoints: _getGeoPoints(coordinates));
multiPoint.geoSerie = geoSerie;
return multiPoint;
}
/// Get a line from coordinates and feature
GeoJsonLine getLine(
{List<dynamic> coordinates, GeoJsonFeature feature, String nameProperty}) {
final line = GeoJsonLine();
final name = _getName(feature: feature, nameProperty: nameProperty);
line.name = name;
final geoSerie = GeoSerie(
name: name,
type: GeoSerieType.line,
geoPoints: _getGeoPoints(coordinates));
line.geoSerie = geoSerie;
return line;
}
/// Get a multi line from coordinates and feature
GeoJsonMultiLine getMultiLine(
{List<dynamic> coordinates, GeoJsonFeature feature, String nameProperty}) {
final name = _getName(feature: feature, nameProperty: nameProperty);
final multiLine = GeoJsonMultiLine(name: name);
var i = 1;
for (final coords in coordinates) {
final line = GeoJsonLine()
..name = _getName(feature: feature, nameProperty: nameProperty, index: i);
final geoSerie = GeoSerie(
name: name,
type: GeoSerieType.line,
geoPoints: _getGeoPoints(coords as List<dynamic>));
line.geoSerie = geoSerie;
multiLine.lines.add(line);
++i;
}
return multiLine;
}
/// Get a polygon from coordinates and feature
GeoJsonPolygon getPolygon(
{List<dynamic> coordinates, GeoJsonFeature feature, String nameProperty}) {
final polygon = GeoJsonPolygon();
final name = _getName(feature: feature, nameProperty: nameProperty);
polygon.name = name;
for (final coords in coordinates) {
final geoSerie = GeoSerie(
name: _getName(feature: feature, nameProperty: nameProperty),
type: GeoSerieType.polygon)
..geoPoints = _getGeoPoints(coords as List<dynamic>);
polygon.geoSeries.add(geoSerie);
}
return polygon;
}
/// Get a multipolygon from coordinates and feature
GeoJsonMultiPolygon getMultipolygon(
{List<dynamic> coordinates, GeoJsonFeature feature, String nameProperty}) {
final multiPolygon = GeoJsonMultiPolygon();
var i = 1;
multiPolygon.name = _getName(feature: feature, nameProperty: nameProperty);
for (final coordsL2 in coordinates) {
final polygon = GeoJsonPolygon();
final name =
_getName(feature: feature, nameProperty: nameProperty, index: i);
polygon.name = name;
for (final coords in coordsL2) {
final geoSerie = GeoSerie(
name: _getName(feature: feature, nameProperty: nameProperty),
type: GeoSerieType.polygon)
..geoPoints = _getGeoPoints(coords as List<dynamic>);
polygon.geoSeries.add(geoSerie);
}
multiPolygon.polygons.add(polygon);
++i;
}
return multiPolygon;
}
String _getName({GeoJsonFeature feature, String nameProperty, int index}) {
String name;
if (nameProperty != null) {
name = feature.properties[nameProperty].toString();
} else {
if (feature.properties.containsKey("name") == true) {
name = feature.properties["name"].toString();
}
}
if (name == null || name == "null") {
name = "serie";
if (index != null) {
name = "serie_$index";
}
}
return name;
}
List<GeoPoint> _getGeoPoints(List<dynamic> coordsList) {
final geoPoints = <GeoPoint>[];
for (final coord in coordsList) {
final geoPoint = GeoPoint(
latitude: double.parse(coord[1].toString()),
longitude: double.parse(coord[0].toString()),
altitude: coord.length >= 3 ? double.parse(coord[2].toString()) : null);
geoPoints.add(geoPoint);
}
return geoPoints;
}