No longer depend on polycounts2

This commit is contained in:
cyi1341 2023-09-07 23:17:51 +08:00
parent 270876fa48
commit aa60d60fd5
1 changed files with 88 additions and 5 deletions

View File

@ -20,6 +20,20 @@ function wrapper(plugin_info) {
let startTime = null;
let exportTime = null;
window.plugin.portalsJSON.pnpoly = function (latlngs, point) {
var length = latlngs.length, c = false;
for (var i = 0, j = length - 1; i < length; j = i++) {
if (((latlngs[i].lat > point.lat) != (latlngs[j].lat > point.lat)) &&
(point.lng < latlngs[i].lng + (latlngs[j].lng - latlngs[i].lng) * (point.lat - latlngs[i].lat) / (latlngs[j].lat - latlngs[i].lat))) {
c = !c;
}
}
return c;
}
window.plugin.portalsJSON.processPortal = function(portal) {
if (!('title' in portal.options.data)) return;
var portalData = {};
@ -54,6 +68,75 @@ function wrapper(plugin_info) {
exportTime = new Date();
}
window.plugin.portalsJSON.circleToSearchCircle = function (drawnItem) {
var circleCenter = drawnItem.getLatLng();
var result = { type: 'circle', radius: drawnItem.getRadius(), center: new L.LatLng(circleCenter.lat, circleCenter.lng) };
return result;
};
window.plugin.portalsJSON.multiPolygonToSearchPolygons = function (drawnItem) {
var result = [];
var polygonArr = [];
if (drawnItem instanceof L.GeodesicPolygon) {
if (typeof drawnItem._latlngs != "undefined" && drawnItem._latlngs.length > 0) {
if (typeof drawnItem._latlngs[0].lng == "number") {
polygonArr = drawnItem._latlngs.map(function (item) { return [item.lng, item.lat] });
polygonArr = [polygonArr];
} else if (typeof drawnItem._latlngs[0][0].lng == "number") {
$.each(drawnItem._latlngs, function (i, latLngs) {
var innerPolygonArr = latLngs.map(function (item) { return [item.lng, item.lat] });
polygonArr.push(innerPolygonArr);
});
}
}
}
$.each(polygonArr, function (i, polygonCoords) {
var searchPolygon = {
type: 'polygon',
outerRing: [],
holes: []
};
if (polygonCoords[0].length == 2 && typeof polygonCoords[0][0] == "number") {
polygonCoords = [polygonCoords];
}
$.each(polygonCoords, function (j, linearRing) {
var latLngArr = [];
$.each(linearRing, function (k, latlng) {
var obj = { lng: latlng[0], lat: latlng[1] };
latLngArr.push(obj);
});
if (j == 0) {
searchPolygon.outerRing = latLngArr;
}
else {
searchPolygon.holes.push(latLngArr);
}
});
result.push(searchPolygon);
});
return result;
};
window.plugin.portalsJSON.pointIsInPolygon = function (point, searchItem) {
var nodeIn = window.plugin.portalsJSON.pnpoly(searchItem.outerRing, point);
$.each(searchItem.holes, function (index, hole) {
var inHole = window.plugin.portalsJSON.pnpoly(hole, point);
if (inHole) {
nodeIn = false;
return false;
}
});
return nodeIn;
};
window.plugin.portalsJSON.pointIsInCircle = function (point, searchItem) {
var found = false;
if (searchItem.center.distanceTo(point) <= searchItem.radius) {
found = true;
}
return found;
};
window.plugin.portalsJSON.exportPolygons = function() {
portalsData.clear(); // Clear the map before exporting
@ -65,11 +148,11 @@ function wrapper(plugin_info) {
if (window.plugin.drawTools && window.plugin.drawTools.drawnItems) {
window.plugin.drawTools.drawnItems.eachLayer(function (drawnItem) {
if (drawnItem instanceof L.GeodesicCircle) {
var searchCircle = window.plugin.polyCounts2.circleToSearchCircle(drawnItem);
var searchCircle = window.plugin.portalsJSON.circleToSearchCircle(drawnItem);
searchItems.push(searchCircle);
}
else if (drawnItem instanceof L.GeodesicPolygon) {
var searchPolygons = window.plugin.polyCounts2.multiPolygonToSearchPolygons(drawnItem);
var searchPolygons = window.plugin.portalsJSON.multiPolygonToSearchPolygons(drawnItem);
$.each(searchPolygons, function (index, searchItem) {
searchItems.push(searchItem);
});
@ -83,7 +166,7 @@ function wrapper(plugin_info) {
window.search.lastSearch.selectedResult.layer) {
window.search.lastSearch.selectedResult.layer.eachLayer(function (drawnItem) {
if (drawnItem instanceof L.Polygon || (typeof L.MultiPolygon == "function" && drawnItem instanceof L.MultiPolygon)) {
var searchPolygons = window.plugin.polyCounts2.multiPolygonToSearchPolygons(drawnItem);
var searchPolygons = window.plugin.portalsJSON.multiPolygonToSearchPolygons(drawnItem);
$.each(searchPolygons, function (index, searchItem) {
searchItems.push(searchItem);
});
@ -98,13 +181,13 @@ function wrapper(plugin_info) {
$.each(searchItems, function (index, searchItem) {
switch (searchItem.type) {
case 'circle':
if (window.plugin.polyCounts2.pointIsInCircle(point, searchItem)) {
if (window.plugin.portalsJSON.pointIsInCircle(point, searchItem)) {
found = true;
return false; // Breaks the loop
}
break;
case 'polygon':
if (window.plugin.polyCounts2.pointIsInPolygon(point, searchItem)) {
if (window.plugin.portalsJSON.pointIsInPolygon(point, searchItem)) {
found = true;
return false; // Breaks the loop
}