-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.dart
142 lines (110 loc) · 4.1 KB
/
main.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
import 'dart:io';
import 'package:fixnum/fixnum.dart';
import 'package:vector_tile/vector_tile_layer.dart';
import '../../lib/vector_tile.dart';
import '../../lib/raw/raw_vector_tile.dart' as raw;
void decodeGeoJsonFeatureCollection() async {
final tileData = await File('../data/sample-12-3262-1923.pbf').readAsBytes();
final tile = await VectorTile.fromBytes(bytes: tileData);
GeoJsonFeatureCollection featureCollection = tile.toGeoJson(x: 3262, y: 1923, z: 12);
print(featureCollection.type); // GeoJsonType.FeatureCollection
print(featureCollection.features.length); // 12049
}
/// Read & Decode given vector tile file
/// Decode raw features to GeoJson format
/// Each GeoJsonType was decode separate
void decodeForEachGeoJsonType() async {
final tileData = await File('../data/sample-12-3262-1923.pbf').readAsBytes();
final tile = await VectorTile.fromBytes(bytes: tileData);
final layer = tile.layers.firstWhere((layer) => layer.name == 'poi');
layer.features.forEach((feature) {
feature.decodeGeometry();
if (feature.geometryType == GeometryType.Point) {
var geojson = feature.toGeoJson<GeoJsonPoint>(x: 3262, y: 1923, z: 12);
print(geojson?.type);
print(geojson?.properties);
print(geojson?.geometry?.type);
print(geojson?.geometry?.coordinates);
print('\n');
}
if (feature.geometryType == GeometryType.MultiPoint) {
var geojson = feature.toGeoJson<GeoJsonMultiPoint>(x: 3262, y: 1923, z: 12);
//...
}
if (feature.geometryType == GeometryType.LineString) {
var geojson = feature.toGeoJson<GeoJsonLineString>(x: 3262, y: 1923, z: 12);
//...
}
if (feature.geometryType == GeometryType.MultiLineString) {
var geojson = feature.toGeoJson<GeoJsonMultiLineString>(x: 3262, y: 1923, z: 12);
//...
}
if (feature.geometryType == GeometryType.Polygon) {
var geojson = feature.toGeoJson<GeoJsonPolygon>(x: 3262, y: 1923, z: 12);
//...
}
if (feature.geometryType == GeometryType.MultiPolygon) {
var geojson = feature.toGeoJson<GeoJsonMultiPolygon>(x: 3262, y: 1923, z: 12);
//...
}
});
}
/// Read & Decode given vector tile file
/// Decode raw features to GeoJson format
/// All GeoJsonType was decode one and
/// Then we must use type cast to read each type specific data
void decodeForAllGeoJsonType() async {
final tileData = await File('../data/sample-12-3262-1923.pbf').readAsBytes();
final tile = await VectorTile.fromBytes(bytes: tileData);
final layer = tile.layers.firstWhere((layer) => layer.name == 'poi');
layer.features.forEach((feature) {
var geojson = feature.toGeoJson(x: 3262, y: 1923, z: 12);
if (feature.geometryType == GeometryType.Point) {
print((geojson as GeoJsonPoint).type);
print((geojson as GeoJsonPoint).properties);
print((geojson as GeoJsonPoint).geometry?.type);
print((geojson as GeoJsonPoint).geometry?.coordinates);
}
if (feature.geometryType == GeometryType.MultiPoint) {
print((geojson as GeoJsonMultiPoint).type);
print((geojson as GeoJsonMultiPoint).properties);
print((geojson as GeoJsonMultiPoint).geometry?.type);
print((geojson as GeoJsonMultiPoint).geometry?.coordinates);
}
// Other types ...
});
}
/// Create & Encode a set of vector tile data from raw format
void encode() async {
var values = [
raw.createVectorTileValue(intValue: Int64(65)),
raw.createVectorTileValue(stringValue: 'basketball'),
];
var features = [
raw.createVectorTileFeature(
id: Int64(31162829580),
tags: [0, 0, 1, 1],
type: raw.VectorTile_GeomType.POINT,
geometry: [9, 8058, 1562],
),
];
var layers = [
raw.createVectorTileLayer(
name: 'poi',
extent: 4096,
version: 2,
keys: ['render_height', 'name'],
values: values,
features: features,
),
];
var tile = raw.createVectorTile(layers: layers);
// Save to disk
await File('../gen/tile.pbf').writeAsBytes(tile.writeToBuffer());
}
main() {
encode();
decodeForEachGeoJsonType();
// decodeForAllGeoJsonType();
// decodeGeoJsonFeatureCollection();
}