Created
January 8, 2018 20:38
-
-
Save MzHub/83851fbbce2e56bfbd121c32fa5299ae to your computer and use it in GitHub Desktop.
An IITC plugin that displays portal counts for different s2 cell levels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @id iitc-plugin-s2-portal-counts@MzHub | |
// @name IITC plugin: s2 Portal Counts | |
// @category Portal Info | |
// @version 0.1.0 | |
// @namespace https://github.com/jonatkins/ingress-intel-total-conversion | |
// @description Displays portal counts for different s2 cell levels | |
// @include https://www.ingress.com/intel* | |
// @include http://www.ingress.com/intel* | |
// @match https://www.ingress.com/intel* | |
// @match http://www.ingress.com/intel* | |
// @grant none | |
// ==/UserScript== | |
function wrapper() { | |
// Ensure plugin framework exists, even if IITC is not yet loaded | |
if (typeof window.plugin !== 'function') { | |
window.plugin = function() {}; | |
} | |
window.plugin.s2PortalCounts = function() {}; | |
window.plugin.s2PortalCounts.setup = setup; | |
if (window.iitcLoaded && typeof window.plugin.s2PortalCounts.setup === 'function') { | |
window.plugin.s2PortalCounts.setup(); | |
} | |
else if (window.bootPlugins) { | |
window.bootPlugins.push(window.plugin.s2PortalCounts.setup); | |
} | |
else { | |
window.bootPlugins = [window.plugin.s2PortalCounts.setup]; | |
} | |
function setup() { | |
var scriptEl; | |
scriptEl = document.createElement('script'); | |
scriptEl.src = 'https://cdn.rawgit.com/dcodeIO/long.js/master/dist/long.min.js'; | |
document.body.appendChild(scriptEl); | |
scriptEl = document.createElement('script'); | |
scriptEl.src = 'https://cdn.rawgit.com/hunterjm/s2-geometry.js/0cc25036/src/s2geometry.js'; | |
document.body.appendChild(scriptEl); | |
var maxLevel = 20; | |
var minLevel = 13; | |
var isActive = false; | |
var polygons = []; | |
map.on('click', handleMapClick); | |
function S2CellId_lsb(idAsLong) { | |
return idAsLong.and(idAsLong.negate()); | |
} | |
function S2CellId_RangeMin(idAsLong) { | |
return idAsLong.sub(S2CellId_lsb(idAsLong).subtract(1)); | |
} | |
function S2CellId_RangeMax(idAsLong) { | |
return idAsLong.add(S2CellId_lsb(idAsLong).subtract(1)); | |
} | |
function S2CellId_Contains(idAsLongA, idAsLongB) { | |
return S2CellId_RangeMin(idAsLongA).lte(idAsLongB) && idAsLongB.lte(S2CellId_RangeMax(idAsLongA)); | |
} | |
function latlngToCellId(latlng, level) { | |
if (level === undefined) { | |
level = 30; | |
} | |
var idAsLong = getIdAsLongFromLatlng(latlng, level); | |
var idAsHex = idAsLong.toString(16); | |
var idTruncated = idAsHex.replace(/0+$/, ''); | |
return idTruncated; | |
} | |
function getIdAsLongFromLatlng(latlng, level) { | |
if (level === undefined) { | |
level = 30; | |
} | |
var cellKey = S2.S2Cell.latLngToKey(latlng.lat, latlng.lng, level); | |
var idAsDecimalString = S2.keyToId(cellKey); | |
var idAsLong = dcodeIO.Long.fromString(idAsDecimalString); | |
return idAsLong; | |
} | |
function getCorners(latlng, level) { | |
if (level === undefined) { | |
level = 30; | |
} | |
var cell = S2.S2Cell.FromLatLng(latlng, level); | |
var corners = cell.getCornerLatLngs(); | |
return corners; | |
} | |
function getContainedPortals(latlng, level) { | |
if (level === undefined) { | |
level = 30; | |
} | |
var containedPortals = []; | |
var idAsLong = getIdAsLongFromLatlng(latlng, level); | |
var portalCellIdAsLong; | |
var portalId; | |
var portal; | |
for (portalId in portals) { | |
portal = portals[portalId]; | |
if (portal.cellIdAsLong === undefined) { | |
portal.cellIdAsLong = getIdAsLongFromLatlng(portal._latlng); | |
} | |
if (S2CellId_Contains(idAsLong, portal.cellIdAsLong)) { | |
containedPortals.push(portal); | |
} | |
} | |
return containedPortals; | |
} | |
function handleMapClick(e) { | |
var i; | |
var len; | |
var level; | |
var polygon; | |
if (isActive) { | |
len = polygons.length; | |
for (i = 0; i < len; i++) { | |
map.removeLayer(polygons[i]); | |
} | |
isActive = false; | |
return; | |
} | |
isActive = true; | |
for (level = minLevel; level <= maxLevel; level++) { | |
polygon = L.polygon(getCorners(e.latlng, level), { | |
color: 'red', | |
weight: level == 14 || level == 15 || level == 17 ? 3 : 1, | |
opacity: 1, | |
fillOpacity: 0.05, | |
}).addTo(map); | |
polygon._origLatlng = e.latlng; | |
polygon._origLevel = level; | |
polygon.on('click', handlePolygonClick); | |
polygon.bindPopup('Level ' + level); | |
polygons.push(polygon); | |
} | |
} | |
function handlePolygonClick() { | |
var containedPortalCount = getContainedPortals(this._origLatlng, this._origLevel).length; | |
this.bindPopup('Level ' + this._origLevel + ' cell<br>Portals: ' + containedPortalCount); | |
} | |
} | |
} | |
// Inject plugin into page | |
var script = document.createElement('script'); | |
script.appendChild(document.createTextNode('(' + wrapper + ')();')); | |
(document.body || document.head || document.documentElement).appendChild(script); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 41:
scriptEl.src = 'https://cdn.rawgit.com/dcodeIO/long.js/master/dist/long.js';