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
//! `gpkg`: a command-line tool over the `geopackage` crate.
use std::path::PathBuf;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
mod copy;
mod coverage;
mod error;
mod index;
mod info;
mod tiles;
mod validate;
/// Read, check and convert OGC GeoPackage files.
#[derive(Parser)]
#[command(name = "gpkg", version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Summarise a file: version, layers, schemas, spatial reference systems,
/// index health and registered extensions.
///
/// Opens the file read-only and leniently, so a file with something wrong
/// with it can still be inspected; any warnings the lenient open collected
/// are printed first. Nothing is modified.
Info {
/// The `.gpkg` file to read.
file: PathBuf,
},
/// Report what is wrong with a file, and what would put it right.
///
/// Exits non-zero when a finding is an error, meaning a reader can get a
/// wrong answer from the file. Nothing is modified.
Validate {
/// The `.gpkg` file to check.
file: PathBuf,
/// Also exit non-zero for warnings, not just errors.
#[arg(long)]
strict: bool,
},
/// Build a spatial index on a layer that has none.
///
/// Creates the RTree virtual table and the GeoPackage 1.4 trigger set,
/// populates the index from the existing rows, and registers the
/// extension. Fails if the layer already has a spatial index.
Index {
/// The `.gpkg` file to write to.
file: PathBuf,
/// The layer to index.
layer: String,
},
/// Repair spatial indexes that a legacy or mixed trigger set maintains, or
/// that were left desynchronised.
///
/// A layer with no index is left alone: that is a choice rather than a
/// defect, and `gpkg index` is how to request one.
Repair {
/// The `.gpkg` file to write to.
file: PathBuf,
/// Repair only this layer, rather than every layer that needs it.
layer: Option<String>,
},
/// Copy the feature and attribute layers of one file into a new one.
///
/// Tiles and the extension tables are not copied; whatever is left behind
/// is named at the end.
Copy {
/// The `.gpkg` file to read.
src: PathBuf,
/// The `.gpkg` file to create. Must not already exist.
dst: PathBuf,
},
/// Tile pyramids: what a file contains, and the bytes of one tile.
Tiles {
#[command(subcommand)]
command: TileCommand,
},
/// Tiled gridded coverages: elevation and other measured grids.
///
/// This command is separate from `tiles` because a coverage is a different
/// kind of content. Its payloads contain measurements, not images, and the
/// meaning of its samples is in ancillary tables that a pyramid does not
/// have.
Coverage {
#[command(subcommand)]
command: CoverageCommand,
},
}
#[derive(Subcommand)]
enum CoverageCommand {
/// Describe every coverage in a file, or a single named coverage.
Info {
/// The `.gpkg` file to read.
file: PathBuf,
/// Describe only this coverage.
coverage: Option<String>,
},
/// Write the stored bytes of the tile at a zoom, column and row.
///
/// The command does not decode samples. It writes the bytes as the file
/// stores them, and prints the values that the payload header and the
/// ancillary row of the tile declare.
Get {
/// The `.gpkg` file to read.
file: PathBuf,
/// The coverage's table name.
coverage: String,
/// Zoom level.
zoom: i64,
/// Tile column, counting east from the extent's west edge.
column: i64,
/// Tile row, counting south from the extent's north edge.
row: i64,
/// Write to this path instead of standard output.
#[arg(long)]
out: Option<PathBuf>,
},
}
#[derive(Subcommand)]
enum TileCommand {
/// Describe every tile pyramid in a file, or a single named pyramid.
Info {
/// The `.gpkg` file to read.
file: PathBuf,
/// Describe only this pyramid.
pyramid: Option<String>,
},
/// Write one tile's stored bytes out, addressed by zoom, column and row.
///
/// No image is decoded: the bytes come out exactly as the file stores them,
/// whatever `--out` is named.
Get {
/// The `.gpkg` file to read.
file: PathBuf,
/// The pyramid's table name.
pyramid: String,
/// Zoom level.
zoom: i64,
/// Tile column, counting east from the extent's west edge.
column: i64,
/// Tile row, counting south from the extent's north edge (the WMTS and
/// XYZ sense, not TMS).
row: i64,
/// Write to this path instead of standard output.
#[arg(long)]
out: Option<PathBuf>,
},
}
fn main() -> ExitCode {
let cli = Cli::parse();
let result = match cli.command {
Command::Info { file } => info::run(&file),
Command::Validate { file, strict } => validate::run(&file, strict),
Command::Index { file, layer } => index::build(&file, &layer),
Command::Repair { file, layer } => index::repair(&file, layer.as_deref()),
Command::Copy { src, dst } => copy::run(&src, &dst),
Command::Tiles { command } => match command {
TileCommand::Info { file, pyramid } => tiles::info(&file, pyramid.as_deref()),
TileCommand::Get {
file,
pyramid,
zoom,
column,
row,
out,
} => tiles::get(
&file,
&pyramid,
geopackage::core::tiles::TileCoord::new(zoom, column, row),
out.as_deref(),
),
},
Command::Coverage { command } => match command {
CoverageCommand::Info { file, coverage } => coverage::info(&file, coverage.as_deref()),
CoverageCommand::Get {
file,
coverage,
zoom,
column,
row,
out,
} => coverage::get(
&file,
&coverage,
geopackage::core::tiles::TileCoord::new(zoom, column, row),
out.as_deref(),
),
},
};
match result {
Ok(code) => code,
Err(error) => {
eprintln!("gpkg: {error}");
ExitCode::FAILURE
}
}
}