Skip to content

Commit 06bd465

Browse files
committed
add lasso draft
1 parent 521a46c commit 06bd465

File tree

14 files changed

+155
-4
lines changed

14 files changed

+155
-4
lines changed

packages/clients/diplan/example/config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ export default {
9898
},
9999
draw: {
100100
enableOptions: true,
101+
lassos: [
102+
{
103+
id: flurstuecke,
104+
},
105+
],
101106
measureOptions: {
102107
metres: true,
103108
kilometres: true,
@@ -111,6 +116,7 @@ export default {
111116
family: 'Arial',
112117
},
113118
},
119+
toastAction: 'plugin/toast/addToast',
114120
style: {
115121
fill: { color: 'rgb(51 117 212 / 50%)' },
116122
stroke: {

packages/clients/diplan/example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ <h3>Steuerung von außen</h3>
2222
<button id="action-toast">Toast</button>
2323
<button id="action-load-geojson">GeoJSON hinzufügen</button>
2424
<button id="action-zoom-to-all">Zoome auf Zeichnung</button>
25+
<button id="action-lasso">Lasso-Funktion</button>
2526
<label>
2627
Adresssuche (erstbestes Ergebnis):
2728
<input id="action-address-search-filler"/>

packages/clients/diplan/example/prod-example.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ <h3>Steuerung von außen</h3>
2222
<button id="action-toast">Toast</button>
2323
<button id="action-load-geojson">GeoJSON hinzufügen</button>
2424
<button id="action-zoom-to-all">Zoome auf Zeichnung</button>
25+
<button id="action-lasso">Lasso-Funktion</button>
2526
<label>
2627
Adresssuche (erstbestes Ergebnis):
2728
<input id="action-address-search-filler"/>

packages/clients/diplan/example/setup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default (client, layerConf, config) => {
7070
const actionFillAddressSearch = document.getElementById(
7171
'action-address-search-filler'
7272
)
73+
const actionLasso = document.getElementById('action-lasso')
7374

7475
actionPlus.onclick = () =>
7576
mapInstance.$store.dispatch('plugin/zoom/increaseZoomLevel')
@@ -96,6 +97,8 @@ export default (client, layerConf, config) => {
9697
autoselect: 'first',
9798
})
9899
)
100+
actionLasso.onclick = () =>
101+
mapInstance.$store.dispatch('plugin/draw/setMode', 'lasso')
99102

100103
const htmlZoom = document.getElementById('subscribed-zoom')
101104
const htmlGfi = document.getElementById('subscribed-gfi')

packages/clients/diplan/src/polar-client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ polarCore.addPlugins([
4242
id: 'layerChooser',
4343
},
4444
{
45-
plugin: Draw({}),
45+
plugin: Draw({
46+
addLoading: 'plugin/loadingIndicator/addLoadingKey',
47+
removeLoading: 'plugin/loadingIndicator/removeLoadingKey',
48+
}),
4649
icon: '$vuetify.icons.ruler',
4750
id: 'draw',
4851
},

packages/plugins/Draw/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ The styling of the drawn features can be configured to overwrite the default ol-
3333

3434
| fieldName | type | description |
3535
| - | - | - |
36+
| addLoading | string? | Expects the path to a mutation within the store. This mutation is committed with a plugin-specific loading key as payload when starting asynchronous procedures that are intended to be communicated to the user. |
3637
| enableOptions | boolean? | If `true`, draw options are displayed, like choosing and changing stroke color. Not available for texts features. Defaults to `false`. |
38+
| lassos | lasso[]? | Allows configuring lasso options. The lasso function allows free-hand drawing a geometry on the map; features completely fitting into that geometry will be copied up to the draw layer from all configured layers. 💡 Please mind that the lasso function currently has no UI in the client, and the functionality must be triggered externally. |
39+
| removeLoading | string? | Expects the path to a mutation within the store. This mutation is committed with a plugin-specific loading key as payload when finishing asynchronous procedures that are intended to be communicated to the user. |
3740
| measureOptions | measureOptions? | If set, an additional radio is being shown to the user to be able to let the (then) drawn features display their length and / or area. See [draw.measureOptions](#drawmeasureoptions) for further information. Not shown by default. |
3841
| selectableDrawModes | string[]? | List 'Point', 'LineString', 'Circle', 'Text' and/or 'Polygon' as desired. All besides 'Text' are selectable by default. |
3942
| snapTo | string[]? | Accepts an array of layer IDs. If these layers are active, they are used as snapping material for geometry manipulation. The Draw layer will also always snap to its own features regardless. Please mind that used layers must provide vector data. The layers referred to must be configured in `mapConfiguration.layers`. |
4043
| style | style? | Please see example below for styling options. Defaults to standard OpenLayers styling. |
4144
| textStyle | textStyle? | Use this object with properties 'font' and 'textColor' to style text feature. |
45+
| toastAction | string? | This string will be used as action to send a toast information to the user to clarify why something happened in edge cases. If this is not defined, the information will be printed to the console for debugging purposes instead. |
4246

4347
For details on the `displayComponent` attribute, refer to the [Global Plugin Parameters](../../core/README.md#global-plugin-parameters) section of `@polar/core`.
4448

@@ -55,8 +59,8 @@ draw: {
5559
},
5660
},
5761
style: {
58-
fill: {
59-
color: 'rgba(255, 255, 255, 0.5)'
62+
fill: {
63+
color: 'rgba(255, 255, 255, 0.5)'
6064
},
6165
stroke: {
6266
color: '#e51313',
@@ -72,6 +76,13 @@ draw: {
7276

7377
</details>
7478

79+
#### draw.lasso
80+
81+
| fieldName | type | description |
82+
| - | - | - |
83+
| id | string | The layer id of a vector layer to copy up vector features from. |
84+
| minZoom | boolean | Defaults to `true`. If a boolean is given, the `minZoom` (if configured) of the `LayerConfiguration` in `mapConfiguration.layers` will be adhered to when copying up geometries from the source. This is to prevent the client from overly burdening feature-rich vector services on accident. |
85+
7586
#### draw.measureOptions
7687

7788
| fieldName | type | description |

packages/plugins/Draw/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
"@turf/center-of-mass": "^7.2.0"
2828
},
2929
"peerDependencies": {
30+
"@masterportal/masterportalapi": "2.45.0",
3031
"@polar/core": "^3.0.0",
3132
"@repositoryname/vuex-generators": "^1.1.2",
33+
"i18next": "^23.11.5",
3234
"ol": "^10.3.1",
3335
"vue": "^2.6.14",
3436
"vuex": "^3.6.2"

packages/plugins/Draw/src/locales.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export const resourcesDe = {
4040
label: {
4141
textSize: 'Textgröße (px) wählen:',
4242
},
43+
lasso: {
44+
notInZoomRange:
45+
'Der Layer "$t({{serviceName}})" wurde nicht für die Lasso-Funktion genutzt, da er auf der derzeitigen Zoomstufe nicht aktivierbar ist. Bitte zoomen Sie weiter rein.',
46+
},
4347
},
4448
},
4549
} as const
@@ -84,6 +88,10 @@ export const resourcesEn = {
8488
label: {
8589
textSize: 'Choose text size (px):',
8690
},
91+
lasso: {
92+
notInZoomRange:
93+
'The layer "$t({{serviceName}})" was not used for the lasso as it is not activatable on the current zoom level. Please zoom in further.',
94+
},
8795
},
8896
},
8997
} as const

packages/plugins/Draw/src/store/actions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import createInteractions from './createInteractions'
1111
import createModifyInteractions from './createInteractions/createModifyInteractions'
1212
import createTextInteractions from './createInteractions/createTextInteractions'
1313
import createTranslateInteractions from './createInteractions/createTranslateInteractions'
14+
import createLassoInteractions from './createInteractions/createLassoInteractions'
1415
import modifyDrawStyle from './createInteractions/modifyDrawStyle'
1516
import modifyTextStyle from './createInteractions/modifyTextStyle'
1617
import createDrawInteractions from './createInteractions/createDrawInteractions'
@@ -25,6 +26,7 @@ export const makeActions = () => {
2526
const actions: PolarActionTree<DrawState, DrawGetters> = {
2627
createInteractions,
2728
createDrawInteractions,
29+
createLassoInteractions,
2830
createModifyInteractions,
2931
createTranslateInteractions,
3032
createTextInteractions,
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import Draw from 'ol/interaction/Draw'
2+
import Interaction from 'ol/interaction/Interaction'
3+
import i18next from 'i18next'
4+
import { rawLayerList } from '@masterportal/masterportalapi'
5+
import { PolarActionContext } from '@polar/lib-custom-types'
6+
import { DrawGetters, DrawState } from '../../types'
7+
8+
const loaderKey = 'drawLasso'
9+
10+
/*
11+
TODO use snippet to confirm the layer type worked on
12+
const source = (
13+
map
14+
.getLayers()
15+
.getArray()
16+
.find((layer) => layer.get('id') === layerId) as VectorLayer
17+
)?.getSource?.()
18+
if (source instanceof VectorSource) {
19+
accumulator.push(new Snap({ source }))
20+
} else {
21+
console.warn(
22+
`@polar/plugin-draw: Layer with ID "${layerId}" configured for 'snapTo', but it has no source to snap to. The layer does probably not hold any vector data.`
23+
)
24+
}
25+
*/
26+
27+
// TODO un – refactor after done
28+
// eslint-disable-next-line max-lines-per-function
29+
export default function ({
30+
rootGetters,
31+
commit,
32+
dispatch,
33+
}: PolarActionContext<DrawState, DrawGetters>): Interaction[] {
34+
const draw = new Draw({
35+
type: 'Polygon',
36+
freehand: true,
37+
})
38+
const { addLoading, removeLoading } = rootGetters.configuration?.draw || {}
39+
// TODO un
40+
// eslint-disable-next-line max-lines-per-function
41+
draw.on('drawend', (e) => {
42+
dispatch('setMode', 'none')
43+
44+
const drawnLasso = e.feature
45+
const lassos = rootGetters.configuration.draw?.lassos || []
46+
const toastAction = rootGetters.configuration.draw?.toastAction || ''
47+
const promises = lassos.reduce((accumulator, { id, minZoom = true }) => {
48+
const layerConfig = rootGetters.configuration.layers?.find(
49+
(layer) => id === layer.id
50+
)
51+
if (
52+
minZoom &&
53+
layerConfig &&
54+
typeof layerConfig.minZoom !== 'undefined' &&
55+
rootGetters.zoomLevel < layerConfig.minZoom
56+
) {
57+
if (toastAction) {
58+
dispatch(toastAction, {
59+
type: 'info',
60+
text: i18next.t('plugins.draw.lasso.notInZoomRange', {
61+
serviceName: layerConfig.name || id,
62+
}),
63+
timeout: 10000,
64+
})
65+
} else {
66+
console.warn(
67+
`Lasso not used with layer with id "${id}". (minZoom not reached)`
68+
)
69+
}
70+
return accumulator
71+
}
72+
const serviceDefinition = rawLayerList.getLayerWhere({ id })
73+
74+
// TODO implement for OAF, use existing getFeatures for WFS
75+
// https://api.hamburg.de/datasets/v1/alkis_vereinfacht/collections/Flurstueck/items?f=json&limit=10&bbox=10.0872,53.5357,10.0883,53.5362
76+
77+
accumulator.push(new Promise(() => ({})))
78+
return accumulator
79+
}, [] as Promise<object>[])
80+
81+
Promise.all(promises)
82+
.then((resolutions) => {
83+
if (addLoading) {
84+
commit(addLoading, loaderKey, { root: true })
85+
}
86+
87+
// TODO parse resolutions
88+
// TODO filter with drawnLasso (we're probably just loading by bbox?)
89+
// TODO add as geoJSON to draw
90+
91+
console.warn(resolutions)
92+
93+
dispatch('addFeatures', { geoJSON: {} })
94+
})
95+
.catch(() => {
96+
/* TODO at least toast something */
97+
})
98+
.finally(
99+
() => removeLoading && commit(removeLoading, loaderKey, { root: true })
100+
)
101+
})
102+
103+
return [draw]
104+
}

packages/plugins/Draw/src/store/createInteractions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default function (
1515
return dispatch('createTranslateInteractions', { drawSource, drawLayer })
1616
} else if (mode === 'delete') {
1717
return createDeleteInteractions(drawSource, drawLayer)
18+
} else if (mode === 'lasso') {
19+
return dispatch('createLassoInteractions')
1820
}
1921
return []
2022
}

packages/plugins/Draw/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface PolarVectorOptions {
1818
style?: StyleLike
1919
}
2020

21-
export type Mode = 'none' | 'draw' | 'edit' | 'translate' | 'delete'
21+
export type Mode = 'none' | 'draw' | 'edit' | 'translate' | 'delete' | 'lasso'
2222

2323
export interface CreateInteractionsPayload {
2424
drawSource: VectorSource

packages/types/custom/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## unpublished
44

55
- Feature: Add `snapTo` to `DrawConfiguration` for specification of layers to snap to.
6+
- Feature: Add `lassos` to `DrawConfiguration`. With this, `addLoading`, `removeLoading`, and `toastAction` have also been introduced to allow the feature to use other plugins via API calls.
67

78
## 2.0.0
89

packages/types/custom/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,19 @@ export interface MeasureOptions {
196196
}
197197

198198
export interface DrawConfiguration extends Partial<PluginOptions> {
199+
addLoading?: string
199200
enableOptions?: boolean
201+
lassos?: {
202+
id: string
203+
minZoom: boolean
204+
}[]
200205
measureOptions?: MeasureOptions
206+
removeLoading?: string
201207
selectableDrawModes?: DrawMode[]
202208
snapTo?: string[]
203209
style?: DrawStyle
204210
textStyle?: TextStyle
211+
toastAction?: string
205212
}
206213

207214
export interface ExportConfiguration extends PluginOptions {

0 commit comments

Comments
 (0)