field.service.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {Injectable} from '@angular/core';
  2. import {GeoJSON} from 'ol/format';
  3. import {Geometry, Polygon} from 'ol/geom';
  4. import {Vector as VectorSource} from 'ol/source';
  5. import {getCenter} from 'ol/extent';
  6. import {transform} from 'ol/proj';
  7. import {HsEventBusService, HsMapService} from 'hslayers-ng';
  8. @Injectable({providedIn: 'root'})
  9. export class FieldService {
  10. fields: VectorSource<Geometry>;
  11. //TODO: temporarry hard-coded hack
  12. field1GeoJSON = {
  13. type: 'Polygon',
  14. coordinates: [
  15. [
  16. [17.008032903697003, 49.259378350222214],
  17. [17.012346093160815, 49.26055145480218],
  18. [17.01341275235608, 49.25661998865008],
  19. [17.01578237120672, 49.251834177355896],
  20. [17.01243531034079, 49.24966645404407],
  21. [17.009126895621744, 49.24897297823598],
  22. [17.003117114964958, 49.253454830509305],
  23. [17.008032903697003, 49.259378350222214],
  24. ],
  25. ],
  26. };
  27. //TODO: temporarry hard-coded hack
  28. field2GeoJSON = {
  29. type: 'Polygon',
  30. coordinates: [
  31. [
  32. [17.002437217427214, 49.25406053771264],
  33. [17.007010834533514, 49.25949599987935],
  34. [17.00652649276053, 49.26616621617807],
  35. [16.994875655608688, 49.25981844649525],
  36. [16.995730316423206, 49.257965703041684],
  37. [17.002437217427214, 49.25406053771264],
  38. ],
  39. ],
  40. };
  41. //TODO: temporarry hard-coded hack
  42. field3GeoJSON = {
  43. type: 'Polygon',
  44. coordinates: [
  45. [
  46. [16.993895078820575, 49.259216136348286],
  47. [16.994671522235812, 49.257860846173166],
  48. [17.00155277758227, 49.25349827686348],
  49. [16.99702124804218, 49.246825312276215],
  50. [16.993862809154038, 49.243797217302074],
  51. [16.97947178114977, 49.24513873718192],
  52. [16.980782403841776, 49.246567855409126],
  53. [16.980234692917616, 49.251993865297734],
  54. [16.982332050970445, 49.25428043019449],
  55. [16.98326065796653, 49.25618643740068],
  56. [16.99037376067523, 49.25783927642802],
  57. [16.993895078820575, 49.259216136348286],
  58. ],
  59. ],
  60. };
  61. selectableLayers = ['Farma'];
  62. selectedField;
  63. constructor(
  64. private hsEventBus: HsEventBusService,
  65. private hsMapService: HsMapService
  66. ) {
  67. this.hsEventBus.vectorQueryFeatureSelection.subscribe((data) => {
  68. if (
  69. !this.isValidGeometry(data.feature) ||
  70. !this.isValidLayer(data.feature)
  71. ) {
  72. return;
  73. }
  74. this.selectedField = data.feature.getGeometry() as Polygon;
  75. });
  76. //TODO: temporary hard-coded hack
  77. this.fields = new VectorSource({
  78. features: [
  79. new GeoJSON().readFeature(this.field1GeoJSON, {
  80. dataProjection: 'EPSG:4326',
  81. featureProjection: 'EPSG:5514',
  82. }),
  83. ],
  84. });
  85. this.fields.addFeatures([
  86. new GeoJSON().readFeature(this.field2GeoJSON, {
  87. dataProjection: 'EPSG:4326',
  88. featureProjection: 'EPSG:5514',
  89. }),
  90. ]);
  91. this.fields.addFeatures([
  92. new GeoJSON().readFeature(this.field3GeoJSON, {
  93. dataProjection: 'EPSG:4326',
  94. featureProjection: 'EPSG:5514',
  95. }),
  96. ]);
  97. }
  98. noFieldSelected(): boolean {
  99. return this.selectedField === undefined;
  100. }
  101. getCentroidOfField(field: Geometry) {
  102. return {
  103. type: 'Point',
  104. coordinates: transform(
  105. getCenter(field.getExtent()),
  106. 'EPSG:5514',
  107. 'EPSG:4326'
  108. ),
  109. };
  110. }
  111. getSelectedFieldCentroid() {
  112. return this.getCentroidOfField(this.selectedField);
  113. }
  114. getSelectedFieldGeoJSON() {
  115. return {
  116. type: 'Polygon',
  117. coordinates: this.selectedField
  118. .clone()
  119. .transform('EPSG:5514', 'EPSG:4326')
  120. .getCoordinates(),
  121. };
  122. }
  123. /**
  124. * Check whether selected feature geometry type is valid
  125. * (API expects polygon only)
  126. */
  127. isValidGeometry(feature) {
  128. if (!feature) {
  129. return false;
  130. }
  131. return feature.getGeometry().getType() == 'Polygon';
  132. }
  133. /**
  134. * Check whether user clicked into one of selectable layers
  135. */
  136. isValidLayer(feature) {
  137. const layer = this.hsMapService.getLayerForFeature(feature);
  138. return this.selectableLayers.includes(layer.get('title'));
  139. }
  140. }