Skip to content

Commit

Permalink
File download in rust and skip vector cli arg
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoRio42 committed Sep 4, 2024
1 parent 2ae0501 commit 0b0ee9f
Show file tree
Hide file tree
Showing 10 changed files with 899 additions and 41 deletions.
846 changes: 835 additions & 11 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ shapefile = "0.6.0"
skia-safe = "0.75.0"
tiff = "0.9.1"
las = { version = "0.8", features = ["laz"] }
reqwest = { version = "=0.10.10", features = ["native-tls-vendored", "blocking"] }
2 changes: 1 addition & 1 deletion docs/src/components/GlossaryLink.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { Content } = await glossaryEntry.render();
<a href={`/reference/glossary#${glossaryEntry.slug}`} class="link">
<span class="link-content"
>{Astro.props.label ?? glossaryEntry.data.defaultLabel}</span
>&nbsp;<Icon name="information" class="icon" />
><Icon name="information" class="icon" />
</a>

<template class="card">
Expand Down
14 changes: 14 additions & 0 deletions docs/src/content/docs/reference/cli-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ The `--skip-lidar` flag will skip the <GlossaryLink slug="lidar" /> processing s
cassini --skip-lidar
```

### `--skip-vector`

<p>

**Type:** `boolean`<br />

</p>

The `--skip-vector` flag will skip the vector processing stage of the pipeline. No file will be downloaded from <GlossaryLink slug="osm" /> and no vector features will be drawn on the map (roads, lakes...).

```sh
cassini --skip-vector
```

### `--threads`

<p>
Expand Down
7 changes: 5 additions & 2 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
time::Duration,
};

pub fn batch(number_of_threads: usize, skip_lidar: bool) {
pub fn batch(number_of_threads: usize, skip_lidar: bool, skip_vector: bool) {
println!("Batch mode");
println!("Generating raw rasters for every tiles");

Expand Down Expand Up @@ -53,7 +53,9 @@ pub fn batch(number_of_threads: usize, skip_lidar: bool) {
}
}

download_osm_files_for_all_tiles_if_needed(&tiles);
if !skip_vector {
download_osm_files_for_all_tiles_if_needed(&tiles);
}

let tiles_chunks: Vec<Vec<TileWithNeighbors>> = tiles_arc
.chunks(chunk_size)
Expand All @@ -72,6 +74,7 @@ pub fn batch(number_of_threads: usize, skip_lidar: bool) {
generate_png_from_dem_vegetation_density_tiff_images_and_vector_file(
tile.tile.clone(),
tile.neighbors.clone(),
skip_vector,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct Args {
#[arg(long)]
pub skip_lidar: bool,
#[arg(long)]
pub skip_vector: bool,
#[arg(long)]
pub batch: bool,
#[arg(long)]
pub threads: Option<usize>,
Expand Down
35 changes: 17 additions & 18 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{
fs::File,
io::{copy, stdout, Write},
path::Path,
process::{Command, ExitStatus, Stdio},
process::{Command, Stdio},
time::Instant,
};

use crate::{constants::BUFFER, tile::TileWithNeighbors};
Expand All @@ -24,7 +27,9 @@ pub fn download_osm_file_if_needed(min_x: i64, min_y: i64, max_x: i64, max_y: i6
return;
}

println!("Downloading osm file");
print!("Downloading osm file");
let _ = stdout().flush();
let start = Instant::now();

let (min_lon, min_lat) = convert_coords_from_lambert_93_to_gps(
(min_x - BUFFER as i64) as f64,
Expand All @@ -36,23 +41,17 @@ pub fn download_osm_file_if_needed(min_x: i64, min_y: i64, max_x: i64, max_y: i6
(max_y + BUFFER as i64) as f64,
);

let download_output = Command::new("wget")
.args([
"-O",
&osm_file_path.to_str().unwrap(),
&format!(
"https://www.openstreetmap.org/api/0.6/map?bbox={}%2C{}%2C{}%2C{}",
min_lon, min_lat, max_lon, max_lat,
),
])
.output()
.expect("failed to execute gdal_contour command");
let mut response = reqwest::blocking::get(&format!(
"https://www.openstreetmap.org/api/0.6/map?bbox={}%2C{}%2C{}%2C{}",
min_lon, min_lat, max_lon, max_lat,
))
.expect("Could not download osm file.");

if ExitStatus::success(&download_output.status) {
println!("{}", String::from_utf8(download_output.stdout).unwrap());
} else {
println!("{}", String::from_utf8(download_output.stderr).unwrap());
}
let mut file = File::create(&osm_file_path).expect("Could not create file for osm download.");
copy(&mut response, &mut file).expect("Could not copy file content.");

let duration = start.elapsed();
println!(" -> Done in {:.1?}", duration);
}

fn convert_coords_from_lambert_93_to_gps(x: f64, y: f64) -> (f64, f64) {
Expand Down
11 changes: 7 additions & 4 deletions src/full_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use crate::{canvas::Canvas, tile::Tile};

pub fn render_full_map_to_png(tile: &Tile, image_width: u32, image_height: u32) {
pub fn render_full_map_to_png(tile: &Tile, image_width: u32, image_height: u32, skip_vector: bool) {
print!("Rendering map to png");
let _ = stdout().flush();
let start = Instant::now();
Expand All @@ -18,13 +18,16 @@ pub fn render_full_map_to_png(tile: &Tile, image_width: u32, image_height: u32)
let mut vegetation_canvas = Canvas::load_from(&vegetation_path.to_str().unwrap());
let contours_path = tile.dir_path.join("contours.png");
let mut contours_canvas = Canvas::load_from(&contours_path.to_str().unwrap());
let vectors_path = tile.dir_path.join("vectors.png");
let mut vectors_canvas = Canvas::load_from(&vectors_path.to_str().unwrap());

full_map_canvas.overlay(&mut vegetation_canvas, 0.0, 0.0);
full_map_canvas.overlay(&mut contours_canvas, 0.0, 0.0);
full_map_canvas.overlay(&mut cliff_canvas, 0.0, 0.0);
full_map_canvas.overlay(&mut vectors_canvas, 0.0, 0.0);

if !skip_vector {
let vectors_path = tile.dir_path.join("vectors.png");
let mut vectors_canvas = Canvas::load_from(&vectors_path.to_str().unwrap());
full_map_canvas.overlay(&mut vectors_canvas, 0.0, 0.0);
}

let full_map_path = tile.dir_path.join("full-map.png");
full_map_canvas.save_as(&full_map_path.to_str().unwrap());
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
if args.batch {
let start = Instant::now();
let number_of_threads = args.threads.unwrap_or(3);
batch(number_of_threads, args.skip_lidar);
batch(number_of_threads, args.skip_lidar, args.skip_vector);
let duration = start.elapsed();
println!("Tiles generated in {:.1?}", duration);

Expand Down Expand Up @@ -83,8 +83,15 @@ fn main() {
top_left: None,
};

download_osm_file_if_needed(tile.min_x, tile.min_y, tile.max_x, tile.max_y);
generate_png_from_dem_vegetation_density_tiff_images_and_vector_file(tile, neighbor_tiles);
if !args.skip_vector {
download_osm_file_if_needed(tile.min_x, tile.min_y, tile.max_x, tile.max_y);
}

generate_png_from_dem_vegetation_density_tiff_images_and_vector_file(
tile,
neighbor_tiles,
args.skip_vector,
);

let duration = start.elapsed();
println!("Tile generated in {:.1?}", duration);
Expand Down
9 changes: 7 additions & 2 deletions src/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
pub fn generate_png_from_dem_vegetation_density_tiff_images_and_vector_file(
tile: Tile,
neighbor_tiles: NeighborTiles,
skip_vector: bool,
) {
let config = get_config();
let image_width = ((tile.max_x - tile.min_x) as f32 * config.dpi_resolution / INCH) as u32;
Expand All @@ -22,6 +23,10 @@ pub fn generate_png_from_dem_vegetation_density_tiff_images_and_vector_file(
create_dem_with_buffer_and_slopes_tiff(&tile, &neighbor_tiles);
generate_contours_with_pullautin_algorithme(&tile, image_width, image_height, &config);
render_cliffs(&tile, image_width, image_height, &config);
render_osm_vector_shapes(&tile, image_width, image_height, &config);
render_full_map_to_png(&tile, image_width, image_height);

if !skip_vector {
render_osm_vector_shapes(&tile, image_width, image_height, &config);
}

render_full_map_to_png(&tile, image_width, image_height, skip_vector);
}

0 comments on commit 0b0ee9f

Please sign in to comment.