-
Notifications
You must be signed in to change notification settings - Fork 60
fix(editable-layers): correct SelectionLayer polygon picking for elongated lassos #635
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
Open
llamington
wants to merge
1
commit into
visgl:master
Choose a base branch
from
llamington:fix/selection-layer-polygon-without-pickobjects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,15 @@ | |
|
|
||
| /* eslint-env browser */ | ||
|
|
||
| import type {CompositeLayerProps, DefaultProps} from '@deck.gl/core'; | ||
| import type {CompositeLayerProps, DefaultProps, Layer, PickingInfo} from '@deck.gl/core'; | ||
| import {CompositeLayer} from '@deck.gl/core'; | ||
| import {PolygonLayer} from '@deck.gl/layers'; | ||
| import {featureCollection, polygon} from '@turf/helpers'; | ||
| import turfBuffer from '@turf/buffer'; | ||
| import turfDifference from '@turf/difference'; | ||
| import booleanPointInPolygon from '@turf/boolean-point-in-polygon'; | ||
| import {point, polygon} from '@turf/helpers'; | ||
|
|
||
| import {EditableGeoJsonLayer} from './editable-geojson-layer'; | ||
| import {DrawRectangleMode} from '../edit-modes/draw-rectangle-mode'; | ||
| import {DrawPolygonMode} from '../edit-modes/draw-polygon-mode'; | ||
| import {DrawRectangleMode} from '../edit-modes/draw-rectangle-mode'; | ||
| import {ViewMode} from '../edit-modes/view-mode'; | ||
| import {EditableGeoJsonLayer} from './editable-geojson-layer'; | ||
|
|
||
| export const SELECTION_TYPE = { | ||
| NONE: null, | ||
|
|
@@ -49,9 +47,7 @@ const EMPTY_DATA = { | |
| features: [] | ||
| }; | ||
|
|
||
| const EXPANSION_KM = 50; | ||
| const LAYER_ID_GEOJSON = 'selection-geojson'; | ||
| const LAYER_ID_BLOCKER = 'selection-blocker'; | ||
|
|
||
| const PASS_THROUGH_PROPS = [ | ||
| 'lineWidthScale', | ||
|
|
@@ -75,18 +71,13 @@ const PASS_THROUGH_PROPS = [ | |
| 'getTentativeFillColor', | ||
| 'getTentativeLineWidth' | ||
| ]; | ||
|
|
||
| export class SelectionLayer<DataT, ExtraPropsT> extends CompositeLayer< | ||
| ExtraPropsT & Required<SelectionLayerProps<DataT>> | ||
| > { | ||
| static layerName = 'SelectionLayer'; | ||
| static defaultProps = defaultProps; | ||
|
|
||
| state: { | ||
| pendingPolygonSelection: { | ||
| bigPolygon: ReturnType<typeof turfDifference>; | ||
| }; | ||
| } = undefined!; | ||
|
|
||
| _selectRectangleObjects(coordinates: any) { | ||
| const {layerIds, onSelect} = this.props; | ||
| const [x1, y1] = this.context.viewport.project(coordinates[0][0]); | ||
|
|
@@ -104,57 +95,26 @@ export class SelectionLayer<DataT, ExtraPropsT> extends CompositeLayer< | |
|
|
||
| _selectPolygonObjects(coordinates: any) { | ||
| const {layerIds, onSelect} = this.props; | ||
| const mousePoints = coordinates[0].map(c => this.context.viewport.project(c)); | ||
|
|
||
| const allX = mousePoints.map(mousePoint => mousePoint[0]); | ||
| const allY = mousePoints.map(mousePoint => mousePoint[1]); | ||
| const x = Math.min(...allX); | ||
| const y = Math.min(...allY); | ||
| const maxX = Math.max(...allX); | ||
| const maxY = Math.max(...allY); | ||
|
|
||
| // Use a polygon to hide the outside, because pickObjects() | ||
| // does not support polygons | ||
| const landPointsPoly = polygon(coordinates); | ||
| const bigBuffer = turfBuffer(landPointsPoly, EXPANSION_KM); | ||
| let bigPolygon; | ||
| try { | ||
| // turfDifference throws an exception if the polygon | ||
| // intersects with itself (TODO: check if true in all versions) | ||
| bigPolygon = turfDifference(featureCollection([bigBuffer, landPointsPoly])); | ||
| } catch (e) { | ||
| // invalid selection polygon | ||
| console.log('turfDifference() error', e); // eslint-disable-line | ||
| return; | ||
| } | ||
|
|
||
| this.setState({ | ||
| pendingPolygonSelection: { | ||
| bigPolygon | ||
| } | ||
| }); | ||
|
|
||
| const blockerId = `${this.props.id}-${LAYER_ID_BLOCKER}`; | ||
|
|
||
| // HACK, find a better way | ||
| setTimeout(() => { | ||
| const pickingInfos = this.context.deck.pickObjects({ | ||
| x, | ||
| y, | ||
| width: maxX - x, | ||
| height: maxY - y, | ||
| layerIds: [blockerId, ...layerIds] | ||
| const selectionPolygon = polygon(coordinates); | ||
|
|
||
| const pickingInfos: SelectionPickingInfo[] = this.context.layerManager | ||
| .getLayers() | ||
| .filter(layer => layerIds.includes(layer.id)) | ||
| .flatMap(layer => { | ||
| const data = layer.props.data; | ||
| if (!Array.isArray(data)) return []; | ||
| return data.flatMap((object, index): SelectionPickingInfo[] => { | ||
| const position = extractPosition(layer, object, index, data); | ||
| if (position === null) return []; | ||
| if (!booleanPointInPolygon(point(position), selectionPolygon)) return []; | ||
| return [{object, layer, index}]; | ||
| }); | ||
| }); | ||
|
|
||
| onSelect({ | ||
| pickingInfos: pickingInfos.filter(item => item.layer.id !== this.props.id) | ||
| }); | ||
| }, 250); | ||
| onSelect({pickingInfos}); | ||
| } | ||
|
|
||
| renderLayers() { | ||
| const {pendingPolygonSelection} = this.state; | ||
|
|
||
| const mode = MODE_MAP[this.props.selectionType] || ViewMode; | ||
| const modeConfig = MODE_CONFIG_MAP[this.props.selectionType]; | ||
|
|
||
|
|
@@ -163,7 +123,7 @@ export class SelectionLayer<DataT, ExtraPropsT> extends CompositeLayer< | |
| if (this.props[p] !== undefined) inheritedProps[p] = this.props[p]; | ||
| }); | ||
|
|
||
| const layers: any[] = [ | ||
| return [ | ||
| new EditableGeoJsonLayer( | ||
| this.getSubLayerProps({ | ||
| id: LAYER_ID_GEOJSON, | ||
|
|
@@ -187,29 +147,41 @@ export class SelectionLayer<DataT, ExtraPropsT> extends CompositeLayer< | |
| }) | ||
| ) | ||
| ]; | ||
|
|
||
| if (pendingPolygonSelection) { | ||
| const {bigPolygon} = pendingPolygonSelection as any; | ||
| layers.push( | ||
| new PolygonLayer( | ||
| this.getSubLayerProps({ | ||
| id: LAYER_ID_BLOCKER, | ||
| pickable: true, | ||
| stroked: false, | ||
| opacity: 1.0, | ||
| data: [bigPolygon], | ||
| getLineColor: _obj => [0, 0, 0, 1], | ||
| getFillColor: _obj => [0, 0, 0, 1], | ||
| getPolygon: o => o.geometry.coordinates | ||
| }) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return layers; | ||
| } | ||
|
|
||
| shouldUpdateState({changeFlags: {stateChanged, propsOrDataChanged}}: Record<string, any>) { | ||
| return stateChanged || propsOrDataChanged; | ||
| } | ||
| } | ||
|
|
||
| type SelectionPickingInfo = Pick<PickingInfo, 'object' | 'layer' | 'index'>; | ||
|
|
||
| function extractPosition( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we want to extract a 3D position here for any reason? @felixpalmer |
||
| layer: Layer, | ||
| object: unknown, | ||
| index: number, | ||
| data: unknown | ||
| ): [number, number] | null { | ||
| const props = layer.props; | ||
| if ('getPosition' in props && typeof props.getPosition === 'function') { | ||
| const result = props.getPosition(object, {index, data, target: []}); | ||
| if (Array.isArray(result) && typeof result[0] === 'number' && typeof result[1] === 'number') { | ||
| return [result[0], result[1]]; | ||
| } | ||
| } | ||
| if (typeof object === 'object' && object !== null) { | ||
| if ('position' in object) { | ||
| const p = object.position; | ||
| if (Array.isArray(p) && typeof p[0] === 'number' && typeof p[1] === 'number') { | ||
| return [p[0], p[1]]; | ||
| } | ||
| } | ||
| if ('coordinates' in object) { | ||
| const c = object.coordinates; | ||
| if (Array.isArray(c) && typeof c[0] === 'number' && typeof c[1] === 'number') { | ||
| return [c[0], c[1]]; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-geo may not all pass this conditional check
This works better than the current approach, so its not blocking, but these instances of turf should be triaged under a more careful pass - which may have been the original intention of the GPU-based selections?
CC @eKerney