forked from 0nza1101/leaflet-tilelayer-mbtiles-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·178 lines (174 loc) · 6.74 KB
/
index.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SQL = require("sql.js");
const L = require("leaflet");
/*
🍂class TileLayer.MBTiles
Loads tiles from a [`.mbtiles` file](https://github.com/mapbox/mbtiles-spec).
If they exist in the given file, it will handle the following metadata rows:
*/
class Utils {
static fetchLocal(url) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest;
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
resolve(new Response(xhr.response, { status: xhr.status }));
};
xhr.onerror = function () {
reject(new TypeError('Local request failed'));
};
xhr.open('GET', url);
xhr.send(null);
});
}
}
exports.Utils = Utils;
L.TileLayer.MBTiles = L.TileLayer.extend({
initialize: function (databaseUrl, options) {
this._databaseIsLoaded = false;
if (typeof databaseUrl === 'string') {
if (L.Browser.android) {
Utils.fetchLocal(databaseUrl).then(response => {
//console.log("Response: ", response);
return response.arrayBuffer();
}).then(buffer => {
//console.log("This: ", this);
this._openDB(buffer);
}).catch(err => {
this.fire('databaseerror', { error: err });
});
}
else {
fetch(databaseUrl).then(response => {
return response.arrayBuffer();
}).then(buffer => {
this._openDB(buffer);
}).catch(err => {
this.fire('databaseerror', { error: err });
});
}
}
else if (databaseUrl instanceof ArrayBuffer) {
this._openDB(databaseUrl);
}
else {
this.fire('databaseerror');
}
this.on('tileunload', (event) => {
if (event.tile && event.tile.src != L.Util.emptyImageUrl) {
URL.revokeObjectURL(event.tile.src);
}
});
const closeDb = () => this._db.close();
this.on('remove', () => {
if (this._databaseIsLoaded) {
closeDb();
}
else {
this.on('databaseloaded', closeDb);
}
});
return L.TileLayer.prototype.initialize.call(this, '', options);
},
_openDB: function (buffer) {
try {
/// This assumes the `SQL` global variable to exist!!
var uInt8Array = new Uint8Array(buffer);
this._db = new SQL.Database(uInt8Array);
this._stmt = this._db.prepare('SELECT tile_data FROM tiles WHERE zoom_level = :z AND tile_column = :x AND tile_row = :y');
// Load some metadata (or at least try to)
var metaStmt = this._db.prepare('SELECT value FROM metadata WHERE name = :key');
var row;
row = metaStmt.getAsObject({ ':key': 'attribution' });
if (row.value) {
this.options.attribution = row.value;
}
row = metaStmt.getAsObject({ ':key': 'minzoom' });
if (row.value) {
this.options.minZoom = Number(row.value);
}
row = metaStmt.getAsObject({ ':key': 'maxzoom' });
if (row.value) {
this.options.maxZoom = Number(row.value);
}
row = metaStmt.getAsObject({ ':key': 'format' });
if (row.value && row.value === 'png') {
this._format = 'image/png';
}
else if (row.value && row.value === 'jpg') {
this._format = 'image/jpg';
}
else {
// Fall back to PNG, hope it works.
this._format = 'image/png';
}
// 🍂event databaseloaded
// Fired when the database has been loaded, parsed, and ready for queries
this.fire('databaseloaded');
this._databaseIsLoaded = true;
}
catch (ex) {
// 🍂event databaseloaded
// Fired when the database could not load for any reason. Might contain
// an `error` property describing the error.
this.fire('databaseerror', { error: ex });
}
},
createTile: function (coords, done) {
var tile = document.createElement('img');
if (this.options.crossOrigin) {
tile.crossOrigin = '';
}
/*
* Alt tag is set to empty string to keep screen readers from reading URL and for compliance reasons
* http://www.w3.org/TR/WCAG20-TECHS/H67
*/
tile.alt = '';
/*
* Set role="presentation" to force screen readers to ignore this
* https://www.w3.org/TR/wai-aria/roles#textalternativecomputation
*/
tile.setAttribute('role', 'presentation');
// In TileLayer.MBTiles, the getTileUrl() method can only be called when
// the database has already been loaded.
if (this._databaseIsLoaded) {
L.DomEvent.on(tile, 'load', L.Util.bind(this._tileOnLoad, this, done, tile));
L.DomEvent.on(tile, 'error', L.Util.bind(this._tileOnError, this, done, tile));
tile.src = this.getTileUrl(coords);
}
else {
this.on('databaseloaded', function () {
L.DomEvent.on(tile, 'load', L.Util.bind(this._tileOnLoad, this, done, tile));
L.DomEvent.on(tile, 'error', L.Util.bind(this._tileOnError, this, done, tile));
tile.src = this.getTileUrl(coords);
}.bind(this));
}
return tile;
},
getTileUrl: function (coords) {
// Luckily, SQL execution is synchronous. If not, this code would get
// much more complicated.
var row = this._stmt.getAsObject({
':x': coords.x,
':y': this._globalTileRange.max.y - coords.y,
':z': coords.z
});
if ('tile_data' in row) {
return window.URL.createObjectURL(new Blob([row.tile_data], { type: this._format }));
}
else {
return L.Util.emptyImageUrl;
}
},
});
/*
🍂factory tileLayer.mbTiles(databaseUrl: String, options: TileLayer options)
Returns a new `L.TileLayer.MBTiles`, fetching and using the database given in `databaseUrl`.
🍂alternative
🍂factory tileLayer.mbTiles(databaseBuffer: Uint8Array, options: TileLayer options)
Returns a new `L.TileLayer.MBTiles`, given a MBTiles database as a javascript binary array.
*/
L.tileLayer.mbTiles = function (databaseUrl, options) {
return new L.TileLayer.MBTiles(databaseUrl, options);
};