@@ -6,8 +6,10 @@ import { MapConfig, PolarActionContext } from '@polar/lib-custom-types'
6
6
import { Dispatch } from 'vuex'
7
7
import { Feature , Map } from 'ol'
8
8
import { Polygon } from 'ol/geom'
9
- import { getWfsFeatures } from '@polar/lib-get-features'
9
+ import { parseWfsResponse } from '@polar/lib-get-features/wfs/parse '
10
10
import { FeatureCollection , Feature as GeoJsonFeature } from 'geojson'
11
+ import VectorLayer from 'ol/layer/Vector'
12
+ import VectorSource from 'ol/source/Vector'
11
13
import { DrawGetters , DrawState } from '../../types'
12
14
13
15
const loaderKey = 'drawLasso'
@@ -63,64 +65,56 @@ const getLassoRequests = (
63
65
lassoIds . reduce ( ( accumulator , id ) => {
64
66
const serviceDefinition = rawLayerList . getLayerWhere ( { id } )
65
67
66
- // TODO add WFS support
67
- // getWfsFeatures(null, serviceDefinition.url, ) FILTER-ONLY atm
68
+ const source = (
69
+ map
70
+ . getLayers ( )
71
+ . getArray ( )
72
+ . find ( ( layer ) => layer . get ( 'id' ) === id ) as VectorLayer
73
+ ) ?. getSource ?.( )
74
+ if ( ! ( source instanceof VectorSource ) ) {
75
+ console . warn (
76
+ `@polar/plugin-draw: Layer with ID "${ id } " configured for 'lasso', but it has no vector source. The layer does probably not hold any vector data.`
77
+ )
78
+ }
68
79
69
80
const [ codeName , codeNumber ] = map
70
81
. getView ( )
71
82
. getProjection ( )
72
83
. getCode ( )
73
84
. split ( ':' )
74
85
75
- const url = [
76
- serviceDefinition . url ,
77
- 'collections' ,
78
- serviceDefinition . collection ,
79
- `items?f=json&limit=100&bbox=${ lasso
80
- . getGeometry ( )
81
- ?. getExtent ( ) } &bbox-crs=http://www.opengis.net/def/crs/${ codeName } /0/${ codeNumber } &crs=http://www.opengis.net/def/crs/${ codeName } /0/${ codeNumber } `,
82
- ] . join ( '/' )
86
+ // TODO use ol/source/Vector::getFeaturesInExtent instead? not easy to ensure data was loaded in extent
87
+
88
+ if ( ! [ 'OAF' , 'WFS' ] . includes ( serviceDefinition . typ ) ) {
89
+ throw new Error ( 'AAAAAAAAAAAAAAAAAAAA' )
90
+ }
91
+
92
+ const url =
93
+ serviceDefinition . typ === 'OAF'
94
+ ? [
95
+ serviceDefinition . url ,
96
+ 'collections' ,
97
+ serviceDefinition . collection ,
98
+ `items?f=json&limit=100&bbox=${ lasso
99
+ . getGeometry ( )
100
+ ?. getExtent ( ) } &bbox-crs=http://www.opengis.net/def/crs/${ codeName } /0/${ codeNumber } &crs=http://www.opengis.net/def/crs/${ codeName } /0/${ codeNumber } `,
101
+ ] . join ( '/' )
102
+ : `${ serviceDefinition . url } ${ [
103
+ `?service=${ serviceDefinition . typ } ` ,
104
+ `version=${ serviceDefinition . version } ` ,
105
+ `request=GetFeature` ,
106
+ `srsName=${ map . getView ( ) . getProjection ( ) . getCode ( ) } ` ,
107
+ `typeName=${ serviceDefinition . featureType } ` ,
108
+ `bbox=${ lasso . getGeometry ( ) ?. getExtent ( ) } ,${ map
109
+ . getView ( )
110
+ . getProjection ( )
111
+ . getCode ( ) } `,
112
+ ] . join ( '&' ) } `
83
113
84
114
accumulator . push ( fetch ( url ) )
85
115
return accumulator
86
116
} , [ ] as Promise < Response > [ ] )
87
117
88
- /*
89
- TODO use snippet to confirm the layer type worked on
90
- const source = (
91
- map
92
- .getLayers()
93
- .getArray()
94
- .find((layer) => layer.get('id') === layerId) as VectorLayer
95
- )?.getSource?.()
96
- if (source instanceof VectorSource) {
97
- accumulator.push(new Snap({ source }))
98
- } else {
99
- console.warn(
100
- `@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.`
101
- )
102
- }
103
- */
104
-
105
- const handleSettledRequests = (
106
- resolutions : PromiseSettledResult < Response > [ ] ,
107
- dispatch : Dispatch
108
- ) =>
109
- Promise . all (
110
- (
111
- resolutions . filter ( ( promiseSettledResult ) => {
112
- if ( promiseSettledResult . status === 'rejected' ) {
113
- console . error ( promiseSettledResult . reason )
114
- // TODO toast
115
- return false
116
- }
117
- return true
118
- } ) as PromiseFulfilledResult < Response > [ ]
119
- ) . map (
120
- async ( resolution ) => ( await resolution . value . json ( ) ) as FeatureCollection
121
- )
122
- )
123
-
124
118
const handleFulfilledRequests = (
125
119
featureCollections : FeatureCollection [ ] ,
126
120
dispatch : Dispatch
@@ -162,7 +156,22 @@ export default function ({
162
156
}
163
157
Promise . allSettled ( requests )
164
158
. then ( ( settledRequests ) =>
165
- handleSettledRequests ( settledRequests , dispatch )
159
+ Promise . all (
160
+ (
161
+ settledRequests . filter ( ( promiseSettledResult ) => {
162
+ if ( promiseSettledResult . status === 'rejected' ) {
163
+ console . error ( promiseSettledResult . reason )
164
+ // TODO toast
165
+ return false
166
+ }
167
+ return true
168
+ } ) as PromiseFulfilledResult < Response > [ ]
169
+ ) . map ( async ( resolution , index ) =>
170
+ rawLayerList . getLayerWhere ( { id : lassoIds [ index ] } ) . typ === 'WFS'
171
+ ? await parseWfsResponse ( resolution . value , undefined , false )
172
+ : ( ( await resolution . value . json ( ) ) as FeatureCollection )
173
+ )
174
+ )
166
175
)
167
176
. then ( ( fulfilledRequests ) =>
168
177
handleFulfilledRequests ( fulfilledRequests , dispatch )
0 commit comments