GeoJSONExporter.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. *
  3. * @author sigeom sa / http://sigeom.ch
  4. * @author Ioda-Net Sàrl / https://www.ioda-net.ch/
  5. * @author Markus Schütz / http://potree.org
  6. *
  7. */
  8. import {Measure} from "../utils/Measure.js";
  9. export class GeoJSONExporter{
  10. static measurementToFeatures (measurement) {
  11. let coords = measurement.points.map(e => e.position.toArray());
  12. let features = [];
  13. if (coords.length === 1) {
  14. let feature = {
  15. type: 'Feature',
  16. geometry: {
  17. type: 'Point',
  18. coordinates: coords[0]
  19. },
  20. properties: {
  21. name: measurement.name
  22. }
  23. };
  24. features.push(feature);
  25. } else if (coords.length > 1 && !measurement.closed) {
  26. let object = {
  27. 'type': 'Feature',
  28. 'geometry': {
  29. 'type': 'LineString',
  30. 'coordinates': coords
  31. },
  32. 'properties': {
  33. name: measurement.name
  34. }
  35. };
  36. features.push(object);
  37. } else if (coords.length > 1 && measurement.closed) {
  38. let object = {
  39. 'type': 'Feature',
  40. 'geometry': {
  41. 'type': 'Polygon',
  42. 'coordinates': [[...coords, coords[0]]]
  43. },
  44. 'properties': {
  45. name: measurement.name
  46. }
  47. };
  48. features.push(object);
  49. }
  50. if (measurement.showDistances) {
  51. measurement.edgeLabels.forEach((label) => {
  52. let labelPoint = {
  53. type: 'Feature',
  54. geometry: {
  55. type: 'Point',
  56. coordinates: label.position.toArray()
  57. },
  58. properties: {
  59. distance: label.text
  60. }
  61. };
  62. features.push(labelPoint);
  63. });
  64. }
  65. if (measurement.showArea) {
  66. let point = measurement.areaLabel.position;
  67. let labelArea = {
  68. type: 'Feature',
  69. geometry: {
  70. type: 'Point',
  71. coordinates: point.toArray()
  72. },
  73. properties: {
  74. area: measurement.areaLabel.text
  75. }
  76. };
  77. features.push(labelArea);
  78. }
  79. return features;
  80. }
  81. static toString (measurements) {
  82. if (!(measurements instanceof Array)) {
  83. measurements = [measurements];
  84. }
  85. measurements = measurements.filter(m => m instanceof Measure);
  86. let features = [];
  87. for (let measure of measurements) {
  88. let f = GeoJSONExporter.measurementToFeatures(measure);
  89. features = features.concat(f);
  90. }
  91. let geojson = {
  92. 'type': 'FeatureCollection',
  93. 'features': features
  94. };
  95. return JSON.stringify(geojson, null, '\t');
  96. }
  97. }