845394df2f41d801525b8efbf51e318894b35811.svn-base 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { cuttingString } from './util'
  2. function average(aver, curr, index) {
  3. aver[0] += curr[0]
  4. aver[1] += curr[1]
  5. if (index) {
  6. aver[0] /= 2
  7. aver[1] /= 2
  8. }
  9. return aver
  10. }
  11. function getMaxPoint(points, index) {
  12. let maxVal = points[0][index],
  13. maxIndex = 0;
  14. for (let i = 1; i < points.length; i++) {
  15. if (points[i][index] > maxVal) {
  16. maxVal = points[i][index]
  17. maxIndex = i
  18. }
  19. }
  20. return points[maxIndex]
  21. }
  22. function getMinPoint(points, index) {
  23. let minVal = points[0][index],
  24. minIndex = 0;
  25. for (let i = 1; i < points.length; i++) {
  26. if (points[i][index] < minVal) {
  27. minVal = points[i][index]
  28. minIndex = i
  29. }
  30. }
  31. return points[minIndex]
  32. }
  33. function grentText(features) {
  34. let texts = []
  35. features.forEach(fe => {
  36. if (fe.properties.name) {
  37. let name = cuttingString(fe.properties.name, 15)
  38. let averagePoint = fe.geometry.coordinates.reduce((tPoint, geometry, index) => {
  39. let mPoints = [
  40. getMaxPoint(geometry, 0),
  41. getMaxPoint(geometry, 1),
  42. getMinPoint(geometry, 0),
  43. getMinPoint(geometry, 1),
  44. ]
  45. return average(
  46. tPoint,
  47. mPoints.reduce(average, [0, 0]),
  48. index
  49. )
  50. }, [0, 0])
  51. texts.push({
  52. name: name.join('\n') + '\n|',
  53. point: averagePoint,
  54. top: fe.height-1,
  55. width: name[0].length * 18 + 20,
  56. height: name.length * 18 + 20,
  57. id: fe.id
  58. })
  59. }
  60. })
  61. return texts
  62. }
  63. export default grentText