1
1
static USAGE : & str = r#"
2
2
Geocodes a location against an updatable local copy of the Geonames cities index.
3
3
4
- It has three subcommands:
4
+ It has three major subcommands:
5
5
* suggest - given a City name, return the closest location coordinate.
6
6
* reverse - given a location coordinate, return the closest City.
7
- * index - operations to update the Geonames cities index used by the geocode command.
7
+ * index-* - operations to update the Geonames cities index used by the geocode command.
8
+ (index-check, index-update, index-load & index-reset)
8
9
9
10
SUGGEST
10
11
Geocodes to the nearest city center point given a location column
@@ -40,12 +41,11 @@ INDEX-<operation>
40
41
Updates the Geonames cities index used by the geocode command.
41
42
42
43
It has four operations:
43
- * check - checks if the local Geonames index is up-to-date.
44
+ * check - checks if the local Geonames index is up-to-date.
44
45
* update - updates the local Geonames index with the latest changes from the Geonames website.
45
- * reset - resets the local Geonames index to the default Geonames cities index, downloading
46
- it from the qsv GitHub repo for that release.
47
- * load - load a Geonames cities index from a file, making it the default index from that point
48
- forward.
46
+ * reset - resets the local Geonames index to the default Geonames cities index, downloading
47
+ it from the qsv GitHub repo for that release.
48
+ * load - load a Geonames cities index from a file, making it the default index going forward.
49
49
50
50
Examples:
51
51
Update the Geonames cities index with the latest changes.
@@ -160,7 +160,9 @@ struct Args {
160
160
flag_progressbar : bool ,
161
161
}
162
162
163
- static DEFAULT_GEOCODE_INDEX_FILENAME : & str = "qsv-geocode-index.bincode" ;
163
+ static QSV_VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
164
+ static DEFAULT_GEOCODE_INDEX_FILENAME : & str =
165
+ concat ! ( "qsv-" , env!( "CARGO_PKG_VERSION" ) , "-geocode-index.bincode" ) ;
164
166
165
167
static DEFAULT_CITIES_DB_URL : & str = "https://download.geonames.org/export/dump/cities15000.zip" ;
166
168
static DEFAULT_CITIES_DB_FILENAME : & str = "cities15000.txt" ;
@@ -214,7 +216,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
214
216
215
217
// main async geocode function that does the actual work
216
218
async fn geocode_main ( args : Args ) -> CliResult < ( ) > {
217
- eprintln ! ( "args: {args:?}" ) ;
219
+ // eprintln!("args: {args:?}");
218
220
219
221
let mut index_cmd = true ;
220
222
@@ -293,6 +295,7 @@ async fn geocode_main(args: Args) -> CliResult<()> {
293
295
GeocodeSubCmd :: IndexCheck => {
294
296
// load geocode engine
295
297
winfo ! ( "Checking main Geonames website for updates..." ) ;
298
+ check_index_file ( & geocode_index_file) ?;
296
299
let engine =
297
300
load_engine ( geocode_index_file. clone ( ) . into ( ) , args. flag_progressbar ) . await ?;
298
301
@@ -303,7 +306,11 @@ async fn geocode_main(args: Args) -> CliResult<()> {
303
306
}
304
307
} ,
305
308
GeocodeSubCmd :: IndexUpdate => {
306
- winfo ! ( "Updating Geonames index. This will take a while..." ) ;
309
+ check_index_file ( & geocode_index_file) ?;
310
+ winfo ! (
311
+ "Updating Geonames index. This will take a while as we need to download \
312
+ ~200mb of data and rebuild the index..."
313
+ ) ;
307
314
let engine = updater. build ( ) . await ?;
308
315
engine. dump_to ( geocode_index_file. clone ( ) , EngineDumpFormat :: Bincode ) ?;
309
316
winfo ! ( "Updates applied: {geocode_index_file}" ) ;
@@ -312,19 +319,7 @@ async fn geocode_main(args: Args) -> CliResult<()> {
312
319
// load alternate geocode index file
313
320
if let Some ( index_file) = args. arg_index_file {
314
321
winfo ! ( "Validating alternate Geonames index: {index_file}..." ) ;
315
- // check if index_file ends with a .bincode extension
316
- if !index_file. ends_with ( ".bincode" ) {
317
- return fail_incorrectusage_clierror ! (
318
- "Alternate Geonames index file {index_file} does not have a .bincode \
319
- extension."
320
- ) ;
321
- }
322
- // check if index_file exist
323
- if !Path :: new ( & index_file) . exists ( ) {
324
- return fail_incorrectusage_clierror ! (
325
- "Alternate Geonames index file {index_file} does not exist."
326
- ) ;
327
- }
322
+ check_index_file ( & index_file) ?;
328
323
329
324
let engine = load_engine ( index_file. clone ( ) . into ( ) , true ) . await ?;
330
325
// we successfully loaded the alternate geocode index file, so its valid
@@ -348,6 +343,10 @@ async fn geocode_main(args: Args) -> CliResult<()> {
348
343
if Path :: new ( & geocode_index_file) . exists ( ) {
349
344
fs:: remove_file ( & geocode_index_file) ?;
350
345
}
346
+ // loading the engine will download the default geocode index from the qsv GitHub
347
+ // repo
348
+ let _ = load_engine ( geocode_index_file. clone ( ) . into ( ) , true ) . await ?;
349
+ winfo ! ( "Default Geonames index file {geocode_index_file} reset." ) ;
351
350
} ,
352
351
_ => unreachable ! ( "index_cmd is true, so this is unreachable." ) ,
353
352
}
@@ -488,6 +487,23 @@ async fn geocode_main(args: Args) -> CliResult<()> {
488
487
Ok ( wtr. flush ( ) ?)
489
488
}
490
489
490
+ // check if index_file exists and ends with a .bincode extension
491
+ fn check_index_file ( index_file : & String ) -> CliResult < ( ) > {
492
+ if !index_file. ends_with ( ".bincode" ) {
493
+ return fail_incorrectusage_clierror ! (
494
+ "Alternate Geonames index file {index_file} does not have a .bincode extension."
495
+ ) ;
496
+ }
497
+ // check if index_file exist
498
+ if !Path :: new ( index_file) . exists ( ) {
499
+ return fail_incorrectusage_clierror ! (
500
+ "Alternate Geonames index file {index_file} does not exist."
501
+ ) ;
502
+ }
503
+ winfo ! ( "Valid: {index_file}" ) ;
504
+ Ok ( ( ) )
505
+ }
506
+
491
507
async fn load_engine ( geocode_index_file : PathBuf , show_progress : bool ) -> CliResult < Engine > {
492
508
let index_file = std:: path:: Path :: new ( & geocode_index_file) ;
493
509
@@ -500,17 +516,15 @@ async fn load_engine(geocode_index_file: PathBuf, show_progress: bool) -> CliRes
500
516
) ;
501
517
}
502
518
} else {
503
- let qsv_version = env ! ( "CARGO_PKG_VERSION" ) ;
504
-
505
519
// initial load, download index file from qsv releases
506
520
if show_progress {
507
521
woutinfo ! (
508
- "No local index found. Downloading geocode index from qsv {qsv_version } release..."
522
+ "No local index found. Downloading geocode index from qsv {QSV_VERSION } release..."
509
523
) ;
510
524
}
511
525
util:: download_file (
512
526
& format ! (
513
- "https://github.com/jqnatividad/qsv/releases/tag/{qsv_version }/qsv-geocode-index.bincode"
527
+ "https://github.com/jqnatividad/qsv/releases/download/{QSV_VERSION }/qsv-{QSV_VERSION} -geocode-index.bincode"
514
528
) ,
515
529
& geocode_index_file. to_string_lossy ( ) ,
516
530
show_progress,
0 commit comments