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

ResourceProvider: Add support to filter ResourceSet features #1838

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion packages/resources-provider-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ ___Note: Can only be used with Signal K server version 2.0.0 or later.___

---

## v1.2.0
## v1.3.1

- **Added**: Filter `ResourceSet` features based on `distance` query.

## v1.3.0

- **Update**: Update plugin configuration to include `charts`.

Expand Down
2 changes: 1 addition & 1 deletion packages/resources-provider-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@signalk/resources-provider",
"version": "1.3.0",
"version": "1.3.1",
"description": "Resources provider plugin for Signal K server.",
"main": "plugin/index.js",
"keywords": [
Expand Down
18 changes: 17 additions & 1 deletion packages/resources-provider-plugin/src/lib/filestorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
} from 'fs/promises'
import path from 'path'
import { IResourceStore, StoreRequestParams } from '../types'
import { passFilter, processParameters } from './utils'
import {
passFilter,
processParameters,
isResourceSet,
isGeomInPolygon
} from './utils'

export const getUuid = (skIdentifier: string) =>
skIdentifier.split(':').slice(-1)[0]
Expand Down Expand Up @@ -155,6 +160,17 @@ export class FileStore implements IResourceStore {
)
// apply param filters
if (passFilter(res, type, params)) {
if (isResourceSet(res) && params.geobounds) {
if (
res.values?.type === 'FeatureCollection' &&
Array.isArray(res.values?.features)
) {
const features = res.values?.features.filter((f: any) =>
isGeomInPolygon(f.geometry, params.geobounds)
)
res.values.features = features
}
}
const uuid = files[f].name
result[uuid] = res
const stats = await stat(path.join(rt.path, files[f].name))
Expand Down
73 changes: 44 additions & 29 deletions packages/resources-provider-plugin/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export const inBounds = (
let ok = false
switch (type) {
case 'notes':
case 'waypoints':
if (val?.feature?.geometry?.coordinates) {
ok = isPointInPolygon(val?.feature?.geometry?.coordinates, polygon)
}
if (val.position) {
ok = isPointInPolygon(val.position, polygon)
}
Expand All @@ -33,34 +29,50 @@ export const inBounds = (
}
break
case 'routes':
if (val.feature.geometry.coordinates) {
val.feature.geometry.coordinates.forEach((pt: any) => {
ok = ok || isPointInPolygon(pt, polygon)
})
}
break
case 'regions':
if (
val.feature.geometry.coordinates &&
val.feature.geometry.coordinates.length > 0
) {
if (val.feature.geometry.type == 'Polygon') {
val.feature.geometry.coordinates.forEach((ls: any) => {
ls.forEach((pt: any) => {
ok = ok || isPointInPolygon(pt, polygon)
})
})
} else if (val.feature.geometry.type == 'MultiPolygon') {
val.feature.geometry.coordinates.forEach((polygon: any) => {
polygon.forEach((ls: any) => {
ls.forEach((pt: any) => {
ok = ok || isPointInPolygon(pt, polygon)
})
})
})
}
case 'waypoints':
if (val.feature) {
ok = ok || isGeomInPolygon(val.feature.geometry, polygon)
}
break
default:
ok = true
}
return ok
}

// test if supplied geometry contains a position inside the polygon
export const isGeomInPolygon = (
geom: any,
polygon: GeolibInputCoordinates[]
) => {
let ok = false
if (!Array.isArray(geom.coordinates)) {
return false
}
if (geom.type === 'Point') {
ok = ok || isPointInPolygon(geom.coordinates, polygon)
}
if (geom.type === 'MultiPoint' || geom.type === 'LineString') {
geom.coordinates.forEach((pt: any) => {
ok = ok || isPointInPolygon(pt, polygon)
})
}
if (geom.type === 'MultiLineString' || geom.type === 'Polygon') {
geom.coordinates.forEach((line: any) => {
line.forEach((pt: any) => {
ok = ok || isPointInPolygon(pt, polygon)
})
})
}
if (geom.type === 'MultiPolygon') {
geom.coordinates.forEach((polygon: any) => {
polygon.forEach((line: any) => {
line.forEach((pt: any) => {
ok = ok || isPointInPolygon(pt, polygon)
})
})
})
}
return ok
}
Expand Down Expand Up @@ -182,3 +194,6 @@ export const toPolygon = (bbox: number[]): GeolibInputCoordinates[] => {
}
return polygon
}

export const isResourceSet = (res: any) =>
res.type && res.type === 'ResourceSet'