@@ -2,14 +2,15 @@ import React, {useEffect, useRef} from 'react';
22
33import { Feature , Map , View } from 'ol' ;
44import { Coordinate } from 'ol/coordinate' ;
5- import { Extent } from " ol/extent" ;
5+ import { Extent } from ' ol/extent' ;
66import { Point , Polygon } from 'ol/geom' ;
77import TileLayer from 'ol/layer/Tile' ;
88import VectorLayer from "ol/layer/Vector" ;
99import { fromLonLat } from 'ol/proj' ;
1010import { OSM } from 'ol/source' ;
1111import VectorSource from 'ol/source/Vector' ;
1212import { Fill , Stroke , Style } from 'ol/style' ;
13+ import CircleStyle from 'ol/style/Circle' ;
1314
1415import 'ol/ol.css' ;
1516import styles from './OpenLayers.module.css' ;
@@ -36,7 +37,9 @@ function createPointVectorLayer(name: string, source: VectorSource<Feature<Point
3637interface Props {
3738 id : string ,
3839 agriCrops : AgriCrop [ ] ,
40+ selectedAgriCrop ?: AgriCrop | undefined ,
3941 selectedGroup : TenantGroup | undefined ,
42+ selectedSensor ?: Sensor | undefined ,
4043 sensors : Sensor [ ]
4144}
4245
@@ -52,7 +55,7 @@ function fitMap(mapView: View | undefined, extent: Extent | undefined) {
5255 }
5356}
5457
55- function OpenLayers ( { agriCrops, id, selectedGroup, sensors } : Props ) {
58+ function OpenLayers ( { agriCrops, id, selectedAgriCrop , selectedGroup, selectedSensor , sensors } : Props ) {
5659
5760 const mapRef = useRef < Map | undefined > ( undefined ) ;
5861
@@ -70,7 +73,7 @@ function OpenLayers({ agriCrops, id, selectedGroup, sensors }: Props) {
7073 let pointVectorLayer = createPointVectorLayer ( pointVectorLayerName , pointVectorSource ) ;
7174 let polygonVectorLayer = createPolygonVectorLayer ( polygonVectorLayerName , polygonVectorSource ) ;
7275
73- const style = new Style ( {
76+ const normalPolygonStyle = new Style ( {
7477 fill : new Fill ( {
7578 color : 'rgba(0, 128, 255, 0.4)' ,
7679 } ) ,
@@ -80,6 +83,29 @@ function OpenLayers({ agriCrops, id, selectedGroup, sensors }: Props) {
8083 } ) ,
8184 } ) ;
8285
86+ const highlightPolygonStyle = new Style ( {
87+ fill : new Fill ( {
88+ color : 'rgba(255, 0, 0, 0.4)' ,
89+ } ) ,
90+ stroke : new Stroke ( {
91+ color : 'red' ,
92+ width : 2 ,
93+ } ) ,
94+ } ) ;
95+
96+ const highlightPointStyle = new Style ( {
97+ image : new CircleStyle ( {
98+ fill : new Fill ( {
99+ color : 'rgba(255, 0, 0, 0.4)'
100+ } ) ,
101+ stroke : new Stroke ( {
102+ color : 'red' ,
103+ width : 1
104+ } ) ,
105+ radius : 5
106+ } )
107+ } ) ;
108+
83109 function updateAgriCrops ( ) {
84110 const map = mapRef . current ;
85111 if ( ! map || agriCrops . length === 0 ) return ;
@@ -93,7 +119,7 @@ function OpenLayers({ agriCrops, id, selectedGroup, sensors }: Props) {
93119 lonLatCoordinates . push ( fromLonLat ( coordinate ) ) ;
94120 } ) ;
95121 const polygonFeature = new Feature ( { geometry : new Polygon ( [ lonLatCoordinates ] ) } ) ;
96- polygonFeature . setStyle ( style ) ;
122+ polygonFeature . setStyle ( agriCrop . id === selectedAgriCrop ?. id ? highlightPolygonStyle : normalPolygonStyle ) ;
97123 features . push ( polygonFeature ) ;
98124 } ) ;
99125 polygonVectorSource . clear ( ) ;
@@ -105,7 +131,9 @@ function OpenLayers({ agriCrops, id, selectedGroup, sensors }: Props) {
105131 }
106132 polygonVectorLayer = createPolygonVectorLayer ( polygonVectorLayerName , polygonVectorSource ) ;
107133 map . addLayer ( polygonVectorLayer ) ;
108- fitMap ( map . getView ( ) , polygonVectorSource . getExtent ( ) ) ;
134+ if ( ! selectedAgriCrop && ! selectedSensor ) {
135+ fitMap ( map . getView ( ) , polygonVectorSource . getExtent ( ) ) ;
136+ }
109137 }
110138
111139 function updateSensors ( ) {
@@ -116,7 +144,11 @@ function OpenLayers({ agriCrops, id, selectedGroup, sensors }: Props) {
116144 return ! selectedGroup || sensor . customGroup === selectedGroup . groupId ;
117145 } ) ;
118146 _sensors . forEach ( ( sensor : Sensor ) => {
119- features . push ( new Feature ( { geometry : new Point ( fromLonLat ( sensor . coordinates ) ) } ) ) ;
147+ const pointFeature = new Feature ( { geometry : new Point ( fromLonLat ( sensor . coordinates ) ) } ) ;
148+ if ( sensor . id === selectedSensor ?. id ) {
149+ pointFeature . setStyle ( highlightPointStyle ) ;
150+ }
151+ features . push ( pointFeature ) ;
120152 } ) ;
121153 pointVectorSource . clear ( ) ;
122154 pointVectorSource . addFeatures ( features ) ;
@@ -148,13 +180,13 @@ function OpenLayers({ agriCrops, id, selectedGroup, sensors }: Props) {
148180 if ( mapRef . current && agriCrops . length > 0 ) {
149181 updateAgriCrops ( ) ;
150182 }
151- } , [ agriCrops , selectedGroup ] ) ;
183+ } , [ agriCrops , selectedGroup , selectedAgriCrop ] ) ;
152184
153185 useEffect ( ( ) => {
154186 if ( mapRef . current && sensors . length > 0 ) {
155187 updateSensors ( ) ;
156188 }
157- } , [ sensors , selectedGroup ] ) ;
189+ } , [ sensors , selectedGroup , selectedSensor ] ) ;
158190
159191 return < div id = { id } className = { styles . map } > </ div > ;
160192}
0 commit comments