-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmbtiles_image_provider.dart
More file actions
70 lines (61 loc) · 2.03 KB
/
mbtiles_image_provider.dart
File metadata and controls
70 lines (61 loc) · 2.03 KB
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
import 'dart:async';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:mbtiles/mbtiles.dart';
/// Image provider with additional caching functionality
class MbTilesImageProvider extends ImageProvider<MbTilesImageProvider> {
/// Default constructor for the [MbTilesImageProvider]
const MbTilesImageProvider({
required this.coordinates,
required this.mbtiles,
required this.silenceTileNotFound,
});
/// The tile coordinates of the requested tile image
final TileCoordinates coordinates;
/// MBTiles database
final MbTiles mbtiles;
/// Whether an exception should get thrown if a tile is not found.
final bool silenceTileNotFound;
@override
Future<MbTilesImageProvider> obtainKey(ImageConfiguration configuration) {
return Future.value(this);
}
@override
ImageStreamCompleter loadImage(
MbTilesImageProvider key,
ImageDecoderCallback decode,
) {
final chunkEvents = StreamController<ImageChunkEvent>();
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key, chunkEvents, decode),
chunkEvents: chunkEvents.stream,
scale: 1,
debugLabel: coordinates.toString(),
informationCollector: () => [
DiagnosticsProperty('Current provider', key),
],
);
}
Future<Codec> _loadAsync(
MbTilesImageProvider key,
StreamController<ImageChunkEvent> chunkEvents,
ImageDecoderCallback decode,
) async {
final tmsY = ((1 << coordinates.z) - 1) - coordinates.y;
final bytes = mbtiles.getTile(z: coordinates.z, x: coordinates.x, y: tmsY);
if (bytes == null) {
if (silenceTileNotFound) {
return decode(
await ImmutableBuffer.fromUint8List(TileProvider.transparentImage),
);
}
throw Exception(
'Tile could not be found in MBTiles '
'(z:${coordinates.z}, x:${coordinates.x}, y:${coordinates.y})',
);
}
return decode(await ImmutableBuffer.fromUint8List(bytes));
}
}