Skip to content

Instantly share code, notes, and snippets.

@IamTechknow
Created July 23, 2017 06:05
Show Gist options
  • Save IamTechknow/7677d6fe57df534f2b816e034c7bee31 to your computer and use it in GitHub Desktop.
Save IamTechknow/7677d6fe57df534f2b816e034c7bee31 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Count Polygon Portals
// @namespace https://github.com/jonatkins/ingress-intel-total-conversion
// @version 1.0
// @description Count portals inside polygons in Ingress
// @author iamtechknow
// @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(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};
// PLUGIN START ////////////////////////////////////////////////////////
// use own namespace for plugin
window.plugin.countPolygonPortals = {
Desc: 'Count portals inside polygons in Ingress'
};
//Helper function to determine if a coordinate is inside a polygon
//From: https://stackoverflow.com/questions/31790344/determine-if-a-point-reside-inside-a-leaflet-polygon
window.plugin.countPolygonPortals.isMarkerInsidePolygon = function (marker, poly) {
var polyPoints = poly.getLatLngs();
var x = marker.getLatLng().lat, y = marker.getLatLng().lng;
var inside = false;
for (var i = 0, j = polyPoints.length - 1; i < polyPoints.length; j = i++) {
var xi = polyPoints[i].lat, yi = polyPoints[i].lng;
var xj = polyPoints[j].lat, yj = polyPoints[j].lng;
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
//Add hook to update polygons when portals loaded
window.plugin.countPolygonPortals.setup = function() {
if (window.plugin.drawTools === undefined) {
alert("'Count Polygon Portals' requires 'draw-tools'");
return;
}
// this plugin also needs to create the draw-tools hook, in case it is initialised before draw-tools
window.pluginCreateHook('pluginDrawTools');
//Define layer group for polygon counts
window.plugin.countPolygonPortals.countLayer = L.layerGroup();
//Add CSS
$("<style>")
.prop("type", "text/css")
.html(".plugin-polygon-count {\
font-size: 14px;\
font-weight: bold;\
color: gold;\
opacity: 0.7;\
text-align: center;\
text-shadow: -1px -1px #000, 1px -1px #000, -1px 1px #000, 1px 1px #000, 0 0 2px #000; \
pointer-events: none;\
}")
.appendTo("head");
addLayerGroup('Portal Counts', window.plugin.countPolygonPortals.countLayer, true);
//Add hooks to update counts when portals loaded or polygon added
addHook('mapDataRefreshEnd', function() { window.plugin.countPolygonPortals.update(); });
addHook('pluginDrawTools', function(e) { //Update for all possible events
window.plugin.countPolygonPortals.update();
});
};
window.plugin.countPolygonPortals.update = function() {
window.plugin.countPolygonPortals.countLayer.clearLayers();
//Get all portals on screen
var displayBounds = map.getBounds();
var portals = new Array();
//Iterate thru each portal, add to array
$.each(window.portals, function(i, portal) {
// just count portals in viewport
if(!displayBounds.contains(portal.getLatLng()))
return true;
portals.push(portal);
});
//For each polygon, create label. If a portal is on a screen, add to count
window.plugin.drawTools.drawnItems.eachLayer(function(layer) {
var count = 0;
if (layer instanceof L.Polygon || layer instanceof L.geodesicPolygon) {
portals.forEach(function(item, index, array) {
//Does the portal's coordinate reside inside the portal boundary?
if(window.plugin.countPolygonPortals.isMarkerInsidePolygon(item, layer))
count++;
});
window.plugin.countPolygonPortals.countLayer.addLayer(
L.marker(layer._latlngs[0], {
icon: L.divIcon({
className: 'plugin-polygon-count',
iconAnchor: [100,5],
iconSize: [200,10],
html: count,
})
})
);
}
});
};
var setup = window.plugin.countPolygonPortals.setup;
// PLUGIN END //////////////////////////////////////////////////////////
setup.info = plugin_info; //add the script info data to the function as a property
if(!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if(window.iitcLoaded && typeof setup === 'function') setup();
} // wrapper end
// inject code into site context
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));
(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