import { cuttingString } from './util' function average(aver, curr, index) { aver[0] += curr[0] aver[1] += curr[1] if (index) { aver[0] /= 2 aver[1] /= 2 } return aver } function getMaxPoint(points, index) { let maxVal = points[0][index], maxIndex = 0; for (let i = 1; i < points.length; i++) { if (points[i][index] > maxVal) { maxVal = points[i][index] maxIndex = i } } return points[maxIndex] } function getMinPoint(points, index) { let minVal = points[0][index], minIndex = 0; for (let i = 1; i < points.length; i++) { if (points[i][index] < minVal) { minVal = points[i][index] minIndex = i } } return points[minIndex] } function grentText(features) { let texts = [] features.forEach(fe => { if (fe.properties.name) { let name = cuttingString(fe.properties.name, 15) let averagePoint = fe.geometry.coordinates.reduce((tPoint, geometry, index) => { let mPoints = [ getMaxPoint(geometry, 0), getMaxPoint(geometry, 1), getMinPoint(geometry, 0), getMinPoint(geometry, 1), ] return average( tPoint, mPoints.reduce(average, [0, 0]), index ) }, [0, 0]) texts.push({ name: name.join('\n') + '\n|', point: averagePoint, top: fe.height-1, width: name[0].length * 18 + 20, height: name.length * 18 + 20, id: fe.id }) } }) return texts } export default grentText