/
utilities.tsx
209 lines (200 loc) · 5.32 KB
/
utilities.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import * as turf from '@turf/turf'
import { Position, Polygon, LineString } from '@turf/turf'
import * as _ from 'lodash'
import { Shape } from '../shape-utils'
import { LengthUnit, METERS } from './units'
import {
GeometryJSON,
Geometry,
Extent,
BUFFER_SHAPE_PROPERTY,
CIRCLE_BUFFER_PROPERTY_VALUE,
DEFAULT_GEOMETRY,
DEFAULT_PROPERTIES,
POLYGON_LINE_BUFFER_PROPERTY_VALUE,
} from './geometry'
import { getDistanceInMeters } from '../internal/distance'
const MINIMUM_POLYGON_OR_LINE_COORDINATE_LENGTH_FOR_TURF_JS = 2
/**
* Creates a full GeometryJSON object from GeoJSON
*
* @param id - unique id for geometry
* @param geometryJSON - base GeoJSON object to extend
* @param color - CSS color for geometry
* @param shape - geometry shape
* @param buffer - buffer size
* @param bufferUnit - buffer size unit of measurement
*
* @returns GeometryJSON
*/
const makeGeometry = (
id: string,
geometryJSON: any,
color: string,
shape: Shape,
buffer: number = 0,
bufferUnit: LengthUnit = METERS
): GeometryJSON => {
const geometry: Geometry = turf.getGeom(_.cloneDeep(geometryJSON))
const json: GeometryJSON = {
type: 'Feature',
properties: {
id,
color,
shape,
buffer,
bufferUnit,
},
geometry,
bbox: [0, 0, 0, 0],
}
json.bbox = geoToExtent(makeBufferedGeo(json))
return json
}
/**
* Creates an empty GeometryJSON object
*
* @param id - unique id for geometry
* @param shape - geometry shape
* @param initialProperties - hash of properties to pass to new GeometryJSON
*
* @returns GeometryJSON
*/
const makeEmptyGeometry = (
id: string,
shape: Shape,
initialProperties: object = {}
): GeometryJSON => ({
type: 'Feature',
properties: {
...DEFAULT_PROPERTIES,
...initialProperties,
shape,
id,
buffer: shape === 'Point Radius' ? Number.MIN_VALUE : 0,
bufferUnit: METERS,
},
bbox: [0, 0, 0, 0],
geometry: _.cloneDeep(DEFAULT_GEOMETRY[shape]),
})
/**
* Creates a buffered GeometryJSON object from a source
* GeometryJSON object using it's geometry and buffer values
*
* @param geo - source GeometryJSON object
*
* @returns GeometryJSON with buffer applied
*/
const makeBufferedGeo = (geo: GeometryJSON): GeometryJSON => {
if (
((geo.geometry.type === 'Polygon' &&
geo.geometry.coordinates[0].length >=
MINIMUM_POLYGON_OR_LINE_COORDINATE_LENGTH_FOR_TURF_JS) ||
(geo.geometry.type === 'LineString' &&
geo.geometry.coordinates.length >=
MINIMUM_POLYGON_OR_LINE_COORDINATE_LENGTH_FOR_TURF_JS) ||
geo.geometry.type === 'Point') &&
geo.properties.shape !== 'Bounding Box' &&
geo.properties.buffer &&
geo.properties.bufferUnit &&
geo.properties.buffer > 0
) {
// Copy JSON since turf.buffer has side effects
geo = _.cloneDeep(geo)
const radius = getDistanceInMeters(
geo.properties.buffer || 0,
geo.properties.bufferUnit
)
let bufferedGeo
if (geo.geometry.type === 'Point') {
if (radius > 0) {
const point = geo.geometry as turf.Point
bufferedGeo = turf.circle(point.coordinates, radius, {
units: 'meters',
})
bufferedGeo = {
...geo,
geometry: bufferedGeo.geometry,
}
} else {
bufferedGeo = geo
}
} else {
bufferedGeo = turf.buffer(geo, radius, {
units: 'meters',
})
}
if (bufferedGeo === undefined) {
return geo
} else if (bufferedGeo.properties) {
bufferedGeo.properties[BUFFER_SHAPE_PROPERTY] =
geo.geometry.type === 'Point'
? CIRCLE_BUFFER_PROPERTY_VALUE
: POLYGON_LINE_BUFFER_PROPERTY_VALUE
}
return bufferedGeo as GeometryJSON
}
return geo
}
/**
* Converts an arbitrary bbox value from GeoJSON to a 2D extent value
*
* @param bbox - 2D/3D GeoJSON bbox value
*
* @returns Extent
*/
const bboxToExtent = (bbox: turf.BBox): Extent => [
bbox[0],
bbox[1],
bbox[2],
bbox[3],
]
/**
* Calculates the 2D extent of a GeometryJSON object
*
* @param geo - GeometryJSON object
*
* @returns Extent
*/
const geoToExtent = (geo: GeometryJSON): Extent => {
return bboxToExtent(turf.bbox(geo))
}
const convertCoordsToDisplay = (coordinates: Position[]) => {
const coords = _.cloneDeep(coordinates)
coords.forEach(coord => {
if (coord[0] < 0) {
coord[0] += 360
}
})
return coords
}
/**
* If the geo crosses the antimeridian, this will updates its coordinates so
* that no longitudes are negative. Typically used before displaying a geo on
* a map. The coordinates are updated in place.
*
* @param geo - GeometryJSON object
*/
const adjustGeoCoordsForAntimeridian = (geo: GeometryJSON) => {
const geometry = geo.geometry
const bbox = geo.bbox || turf.bbox(geo.geometry)
const width = Math.abs(bbox[0] - bbox[2])
const crossesAntimeridian = width > 180
if (crossesAntimeridian) {
if (geometry.type === 'LineString') {
const lineStringCoords = (geometry as LineString).coordinates
geometry.coordinates = convertCoordsToDisplay(lineStringCoords)
} else if (geometry.type === 'Polygon') {
const coords = (geometry as Polygon).coordinates[0]
geometry.coordinates[0] = convertCoordsToDisplay(coords)
}
}
}
export {
bboxToExtent,
geoToExtent,
makeGeometry,
makeBufferedGeo,
makeEmptyGeometry,
adjustGeoCoordsForAntimeridian,
}