Skip to content

Commit

Permalink
Manage the temporary files in the importer better, and don't re-download
Browse files Browse the repository at this point in the history
pbf. #523

But now we don't know which map to switch to in the UI!
  • Loading branch information
dabreegster committed Mar 15, 2021
1 parent ef95252 commit f87c1c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 22 additions & 5 deletions importer/src/bin/one_step_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::process::{Command, Stdio};

use anyhow::Result;

use abstio::CityName;
use abstutil::{must_run_cmd, CmdArgs};

/// Import a one-shot A/B Street map in a single command. Takes a GeoJSON file with a boundary as
Expand Down Expand Up @@ -48,30 +49,42 @@ fn main() -> Result<()> {
String::from_utf8(out.stdout)?
};

// Name the temporary map based on the Geofabrik region.
let name = CityName::new("zz", "oneshot");
let pbf = name.input_path(format!("osm/{}.pbf", abstutil::basename(&url)));
let osm = name.input_path(format!(
"osm/{}.osm",
abstutil::basename(&url)
.strip_suffix("-latest.osm")
.unwrap()
));
std::fs::create_dir_all(std::path::Path::new(&osm).parent().unwrap())
.expect("Creating parent dir failed");

// Download it!
// TODO This is timing out. Also, really could use progress bars.
{
if !abstio::file_exists(&pbf) {
println!("Downloading {}", url);
let resp = reqwest::blocking::get(&url)?;
assert!(resp.status().is_success());
let bytes = resp.bytes()?;
let mut out = std::fs::File::create("raw.pbf")?;
let mut out = std::fs::File::create(&pbf)?;
out.write_all(&bytes)?;
}

// Clip it
println!("Clipping osm.pbf file to your boundary");
must_run_cmd(
Command::new(format!("{}/release/clip_osm", bin_dir))
.arg("--pbf=raw.pbf")
.arg(format!("--pbf={}", pbf))
.arg("--clip=boundary0.poly")
.arg("--out=clipped.osm"),
.arg(format!("--out={}", osm)),
);

// Import!
{
let mut cmd = Command::new(format!("{}/release/importer", bin_dir));
cmd.arg("--oneshot=clipped.osm");
cmd.arg(format!("--oneshot={}", osm));
cmd.arg("--oneshot_clip=boundary0.poly");
if drive_on_left {
cmd.arg("--oneshot_drive_on_left");
Expand All @@ -80,5 +93,9 @@ fn main() -> Result<()> {
must_run_cmd(&mut cmd);
}

// Clean up temporary files. If we broke before this, deliberately leave them around for
// debugging.
abstio::delete_file("boundary0.poly");

Ok(())
}
2 changes: 2 additions & 0 deletions map_gui/src/tools/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl<A: AppLike + 'static> State<A> for ImportCity<A> {
args,
Box::new(|_, _, success| {
if success {
abstio::delete_file("boundary.geojson");

Transition::ReplaceWithData(Box::new(move |state, ctx, app| {
let mut import =
state.downcast::<ImportCity<A>>().ok().unwrap();
Expand Down

0 comments on commit f87c1c3

Please sign in to comment.