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
//! GeoPackage Binary (GPB) encoder and feature-row encoder.
//!
//! Provides the minimal byte-layout needed to write point geometries into a
//! GeoPackage feature table. The encoding follows:
//! - OGC GeoPackage Encoding Standard v1.3.1 §2.1.3 (GeoPackageBinary format)
//! - OGC Simple Features WKB specification (ISO 19125-1)
// ─────────────────────────────────────────────────────────────────────────────
// GeoPackage Binary header (GPB)
// ─────────────────────────────────────────────────────────────────────────────
//
// Byte layout:
// 0-1 magic = 0x47 0x50 ('G', 'P')
// 2 version = 0x00
// 3 flags:
// bits 0-2 envelope indicator (0 = no envelope)
// bit 3 empty-geometry flag (0 = not empty)
// bit 4 reserved (0)
// bit 5 byte-order (1 = little-endian WKB)
// bits 6-7 reserved (0)
// 4-7 srs_id (little-endian i32)
// [8+] WKB body
//
// Minimum flags byte for a non-empty 2-D point with no envelope, LE WKB:
// 0b0010_0001 = 0x21 (little-endian, no envelope, not empty)
/// GPB flags byte: little-endian WKB, no envelope, not empty.
const GPB_FLAGS_LE_NO_ENV: u8 = 1 << 5; // bit 5 = LE
// ─────────────────────────────────────────────────────────────────────────────
// WKB constants
// ─────────────────────────────────────────────────────────────────────────────
/// WKB byte-order for little-endian.
const WKB_LE: u8 = 0x01;
/// WKB geometry type for Point (2D), in little-endian u32.
const WKB_POINT_LE: = 1u32.to_le_bytes;
// ─────────────────────────────────────────────────────────────────────────────
// Public encoder functions
// ─────────────────────────────────────────────────────────────────────────────
/// Encode a 2-D point as a GeoPackage Binary (GPB) blob.
///
/// The returned bytes are suitable for storage in a `BLOB` geometry column.
///
/// Layout of the returned bytes:
/// ```text
/// [0..2] magic bytes (0x47, 0x50)
/// [2] version (0x00)
/// [3] flags (0x20 = LE, no envelope, not empty)
/// [4..8] srs_id (little-endian i32)
/// [8] WKB byte order (0x01 = LE)
/// [9..13] WKB type = 1 (Point), little-endian u32
/// [13..21] x coordinate, little-endian f64
/// [21..29] y coordinate, little-endian f64
/// ```
///
/// Total: 29 bytes.
// ─────────────────────────────────────────────────────────────────────────────
// Feature row encoder
// ─────────────────────────────────────────────────────────────────────────────
/// Compute the bounding box `(min_x, min_y, max_x, max_y)` for a set of 2-D
/// points. Returns `None` when the point set is empty.