|
1 | | -import React, { useEffect } from 'react'; |
| 1 | +import React, {useEffect} from 'react'; |
2 | 2 |
|
| 3 | +import {Feature, Map, View} from 'ol'; |
| 4 | +import {Coordinate} from 'ol/coordinate'; |
| 5 | +import {Extent} from "ol/extent"; |
| 6 | +import {Point, Polygon} from 'ol/geom'; |
3 | 7 | import TileLayer from 'ol/layer/Tile'; |
4 | | -import { OSM } from 'ol/source'; |
5 | | -import { Map, View } from 'ol'; |
| 8 | +import VectorLayer from "ol/layer/Vector"; |
| 9 | +import {fromLonLat} from 'ol/proj'; |
| 10 | +import {OSM} from 'ol/source'; |
| 11 | +import VectorSource from 'ol/source/Vector'; |
| 12 | +import {Fill, Stroke, Style} from 'ol/style'; |
6 | 13 |
|
7 | 14 | import 'ol/ol.css'; |
8 | 15 | import styles from './OpenLayers.module.css'; |
| 16 | +import {getAgriCropPolygon, getAgvolutionSensorsLocations, getSentekSensorsLocations} from '../services/fiwareService'; |
9 | 17 |
|
10 | 18 | interface Props { |
11 | 19 | id: string |
12 | 20 | } |
13 | 21 |
|
14 | | -function OpenLayers({ id }: Props) { |
15 | | - useEffect(() => { |
16 | | - const osmLayer = new TileLayer({ |
17 | | - preload: Infinity, |
18 | | - source: new OSM(), |
| 22 | +interface SensorResponse { |
| 23 | + id: string, |
| 24 | + type: string, |
| 25 | + location: { |
| 26 | + type: string, |
| 27 | + coordinates: Coordinate |
| 28 | + } | null |
| 29 | +} |
| 30 | + |
| 31 | +interface Sensor { |
| 32 | + id: string, |
| 33 | + type: string, |
| 34 | + coordinates: Coordinate |
| 35 | +} |
| 36 | + |
| 37 | +interface AgriCropResponse { |
| 38 | + id: string, |
| 39 | + type: string, |
| 40 | + location: { |
| 41 | + type: string, |
| 42 | + coordinates: Coordinate[] |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +interface AgriCrop { |
| 47 | + id: string, |
| 48 | + coordinates: Coordinate[] |
| 49 | +} |
| 50 | + |
| 51 | +function removeSensorByIdAndType(sensors: Sensor[], id: string, type: string) { |
| 52 | + const index: number = sensors |
| 53 | + .findIndex((sensor: Sensor): boolean => sensor.id === id && sensor.type === type); |
| 54 | + if (index >= 0) { |
| 55 | + delete sensors[index]; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function removeAgriCropById(agriCrops: AgriCrop[], id: string) { |
| 60 | + const index: number = agriCrops.findIndex((agriCrop: AgriCrop) => agriCrop.id === id); |
| 61 | + if (index > 0) { |
| 62 | + delete agriCrops[index]; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function updateSensors(map: Map, vectorSource: VectorSource<Feature<Point>>, sensors: Sensor[]) { |
| 67 | + const features: Feature<Point>[] = []; |
| 68 | + sensors.map((sensor: Sensor) => { |
| 69 | + features.push(new Feature({ geometry: new Point(fromLonLat(sensor.coordinates)) })); |
| 70 | + }); |
| 71 | + vectorSource.clear(); |
| 72 | + vectorSource.addFeatures(features); |
| 73 | +} |
| 74 | + |
| 75 | +function updateAgriCrops(map: Map, vectorSource: VectorSource<Feature<Polygon>>, agriCrops: AgriCrop[]) { |
| 76 | + const features: Feature<Polygon>[] = []; |
| 77 | + agriCrops.map((agriCrop: AgriCrop) => { |
| 78 | + const lonLatCoordinates: Coordinate[] = []; |
| 79 | + agriCrop.coordinates.map((coordinate) => { |
| 80 | + lonLatCoordinates.push(fromLonLat(coordinate)); |
19 | 81 | }) |
| 82 | + const polygonFeature = new Feature({ geometry: new Polygon([lonLatCoordinates]) }); |
| 83 | + polygonFeature.setStyle(style); |
| 84 | + features.push(polygonFeature); |
| 85 | + }); |
| 86 | + vectorSource.clear(); |
| 87 | + vectorSource.addFeatures(features); |
| 88 | + fitMap(map, vectorSource.getExtent()); |
| 89 | +} |
| 90 | + |
| 91 | +function fitMap(map: Map, extent: Extent | undefined) { |
| 92 | + if (extent && extent.length === 4 && extent.every((element: number): boolean => isFinite(element))) { |
| 93 | + map.getView().fit(extent, { padding: [50, 50, 50, 50] }); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function handleSensorsResponse(_sensors: SensorResponse[], map: Map, vectorSource: VectorSource<Feature<Point>>, sensors: Sensor[]) { |
| 98 | + if (Array.isArray(_sensors)) { |
| 99 | + _sensors.map((_sensor: SensorResponse) => { |
| 100 | + removeSensorByIdAndType(sensors, _sensor.id, _sensor.type); |
| 101 | + if (_sensor.location !== null) { |
| 102 | + sensors.push({ |
| 103 | + id: _sensor.id, |
| 104 | + type: _sensor.type, |
| 105 | + coordinates: _sensor.location.coordinates |
| 106 | + }); |
| 107 | + } |
| 108 | + updateSensors(map, vectorSource, sensors); |
| 109 | + }); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function handleAgriCropResponse(_agriCrops: AgriCropResponse[], map: Map, vectorSource: VectorSource<Feature<Polygon>>, agriCrops: AgriCrop[]) { |
| 114 | + if (Array.isArray(_agriCrops)) { |
| 115 | + _agriCrops.map((_agriCrop: AgriCropResponse) => { |
| 116 | + removeAgriCropById(agriCrops, _agriCrop.id) |
| 117 | + agriCrops.push({ |
| 118 | + id: _agriCrop.id, |
| 119 | + coordinates: _agriCrop.location.coordinates |
| 120 | + }); |
| 121 | + }); |
| 122 | + updateAgriCrops(map, vectorSource, agriCrops); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +const style = new Style({ |
| 127 | + fill: new Fill({ |
| 128 | + color: 'rgba(0, 128, 255, 0.4)', |
| 129 | + }), |
| 130 | + stroke: new Stroke({ |
| 131 | + color: 'blue', |
| 132 | + width: 2, |
| 133 | + }), |
| 134 | +}); |
| 135 | + |
| 136 | +function OpenLayers({ id }: Props) { |
20 | 137 |
|
| 138 | + const osmLayer = new TileLayer({ |
| 139 | + preload: Infinity, |
| 140 | + source: new OSM(), |
| 141 | + }); |
| 142 | + |
| 143 | + const pointFeatures: Feature<Point>[] = []; |
| 144 | + const polygonFeatures: Feature<Polygon>[] = []; |
| 145 | + const sensors: Sensor[] = []; |
| 146 | + const agriCrops: AgriCrop[] = []; |
| 147 | + |
| 148 | + const pointVectorSource = new VectorSource({ features: pointFeatures }); |
| 149 | + const polygonVectorSource = new VectorSource({ features: polygonFeatures }); |
| 150 | + |
| 151 | + const pointVectorLayer = new VectorLayer({ source: pointVectorSource }); |
| 152 | + const polygonVectorLayer = new VectorLayer({ source: polygonVectorSource }); |
| 153 | + |
| 154 | + useEffect(() => { |
21 | 155 | const map = new Map({ |
22 | 156 | target: id, |
23 | | - layers: [ osmLayer ], |
| 157 | + layers: [ osmLayer, polygonVectorLayer, pointVectorLayer ], |
24 | 158 | view: new View({ |
25 | 159 | center: [0, 0], |
26 | 160 | zoom: 0, |
27 | 161 | }), |
28 | 162 | }); |
| 163 | + |
| 164 | + getAgvolutionSensorsLocations() |
| 165 | + .then((response) => handleSensorsResponse( |
| 166 | + response.data, |
| 167 | + map, |
| 168 | + pointVectorSource, |
| 169 | + sensors |
| 170 | + )) |
| 171 | + .catch((error) => { |
| 172 | + console.debug(error); |
| 173 | + }); |
| 174 | + |
| 175 | + getSentekSensorsLocations() |
| 176 | + .then((response) => handleSensorsResponse( |
| 177 | + response.data, |
| 178 | + map, |
| 179 | + pointVectorSource, |
| 180 | + sensors |
| 181 | + )) |
| 182 | + .catch((error) => { |
| 183 | + console.debug(error); |
| 184 | + }); |
| 185 | + |
| 186 | + getAgriCropPolygon() |
| 187 | + .then((response) => handleAgriCropResponse( |
| 188 | + response.data, |
| 189 | + map, |
| 190 | + polygonVectorSource, |
| 191 | + agriCrops |
| 192 | + )) |
| 193 | + .catch((error) => { |
| 194 | + console.debug(error); |
| 195 | + }); |
| 196 | + |
29 | 197 | return () => map.setTarget(undefined) |
30 | 198 | }); |
31 | 199 |
|
|
0 commit comments