@totvs-agro/core-mobile
Version:
Core Mobile Totvs Agro (Front-End) para utilização dos estilos do T-Faces
198 lines • 8.52 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable } from "@angular/core";
import * as turf from '@turf/turf';
import { Spherical } from "@ionic-native/google-maps";
var PolygonUtil = /** @class */ (function () {
function PolygonUtil() {
console.log('Criado PolygonUtil');
}
PolygonUtil.prototype.findPolygonCenter = function (polygon) {
var coordsTurf = this.coordsGoogleToTurf(polygon.getPoints().getArray());
var polygonTurf = turf.polygon([coordsTurf]);
var centerTurf = this.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 = this.findCenterTurfReduce(polygonTurf);
centerLatLng =
{
lat: centerTurfReduce.geometry.coordinates[0],
lng: centerTurfReduce.geometry.coordinates[1]
};
console.log("Centro encontrado com turfPolygonReduce", centerTurfReduce);
}
return centerLatLng;
};
PolygonUtil.prototype.findCenterTurf = function (polygonTurf) {
return turf.centerOfMass(polygonTurf);
};
PolygonUtil.prototype.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.prototype.calcPolygonAreaHa = function (polygon) {
return Math.abs(Spherical.computeSignedArea(polygon.getPoints()) / 10000);
};
PolygonUtil.prototype.coordsGoogleToTurf = function (coordsGoogle) {
var coordsTurf = [];
coordsGoogle.forEach(function (coord) {
coordsTurf.push([coord.lat, coord.lng]);
});
coordsTurf.push(coordsTurf[0]);
return coordsTurf;
};
PolygonUtil.prototype.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.prototype.getShapeImage = function (points, size) {
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 = '#0c9abe';
ctx.strokeStyle = '#0c9abe';
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;
};
PolygonUtil = __decorate([
Injectable(),
__metadata("design:paramtypes", [])
], PolygonUtil);
return PolygonUtil;
}());
export { PolygonUtil };
//# sourceMappingURL=polygon-util.js.map