diff --git a/js/pmtiles.js b/js/pmtiles.js index 86b9517..49ee68d 100644 --- a/js/pmtiles.js +++ b/js/pmtiles.js @@ -62,32 +62,24 @@ this.outstanding_requests = new Map() } - getLeaf = tilestr => { + getLeaf = (offset, len) => { return new Promise((resolve,reject) => { - if (this.leaves.has(tilestr)) { - resolve(this.leaves.get(tilestr)) - } else if (this.outstanding_requests.has(tilestr)) { - this.outstanding_requests.get(tilestr).push(resolve) + if (this.leaves.has(offset)) { + resolve(this.leaves.get(offset)) + } else if (this.outstanding_requests.has(offset)) { + this.outstanding_requests.get(offset).push(resolve) } else { - this.outstanding_requests.set(tilestr,[]) - this.rootdir.then(rootdir => { - if (rootdir.has(tilestr)) { - var val = rootdir.get(tilestr) - if (val[2] == 1) { // it is a directory - fetch(this.url, {headers:{'range':'bytes=' + val[0] + '-' + (val[0] + val[1]-1)}}).then(resp => { - return resp.arrayBuffer() - }).then(buf => { - var map = bytesToMap(new DataView(buf),val[1]/17) - this.leaves.set(tilestr,map) - resolve(map) - this.outstanding_requests.get(tilestr).forEach(f => { - f(map) - }) - // TODO fix duplication of multiple leaves in same directory - console.log("leaves: ", this.leaves.size) - }) - } - } + this.outstanding_requests.set(offset,[]) + fetch(this.url, {headers:{'range':'bytes=' + offset + '-' + (offset + len-1)}}).then(resp => { + return resp.arrayBuffer() + }).then(buf => { + var map = bytesToMap(new DataView(buf),len/17) + this.leaves.set(offset,map) + resolve(map) + this.outstanding_requests.get(offset).forEach(f => { + f(map) + }) + console.log("leaves: ", this.leaves.size) }) } }) @@ -103,12 +95,15 @@ var z7_tile_diff = (z - 7) var z7_tile = [7,Math.trunc(x / (1 << z7_tile_diff)), Math.trunc(y / (1 << z7_tile_diff))] var z7_tile_str = z7_tile[0] + "_" + z7_tile[1] + "_" + z7_tile[2] - return this.getLeaf(z7_tile_str).then(leafdir => { - if (leafdir.has(strid)) { - return leafdir.get(strid) - } - return null - }) + if (rootdir.has(z7_tile_str)) { + const val = rootdir.get(z7_tile_str) + return this.getLeaf(val[0],val[1]).then(leafdir => { + if (leafdir.has(strid)) { + return leafdir.get(strid) + } + return null + }) + } } } return null