@@ -43,10 +43,11 @@ import { singleClick } from 'ol/events/condition';
4343import { GeoJSON , MVT } from 'ol/format' ;
4444import { Geometry , Point } from 'ol/geom' ;
4545import { Type } from 'ol/geom/Geometry' ;
46- import { DragAndDrop , Interaction , Select } from 'ol/interaction' ;
47- import Draw from 'ol/interaction/Draw.js' ;
48- import Modify from 'ol/interaction/Modify.js' ;
49- import Snap from 'ol/interaction/Snap.js' ;
46+ import DragAndDrop from 'ol/interaction/DragAndDrop' ;
47+ import Draw from 'ol/interaction/Draw' ;
48+ import Modify from 'ol/interaction/Modify' ;
49+ import Select from 'ol/interaction/Select' ;
50+ import Snap from 'ol/interaction/Snap' ;
5051import {
5152 Heatmap as HeatmapLayer ,
5253 Image as ImageLayer ,
@@ -250,7 +251,7 @@ export class MainView extends React.Component<IProps, IStates> {
250251
251252 this . _model . sharedModel . awareness . off (
252253 'change' ,
253- this . _onAwarenessChangeHandler ,
254+ this . _onSelectedLayerChangeHandler ,
254255 ) ;
255256 this . _mainViewModel . dispose ( ) ;
256257 }
@@ -408,22 +409,25 @@ export class MainView extends React.Component<IProps, IStates> {
408409 } ,
409410 } ) ) ;
410411
412+ /* generate select, modify and snap interactions for features of layers already added to the Map */
413+ this . _select = new Select ( ) ;
414+ this . _modify = new Modify ( {
415+ features : this . _select . getFeatures ( ) ,
416+ } ) ;
417+
418+ this . _Map . addInteraction ( this . _select ) ;
419+ this . _Map . addInteraction ( this . _modify ) ;
420+
421+ this . _select . setActive ( true ) ;
422+ this . _modify . setActive ( true ) ;
423+
424+ /* Track changes of selected layers
425+ Get the vector source of the selected layer
426+ Edit the vector layer*/
411427 this . _model . sharedModel . awareness . on (
412428 'change' ,
413- this . _onAwarenessChangeHandler ,
429+ this . _onSelectedLayerChangeHandler ,
414430 ) ;
415-
416- /* Snap and Modify interactions */
417- //this._snap = new Snap();
418- //this._modify = new Modify({});
419- //this._Map.addInteraction(this._snap);
420- //this._Map.addInteraction(this._modify);
421-
422- const drawGeometryLabel = 'Select' ;
423- this . setState ( old => ( {
424- ...old ,
425- drawGeometryLabel,
426- } ) ) ;
427431 }
428432 }
429433
@@ -2112,12 +2116,12 @@ export class MainView extends React.Component<IProps, IStates> {
21122116 } ;
21132117
21142118 private _handleDrawGeometryTypeChange = (
2115- /* handle with the change of geometry and instantiate new draw interaction accordingly*/
2119+ /* handle with the change of geometry and instantiate new draw interaction and other ones accordingly*/
21162120 event : React . ChangeEvent < HTMLSelectElement > ,
21172121 ) => {
21182122 const drawGeometryLabel = event . target . value ;
21192123 this . _currentDrawGeometry = drawGeometryLabel as Type ;
2120- this . _updateDrawInteraction ( ) ;
2124+ this . _updateInteractions ( ) ;
21212125 this . _updateDrawSource ( ) ;
21222126 this . setState ( old => ( {
21232127 ...old ,
@@ -2146,31 +2150,32 @@ export class MainView extends React.Component<IProps, IStates> {
21462150 const geojsonWriter = new GeoJSON ( {
21472151 featureProjection : this . _Map . getView ( ) . getProjection ( ) ,
21482152 } ) ;
2153+ if ( this . _currentVectorSource ) {
2154+ const features = this . _currentVectorSource
2155+ ?. getFeatures ( )
2156+ . map ( feature => geojsonWriter . writeFeatureObject ( feature ) ) ;
21492157
2150- const features = this . _currentVectorSource
2151- ?. getFeatures ( )
2152- . map ( feature => geojsonWriter . writeFeatureObject ( feature ) ) ;
2153-
2154- const updatedData = {
2155- type : 'FeatureCollection' ,
2156- features : features ,
2157- } ;
2158-
2159- if ( this . _currentDrawSource ) {
2160- const updatedJGISLayerSource : IJGISSource = {
2161- name : this . _currentDrawSource . name ,
2162- type : this . _currentDrawSource . type ,
2163- parameters : {
2164- data : updatedData ,
2165- } ,
2158+ const updatedData = {
2159+ type : 'FeatureCollection' ,
2160+ features : features ,
21662161 } ;
21672162
2168- this . _currentDrawSource = updatedJGISLayerSource ;
2169- if ( this . _currentDrawSourceID ) {
2170- this . _model . sharedModel . updateSource (
2171- this . _currentDrawSourceID ,
2172- updatedJGISLayerSource ,
2173- ) ;
2163+ if ( this . _currentDrawSource ) {
2164+ const updatedJGISLayerSource : IJGISSource = {
2165+ name : this . _currentDrawSource . name ,
2166+ type : this . _currentDrawSource . type ,
2167+ parameters : {
2168+ data : updatedData ,
2169+ } ,
2170+ } ;
2171+
2172+ this . _currentDrawSource = updatedJGISLayerSource ;
2173+ if ( this . _currentDrawSourceID ) {
2174+ this . _model . sharedModel . updateSource (
2175+ this . _currentDrawSourceID ,
2176+ updatedJGISLayerSource ,
2177+ ) ;
2178+ }
21742179 }
21752180 }
21762181 } ;
@@ -2181,39 +2186,39 @@ export class MainView extends React.Component<IProps, IStates> {
21812186 }
21822187 } ;
21832188
2184- _updateDrawInteraction = ( ) => {
2189+ _updateInteractions = ( ) => {
21852190 if ( this . _draw ) {
21862191 this . _removeDrawInteraction ( ) ;
21872192 }
2193+ if ( this . _select ) {
2194+ this . _removeSelectInteraction ( ) ;
2195+ }
2196+ if ( this . _modify ) {
2197+ this . _removeModifyInteraction ( ) ;
2198+ }
2199+ if ( this . _snap ) {
2200+ this . _removeSnapInteraction ( ) ;
2201+ }
21882202 this . _draw = new Draw ( {
21892203 style : drawInteractionStyle ,
21902204 type : this . _currentDrawGeometry ,
21912205 source : this . _currentVectorSource ,
21922206 } ) ;
2193- this . _draw . setActive ( true ) ;
2194- this . _Map . addInteraction ( this . _draw ) ;
2195- } ;
2196-
2197- _updateModifyInteraction = ( ) => {
2198- if ( this . _modify ) {
2199- this . _removeModifyInteraction ( ) ;
2200- }
2207+ this . _select = new Select ( ) ;
22012208 this . _modify = new Modify ( {
2202- source : this . _currentVectorSource ,
2209+ features : this . _select . getFeatures ( ) ,
22032210 } ) ;
2204- this . _modify . setActive ( true ) ;
2205- this . _Map . addInteraction ( this . _modify ) ;
2206- } ;
2207-
2208- _updateSnapInteraction = ( ) => {
2209- if ( this . _snap ) {
2210- this . _removeSnapInteraction ( ) ;
2211- }
22122211 this . _snap = new Snap ( {
22132212 source : this . _currentVectorSource ,
22142213 } ) ;
2215- this . _snap . setActive ( true ) ;
2214+ this . _Map . addInteraction ( this . _draw ) ;
2215+ this . _Map . addInteraction ( this . _select ) ;
2216+ this . _Map . addInteraction ( this . _modify ) ;
22162217 this . _Map . addInteraction ( this . _snap ) ;
2218+ this . _draw . setActive ( true ) ;
2219+ this . _select . setActive ( false ) ;
2220+ this . _modify . setActive ( false ) ;
2221+ this . _snap . setActive ( true ) ;
22172222 } ;
22182223
22192224 _editVectorLayer = ( ) => {
@@ -2223,51 +2228,45 @@ export class MainView extends React.Component<IProps, IStates> {
22232228 this . _currentDrawLayerID ,
22242229 ) ;
22252230 if ( this . _currentVectorSource && this . _currentDrawGeometry ) {
2226- this . _updateDrawInteraction ( ) ; /* remove previous draw interactions and instantiate a new one */
2227- this . _updateSnapInteraction ( ) ;
2228- this . _updateModifyInteraction ( ) ;
2231+ this . _updateInteractions ( ) ; /* remove previous interactions and instantiate new ones */
22292232 this . _updateDrawSource ( ) ; /*add new features, update source and get changes reported to the JGIS Document in geoJSON format */
22302233 }
22312234 }
22322235 } ;
22332236
22342237 private _removeDrawInteraction = ( ) => {
2238+ this . _draw . setActive ( false ) ;
22352239 this . _Map . removeInteraction ( this . _draw ) ;
22362240 } ;
22372241
2242+ private _removeSelectInteraction = ( ) => {
2243+ this . _select . setActive ( false ) ;
2244+ this . _Map . removeInteraction ( this . _select ) ;
2245+ } ;
2246+
22382247 private _removeSnapInteraction = ( ) => {
2248+ this . _snap . setActive ( false ) ;
22392249 this . _Map . removeInteraction ( this . _snap ) ;
22402250 } ;
22412251
22422252 private _removeModifyInteraction = ( ) => {
2253+ this . _modify . setActive ( false ) ;
22432254 this . _Map . removeInteraction ( this . _modify ) ;
22442255 } ;
22452256
22462257 private _onVectorSourceChangeHandler = ( ) => {
22472258 this . _onVectorSourceChange ( ) ;
22482259 } ;
22492260
2250- private _onAwarenessChangeHandler = ( ) => {
2261+ private _onSelectedLayerChangeHandler = ( ) => {
22512262 const selectedLayers =
22522263 this . _model . sharedModel . awareness . getLocalState ( ) ?. selected ?. value ;
22532264 const selectedLayerId = selectedLayers
22542265 ? Object . keys ( selectedLayers ) [ 0 ]
2255- : null ;
2256-
2266+ : undefined ;
22572267 if ( selectedLayerId && selectedLayerId !== this . _previousDrawLayerID ) {
22582268 this . _previousDrawLayerID = selectedLayerId ;
22592269 this . _currentDrawLayerID = selectedLayerId ;
2260-
2261- this . _currentVectorSource =
2262- this . _getVectorSourceFromLayerID ( selectedLayerId ) ;
2263-
2264- if ( this . _currentVectorSource ) {
2265- this . _currentVectorSource . on (
2266- 'change' ,
2267- this . _onVectorSourceChangeHandler ,
2268- ) ;
2269- }
2270-
22712270 this . _editVectorLayer ( ) ;
22722271 }
22732272 } ;
@@ -2391,9 +2390,10 @@ export class MainView extends React.Component<IProps, IStates> {
23912390 private _loadingLayers : Set < string > ;
23922391 private _originalFeatures : IDict < Feature < Geometry > [ ] > = { } ;
23932392 private _highlightLayer : VectorLayer < VectorSource > ;
2394- private _draw : Interaction ;
2395- private _snap : Interaction ;
2396- private _modify : Interaction ;
2393+ private _draw : Draw ;
2394+ private _snap : Snap ;
2395+ private _modify : Modify ;
2396+ private _select : Select ;
23972397 private _currentDrawLayerID : string | undefined ;
23982398 private _previousDrawLayerID : string | undefined ;
23992399 private _currentDrawSource : IJGISSource | undefined ;
0 commit comments