Skip to content

Commit ce17370

Browse files
Update all interactions in a single updateInteractions.
Add Select interaction. Set activity of the different interactions properly.
1 parent 4a1c7e6 commit ce17370

File tree

2 files changed

+89
-130
lines changed

2 files changed

+89
-130
lines changed

examples/earthquakes.jGIS

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"layerTree": [
33
"8de7c2c0-6024-4716-b542-031a89fb87f9",
4-
"3e21d680-406f-4099-bd9e-3a4edb9a2c8b",
5-
"5ff49984-5480-4cd4-aabd-3a43186350ba",
6-
"f44d7a8c-2d45-484b-89e3-e59edea8f988"
4+
"3e21d680-406f-4099-bd9e-3a4edb9a2c8b"
75
],
86
"layers": {
97
"3e21d680-406f-4099-bd9e-3a4edb9a2c8b": {
@@ -31,60 +29,32 @@
3129
"type": "VectorLayer",
3230
"visible": true
3331
},
34-
"5ff49984-5480-4cd4-aabd-3a43186350ba": {
35-
"name": "Editable GeoJSON Layer 1",
36-
"parameters": {
37-
"opacity": 1.0,
38-
"source": "e5b8b9db-22b3-4439-8db3-59da882636ac"
39-
},
40-
"type": "VectorLayer",
41-
"visible": true
42-
},
4332
"8de7c2c0-6024-4716-b542-031a89fb87f9": {
4433
"name": "OpenStreetMap.Mapnik Layer",
4534
"parameters": {
4635
"source": "b2ea427a-a51b-43ad-ae72-02cd900736d5"
4736
},
4837
"type": "RasterLayer",
4938
"visible": true
50-
},
51-
"f44d7a8c-2d45-484b-89e3-e59edea8f988": {
52-
"name": "Editable GeoJSON Layer 2",
53-
"parameters": {
54-
"opacity": 1.0,
55-
"source": "210844ec-68e3-49d5-afd3-bc9bd872c695"
56-
},
57-
"type": "VectorLayer",
58-
"visible": true
5939
}
6040
},
6141
"metadata": {},
6242
"options": {
6343
"bearing": 0.0,
6444
"extent": [
65-
-16434055.989266738,
66-
-3411504.4661820857,
67-
-2212097.8396098875,
68-
14828095.240483545
45+
-15808389.545988183,
46+
-6461894.596411711,
47+
-846988.1649800241,
48+
14932662.897352839
6949
],
70-
"latitude": 45.54854082519543,
71-
"longitude": -83.75062487261803,
50+
"latitude": 35.52446437432016,
51+
"longitude": -74.80890180273175,
7252
"pitch": 0.0,
7353
"projection": "EPSG:3857",
7454
"zoom": 2.6670105136699993
7555
},
7656
"schemaVersion": "0.5.0",
7757
"sources": {
78-
"210844ec-68e3-49d5-afd3-bc9bd872c695": {
79-
"name": "Editable GeoJSON Layer 2 Source",
80-
"parameters": {
81-
"data": {
82-
"features": [],
83-
"type": "FeatureCollection"
84-
}
85-
},
86-
"type": "GeoJSONSource"
87-
},
8858
"348d85fa-3a71-447f-8a64-e283ec47cc7c": {
8959
"name": "Custom GeoJSON Layer Source",
9060
"parameters": {
@@ -103,16 +73,6 @@
10373
"urlParameters": {}
10474
},
10575
"type": "RasterSource"
106-
},
107-
"e5b8b9db-22b3-4439-8db3-59da882636ac": {
108-
"name": "Custom GeoJSON Layer Source",
109-
"parameters": {
110-
"data": {
111-
"features": [],
112-
"type": "FeatureCollection"
113-
}
114-
},
115-
"type": "GeoJSONSource"
11676
}
11777
}
118-
}
78+
}

packages/base/src/mainview/mainView.tsx

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ import { singleClick } from 'ol/events/condition';
4343
import { GeoJSON, MVT } from 'ol/format';
4444
import { Geometry, Point } from 'ol/geom';
4545
import { 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';
5051
import {
5152
Heatmap as HeatmapLayer,
5253
Image as ImageLayer,
@@ -96,7 +97,6 @@ import { MainViewModel } from './mainviewmodel';
9697
//import Modify from 'ol/interaction/Modify.js';
9798
//import Snap from 'ol/interaction/Snap.js';
9899

99-
100100
const drawGeometries = ['Point', 'LineString', 'Polygon'];
101101

102102
interface IProps {
@@ -250,7 +250,7 @@ export class MainView extends React.Component<IProps, IStates> {
250250

251251
this._model.sharedModel.awareness.off(
252252
'change',
253-
this._onAwarenessChangeHandler,
253+
this._onSelectedLayerChangeHandler,
254254
);
255255
this._mainViewModel.dispose();
256256
}
@@ -408,22 +408,25 @@ export class MainView extends React.Component<IProps, IStates> {
408408
},
409409
}));
410410

411+
/* generate select, modify and snap interactions for features of layers already added to the Map */
412+
this._select = new Select();
413+
this._modify = new Modify({
414+
features: this._select.getFeatures(),
415+
});
416+
417+
this._Map.addInteraction(this._select);
418+
this._Map.addInteraction(this._modify);
419+
420+
this._select.setActive(true);
421+
this._modify.setActive(true);
422+
423+
/* Track changes of selected layers
424+
Get the vector source of the selected layer
425+
Edit the vector layer*/
411426
this._model.sharedModel.awareness.on(
412427
'change',
413-
this._onAwarenessChangeHandler,
428+
this._onSelectedLayerChangeHandler,
414429
);
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-
}));
427430
}
428431
}
429432

@@ -2112,12 +2115,12 @@ export class MainView extends React.Component<IProps, IStates> {
21122115
};
21132116

21142117
private _handleDrawGeometryTypeChange = (
2115-
/* handle with the change of geometry and instantiate new draw interaction accordingly*/
2118+
/* handle with the change of geometry and instantiate new draw interaction and other ones accordingly*/
21162119
event: React.ChangeEvent<HTMLSelectElement>,
21172120
) => {
21182121
const drawGeometryLabel = event.target.value;
21192122
this._currentDrawGeometry = drawGeometryLabel as Type;
2120-
this._updateDrawInteraction();
2123+
this._updateInteractions();
21212124
this._updateDrawSource();
21222125
this.setState(old => ({
21232126
...old,
@@ -2146,31 +2149,32 @@ export class MainView extends React.Component<IProps, IStates> {
21462149
const geojsonWriter = new GeoJSON({
21472150
featureProjection: this._Map.getView().getProjection(),
21482151
});
2152+
if (this._currentVectorSource) {
2153+
const features = this._currentVectorSource
2154+
?.getFeatures()
2155+
.map(feature => geojsonWriter.writeFeatureObject(feature));
21492156

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-
},
2157+
const updatedData = {
2158+
type: 'FeatureCollection',
2159+
features: features,
21662160
};
21672161

2168-
this._currentDrawSource = updatedJGISLayerSource;
2169-
if (this._currentDrawSourceID) {
2170-
this._model.sharedModel.updateSource(
2171-
this._currentDrawSourceID,
2172-
updatedJGISLayerSource,
2173-
);
2162+
if (this._currentDrawSource) {
2163+
const updatedJGISLayerSource: IJGISSource = {
2164+
name: this._currentDrawSource.name,
2165+
type: this._currentDrawSource.type,
2166+
parameters: {
2167+
data: updatedData,
2168+
},
2169+
};
2170+
2171+
this._currentDrawSource = updatedJGISLayerSource;
2172+
if (this._currentDrawSourceID) {
2173+
this._model.sharedModel.updateSource(
2174+
this._currentDrawSourceID,
2175+
updatedJGISLayerSource,
2176+
);
2177+
}
21742178
}
21752179
}
21762180
};
@@ -2181,39 +2185,39 @@ export class MainView extends React.Component<IProps, IStates> {
21812185
}
21822186
};
21832187

2184-
_updateDrawInteraction = () => {
2188+
_updateInteractions = () => {
21852189
if (this._draw) {
21862190
this._removeDrawInteraction();
21872191
}
2192+
if (this._select) {
2193+
this._removeSelectInteraction();
2194+
}
2195+
if (this._modify) {
2196+
this._removeModifyInteraction();
2197+
}
2198+
if (this._snap) {
2199+
this._removeSnapInteraction();
2200+
}
21882201
this._draw = new Draw({
21892202
style: drawInteractionStyle,
21902203
type: this._currentDrawGeometry,
21912204
source: this._currentVectorSource,
21922205
});
2193-
this._draw.setActive(true);
2194-
this._Map.addInteraction(this._draw);
2195-
};
2196-
2197-
_updateModifyInteraction = () => {
2198-
if (this._modify) {
2199-
this._removeModifyInteraction();
2200-
}
2206+
this._select = new Select();
22012207
this._modify = new Modify({
2202-
source: this._currentVectorSource,
2208+
features: this._select.getFeatures(),
22032209
});
2204-
this._modify.setActive(true);
2205-
this._Map.addInteraction(this._modify);
2206-
};
2207-
2208-
_updateSnapInteraction = () => {
2209-
if (this._snap) {
2210-
this._removeSnapInteraction();
2211-
}
22122210
this._snap = new Snap({
22132211
source: this._currentVectorSource,
22142212
});
2215-
this._snap.setActive(true);
2213+
this._Map.addInteraction(this._draw);
2214+
this._Map.addInteraction(this._select);
2215+
this._Map.addInteraction(this._modify);
22162216
this._Map.addInteraction(this._snap);
2217+
this._draw.setActive(true);
2218+
this._select.setActive(false);
2219+
this._modify.setActive(false);
2220+
this._snap.setActive(true);
22172221
};
22182222

22192223
_editVectorLayer = () => {
@@ -2223,51 +2227,45 @@ export class MainView extends React.Component<IProps, IStates> {
22232227
this._currentDrawLayerID,
22242228
);
22252229
if (this._currentVectorSource && this._currentDrawGeometry) {
2226-
this._updateDrawInteraction(); /* remove previous draw interactions and instantiate a new one */
2227-
this._updateSnapInteraction();
2228-
this._updateModifyInteraction();
2230+
this._updateInteractions(); /* remove previous interactions and instantiate new ones */
22292231
this._updateDrawSource(); /*add new features, update source and get changes reported to the JGIS Document in geoJSON format */
22302232
}
22312233
}
22322234
};
22332235

22342236
private _removeDrawInteraction = () => {
2237+
this._draw.setActive(false);
22352238
this._Map.removeInteraction(this._draw);
22362239
};
22372240

2241+
private _removeSelectInteraction = () => {
2242+
this._select.setActive(false);
2243+
this._Map.removeInteraction(this._select);
2244+
};
2245+
22382246
private _removeSnapInteraction = () => {
2247+
this._snap.setActive(false);
22392248
this._Map.removeInteraction(this._snap);
22402249
};
22412250

22422251
private _removeModifyInteraction = () => {
2252+
this._modify.setActive(false);
22432253
this._Map.removeInteraction(this._modify);
22442254
};
22452255

22462256
private _onVectorSourceChangeHandler = () => {
22472257
this._onVectorSourceChange();
22482258
};
22492259

2250-
private _onAwarenessChangeHandler = () => {
2260+
private _onSelectedLayerChangeHandler = () => {
22512261
const selectedLayers =
22522262
this._model.sharedModel.awareness.getLocalState()?.selected?.value;
22532263
const selectedLayerId = selectedLayers
22542264
? Object.keys(selectedLayers)[0]
2255-
: null;
2256-
2265+
: undefined;
22572266
if (selectedLayerId && selectedLayerId !== this._previousDrawLayerID) {
22582267
this._previousDrawLayerID = selectedLayerId;
22592268
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-
22712269
this._editVectorLayer();
22722270
}
22732271
};
@@ -2391,9 +2389,10 @@ export class MainView extends React.Component<IProps, IStates> {
23912389
private _loadingLayers: Set<string>;
23922390
private _originalFeatures: IDict<Feature<Geometry>[]> = {};
23932391
private _highlightLayer: VectorLayer<VectorSource>;
2394-
private _draw: Interaction;
2395-
private _snap: Interaction;
2396-
private _modify: Interaction;
2392+
private _draw: Draw;
2393+
private _snap: Snap;
2394+
private _modify: Modify;
2395+
private _select: Select;
23972396
private _currentDrawLayerID: string | undefined;
23982397
private _previousDrawLayerID: string | undefined;
23992398
private _currentDrawSource: IJGISSource | undefined;

0 commit comments

Comments
 (0)