@totvs-agro/mobile-components
Version:
Componentes Mobile Totvs (Front-End) para utilização dos estilos do T-Faces e Ionic v3
233 lines • 9.84 kB
JavaScript
import * as turf from '@turf/turf';
import { Spherical } from "@ionic-native/google-maps";
import * as tinycolor2 from "tinycolor2";
var PolygonUtil = /** @class */ (function () {
function PolygonUtil() {
}
PolygonUtil.findPolygonCenter = function (polygon) {
var coordsTurf = PolygonUtil.coordsGoogleToTurf(polygon.getPoints().getArray());
var polygonTurf = turf.polygon([coordsTurf]);
var centerTurf = PolygonUtil.findCenterTurf(polygonTurf);
var centerLatLng;
if (turf.booleanPointInPolygon(centerTurf, polygonTurf)) {
centerLatLng =
{
lat: centerTurf.geometry.coordinates[0],
lng: centerTurf.geometry.coordinates[1]
};
console.log("Centro encontrado com turf.centerOfMass", centerTurf);
}
else {
var centerTurfReduce = PolygonUtil.findCenterTurfReduce(polygonTurf);
centerLatLng =
{
lat: centerTurfReduce.geometry.coordinates[0],
lng: centerTurfReduce.geometry.coordinates[1]
};
console.log("Centro encontrado com turfPolygonReduce", centerTurfReduce);
}
return centerLatLng;
};
PolygonUtil.findCenterTurf = function (polygonTurf) {
return turf.centerOfMass(polygonTurf);
};
PolygonUtil.findCenterTurfReduce = function (polygonTurf) {
var tolerance = 0.1;
if (polygonTurf.geometry === void 0 || polygonTurf.geometry.type !== 'Polygon')
throw ('"polygonreduce" only accepts polygon type input');
// init defaults
tolerance = (tolerance === void 0 || isNaN(tolerance) || tolerance === 0) ? 0.1 : Math.abs(tolerance);
var area = turf.area(polygonTurf),
// max number of points to force a simplify
maxcount = /*(fine) ? 500 : 250*/ 375,
// factor of shrinking ~ polygonTurf.area^1/2
factor,
// check if multiple islands and choose the bigger one
// simplify if needed
multi2simple = function (e) {
var e2 = (e.features !== void 0) ? e.features[0] : e, a = 0, j = -1, p, count;
if (e2.geometry.type == 'MultiPolygon') {
for (var i = 0; i < e2.geometry.coordinates.length; i++) {
p = turf.polygon(e2.geometry.coordinates[i]);
if (turf.area(p) > a) {
a = turf.area(p);
j = i;
}
}
e2.geometry.coordinates = [e2.geometry.coordinates[j][0]];
e2.geometry.type = 'Polygon';
}
count = e2.geometry.coordinates.reduce(function (a, b) { return a + b.length; }, 0);
return (count > maxcount) ? turf.simplify(e2) : e2;
};
// iteration loop, limited to area > 1 m^2 to avoid lockings
while (area > 1) {
factor = -1 * tolerance * Math.sqrt(area);
try {
polygonTurf = turf.buffer(polygonTurf, factor, { units: 'meters' });
}
catch (err) {
/* it usually crashes before getting smaller than 1 m^2
because it tries to buffer the "unbufferable" and crashes
when processing a 0-vertex polygon (turf.js, line 12068)*/
return turf.centroid(polygonTurf);
}
polygonTurf = multi2simple(polygonTurf);
area = turf.area(polygonTurf);
}
// finally, if area<=1
return turf.centroid(polygonTurf);
};
PolygonUtil.calcPolygonAreaHa = function (polygon) {
return Math.abs(Spherical.computeSignedArea(polygon.getPoints()) / 10000);
};
PolygonUtil.coordsGoogleToTurf = function (coordsGoogle) {
var coordsTurf = [];
coordsGoogle.forEach(function (coord) {
coordsTurf.push([coord.lat, coord.lng]);
});
return coordsTurf;
};
PolygonUtil.coordsTurfToGoogle = function (coordsTurf) {
coordsTurf.splice(coordsTurf.length - 1);
var coordsGoogle = [];
coordsTurf.forEach(function (coord) {
coordsGoogle.push({ lat: coord[0], lng: coord[1] });
});
return coordsGoogle;
};
PolygonUtil.isPointInsidePolygon = function (point, polygon) {
return PolygonUtil.pointInPolygons(point, [polygon]).length > 0;
};
PolygonUtil.pointInPolygons = function (point, polygons) {
var insideList = [];
var pointTurf = turf.point(PolygonUtil.coordsGoogleToTurf([point])[0]);
polygons.forEach(function (polygon, index) {
var polygonTurf = turf.polygon([PolygonUtil.coordsGoogleToTurf(polygon)]);
var isInside = turf.booleanPointInPolygon(pointTurf, polygonTurf);
if (isInside)
insideList.push(index);
});
return insideList;
};
PolygonUtil.findReferenceNearestPolygonPoint = function (polygon, reference) {
var turfCoordinates = PolygonUtil.coordsGoogleToTurf(polygon);
var turfPolygon = turf.polygon([turfCoordinates]);
var turfPolygonLine = turf.polygonToLine(turfPolygon);
var nearestPoint = turf.nearestPointOnLine(turfPolygonLine, [reference.lat, reference.lng]);
return {
lat: nearestPoint.geometry.coordinates[0],
lng: nearestPoint.geometry.coordinates[1]
};
};
PolygonUtil.getMarkerImage = function (text, backgroundColor) {
if (backgroundColor === void 0) { backgroundColor = "#ffffff"; }
var tinyBackgroundColor = tinycolor2(backgroundColor);
var tinyFontColor = tinycolor2("#000000"); //#4a5c60
if (tinyBackgroundColor.getBrightness() < 100) {
tinyFontColor = tinycolor2("#ffffff");
}
var canvas = document.createElement("canvas");
var size = 26;
canvas.width = size;
canvas.height = size;
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, size, size);
ctx.beginPath();
ctx.arc(size / 2, size / 2, (size / 2) - 2, 0, 2 * Math.PI);
ctx.fillStyle = tinyBackgroundColor.toHexString();
ctx.fill();
ctx.font = "bold 10px NunitoSans";
ctx.fillStyle = tinyFontColor.toHexString();
ctx.fillText(text, 7, 16);
ctx.closePath();
var base64Image = canvas.toDataURL();
canvas.remove();
return base64Image;
};
PolygonUtil.getShapeImage = function (points, size, color) {
if (color === void 0) { color = "#0c9abe"; }
var immutablePoints = points.map(function (p) {
return { lat: p.lat, lng: p.lng };
});
var margin = 10;
// corrigir plano cartesiano em Y
immutablePoints.forEach(function (item) {
item.lat = (item.lat - 1) * (-1);
});
// posicionar na origem do plano cartesiano
var minLat = immutablePoints[0].lat;
var minLng = immutablePoints[0].lng;
immutablePoints.forEach(function (item) {
if (minLat > item.lat)
minLat = item.lat;
if (minLng > item.lng)
minLng = item.lng;
});
immutablePoints.forEach(function (item) {
item.lat -= minLat;
item.lng -= minLng;
});
// normalizar (valores entre 0 e 1)
var max = 0;
immutablePoints.forEach(function (item) {
if (max < item.lat)
max = item.lat;
if (max < item.lng)
max = item.lng;
});
immutablePoints.forEach(function (item) {
item.lat *= (1 / max);
item.lng *= (1 / max);
});
// centralizar polígono
var maxLat = 0;
var maxLng = 0;
immutablePoints.forEach(function (item) {
if (maxLat < item.lat)
maxLat = item.lat;
if (maxLng < item.lng)
maxLng = item.lng;
});
if (maxLat < maxLng) {
immutablePoints.forEach(function (item) {
item.lat = item.lat + ((1 - maxLat) / 2);
});
}
else {
immutablePoints.forEach(function (item) {
item.lng = item.lng + ((1 - maxLng) / 2);
});
}
// redimensionar
immutablePoints.forEach(function (item) {
item.lng = (item.lng * size) + margin;
item.lat = (item.lat * size) + margin;
});
// criar elemento canvas
var polygonCanvas = document.createElement("canvas");
polygonCanvas.width = size + (margin * 2);
polygonCanvas.height = size + (margin * 2);
// desenhar polígono
var ctx = polygonCanvas.getContext("2d");
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.lineWidth = 4;
ctx.clearRect(0, 0, polygonCanvas.width, polygonCanvas.height);
ctx.beginPath();
ctx.moveTo(immutablePoints[0].lng, immutablePoints[0].lat);
for (var i = 1; i < immutablePoints.length; i++) {
ctx.lineTo(immutablePoints[i].lng, immutablePoints[i].lat);
}
ctx.closePath();
//ctx.fill();
ctx.stroke();
var base64Image = polygonCanvas.toDataURL();
polygonCanvas.remove();
// retornar Base64
return base64Image;
};
return PolygonUtil;
}());
export { PolygonUtil };
//# sourceMappingURL=polygon-util.js.map