Skip to content

Commit

Permalink
Plumb through some config
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jun 11, 2024
1 parent db10fc2 commit 9455897
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 49 deletions.
13 changes: 13 additions & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ console_log = "1.0.0"
geo = "0.28.0"
geojson = { git = "https://github.com/georust/geojson", features = ["geo-types"] }
log = "0.4.20"
serde-wasm-bindgen = "0.6.5"
serde_json = "1.0.117"
wasm-bindgen = "0.2.87"
web-sys = { version = "0.3.64", features = ["console"] }
Expand Down
8 changes: 5 additions & 3 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ static START: Once = Once::new();

/// Takes GeoJSON with polygons in WGS84
#[wasm_bindgen(js_name = findWidths)]
pub fn find_widths(input: String) -> Result<String, JsValue> {
pub fn find_widths(input: String, raw_cfg: JsValue) -> Result<String, JsValue> {
// Panics shouldn't happen, but if they do, console.log them.
console_error_panic_hook::set_once();
START.call_once(|| {
console_log::init_with_level(log::Level::Info).unwrap();
});

let cfg: widths::Config = serde_wasm_bindgen::from_value(raw_cfg)?;

let mut input_polygons = Vec::new();
let mut skeletons = Vec::new();
let mut perps = Vec::new();
let mut thickened = Vec::new();

let (pavements, mercator) = widths::utils::read_gj_input(input).map_err(err_to_js)?;
let (pavements, mercator) = widths::utils::read_gj_input(input, &cfg).map_err(err_to_js)?;
for mut pavement in pavements {
pavement.calculate();
pavement.calculate(&cfg);

input_polygons.push(pavement.polygon);
skeletons.extend(pavement.skeletons);
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use indicatif::{ProgressBar, ProgressStyle};

use widths::{
utils::{read_gj_input, to_mercator},
Mercator, Pavement,
Config, Mercator, Pavement,
};

fn main() -> Result<()> {
let (pavements, mercator) = read_gj_input(std::fs::read_to_string(
"../test_input/small_pavements.geojson",
)?)?;
let cfg = Config::default();
let (pavements, mercator) = read_gj_input(
std::fs::read_to_string("../test_input/small_pavements.geojson")?,
&cfg,
)?;
//let (pavements, mercator) = read_gj_input(std::fs::read_to_string("../test_input/small_road_polygons.geojson")?)?;
//let (pavements, mercator) = read_gpkg_input("../test_input/large.gpkg", "Roadside")?;
//let pavements = read_gpkg_input("../test_input/large.gpkg", "Road Or Track")?;
Expand All @@ -27,7 +29,7 @@ fn main() -> Result<()> {

for mut pavement in pavements {
progress.inc(1);
pavement.calculate();
pavement.calculate(&cfg);

input_polygons.push(pavement.polygon);
skeletons.extend(pavement.skeletons);
Expand Down Expand Up @@ -64,7 +66,11 @@ fn dump_gj<IG: Into<Geometry>>(
}

#[allow(unused)]
fn read_gpkg_input(filename: &str, descriptive_group: &str) -> Result<(Vec<Pavement>, Mercator)> {
fn read_gpkg_input(
filename: &str,
descriptive_group: &str,
cfg: &Config,
) -> Result<(Vec<Pavement>, Mercator)> {
let mut polygons = Vec::new();
let dataset = Dataset::open(filename)?;
// Assume only one layer
Expand All @@ -87,5 +93,5 @@ fn read_gpkg_input(filename: &str, descriptive_group: &str) -> Result<(Vec<Pavem
polygons.push(polygon);
}

Ok(to_mercator(polygons))
Ok(to_mercator(polygons, cfg))
}
45 changes: 36 additions & 9 deletions web/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PolygonControls,
PolygonToolLayer,
} from "maplibre-draw-polygon";
import Settings from "./Settings.svelte";
let maptilerApiKey = "MZEJTanw3WpxRvt7qDfo";
Expand All @@ -34,24 +35,45 @@
let fileInput: HTMLInputElement;
let polygonTool: PolygonTool | null = null;
async function handleInput(gj: string) {
let inputString = "";
let cfg = {
remove_holes: 100.0,
flip_orientation: false,
filter_skeletons_outside: true,
filter_skeletons_near_boundary: 0.1,
join_skeletons: true,
make_perps_step_size: 5.0,
};
let shouldZoom = true;
async function handleInput(gj: string, cfg: any) {
if (!gj) {
return;
}
await init();
let results = JSON.parse(findWidths(gj));
let results = JSON.parse(findWidths(gj, cfg));
input = results.input;
skeletons = results.skeletons;
perps = results.perps;
thickened = results.thickened;
map?.fitBounds(bbox(input!) as [number, number, number, number], {
animate: false,
padding: 10,
});
if (shouldZoom) {
map?.fitBounds(bbox(input!) as [number, number, number, number], {
animate: false,
padding: 10,
});
// Only do it once per input; when cfg changes, don't jump around
shouldZoom = false;
}
}
$: handleInput(inputString, cfg);
async function loadFile(e: Event) {
let gj = await fileInput.files![0].text();
await handleInput(gj);
shouldZoom = true;
inputString = await fileInput.files![0].text();
}
function startPolygonTool() {
Expand All @@ -62,7 +84,8 @@
polygonTool.startNew();
polygonTool.addEventListenerSuccess(async (f) => {
polygonTool = null;
await handleInput(JSON.stringify(f));
shouldZoom = true;
inputString = JSON.stringify(f);
});
polygonTool.addEventListenerFailure(() => {
polygonTool = null;
Expand Down Expand Up @@ -105,6 +128,10 @@
<input type="checkbox" bind:checked={showThickened} />
Show thickened polygons
</label>

<hr />

<Settings bind:cfg />
</div>
<div slot="main" style="position:relative; width: 100%; height: 100vh;">
<MapLibre
Expand Down
20 changes: 20 additions & 0 deletions web/src/OptionalNumber.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script lang="ts">
export let label: string;
export let value: number | undefined;
export let defaultNumber: number;
let checked = value != undefined;
$: if (checked && value == undefined) {
value = defaultNumber;
} else if (!checked) {
value = undefined;
}
</script>

<label>
<input type="checkbox" bind:checked />
{label}

<input type="number" bind:value disabled={!checked} />
</label>
55 changes: 55 additions & 0 deletions web/src/Settings.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<script lang="ts">
import OptionalNumber from "./OptionalNumber.svelte";
export let cfg: any;
</script>

<details open>
<summary>Config</summary>

<OptionalNumber
label="Remove holes in input polygons larger than this area (sq m)"
bind:value={cfg.remove_holes}
defaultNumber={100}
/>

<div>
<label>
<input type="checkbox" bind:checked={cfg.flip_orientation} />
Flip orientation of polygons (need to try both depending on input)
</label>
</div>

<div>
<label>
<input type="checkbox" bind:checked={cfg.filter_skeletons_outside} />
Filter skeleton lines outside polygon
</label>
</div>

<OptionalNumber
label="Filter skeleton lines closer than this to the boundary (m)"
bind:value={cfg.filter_skeletons_near_boundary}
defaultNumber={0.1}
/>

<div>
<label>
<input type="checkbox" bind:checked={cfg.join_skeletons} />
Join skeleton lines
</label>
</div>

<OptionalNumber
label="Generate perpendicular lines at this step size (m)"
bind:value={cfg.make_perps_step_size}
defaultNumber={5.0}
/>
</details>

<style>
details {
border: 1px solid white;
padding: 4px;
}
</style>
1 change: 1 addition & 0 deletions widths/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ geo = "0.28.0"
geo-buffer = { git = "https://github.com/MarcioOrdonez/geo-buffer", branch = "chore/bump-geo-version-to-v.0.25.0" }
geojson = { git = "https://github.com/georust/geojson", features = ["geo-types"] }
log = "0.4.21"
serde = { version = "1.0.203", features = ["derive"] }
Loading

0 comments on commit 9455897

Please sign in to comment.