2020-04-07 20:41:57 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
|
|
|
|
<link rel="stylesheet" href="../../css/spectre.min.css">
|
2022-01-12 13:54:21 +00:00
|
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet-geosearch@3.6.0/dist/geosearch.css"/>
|
2020-04-07 20:41:57 +00:00
|
|
|
</head>
|
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
html, body, #map {
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
#map { z-index: 1; }
|
|
|
|
#controls {
|
|
|
|
padding: 10px;
|
|
|
|
margin: 10px;
|
|
|
|
border: 1px solid black;
|
|
|
|
position:absolute;
|
|
|
|
right:0px;top:0px;
|
|
|
|
background-color: rgb(255, 255, 255);
|
|
|
|
z-index: 100;
|
|
|
|
}
|
|
|
|
#maptiles {
|
|
|
|
width: 256px;
|
|
|
|
height: 256px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="map">
|
|
|
|
</div>
|
|
|
|
<div id="controls">
|
2022-03-17 08:28:55 +00:00
|
|
|
<div style="display:inline-block;text-align:center;vertical-align: top;" id="3bitdiv"> <input type="checkbox" id="3bit"></input><br/><span>3 bit</span></div>
|
2020-04-08 20:22:51 +00:00
|
|
|
<button id="getmap" class="btn btn-primary">Get Map</button><br/>
|
|
|
|
<canvas id="maptiles" style="display:none"></canvas>
|
|
|
|
<div id="uploadbuttons" style="display:none"><button id="upload" class="btn btn-primary">Upload</button>
|
|
|
|
<button id="cancel" class="btn">Cancel</button></div>
|
2020-04-07 20:41:57 +00:00
|
|
|
</div>
|
|
|
|
|
2020-08-24 10:59:52 +00:00
|
|
|
<script src="../../core/lib/customize.js"></script>
|
2020-04-07 20:41:57 +00:00
|
|
|
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
|
|
|
|
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
|
2020-08-24 10:59:52 +00:00
|
|
|
<script src="../../core/lib/heatshrink.js"></script>
|
|
|
|
<script src="../../core/lib/imageconverter.js"></script>
|
2022-01-12 13:54:21 +00:00
|
|
|
<script src="https://unpkg.com/leaflet-geosearch@3.6.0/dist/bundle.min.js"></script>
|
2020-04-07 20:41:57 +00:00
|
|
|
|
|
|
|
<script>
|
|
|
|
/*
|
|
|
|
|
|
|
|
TODO:
|
|
|
|
|
|
|
|
* Could maybe use palettised output?
|
|
|
|
* Could potentially use a custom 16 color palette?
|
|
|
|
* Allow user to choose size of map area to be uploaded (small/med/large)
|
|
|
|
* What is faster? Storing as a compressed image and decompressing, or storing decompressed?
|
|
|
|
|
|
|
|
*/
|
2022-03-17 08:28:55 +00:00
|
|
|
var TILESIZE = 96; // Size of our tiles
|
|
|
|
var OSMTILESIZE = 256; // Size of openstreetmap tiles
|
|
|
|
var MAPSIZE = TILESIZE*5; ///< 480 - Size of map we download
|
|
|
|
var OSMTILECOUNT = 3; // how many tiles do we download in each direction (Math.floor(MAPSIZE / OSMTILESIZE)+1)
|
2020-04-08 20:34:40 +00:00
|
|
|
/* Can see possible tiles on http://leaflet-extras.github.io/leaflet-providers/preview/
|
|
|
|
However some don't allow cross-origin use */
|
|
|
|
var TILELAYER = 'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png'; // simple, high contrast
|
2021-12-03 12:04:40 +00:00
|
|
|
var PREVIEWTILELAYER = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
|
2020-04-08 20:34:40 +00:00
|
|
|
//var TILELAYER = 'http://a.tile.stamen.com/toner/{z}/{x}/{y}.png'; // black and white
|
2021-12-03 12:04:40 +00:00
|
|
|
|
2022-01-12 14:21:18 +00:00
|
|
|
// Create map and try and set the location to where the browser thinks we are
|
|
|
|
var map = L.map('map').locate({setView: true, maxZoom: 16, enableHighAccuracy:true});
|
2021-12-03 12:04:40 +00:00
|
|
|
// Tiles used for Bangle.js itself
|
|
|
|
var bangleTileLayer = L.tileLayer(TILELAYER, {
|
|
|
|
maxZoom: 18,
|
|
|
|
attribution: 'Map data © <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors</a>'
|
|
|
|
});
|
|
|
|
// Tiles used for the may the user sees (faster)
|
|
|
|
var previewTileLayer = L.tileLayer(PREVIEWTILELAYER, {
|
2020-04-07 20:41:57 +00:00
|
|
|
maxZoom: 18,
|
|
|
|
attribution: 'Map data © <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors</a>'
|
|
|
|
});
|
2020-04-08 20:34:40 +00:00
|
|
|
// Could optionally overlay trails: https://wiki.openstreetmap.org/wiki/Tiles
|
|
|
|
|
2022-03-17 08:28:55 +00:00
|
|
|
// Search box:
|
|
|
|
const searchProvider = new window.GeoSearch.OpenStreetMapProvider();
|
|
|
|
const searchControl = new GeoSearch.GeoSearchControl({
|
|
|
|
provider: searchProvider,
|
|
|
|
style: 'button',
|
|
|
|
updateMap: true,
|
|
|
|
autoClose: true,
|
|
|
|
showMarker: false,
|
|
|
|
keepResult: true,
|
|
|
|
autoComplete: false
|
|
|
|
});
|
|
|
|
map.addControl(searchControl);
|
|
|
|
|
|
|
|
function onInit(device) {
|
|
|
|
if (device && device.info && device.info.g) {
|
|
|
|
// On 3 bit devices, don't even offer the option. 3 bit is the only way
|
|
|
|
if (device.info.g.bpp==3) {
|
|
|
|
document.getElementById("3bit").checked = true;
|
|
|
|
document.getElementById("3bitdiv").style = "display:none";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:22:51 +00:00
|
|
|
var mapFiles = [];
|
2021-12-03 12:04:40 +00:00
|
|
|
previewTileLayer.addTo(map);
|
2020-04-07 20:41:57 +00:00
|
|
|
|
|
|
|
function tilesLoaded(ctx, width, height) {
|
2022-03-17 08:28:55 +00:00
|
|
|
var options = {
|
|
|
|
compression:false, output:"raw",
|
|
|
|
mode:"web"
|
|
|
|
};
|
2021-06-11 10:34:05 +00:00
|
|
|
if (document.getElementById("3bit").checked) {
|
|
|
|
options = {
|
2021-06-21 12:46:16 +00:00
|
|
|
compression:false, output:"raw",
|
2022-01-19 15:13:13 +00:00
|
|
|
mode:"3bit",
|
2021-06-11 10:34:05 +00:00
|
|
|
};
|
2022-01-19 15:13:13 +00:00
|
|
|
/* If in 3 bit mode, go through all the data beforehand and
|
|
|
|
turn the saturation up to maximum, so when thresholded it
|
|
|
|
works a lot better */
|
|
|
|
var imageData = ctx.getImageData(0,0,width,height);
|
|
|
|
var rgba = imageData.data;
|
|
|
|
var l = width*height*4;
|
|
|
|
for (var i=0;i<l;i+=4) {
|
|
|
|
var min = Math.min(rgba[i+0],rgba[i+1],rgba[i+2]);
|
|
|
|
var max = Math.max(rgba[i+0],rgba[i+1],rgba[i+2]);
|
|
|
|
var d = max-min;
|
|
|
|
if (max<120) { // black
|
|
|
|
rgba[i+0]=0;
|
|
|
|
rgba[i+1]=0;
|
|
|
|
rgba[i+2]=0;
|
|
|
|
} else if (min>240 || d<8) { // white or grey
|
|
|
|
rgba[i+0]=255;
|
|
|
|
rgba[i+1]=255;
|
|
|
|
rgba[i+2]=255;
|
|
|
|
} else { // another colour - use max saturation
|
|
|
|
rgba[i+0] = (rgba[i+0]-min) * 255 / d;
|
|
|
|
rgba[i+1] = (rgba[i+1]-min) * 255 / d;
|
|
|
|
rgba[i+2] = (rgba[i+2]-min) * 255 / d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.putImageData(imageData,0,0);
|
2021-06-11 10:34:05 +00:00
|
|
|
}
|
|
|
|
console.log("Compression options", options);
|
2020-04-07 20:41:57 +00:00
|
|
|
var w = Math.round(width / TILESIZE);
|
|
|
|
var h = Math.round(height / TILESIZE);
|
|
|
|
var tiles = [];
|
|
|
|
for (var y=0;y<h;y++) {
|
|
|
|
for (var x=0;x<w;x++) {
|
|
|
|
var imageData = ctx.getImageData(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE);
|
|
|
|
var rgba = imageData.data;
|
|
|
|
options.rgbaOut = rgba;
|
|
|
|
options.width = TILESIZE;
|
|
|
|
options.height = TILESIZE;
|
|
|
|
var imgstr = imageconverter.RGBAtoString(rgba, options);
|
2022-01-19 15:13:13 +00:00
|
|
|
ctx.putImageData(imageData,x*TILESIZE, y*TILESIZE); // write preview
|
2020-04-07 20:41:57 +00:00
|
|
|
/*var compress = 'require("heatshrink").decompress('
|
|
|
|
if (!imgstr.startsWith(compress)) throw "Data in wrong format";
|
|
|
|
imgstr = imgstr.slice(compress.length,-1);*/
|
|
|
|
tiles.push({
|
|
|
|
name:"openstmap-"+x+"-"+y+".img",
|
2022-03-17 08:28:55 +00:00
|
|
|
content:imgstr
|
2020-04-07 20:41:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tiles;
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:22:51 +00:00
|
|
|
document.getElementById("getmap").addEventListener("click", function() {
|
2020-04-07 20:41:57 +00:00
|
|
|
var zoom = map.getZoom();
|
2021-12-03 12:04:40 +00:00
|
|
|
var centerlatlon = map.getBounds().getCenter();
|
|
|
|
var center = map.project(centerlatlon, zoom).divideBy(OSMTILESIZE);
|
2021-12-06 15:35:24 +00:00
|
|
|
// Reason for 16px adjustment below not 100% known, but it seems to
|
|
|
|
// align everything perfectly: https://github.com/espruino/BangleApps/issues/984
|
|
|
|
var ox = Math.round((center.x - Math.floor(center.x)) * OSMTILESIZE) + 16;
|
|
|
|
var oy = Math.round((center.y - Math.floor(center.y)) * OSMTILESIZE) + 16;
|
2021-12-03 12:04:40 +00:00
|
|
|
center = center.floor(); // make sure we're in the middle of a tile
|
|
|
|
// JS version of Bangle.js's projection
|
|
|
|
function bproject(lat, lon) {
|
|
|
|
const degToRad = Math.PI / 180; // degree to radian conversion
|
|
|
|
const latMax = 85.0511287798; // clip latitude to sane values
|
|
|
|
const R = 6378137; // earth radius in m
|
|
|
|
if (lat > latMax) lat=latMax;
|
|
|
|
if (lat < -latMax) lat=-latMax;
|
|
|
|
var s = Math.sin(lat * degToRad);
|
|
|
|
return new L.Point(
|
|
|
|
(R * lon * degToRad),
|
|
|
|
(R * Math.log((1 + s) / (1 - s)) / 2)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Work out scale factors (how much from Bangle.project does one pixel equate to?)
|
|
|
|
var pc = map.unproject(center.multiplyBy(OSMTILESIZE), zoom);
|
|
|
|
var pd = map.unproject(center.multiplyBy(OSMTILESIZE).add({x:1,y:0}), zoom);
|
|
|
|
var bc = bproject(pc.lat, pc.lng)
|
|
|
|
var bd = bproject(pd.lat, pd.lng)
|
|
|
|
var scale = bc.distanceTo(bd);
|
2020-04-07 20:41:57 +00:00
|
|
|
|
2021-12-06 15:35:24 +00:00
|
|
|
// test
|
|
|
|
/*var p = bproject(centerlatlon.lat, centerlatlon.lng);
|
|
|
|
var q = bproject(mylat, mylon);
|
|
|
|
var testPt = {
|
|
|
|
x : (q.x-p.x)/scale + (MAPSIZE/2),
|
|
|
|
y : (MAPSIZE/2) - (q.y-p.y)/scale
|
|
|
|
};*/
|
2020-04-07 20:41:57 +00:00
|
|
|
|
2021-12-06 15:35:24 +00:00
|
|
|
var tileGetters = [];
|
2021-12-03 12:04:40 +00:00
|
|
|
// Render everything to a canvas...
|
2020-04-07 20:41:57 +00:00
|
|
|
var canvas = document.getElementById("maptiles");
|
2020-04-08 20:22:51 +00:00
|
|
|
canvas.style.display="";
|
2020-04-07 20:41:57 +00:00
|
|
|
var ctx = canvas.getContext('2d');
|
2022-03-17 08:28:55 +00:00
|
|
|
canvas.width = MAPSIZE;
|
|
|
|
canvas.height = MAPSIZE;
|
|
|
|
for (var i = 0; i < OSMTILECOUNT; i++) {
|
|
|
|
for (var j = 0; j < OSMTILECOUNT; j++) {
|
2020-04-07 20:41:57 +00:00
|
|
|
(function(i,j){
|
|
|
|
var coords = new L.Point(center.x+i-1, center.y+j-1);
|
|
|
|
coords.z = zoom;
|
|
|
|
var img = new Image();
|
|
|
|
img.crossOrigin = "Anonymous";
|
|
|
|
tileGetters.push(new Promise(function(resolve,reject) {
|
|
|
|
img.onload = function(){
|
|
|
|
ctx.drawImage(img,i*OSMTILESIZE - ox, j*OSMTILESIZE - oy);
|
2021-12-06 15:35:24 +00:00
|
|
|
/*if (testPt) {
|
|
|
|
ctx.fillStyle="green";
|
|
|
|
ctx.fillRect(testPt.x-1, testPt.y-5, 3,10);
|
|
|
|
ctx.fillRect(testPt.x-5, testPt.y-1, 10,3);
|
|
|
|
}*/
|
2020-04-07 20:41:57 +00:00
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
}));
|
2021-12-03 12:04:40 +00:00
|
|
|
bangleTileLayer._tileZoom = previewTileLayer._tileZoom;
|
|
|
|
img.src = bangleTileLayer.getTileUrl(coords);
|
2020-04-07 20:41:57 +00:00
|
|
|
})(i,j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Promise.all(tileGetters).then(() => {
|
2020-04-08 20:22:51 +00:00
|
|
|
document.getElementById("uploadbuttons").style.display="";
|
|
|
|
mapFiles = tilesLoaded(ctx, canvas.width, canvas.height);
|
|
|
|
mapFiles.unshift({name:"openstmap.json",content:JSON.stringify({
|
2020-04-07 20:41:57 +00:00
|
|
|
imgx : canvas.width,
|
|
|
|
imgy : canvas.height,
|
|
|
|
tilesize : TILESIZE,
|
2021-12-03 12:04:40 +00:00
|
|
|
scale : scale, // how much of Bangle.project(latlon) does one pixel equate to?
|
2020-04-07 20:41:57 +00:00
|
|
|
lat : centerlatlon.lat,
|
|
|
|
lon : centerlatlon.lng
|
|
|
|
})});
|
2020-04-08 20:22:51 +00:00
|
|
|
console.log(mapFiles);
|
2020-04-07 20:41:57 +00:00
|
|
|
});
|
2020-04-08 20:22:51 +00:00
|
|
|
});
|
2020-04-07 20:41:57 +00:00
|
|
|
|
2020-04-08 20:22:51 +00:00
|
|
|
document.getElementById("upload").addEventListener("click", function() {
|
|
|
|
sendCustomizedApp({
|
|
|
|
storage:mapFiles
|
|
|
|
});
|
|
|
|
});
|
2020-04-07 20:41:57 +00:00
|
|
|
|
2020-04-08 20:22:51 +00:00
|
|
|
document.getElementById("cancel").addEventListener("click", function() {
|
|
|
|
document.getElementById("maptiles").style.display="none";
|
|
|
|
document.getElementById("uploadbuttons").style.display="none";
|
2020-04-07 20:41:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|