Skip to content

Commit

Permalink
git subrepo pull common
Browse files Browse the repository at this point in the history
subrepo:
  subdir:   "common"
  merged:   "3e799e5"
upstream:
  origin:   "git@github.com:interactive-game-maps/common.git"
  branch:   "master"
  commit:   "3e799e5"
git-subrepo:
  version:  "0.4.9"
  origin:   "???"
  commit:   "???"
  • Loading branch information
interactive-game-maps committed Oct 4, 2024
1 parent 9b65b6f commit 79741b6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
6 changes: 3 additions & 3 deletions common/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[subrepo]
remote = git@github.com:interactive-game-maps/common.git
branch = master
commit = 6803ba27211901a896a5496679f62f3410a1e6be
parent = 128fbf2ba44392a7f4d3254c034e6c7537d77ee6
commit = 3e799e5362c40a0297b779f71f98e3436c14c569
parent = 9b65b6fb469e6302e990c771d818ff56236bddb6
method = merge
cmdver = 0.4.3
cmdver = 0.4.9
52 changes: 51 additions & 1 deletion common/interactive_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ class InteractiveMap {
minNativeZoom: 3,
maxNativeZoom: 5,
noWrap: true,
detectRetina: true
detectRetina: true,
bounds: this.#getTileLayerBounds(url),
}

console.log(defaults.bounds)

let params = { ...defaults, ...args };
params.maxNativeZoom = L.Browser.retina ? params.maxNativeZoom - 1 : params.maxNativeZoom; // 1 level LOWER for high pixel ratio device.

Expand Down Expand Up @@ -524,4 +528,50 @@ class InteractiveMap {

return interactive_layer;
}

/**
* Tries to read an adjacent tilemapresource.xml and calculate the bounds for this tile layer.
*
* Falls back to 256.
*
* @param {string} url Location of the tiles
*/
#getTileLayerBounds(url) {
if (window.location.protocol !== 'file:') {
// This request has to be synchronous because we can't set the tile layer bounds after initialization
const request = new XMLHttpRequest();
request.open("GET", url.replace("{z}/{x}/{y}.png", "tilemapresource.xml"), false); // `false` makes the request synchronous
request.send(null);

if (request.status === 200) {
try {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(request.responseText, "text/xml");
const boundingBox = xmlDoc.getElementsByTagName("BoundingBox")[0];
const reducedBounds = this.#reduceTileSizeBelow256(Math.abs(boundingBox.getAttribute("miny")), Math.abs(boundingBox.getAttribute("maxx")));

return L.latLngBounds(L.latLng(0, 0), L.latLng(-reducedBounds[0], reducedBounds[1]));
} catch {
console.log("Failed reading tilemapresource.xml");
}
}
}

return L.latLngBounds(L.latLng(0, 0), L.latLng(-256, 256)); // gdal2tiles.py never produces tiles larger than 256
}

/**
* Takes a two numbers and halfs them simultaneously until both are smaller than 256.
* @param {number} size1 Number to minify
* @param {number} size2 Number to minify similarly
* @returns [number, number]
*/
#reduceTileSizeBelow256(size1, size2) {
while (size1 > 256 || size2 > 256) {
size1 = Math.floor(size1 / 2);
size2 = Math.floor(size2 / 2);
}

return [size1, size2];
}
}

0 comments on commit 79741b6

Please sign in to comment.