Skip to content

Commit

Permalink
Support raster-dem in offline mapbox#42 - use makeRequest aware of mb…
Browse files Browse the repository at this point in the history
…tiles:// protocol.
  • Loading branch information
HarelM committed May 18, 2019
1 parent 5dd8882 commit 2cb233d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { extend } from './util';
import { isMapboxHTTPURL } from './mapbox';
import config from './config';
import assert from 'assert';
import pako from 'pako/lib/inflate'

import type { Callback } from '../types/callback';
import type { Cancelable } from '../types/cancelable';
Expand Down Expand Up @@ -90,6 +91,34 @@ export const getReferrer = isWorker() ?
}
};

function makeDatabaseRequest(requestParameters: RequestParameters, callback: ResponseCallback<any>) {
const splitUrl = requestParameters.url.replace("mbtiles://", "").split("/");
const dbName = splitUrl[0];
const z = parseInt(splitUrl[1]);
const x = parseInt(splitUrl[2]);
const y = Math.pow(2, z) - parseInt(splitUrl[3]) - 1; // Tiles on database are tms (inverted y axis)
const params = [z, x, y];
const query = 'SELECT hex(tile_data) AS tile_data FROM tiles WHERE zoom_level=? AND tile_column=? AND tile_row=?';
const db = window.openDatabase(dbName, '1.0', dbName, 200 * 1024 * 1024);
db.transaction(function (txn) {
txn.executeSql(query, params, function (tx, res) {
if (res.rows.length) {
const tileData = res.rows.item(0).tile_data;
var rawData = new Uint8Array(tileData.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
let isGzipped = rawData[0] === 0x1f && rawData[1] === 0x8b;
if (isGzipped) {
rawData = pako.inflate(rawData);
}
callback(null, rawData.buffer, null /*'Cache-Control'*/, null /*'Expires'*/);
} else {
callback(new Error('tile ' + params.join(',') + ' not found'));
}
});
}, function (error) {
callback(error); // Error executing SQL
});
}

function makeFetchRequest(requestParameters: RequestParameters, callback: ResponseCallback<any>): Cancelable {
const controller = new window.AbortController();
const request = new window.Request(requestParameters.url, {
Expand Down Expand Up @@ -169,6 +198,12 @@ export const makeRequest = function(requestParameters: RequestParameters, callba
// some versions (see https://bugs.webkit.org/show_bug.cgi?id=174980#c2)
// - Requests for resources with the file:// URI scheme don't work with the Fetch API either. In
// this case we unconditionally use XHR on the current thread since referrers don't matter.
if (/^mbtiles:/.test(requestParameters.url) && isWorker() && self.worker && self.worker.actor) {
return self.worker.actor.send('getResource', requestParameters, callback);
}
if (/^mbtiles:/.test(requestParameters.url) && !isWorker()) {
return makeDatabaseRequest(requestParameters, callback);
}
if (!/^file:/.test(requestParameters.url)) {
if (window.fetch && window.Request && window.AbortController && window.Request.prototype.hasOwnProperty('signal')) {
return makeFetchRequest(requestParameters, callback);
Expand Down

0 comments on commit 2cb233d

Please sign in to comment.