Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapp.utils.polygonIntersectFeatures #1200

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/utils/_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import merge from './merge.mjs'

import paramString from './paramString.mjs'

import {polygonIntersectFeatures} from './polygonIntersectFeatures.mjs'

import promiseAll from './promiseAll.mjs'

import queryParams from './queryParams.mjs'
Expand Down Expand Up @@ -98,6 +100,7 @@ export default {
merge,
olScript,
paramString,
polygonIntersectFeatures,
promiseAll,
queryParams,
style,
Expand Down
59 changes: 59 additions & 0 deletions lib/utils/polygonIntersectFeatures.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
## mapp.utils.polygonIntersectFeatures()

@module /utils/polygonIntersectFeatures
*/

/**
* Checks if features intersect with a drawn polygon on a map view.
* @function polygonIntersectFeatures
* @param {Object} params - The parameters for the function.
* @param {Object} params.mapview - The map view instance.
* @param {Function} [params.drawendCallback] - Callback function to be called with intersecting features.
* @param {Object} [params.interaction] - Additional interaction options to be merged with the default config.
*/
export function polygonIntersectFeatures(params) {

if (!params.mapview) return;

function getArrayDepth(arr) {
return Array.isArray(arr) ?
1 + Math.max(0, ...arr.map(getArrayDepth)) :
0;
}

// Config for mapview draw interaction.
let interaction = {

// Draw polygon.
type: 'Polygon',

drawend: e => {

const poly = e.feature

const polyGeom = poly.getGeometry()

let features = params.mapview.interaction.snap.source.getFeatures()

features = features
.filter(feature => {

let coordinates = feature.getGeometry().getCoordinates()//.flat(2)

coordinates = coordinates.flat(getArrayDepth(coordinates) - 2)

return coordinates.some(coord => polyGeom.intersectsCoordinate(coord))
})

if (params.drawendCallback) {

params.drawendCallback(features)
}
},
...params
}

// Initiate drawing on mapview with config as interaction argument.
params.mapview.interactions.draw(interaction)
}
Loading