forked from geoarrow/geoarrow-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.ts
142 lines (118 loc) · 3.11 KB
/
type.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
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
import {
type Binary,
type Struct,
type Float,
type List,
type FixedSizeList,
DataType,
} from "apache-arrow";
// Note: this apparently has to be arrow.Float and not arrow.Float64 to ensure
// that recreating a data instance with arrow.makeData type checks using the
// input's data type.
export type InterleavedCoord = FixedSizeList<Float>;
export type SeparatedCoord = Struct<{
x: Float;
y: Float;
}>;
// TODO: support separated coords
export type Coord = InterleavedCoord; // | SeparatedCoord;
export type Point = Coord;
export type LineString = List<Coord>;
export type Polygon = List<List<Coord>>;
export type MultiPoint = List<Coord>;
export type MultiLineString = List<List<Coord>>;
export type MultiPolygon = List<List<List<Coord>>>;
export type GeoArrowType =
| Point
| LineString
| Polygon
| MultiPoint
| MultiLineString
| MultiPolygon;
export type WKB = Binary;
/** Check that the given type is a Point data type */
export function isPoint(type: DataType): type is Point {
if (DataType.isFixedSizeList(type)) {
// Check list size
if (![2, 3, 4].includes(type.listSize)) {
return false;
}
// Check child of FixedSizeList is floating type
if (!DataType.isFloat(type.children[0])) {
return false;
}
return true;
}
if (DataType.isStruct(type)) {
// Check number of children
if (![2, 3, 4].includes(type.children.length)) {
return false;
}
// Check that children have correct field names
if (
!type.children.every((field) => ["x", "y", "z", "m"].includes(field.name))
) {
return false;
}
if (!type.children.every((field) => DataType.isFloat(field))) {
return false;
}
return true;
}
return false;
}
export function isLineString(type: DataType): type is LineString {
// Check the outer type is a List
if (!DataType.isList(type)) {
return false;
}
// Check the child is a point type
if (!isPoint(type.children[0].type)) {
return false;
}
return true;
}
export function isPolygon(type: DataType): type is Polygon {
// Check the outer vector is a List
if (!DataType.isList(type)) {
return false;
}
// Check the child is a linestring vector
if (!isLineString(type.children[0].type)) {
return false;
}
return true;
}
export function isMultiPoint(type: DataType): type is MultiPoint {
// Check the outer vector is a List
if (!DataType.isList(type)) {
return false;
}
// Check the child is a point vector
if (!isPoint(type.children[0].type)) {
return false;
}
return true;
}
export function isMultiLineString(type: DataType): type is MultiLineString {
// Check the outer vector is a List
if (!DataType.isList(type)) {
return false;
}
// Check the child is a linestring vector
if (!isLineString(type.children[0].type)) {
return false;
}
return true;
}
export function isMultiPolygon(type: DataType): type is MultiPolygon {
// Check the outer vector is a List
if (!DataType.isList(type)) {
return false;
}
// Check the child is a polygon vector
if (!isPolygon(type.children[0].type)) {
return false;
}
return true;
}