/
inGeoBoundingBox.ts
76 lines (73 loc) · 2.79 KB
/
inGeoBoundingBox.ts
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
import { inGeoBoundingBoxFP, isGeoPoint } from '@terascope/utils';
import { FieldType, GeoPointInput } from '@terascope/types';
import {
FieldValidateConfig,
ProcessMode,
FunctionDefinitionType,
FunctionDefinitionCategory,
} from '../interfaces';
export interface InGeoBoundingBoxArgs {
top_left: GeoPointInput;
bottom_right: GeoPointInput
}
export const inGeoBoundingBoxConfig: FieldValidateConfig<InGeoBoundingBoxArgs> = {
name: 'inGeoBoundingBox',
type: FunctionDefinitionType.FIELD_VALIDATION,
aliases: ['geoBox'],
process_mode: ProcessMode.INDIVIDUAL_VALUES,
category: FunctionDefinitionCategory.GEO,
examples: [
{
args: { top_left: '33.906320,-112.758421', bottom_right: '32.813646,-111.058902' },
config: { version: 1, fields: { testField: { type: FieldType.String } } },
field: 'testField',
input: '33.2,-112.3',
output: '33.2,-112.3',
},
{
args: { top_left: '33.906320,-112.758421', bottom_right: '32.813646,-111.058902' },
config: { version: 1, fields: { testField: { type: FieldType.GeoPoint } } },
field: 'testField',
input: '43,-132',
output: null
},
{
args: { top_left: '33.906320,-112.758421', bottom_right: '32.813646,-111.058902' },
config: { version: 1, fields: { testField: { type: FieldType.GeoJSON } } },
field: 'testField',
input: { type: 'Point', coordinates: [-112, 33] },
output: { type: 'Point', coordinates: [-112, 33] },
},
],
description: 'Returns the input if it is within the geo bounding box, otherwise returns null',
create({ args: { top_left, bottom_right } }) {
return inGeoBoundingBoxFP(top_left, bottom_right);
},
accepts: [
FieldType.GeoJSON,
FieldType.GeoPoint,
FieldType.Geo,
FieldType.Object,
FieldType.String,
FieldType.Number
],
argument_schema: {
top_left: {
type: FieldType.Any,
description: 'The top-left geo-point used to construct the geo bounding box, must be a valid geo-point input'
},
bottom_right: {
type: FieldType.Any,
description: 'The bottom_right geo-point used to construct the geo bounding box, must be a valid geo-point input'
}
},
required_arguments: ['top_left', 'bottom_right'],
validate_arguments({ top_left, bottom_right }) {
if (!isGeoPoint(top_left)) {
throw new Error(`Invalid parameter top_left: ${top_left}, is not a valid geo-point`);
}
if (!isGeoPoint(bottom_right)) {
throw new Error(`Invalid parameter bottom_right: ${bottom_right}, is not a valid geo-point`);
}
}
};