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

Remove dependency on deck.gl from ElevationHandler #223

Merged
merged 4 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions examples/deck/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SelectionLayer,
CompositeModeHandler,
ModifyHandler,
ElevationHandler,
DrawLineStringHandler,
ElevatedEditHandleLayer,
SELECTION_TYPE
Expand Down Expand Up @@ -532,6 +533,14 @@ export default class Example extends Component<
width: window.innerWidth
};

if (mode === 'elevation') {
modeConfig.calculateElevationChange = ({ pointerDownScreenCoords, screenCoords }) =>
ElevationHandler.calculateElevationChangeWithViewport(viewport, {
pointerDownScreenCoords,
screenCoords
});
}

const editableGeoJsonLayer = new EditableGeoJsonLayer({
id: 'geojson',
data: testFeatures,
Expand Down
43 changes: 38 additions & 5 deletions modules/layers/src/mode-handlers/elevation-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ import type { EditAction } from './mode-handler.js';
import { getPickedEditHandle } from './mode-handler.js';
import { ModifyHandler } from './modify-handler.js';

function defaultCalculateElevationChange({
pointerDownScreenCoords,
screenCoords
}: {
pointerDownScreenCoords: Position,
screenCoords: Position
}) {
return 10 * (pointerDownScreenCoords[1] - screenCoords[1]);
}

export class ElevationHandler extends ModifyHandler {
makeElevatedEvent(event: PointerMoveEvent | StopDraggingEvent, position: Position): Object {
const { min = 0, max = 20000 } = this._modeConfig || {};
if (!event.pointerDownScreenCoords) {
return event;
}

const [, yBot] = this._context.viewport.project([position[0], position[1], 0]);
const [, yTop] = this._context.viewport.project([position[0], position[1], 1000]);
const [, y] = event.screenCoords;
const { min = 0, max = 20000, calculateElevationChange = defaultCalculateElevationChange } =
this._modeConfig || {};

let elevation = ((yBot - y) * 1000.0) / (yBot - yTop);
// $FlowFixMe - really, I know it has something at index 2
let elevation = position.length === 3 ? position[2] : 0;
elevation += calculateElevationChange({
pointerDownScreenCoords: event.pointerDownScreenCoords,
screenCoords: event.screenCoords
});
elevation = Math.min(elevation, max);
elevation = Math.max(elevation, min);

Expand Down Expand Up @@ -41,4 +57,21 @@ export class ElevationHandler extends ModifyHandler {
}
return cursor;
}

static calculateElevationChangeWithViewport(
viewport: any,
{
pointerDownScreenCoords,
screenCoords
}: {
pointerDownScreenCoords: Position,
screenCoords: Position
}
): number {
// Source: https://gis.stackexchange.com/a/127949/111804
const metersPerPixel =
(156543.03392 * Math.cos((viewport.latitude * Math.PI) / 180)) / Math.pow(2, viewport.zoom);

return metersPerPixel * (pointerDownScreenCoords[1] - screenCoords[1]);
}
}