Skip to content

Commit 7c0dbda

Browse files
Update the newDrawVectorLayer command and the _handleDrawGeometryTypeChange method to update the source in the JupyterGISDoc.
1 parent 9b1ad34 commit 7c0dbda

File tree

5 files changed

+184
-34
lines changed

5 files changed

+184
-34
lines changed

editable.JGIS

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"layerTree": [
3+
"8de7c2c0-6024-4716-b542-031a89fb87f9",
4+
"3e21d680-406f-4099-bd9e-3a4edb9a2c8b"
5+
],
6+
"layers": {
7+
"3e21d680-406f-4099-bd9e-3a4edb9a2c8b": {
8+
"filters": {
9+
"appliedFilters": [],
10+
"logicalOp": "all"
11+
},
12+
"name": "Editable GeoJSON Layer",
13+
"parameters": {
14+
"color": {
15+
"circle-fill-color": "#f66151",
16+
"circle-radius": 5.0,
17+
"circle-stroke-color": "#62a0ea",
18+
"circle-stroke-line-cap": "round",
19+
"circle-stroke-line-join": "round",
20+
"circle-stroke-width": 1.25
21+
},
22+
"opacity": 1.0,
23+
"source": "348d85fa-3a71-447f-8a64-e283ec47cc7c",
24+
"symbologyState": {
25+
"renderType": "Single Symbol"
26+
},
27+
"type": "circle"
28+
},
29+
"type": "VectorLayer",
30+
"visible": true
31+
},
32+
"8de7c2c0-6024-4716-b542-031a89fb87f9": {
33+
"name": "OpenStreetMap.Mapnik Layer",
34+
"parameters": {
35+
"source": "b2ea427a-a51b-43ad-ae72-02cd900736d5"
36+
},
37+
"type": "RasterLayer",
38+
"visible": true
39+
}
40+
},
41+
"metadata": {},
42+
"options": {
43+
"bearing": 0.0,
44+
"extent": [
45+
-21859499.989213064,
46+
-5919636.226754084,
47+
5204122.278244857,
48+
14390404.527695213
49+
],
50+
"latitude": 35.52446437432016,
51+
"longitude": -74.80890180273175,
52+
"pitch": 0.0,
53+
"projection": "EPSG:3857",
54+
"zoom": 2.6670105136699993
55+
},
56+
"schemaVersion": "0.5.0",
57+
"sources": {
58+
"348d85fa-3a71-447f-8a64-e283ec47cc7c": {
59+
"name": "Editable GeoJSON Layer Source",
60+
"parameters": {
61+
"data": {
62+
"features": [
63+
{
64+
"geometry": {
65+
"coordinates": [
66+
102.0,
67+
0.5
68+
],
69+
"type": "Point"
70+
},
71+
"type": "Feature"
72+
},
73+
{
74+
"geometry": {
75+
"coordinates": [
76+
102.0,
77+
0.5
78+
],
79+
"type": "Point"
80+
},
81+
"type": "Feature"
82+
}
83+
],
84+
"type": "FeatureCollection"
85+
}
86+
},
87+
"type": "GeoJSONSource"
88+
},
89+
"b2ea427a-a51b-43ad-ae72-02cd900736d5": {
90+
"name": "OpenStreetMap.Mapnik",
91+
"parameters": {
92+
"attribution": "(C) OpenStreetMap contributors",
93+
"maxZoom": 19.0,
94+
"minZoom": 0.0,
95+
"provider": "OpenStreetMap",
96+
"url": "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
97+
"urlParameters": {}
98+
},
99+
"type": "RasterSource"
100+
}
101+
}
102+
}

packages/base/src/commands.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
22
IDict,
33
IJGISFormSchemaRegistry,
4+
IJGISLayer,
45
IJGISLayerBrowserRegistry,
56
IJGISLayerGroup,
67
IJGISLayerItem,
8+
IJGISSource,
79
IJupyterGISModel,
810
JgisCoordinates,
911
LayerType,
@@ -881,31 +883,56 @@ export function addCommands(
881883
commands.addCommand(CommandIDs.newDrawVectorLayer, {
882884
label: trans.__('Create New Draw Vector Layer'),
883885
isToggled: () => {
884-
const selectedLayer = getSingleSelectedLayer(tracker);
885-
if (!selectedLayer) {
886-
return false;
887-
}
888886
if (tracker.currentWidget instanceof JupyterGISDocumentWidget) {
889887
const model = tracker.currentWidget?.content.currentViewModel
890888
.jGISModel as IJupyterGISModel;
891-
const parameters = selectedLayer.parameters;
892-
if (parameters) {
889+
let selectedLayer = getSingleSelectedLayer(tracker);
890+
if (!selectedLayer) {
891+
const emptySource: IJGISSource = {
892+
name: 'Editable GeoJSON Layer Source',
893+
type: 'GeoJSONSource',
894+
parameters: {
895+
data: {
896+
type: 'FeatureCollection',
897+
features: []
898+
}
899+
}
900+
};
901+
const emptyLayer: IJGISLayer = {
902+
name: 'Editable GeoJSON Layer',
903+
type: 'VectorLayer',
904+
visible: true,
905+
parameters: {
906+
source: emptySource
907+
}
908+
};
909+
910+
selectedLayer = emptyLayer;
911+
model.addLayer('id', emptyLayer);
912+
model.sharedModel.updateLayer('id', emptyLayer);
913+
if (
914+
emptySource?.type === 'GeoJSONSource' &&
915+
emptySource?.parameters?.data
916+
) {
917+
return true;
918+
} else {
919+
return;
920+
false;
921+
}
922+
} else {
893923
const selectedSource = model.getSource(
894924
selectedLayer.parameters?.source
895925
);
896-
const canDrawVectorLayer =
926+
if (
897927
selectedSource?.type === 'GeoJSONSource' &&
898-
selectedSource?.parameters?.data;
899-
if (canDrawVectorLayer) {
900-
const selectedvectorLayerSourceId = parameters.source;
901-
model.selectedVectorLayerSourceId = selectedvectorLayerSourceId;
928+
selectedSource?.parameters?.data
929+
) {
930+
return true;
931+
} else {
932+
return false;
902933
}
903934
}
904-
905-
return model.isDrawVectorLayerEnabled;
906-
} else {
907-
return false;
908-
}
935+
} else {return false;}
909936
},
910937
isEnabled: () => {
911938
const selectedLayer = getSingleSelectedLayer(tracker);

packages/base/src/mainview/mainView.tsx

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import TemporalSlider from './TemporalSlider';
8686
import { MainViewModel } from './mainviewmodel';
8787
import { Spinner } from './spinner';
8888
import { Geometry, Point } from 'ol/geom';
89+
import BaseLayer from 'ol/layer/Base';
8990
import Draw from 'ol/interaction/Draw.js';
9091
import Modify from 'ol/interaction/Modify.js';
9192
import Snap from 'ol/interaction/Snap.js';
@@ -2034,28 +2035,29 @@ export class MainView extends React.Component<IProps, IStates> {
20342035
event: React.ChangeEvent<HTMLSelectElement>
20352036
) => {
20362037
const drawGeometryType = event.target.value;
2038+
20372039
if (this._model.isDrawVectorLayerEnabled) {
20382040
if (this._currentDrawInteraction) {
20392041
this._removeCurrentDrawInteraction();
20402042
}
2043+
const localState = this._model?.sharedModel.awareness.getLocalState();
2044+
const localStateLayer = localState?.selected?.value;
2045+
const localStateLayerID = Object.keys(localStateLayer)[0];
2046+
const jGISLayer = this._model.getLayer(localStateLayerID);
2047+
let jGISLayerSource = this._model.getSource(
2048+
jGISLayer?.parameters?.source
2049+
);
20412050

2042-
const layers = this._Map.getLayers().getArray();
2043-
const matchingLayer = layers.find(layer => {
2044-
const layerSource = layer.get('source');
2045-
return (
2046-
layerSource.get('id') === this._model.selectedVectorLayerSourceId
2047-
);
2048-
});
2049-
let source;
2050-
if (matchingLayer) {
2051-
source = matchingLayer.get('source');
2052-
} else {
2053-
source = new VectorSource();
2054-
}
2055-
const modify = new Modify({ source: source });
2051+
const layerSource: VectorSource | undefined = this._Map
2052+
.getLayers()
2053+
.getArray()
2054+
.find((layer: BaseLayer) => layer.get('id') === localStateLayerID)
2055+
?.get('source');
2056+
2057+
const modify = new Modify({ source: layerSource });
20562058
this._Map.addInteraction(modify);
20572059
const draw = new Draw({
2058-
source: source,
2060+
source: layerSource,
20592061
style: {
20602062
'fill-color': 'rgba(255, 255, 255, 0.2)',
20612063
'stroke-color': '#ffcc33',
@@ -2065,7 +2067,7 @@ export class MainView extends React.Component<IProps, IStates> {
20652067
},
20662068
type: drawGeometryType as Type // Type being a geometry type here
20672069
});
2068-
const snap = new Snap({ source: source });
2070+
const snap = new Snap({ source: layerSource });
20692071

20702072
this._Map.addInteraction(draw);
20712073
this._Map.addInteraction(snap);
@@ -2074,6 +2076,27 @@ export class MainView extends React.Component<IProps, IStates> {
20742076
...old,
20752077
drawGeometryType
20762078
}));
2079+
2080+
draw.on('drawend', event => {
2081+
if (jGISLayerSource) {
2082+
const updatedData = {
2083+
type: 'FeatureCollection',
2084+
features: layerSource?.getFeatures()
2085+
};
2086+
const updatedJGISLayerSource: IJGISSource = {
2087+
name: jGISLayerSource.name,
2088+
type: jGISLayerSource.type,
2089+
parameters: {
2090+
data: updatedData
2091+
}
2092+
};
2093+
jGISLayerSource = updatedJGISLayerSource;
2094+
this._model.sharedModel.updateSource(
2095+
localStateLayerID,
2096+
updatedJGISLayerSource
2097+
);
2098+
}
2099+
});
20772100
} else {
20782101
return;
20792102
}

packages/schema/src/interfaces.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ export interface IJupyterGISModel extends DocumentRegistry.IModel {
235235
disposed: ISignal<any, void>;
236236
isDrawVectorLayerEnabled: boolean;
237237
updateIsDrawVectorLayerEnabled(): void;
238-
selectedVectorLayerSourceId: string;
239238
}
240239

241240
export interface IUserData {

packages/schema/src/model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,6 @@ export class JupyterGISModel implements IJupyterGISModel {
791791

792792
public isDrawVectorLayerEnabled: boolean;
793793
public drawVectorLayerChanged = new Signal<this, boolean>(this);
794-
public selectedVectorLayerSourceId = '';
795794
}
796795

797796
export namespace JupyterGISModel {

0 commit comments

Comments
 (0)