Skip to content

Instantly share code, notes, and snippets.

@aavezel
Created October 8, 2016 05:49
Show Gist options
  • Save aavezel/fa0d6972d05b3aa7ab9d711ad2de18d1 to your computer and use it in GitHub Desktop.
Save aavezel/fa0d6972d05b3aa7ab9d711ad2de18d1 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @id iitc-plugin-CSV@pad
// @name IITC plugin: Ingress KML Exporter
// @category Keys
// @version 1.0.20150105.02
// @namespace https://github.com/jonatkins/ingress-intel-total-conversion
// @updateURL https://drive.google.com/open?id=0By5o6ue8fgYWYW0yXzlkYjZsZXc&authuser=0
// @downloadURL https://drive.google.com/open?id=0By5o6ue8fgYWYW0yXzlkYjZsZXc&authuser=0
// @description Exports portals currently in view for use with Google Map ( KML Format ).
// @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() {
// in case IITC is not available yet, define the base plugin object
if (typeof window.plugin !== "function") {
window.plugin = function() {};
}
// base context for plugin
window.plugin.ingressKMLexporter = function() {};
var self = window.plugin.ingressKMLexporter;
// custom dialog wrapper with more flexibility
self.portalInScreen = function portalInScreen(p) {
return map.getBounds().contains(p.getLatLng());
};
// adapted from
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/is-point-in-poly [rev. #0]
self.portalInPolygon = function portalInPolygon(polygon, portal) {
var poly = polygon.getLatLngs();
var pt = portal.getLatLng();
var c = false;
for (var i = -1, l = poly.length, j = l - 1; ++i < l; j = i) {
((poly[i].lat <= pt.lat && pt.lat < poly[j].lat) || (poly[j].lat <= pt.lat && pt.lat < poly[i].lat)) && (pt.lng < (poly[j].lng - poly[i].lng) * (pt.lat - poly[i].lat) / (poly[j].lat - poly[i].lat) + poly[i].lng) && (c = !c);
}
return c;
};
// return if the portal is within the drawtool objects.
// Polygon and circles are available, and circles are implemented
// as round polygons.
self.portalInForm = function(layer) {
if (layer instanceof L.Rectangle) {
return true;
}
if (layer instanceof L.Circle) {
return true;
}
return false;
};
self.portalInGeo = function(layer) {
if (layer instanceof L.GeodesicPolygon) {
return true;
}
if (layer instanceof L.GeodesicCircle) {
return true;
}
return false;
};
self.portalInDrawnItems = function(portal) {
var c = false;
window.plugin.drawTools.drawnItems.eachLayer(function(layer) {
if (!(self.portalInForm(layer) || self.portalInGeo(layer))) {
return false;
}
if (self.portalInPolygon(layer, portal)) {
c = true;
}
});
return c;
};
self.inBounds = function(portal) {
if (window.plugin.drawTools && window.plugin.drawTools.drawnItems.getLayers().length) {
return self.portalInDrawnItems(portal);
} else {
return self.portalInScreen(portal);
}
};
self.gen = function gen() {
var o = [];
var string = "Portal selection based on screen boundaries.";
o.push('<?xml version="1.0" encoding="UTF-8"?>');
o.push('<kml xmlns="http://www.opengis.net/kml/2.2">');
o.push(' <Document>');
o.push(' <name>Import from IITC</name>');
for (var x in window.portals) {
var p = window.portals[x];
if (self.inBounds(p)) {
o.push(' <Placemark>');
o.push(' <styleUrl>#cercle</styleUrl>');
o.push(' <name>' + p.options.data.title.replace(/\"/g, "\"\"") + '</name>');
o.push(' <ExtendedData>');
o.push(' </ExtendedData>');
o.push(' <Point>');
o.push(' <coordinates>' + p._latlng.lng + "," + p._latlng.lat + ',0.0</coordinates>');
o.push(' </Point>');
o.push(' </Placemark>');
}
}
o.push(' <Style id=\'cercle\'>');
o.push(' <IconStyle>');
o.push(' <color>ffF08641</color>');
o.push(' <scale>1</scale>');
o.push(' <Icon>');
o.push(' <href>http://www.gstatic.com/mapspro/images/stock/959-wht-circle-blank.png</href>');
o.push(' </Icon>');
o.push(' </IconStyle>');
o.push(' </Style>');
o.push(' </Document>');
o.push('</kml>');
var dialog = window.dialog({
title: "Ingress KML Exporter",
// body must be wrapped in an outer tag (e.g. <div>content</div>)
html: '<span>Save the data below to a KML file and import it on <code> https://www.google.com/maps/d </code>.</span><textarea id="idKMLexporter" rows="30" style="width: 100%;"></textarea>'
}).parent();
$(".ui-dialog-buttonpane", dialog).remove();
dialog.css("width", "600px")
.css("top", ($(window).height() - dialog.height()) / 2)
.css("left", ($(window).width() - dialog.width()) / 2);
$("#idKMLexporter").val(o.join("\n"));
return dialog;
};
// setup function called by IITC
self.setup = function init() {
// add controls to toolbox
var link = $("<a onclick=\"window.plugin.ingressKMLexporter.gen();\" title=\"Generate KML list of portals and locations.\">KML Export</a>");
$("#toolbox").append(link);
// delete setup to ensure init can't be run again
delete self.setup;
};
// IITC plugin setup
if (window.iitcLoaded && typeof self.setup === "function") {
self.setup();
} else if (window.bootPlugins) {
window.bootPlugins.push(self.setup);
} else {
window.bootPlugins = [self.setup];
}
}
// 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