forked from navibyte/geospatial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocore_example.dart
161 lines (138 loc) · 4.88 KB
/
geocore_example.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2020-2021 Navibyte (https://navibyte.com). All rights reserved.
// Use of this source code is governed by a “BSD-3-Clause”-style license that is
// specified in the LICENSE file.
//
// Docs: https://github.com/navibyte/geospatial
import 'package:equatable/equatable.dart';
import 'package:geocore/geocore.dart';
/*
To test run this from command line:
dart example/geocore_example.dart
*/
void main() {
// configure Equatable to apply toString() default impls
EquatableConfig.stringify = true;
// call simple demos
_parseGeoJSON();
_basicStructures();
}
void _parseGeoJSON() {
print('Parse GeoJSON sample data.');
// sample GeoJSON data
const sample = '''
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "ROG",
"geometry": {
"type": "Point",
"coordinates": [-0.0014, 51.4778, 45.0]
},
"properties": {
"title": "Royal Observatory",
"place": "Greenwich",
"city": "London",
"isMuseum": true,
"code": "000",
"founded": 1675,
"prime": "1884-10-22",
"measure": 5.79
}
}
]
}
''';
// parse FeatureCollection using the default GeoJSON factory
final fc = geoJSON.featureCollection(sample);
// loop through features and print id, geometry and properties for each
fc.features.forEach((f) {
print('Feature with id: ${f.id}');
print(' geometry: ${f.geometry}');
print(' properties:');
f.properties.map.forEach((key, value) => print(' $key: $value'));
});
}
void _basicStructures() {
print('');
print('Create some basic data structures.');
// Geospatial feature with id, geometry and properties
print(Feature.view(
id: 'ROG',
geometry: GeoPoint3.from([-0.0014, 51.4778, 45.0]),
properties: {
'title': 'Royal Observatory',
'place': 'Greenwich',
'city': 'London',
'isMuseum': true,
'code': '000',
'founded': 1675,
'prime': DateTime.utc(1884, 10, 22),
'measure': 5.79,
},
));
// Geographic points (lon-lat, lon-lat-m, lon-lat-elev, lon-lat-elev-m)
print(GeoPoint2.lonLat(-0.0014, 51.4778));
print(GeoPoint2m.lonLatM(-0.0014, 51.4778, 123.0));
print(GeoPoint3.lonLatElev(-0.0014, 51.4778, 45.0));
print(GeoPoint3m.lonLatElevM(-0.0014, 51.4778, 45.0, 123.0));
// Geographic points (lat-lon, lat-lon-m, lat-lon-elev, lat-lon-elev-m)
print(GeoPoint2.latLon(51.4778, -0.0014));
print(GeoPoint2m.latLonM(51.4778, -0.0014, 123.0));
print(GeoPoint3.latLonElev(51.4778, -0.0014, 45.0));
print(GeoPoint3m.latLonElevM(51.4778, -0.0014, 45.0, 123.0));
// Geographic bounds represented as Bounds<GeoPoint>
print(Bounds.of(
min: GeoPoint2.lonLat(-180.0, -90.0),
max: GeoPoint2.lonLat(180.0, 90.0)));
print(Bounds.of(
min: GeoPoint3.lonLatElev(-180.0, -90.0, 50.0),
max: GeoPoint3.lonLatElev(180.0, 90.0, 100.0)));
print(Bounds.of(
min: GeoPoint2.latLon(-90.0, -180.0),
max: GeoPoint2.latLon(90.0, 180.0)));
// Geographic bounds as GeoBounds (that implements Bounds<GeoPoint>)
print(GeoBounds.world());
print(GeoBounds.bboxLonLat(-180.0, -90.0, 180.0, 90.0));
print(GeoBounds.bboxLonLatElev(-180.0, -90.0, 50.0, 180.0, 90.0, 100.0));
// Projected points (XY, XYM, XYZ and XYZM) using doubles
print(Point2.xy(708221.0, 5707225.0));
print(Point2m.xym(708221.0, 5707225.0, 123.0));
print(Point3.xyz(708221.0, 5707225.0, 45.0));
print(Point3m.xyzm(708221.0, 5707225.0, 45.0, 123.0));
// Projected points (XY, XYZ) using integers
print(Point2i.xy(708221, 5707225));
print(Point3i.xyz(708221, 5707225, 45));
// Series of points containg all types of points
final points = PointSeries<Point>.view([
// coords stored as double, GeoPoint3 implements Point<double>
GeoPoint3.origin(),
// coords stored as num (given as double or int),
// Point2 implements Point<num>
Point2.xy(708221.0, 5707225),
// coords stored as int, Point3i implements Point<int>
Point3i.xyz(708221, 5707225, 45)
]);
print(points);
print(
'Testing int coord value: ${points[2].x} ${points[2].y} (type: ${points[2].z.runtimeType})');
// Temporal intervals (open, open-started, open-ended, closed)
print(Interval.open());
print(Interval.openStart(DateTime.utc(2020, 10, 31)));
print(Interval.openEnd(DateTime.utc(2020, 10, 01)));
print(
Interval.closed(DateTime.utc(2020, 10, 01), DateTime.utc(2020, 10, 31)));
// Temporal instant
print(Instant.parse('2020-10-31'));
// Coordinate reference system (identifiers)
print(CRS84);
print(CRS84h);
print(CRS.id('urn:ogc:def:crs:EPSG::4326'));
// Extent with spatial and temporal parts
print(Extent.single(
crs: CRS84,
bounds: GeoBounds.bboxLonLat(-180.0, -90.0, 180.0, 90.0),
interval: Interval.fromJson(['..', '2020-10-31']),
));
}