Setting the file. One moment.
Geo · Clickhouse Js Node Rowbinary · ClickHouse/agent-skills · Skills Docs
ContentsBack to the top of the page src/readers/ geo.ts
TypeScript · 109 lines · 4 KB
number
];
8
9 // Geo types are concrete compositions of Point = Tuple(Float64, Float64). They
10 // are monomorphic (no sub-readers) — the generator can emit them as-is.
11
12 /** Read a `Point`: `Tuple(Float64, Float64)` -> `[x, y]`. */
13 export function readPoint ( state : Cursor ) : Point {
14 const x = readFloat64 (state);
15 const y = readFloat64 (state);
16 return [x, y];
17 }
18
19 /**
20 * Read a `Ring`: `Array(Point)` — a LEB128 point count, then that many points.
21 * `LineString` has the identical wire (see { @link readLineString } ). `readPoint`
22 * is inlined here (two `readFloat64`s) to drop a call per point on this hot path.
23 */
24 export function readRing ( state : Cursor ) : Point [] {
25 const n = readUVarint (state);
26 const out : Point [] = [];
27 for ( let i = 0 ; i < n; i ++ ) {
28 const x = readFloat64 (state);
29 const y = readFloat64 (state);
30 out. push ([x, y]);
31 }
32 return out;
33 }
34
35 /**
36 * Read a `LineString`: `Array(Point)` (identical wire to a `Ring`). Points are
37 * inlined (two `readFloat64`s) to drop a call per point on this hot path.
38 */
39 export function readLineString ( state : Cursor ) : Point [] {
40 const n = readUVarint (state);
41 const out : Point [] = [];
42 for ( let i = 0 ; i < n; i ++ ) {
43 const x = readFloat64 (state);
44 const y = readFloat64 (state);
45 out. push ([x, y]);
46 }
47 return out;
48 }
49
50 /** Read a `Polygon`: `Array(Ring)` — the outer ring first, then any holes. */
51 export function readPolygon ( state : Cursor ) : Point [][] {
52 const n = readUVarint (state);
53 const out : Point [][] = [];
54 for ( let i = 0 ; i < n; i ++ ) out. push ( readRing (state));
55 return out;
56 }
57
58 /** Read a `MultiLineString`: `Array(LineString)` (identical wire to a `Polygon`). */
59 export function readMultiLineString ( state : Cursor ) : Point [][] {
60 const n = readUVarint (state);
61 const out : Point [][] = [];
62 for ( let i = 0 ; i < n; i ++ ) out. push ( readLineString (state));
63 return out;
64 }
65
66 /** Read a `MultiPolygon`: `Array(Polygon)`. */
67 export function readMultiPolygon ( state : Cursor ) : Point [][][] {
68 const n = readUVarint (state);
69 const out : Point [][][] = [];
70 for ( let i = 0 ; i < n; i ++ ) out. push ( readPolygon (state));
71 return out;
72 }
73
74 /**
75 * Read a `Geometry`: a named `Variant` over the six geo types. This is the
76 * MONOMORPHIZED form of `readVariant` for a concrete variant — a switch over the
77 * discriminant with each branch inlined, no reader array. The alternatives,
78 * sorted by type name (so in discriminant order), are LineString(0),
79 * MultiLineString(1), MultiPolygon(2), Point(3), Polygon(4), Ring(5); 0xFF is NULL.
80 *
81 * NOTE: the value shapes overlap — LineString and Ring are both `Point[]`,
82 * MultiLineString and Polygon both `Point[][]` — so the value alone does not say
83 * which geo type it was. If you need the kind, branch on the discriminant.
84 */
85 export function readGeometry (
86 state : Cursor ,
87 ) : Point | Point [] | Point [][] | Point [][][] | null {
88 const discriminant = readUInt8 (state);
89 switch (discriminant) {
90 case 0 :
91 return readLineString (state);
92 case 1 :
93 return readMultiLineString (state);
94 case 2 :
95 return readMultiPolygon (state);
96 case 3 :
97 return readPoint (state);
98 case 4 :
99 return readPolygon (state);
100 case 5 :
101 return readRing (state);
102 case 0xff :
103 return null ;
104 default :
105 throw new RangeError (
106 `RowBinary: unknown Geometry discriminant ${ discriminant }` ,
107 );
108 }
109 }