forked from uber/nebula.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'Draw Square From Center' mode (uber#531)
- Loading branch information
1 parent
d2b9610
commit 6bfc647
Showing
7 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
46 changes: 46 additions & 0 deletions
46
modules/edit-modes/src/lib/draw-square-from-center-mode.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import bboxPolygon from '@turf/bbox-polygon'; | ||
import turfDistance from '@turf/distance'; | ||
import turfAlong from '@turf/along'; | ||
import { point, lineString as turfLineString } from '@turf/helpers'; | ||
import { Position, Polygon, FeatureOf } from '../geojson-types'; | ||
import { TwoClickPolygonMode } from './two-click-polygon-mode'; | ||
|
||
export class DrawSquareFromCenterMode extends TwoClickPolygonMode { | ||
getTwoClickPolygon(coord1: Position, coord2: Position, modeConfig: any): FeatureOf<Polygon> { | ||
// get the coordinates of the other two rectangle vertices | ||
const coord3 = [coord2[0], coord1[1]]; | ||
const coord4 = [coord1[0], coord2[1]]; | ||
|
||
// determine the shortest distance to the origin, which will be the length of each square side | ||
const distance1 = turfDistance(point(coord3), point(coord1)); | ||
const distance2 = turfDistance(point(coord4), point(coord1)); | ||
const shortestDistance = distance1 <= distance2 ? distance1 : distance2; | ||
|
||
// determine which coordinate pair of the two is closest to the origin | ||
const closestPoint = distance1 <= distance2 ? coord3 : coord4; | ||
|
||
// create a linestring which will used to locate the second square vertex | ||
const line = turfLineString([closestPoint, coord2]); | ||
|
||
// get the coordinates of the second square vertex | ||
const newPoint = turfAlong(line, shortestDistance); | ||
const corner = newPoint.geometry.coordinates; | ||
|
||
// determine the longitude and latitude values of the opposite corner | ||
const longitude = | ||
coord1[0] > corner[0] | ||
? coord1[0] + Math.abs(coord1[0] - corner[0]) | ||
: coord1[0] - Math.abs(coord1[0] - corner[0]); | ||
const latitude = | ||
coord1[1] > corner[1] | ||
? coord1[1] + Math.abs(coord1[1] - corner[1]) | ||
: coord1[1] - Math.abs(coord1[1] - corner[1]); | ||
|
||
const square = bboxPolygon([longitude, latitude, corner[0], corner[1]]); | ||
square.properties = square.properties || {}; | ||
square.properties.shape = 'Square'; | ||
|
||
// @ts-ignore | ||
return square; | ||
} | ||
} |
This file contains 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
This file contains 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