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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! `gpkg`: a command-line tool over the `geopackage` crate.
use std::io::{self, ErrorKind, Write};
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 you can still inspect a file
/// with problems. Any warnings from the lenient open 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 legacy or mixed triggers maintain, or that
/// are out of sync.
///
/// A layer with no index is left alone, because that is a choice rather
/// than a defect. Use `gpkg index` to add 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. The command names
/// whatever it left behind at the end.
Copy {
/// The `.gpkg` file to read.
src: PathBuf,
/// The `.gpkg` file to create. Must not already exist.
dst: PathBuf,
},
/// List a file's tile pyramids, or extract one tile's bytes.
Tiles {
#[command(subcommand)]
command: TileCommand,
},
/// Inspect 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 the stored bytes of one tile, addressed by zoom, column and row.
///
/// The command does not decode the image. It writes the bytes exactly as
/// the file stores them, whatever name you give `--out`.
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 stdout = &mut io::stdout().lock();
let result = match cli.command {
Command::Info { file } => info::run(&file, stdout),
Command::Validate { file, strict } => validate::run(&file, strict, stdout),
Command::Index { file, layer } => index::build(&file, &layer, stdout),
Command::Repair { file, layer } => index::repair(&file, layer.as_deref(), stdout),
Command::Copy { src, dst } => copy::run(&src, &dst, stdout),
Command::Tiles { command } => match command {
TileCommand::Info { file, pyramid } => tiles::info(&file, pyramid.as_deref(), stdout),
TileCommand::Get {
file,
pyramid,
zoom,
column,
row,
out,
} => tiles::get(
&file,
&pyramid,
geopackage::core::tiles::TileCoord::new(zoom, column, row),
out.as_deref(),
stdout,
),
},
Command::Coverage { command } => match command {
CoverageCommand::Info { file, coverage } => {
coverage::info(&file, coverage.as_deref(), stdout)
}
CoverageCommand::Get {
file,
coverage,
zoom,
column,
row,
out,
} => coverage::get(
&file,
&coverage,
geopackage::core::tiles::TileCoord::new(zoom, column, row),
out.as_deref(),
stdout,
),
},
};
// Flush here, so that a failed write of the buffered output is an error
// like any other. Rust ignores flush errors at exit.
let result = result.and_then(|code| {
stdout.flush()?;
Ok(code)
});
match result {
Ok(code) => code,
// The reader closed the pipe, as `gpkg info f | head` does. Exit with no
// message and with the status a shell gives a process that SIGPIPE stops.
Err(error::Error::Io(error)) if error.kind() == ErrorKind::BrokenPipe => {
ExitCode::from(141)
}
Err(error) => {
eprintln!("gpkg: {error}");
ExitCode::FAILURE
}
}
}