// ==UserScript== // @id iitc-plugin-s2-cells@vib // @name IITC plugin: Show Configurable S2 Cells // @author vib+ Dragonsangel+nikolawannabe // @category Layer // @version 0.1.11 // @namespace https://github.com/nikolawannabe/s2-cells // @updateURL https://raw.githubusercontent.com/nikolawannabe/s2-cells/master/s2-cells.meta.js // @downloadURL https://github.com/nikolawannabe/s2-cells/raw/master/s2-cells.user.js // @description IITC: Shows configurable S2 level cells on the map // @include https://*.ingress.com/intel* // @include http://*.ingress.com/intel* // @match https://*.ingress.com/intel* // @match http://*.ingress.com/intel* // @include https://*.ingress.com/mission/* // @include http://*.ingress.com/mission/* // @match https://*.ingress.com/mission/* // @match http://*.ingress.com/mission/* // @grant none // ==/UserScript== // This plugin is a simple fork of the Regions plugin by Jonatkins // // original plugin at: // https://github.com/jonatkins/ingress-intel-total-conversion function wrapper(plugin_info) { // ensure plugin framework is there, even if iitc is not yet loaded if (typeof window.plugin !== 'function') window.plugin = function() {}; //PLUGIN AUTHORS: writing a plugin outside of the IITC build environment? if so, delete these lines!! //(leaving them in place might break the 'About IITC' page or break update checks) plugin_info.buildName = 's2-cells'; plugin_info.dateTimeVersion = '20180504.121800'; plugin_info.pluginId = 's2-cells'; //END PLUGIN AUTHORS NOTE // PLUGIN START //////////////////////////////////////////////////////// // http://jsbeautifier.org/ // use own namespace for plugin window.plugin.showcells = function() {}; // SET THIS TO TRUE WHILE DEBUGGING window.plugin.showcells.debug = false; window.plugin.showcells.storage = { lightCell: 17, darkCell: 14, lightColor: '#f5fffa', darkColor: '#3cb371', lightWidth: 1, darkWidth: 8}; window.plugin.showcells.storageKey = 'showcells-storage'; // update the localStorage datas window.plugin.showcells.saveStorage = function() { localStorage[window.plugin.showcells.storageKey] = JSON.stringify(window.plugin.showcells.storage); }; // load the localStorage datas window.plugin.showcells.loadStorage = function() { if (typeof localStorage[window.plugin.showcells.storageKey] != "undefined") { window.plugin.showcells.storage = JSON.parse(localStorage[window.plugin.showcells.storageKey]); } }; window.plugin.showcells.setCellLevel = function() { window.plugin.showcells.loadStorage(); var lightCell = window.plugin.showcells.storage.lightCell; var darkCell = window.plugin.showcells.storage.darkCell; var lightColor = window.plugin.showcells.storage.lightColor; var darkColor = window.plugin.showcells.storage.darkColor; var lightWidth = window.plugin.showcells.storage.lightWidth; var darkWidth = window.plugin.showcells.storage.darkWidth; if (lightCell == isNaN || darkCell == isNaN) { window.plugin.showcells.storage.lightCell = 17; lightCell = 17; window.plugin.showcells.storage.darkCell = 14; darkCell = 14; window.plugin.showcells.saveStorage(); } if (lightWidth == isNaN || darkWidth == isNaN) { window.plugin.showcells.storage.lightWidth = 1; lightWidth = 1; window.plugin.showcells.storage.darkWidth = 8; darkWidth = 8; window.plugin.showcells.saveStorage(); } var dialogHtml = "
" + "Inner Cells
" + "Color: " + "Width: " + "
" + "Outer Cells
" + "Color: " + "Width: " + "
" + "
Note that if your choices would cause too many cells to be rendered, we will try not to display them.
" + "
See the Cell Guidelines " + "for tips on what these numbers can be used for.
" ; var d = dialog({ title: "Set Cell Levels", html: dialogHtml, width:'auto', buttons:{ 'Reset to Defaults': function() { window.plugin.showcells.storage = { lightCell: 17, darkCell: 14, lightColor: '#f5fffa', darkColor: '#3cb371', lightWidth: 1, darkWidth: 8}; window.plugin.showcells.saveStorage(); window.plugin.showcells.update(); return; }, 'Save': function() { var darkCell = parseInt($("#dark-cell").val(), 10); var lightCell = parseInt($("#light-cell").val(), 10); var darkColor = $("#dark-color").val(); var lightColor = $("#light-color").val(); var lightWidth = parseInt($("#light-width").val(), 10); var darkWidth = parseInt($("#dark-width").val(), 10); console.log("light color: " + lightColor); console.log("dark color: " + darkColor); if (lightCell !== isNaN && darkCell !== isNaN && lightCell >= 2 && lightCell < 21 && darkCell >= 2 && darkCell < 21) { window.plugin.showcells.storage.darkCell = darkCell; window.plugin.showcells.storage.lightCell = lightCell; window.plugin.showcells.storage.lightColor = lightColor; window.plugin.showcells.storage.darkColor = darkColor; window.plugin.showcells.storage.lightWidth = lightWidth; window.plugin.showcells.storage.darkWidth = darkWidth; window.plugin.showcells.saveStorage(); window.plugin.showcells.update(); } else { alert("Invalid value(s). Cell levels must be numbers between 2 and 20"); } return; } } }); }; window.plugin.showcells.setup = function() { window.plugin.showcells.loadStorage(); // Add a link to the Toolbox to change S2 Level instead of a button $('#toolbox').append(' Change S2 Level'); /// S2 Geometry functions // the regional scoreboard is based on a level 6 S2 Cell // - https://docs.google.com/presentation/d/1Hl4KapfAENAOf4gv-pSngKwvS_jwNVHRPZTTDzXXn6Q/view?pli=1#slide=id.i22 // at the time of writing there's no actual API for the intel map to retrieve scoreboard data, // but it's still useful to plot the score cells on the intel map // the S2 geometry is based on projecting the earth sphere onto a cube, with some scaling of face coordinates to // keep things close to approximate equal area for adjacent cells // to convert a lat,lng into a cell id: // - convert lat,lng to x,y,z // - convert x,y,z into face,u,v // - u,v scaled to s,t with quadratic formula // - s,t converted to integer i,j offsets // - i,j converted to a position along a Hubbert space-filling curve // - combine face,position to get the cell id //NOTE: compared to the google S2 geometry library, we vary from their code in the following ways // - cell IDs: they combine face and the hilbert curve position into a single 64 bit number. this gives efficient space // and speed. javascript doesn't have appropriate data types, and speed is not cricical, so we use // as [face,[bitpair,bitpair,...]] instead // - i,j: they always use 30 bits, adjusting as needed. we use 0 to (1< temp[1]) { if (temp[0] > temp[2]) { return 0; } else { return 2; } } else { if (temp[1] > temp[2]) { return 1; } else { return 2; } } }; var faceXYZToUV = function(face, xyz) { var u, v; switch (face) { case 0: u = xyz[1] / xyz[0]; v = xyz[2] / xyz[0]; break; case 1: u = -xyz[0] / xyz[1]; v = xyz[2] / xyz[1]; break; case 2: u = -xyz[0] / xyz[2]; v = -xyz[1] / xyz[2]; break; case 3: u = xyz[2] / xyz[0]; v = xyz[1] / xyz[0]; break; case 4: u = xyz[2] / xyz[1]; v = -xyz[0] / xyz[1]; break; case 5: u = -xyz[1] / xyz[2]; v = -xyz[0] / xyz[2]; break; default: throw { error: 'Invalid face' }; break; } return [u, v]; } var XYZToFaceUV = function(xyz) { var face = largestAbsComponent(xyz); if (xyz[face] < 0) { face += 3; } uv = faceXYZToUV(face, xyz); return [face, uv]; }; var FaceUVToXYZ = function(face, uv) { var u = uv[0]; var v = uv[1]; switch (face) { case 0: return [1, u, v]; case 1: return [-u, 1, v]; case 2: return [-u, -v, 1]; case 3: return [-1, -v, -u]; case 4: return [v, -1, -u]; case 5: return [v, u, -1]; default: throw { error: 'Invalid face' }; }; }; var STToUV = function(st) { var singleSTtoUV = function(st) { if (st >= 0.5) { return (1 / 3.0) * (4 * st * st - 1); } else { return (1 / 3.0) * (1 - (4 * (1 - st) * (1 - st))); } }; return [singleSTtoUV(st[0]), singleSTtoUV(st[1])]; }; var UVToST = function(uv) { var singleUVtoST = function(uv) { if (uv >= 0) { return 0.5 * Math.sqrt(1 + 3 * uv); } else { return 1 - 0.5 * Math.sqrt(1 - 3 * uv); } }; return [singleUVtoST(uv[0]), singleUVtoST(uv[1])]; }; var STToIJ = function(st, order) { var maxSize = (1 << order); var singleSTtoIJ = function(st) { var ij = Math.floor(st * maxSize); return Math.max(0, Math.min(maxSize - 1, ij)); }; return [singleSTtoIJ(st[0]), singleSTtoIJ(st[1])]; }; var IJToST = function(ij, order, offsets) { var maxSize = (1 << order); return [ (ij[0] + offsets[0]) / maxSize, (ij[1] + offsets[1]) / maxSize ]; }; // hilbert space-filling curve // based on http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves // note: rather then calculating the final integer hilbert position, we just return the list of quads // this ensures no precision issues whth large orders (S3 cell IDs use up to 30), and is more // convenient for pulling out the individual bits as needed later var pointToHilbertQuadList = function(x, y, order) { var hilbertMap = { 'a': [ [0, 'd'], [1, 'a'], [3, 'b'], [2, 'a'] ], 'b': [ [2, 'b'], [1, 'b'], [3, 'a'], [0, 'c'] ], 'c': [ [2, 'c'], [3, 'd'], [1, 'c'], [0, 'b'] ], 'd': [ [0, 'a'], [3, 'c'], [1, 'd'], [2, 'd'] ] }; var currentSquare = 'a'; var positions = []; for (var i = order - 1; i >= 0; i--) { var mask = 1 << i; var quad_x = x & mask ? 1 : 0; var quad_y = y & mask ? 1 : 0; var t = hilbertMap[currentSquare][quad_x * 2 + quad_y]; positions.push(t[0]); currentSquare = t[1]; } return positions; }; // S2Cell class S2.S2Cell = function() {}; //static method to construct S2.S2Cell.FromLatLng = function(latLng, level) { var xyz = LatLngToXYZ(latLng); var faceuv = XYZToFaceUV(xyz); var st = UVToST(faceuv[1]); var ij = STToIJ(st, level); return S2.S2Cell.FromFaceIJ(faceuv[0], ij, level); }; S2.S2Cell.FromFaceIJ = function(face, ij, level) { var cell = new S2.S2Cell(); cell.face = face; cell.ij = ij; cell.level = level; return cell; }; S2.S2Cell.prototype.toString = function() { return 'F' + this.face + 'ij[' + this.ij[0] + ',' + this.ij[1] + ']@' + this.level; }; S2.S2Cell.prototype.getLatLng = function() { var st = IJToST(this.ij, this.level, [0.5, 0.5]); var uv = STToUV(st); var xyz = FaceUVToXYZ(this.face, uv); return XYZToLatLng(xyz); }; S2.S2Cell.prototype.getCornerLatLngs = function() { var result = []; var offsets = [ [0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0] ]; for (var i = 0; i < 4; i++) { var st = IJToST(this.ij, this.level, offsets[i]); var uv = STToUV(st); var xyz = FaceUVToXYZ(this.face, uv); result.push(XYZToLatLng(xyz)); } return result; }; S2.S2Cell.prototype.getFaceAndQuads = function() { var quads = pointToHilbertQuadList(this.ij[0], this.ij[1], this.level); return [this.face, quads]; }; S2.S2Cell.prototype.getNeighbors = function() { var fromFaceIJWrap = function(face, ij, level) { var maxSize = (1 << level); if (ij[0] >= 0 && ij[1] >= 0 && ij[0] < maxSize && ij[1] < maxSize) { // no wrapping out of bounds return S2.S2Cell.FromFaceIJ(face, ij, level); } else { // the new i,j are out of range. // with the assumption that they're only a little past the borders we can just take the points as // just beyond the cube face, project to XYZ, then re-create FaceUV from the XYZ vector var st = IJToST(ij, level, [0.5, 0.5]); var uv = STToUV(st); var xyz = FaceUVToXYZ(face, uv); var faceuv = XYZToFaceUV(xyz); face = faceuv[0]; uv = faceuv[1]; st = UVToST(uv); ij = STToIJ(st, level); return S2.S2Cell.FromFaceIJ(face, ij, level); } }; var face = this.face; var i = this.ij[0]; var j = this.ij[1]; var level = this.level; return [ fromFaceIJWrap(face, [i - 1, j], level), fromFaceIJWrap(face, [i, j - 1], level), fromFaceIJWrap(face, [i + 1, j], level), fromFaceIJWrap(face, [i, j + 1], level) ]; }; })(); window.plugin.showcells.regionLayer = L.layerGroup(); $("