Skip to content

Commit

Permalink
Allow to embed geojson inside solution.extras if used from interop api
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Jul 26, 2023
1 parent 68e3a20 commit e357a10
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/src/getting-started/solver.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,17 @@ overridden using `init-size` option.
Writing solution into file is controlled by `-o` or `--out-result` setting. When it is omitted, then solution is written
in std out.

#### Geojson

Pragmatic format supports option `-g` or `--geo-json` which writes solution in separate file in geojson format.
If the library iw used from the interop api (e.g. python or c), then solution in geojson can be returned inside `extras.features`
with the config option specified:
s
```json
{
"output": {
"includeGeojson": true
}
}
```
s
3 changes: 3 additions & 0 deletions examples/data/config/config.full.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,8 @@
"prefix": "[config.full]"
},
"isExperimental": false
},
"output": {
"includeGeojson": true
}
}
10 changes: 10 additions & 0 deletions vrp-cli/src/extensions/solve/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Config {
pub environment: Option<EnvironmentConfig>,
/// Specifies telemetry configuration.
pub telemetry: Option<TelemetryConfig>,
/// Specifies output configuration.
pub output: Option<OutputConfig>,
}

/// An evolution configuration.
Expand Down Expand Up @@ -424,6 +426,14 @@ pub struct NameWeight {
pub weight: usize,
}

/// Specifies output configuration.
#[derive(Clone, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OutputConfig {
/// True if the solution, serialized as geojson features, should be included in solution.extras.
pub include_geojson: Option<bool>,
}

fn configure_from_evolution(
mut builder: ProblemConfigBuilder,
problem: Arc<Problem>,
Expand Down
10 changes: 8 additions & 2 deletions vrp-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use std::sync::Arc;
use vrp_core::models::Problem as CoreProblem;
use vrp_core::prelude::Solver;
use vrp_pragmatic::format::problem::{serialize_problem, PragmaticProblem, Problem};
use vrp_pragmatic::format::solution::write_pragmatic;
use vrp_pragmatic::format::solution::{write_pragmatic, PragmaticOutputType};
use vrp_pragmatic::format::FormatError;
use vrp_pragmatic::get_unique_locations;
use vrp_pragmatic::validation::ValidationContext;
Expand Down Expand Up @@ -524,8 +524,14 @@ pub fn get_solution_serialized(problem: Arc<CoreProblem>, config: Config) -> Res
.to_json()
})?;

let output_type = if config.output.and_then(|output_cfg| output_cfg.include_geojson).unwrap_or(false) {
PragmaticOutputType::Combined
} else {
Default::default()
};

let mut writer = BufWriter::new(Vec::new());
write_pragmatic(problem.as_ref(), &solution, Default::default(), &mut writer)?;
write_pragmatic(problem.as_ref(), &solution, output_type, &mut writer)?;

let bytes = writer.into_inner().map_err(|err| format!("{err}"))?;
let result = String::from_utf8(bytes).map_err(|err| format!("{err}"))?;
Expand Down
4 changes: 4 additions & 0 deletions vrp-cli/tests/unit/extensions/solve/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ fn can_read_full_config() {
let logging = environment.logging.expect("no logging config");
assert!(logging.enabled);
assert_eq!(logging.prefix, Some("[config.full]".to_string()));

let output_cfg = config.output.expect("cannot read output config");
assert_eq!(output_cfg.include_geojson, Some(true));
}

#[test]
Expand All @@ -142,6 +145,7 @@ fn can_configure_telemetry_metrics() {
progress: None,
metrics: Some(MetricsConfig { enabled: true, track_population: Some(10) }),
}),
output: None,
};

let solution = create_builder_from_config(create_example_problem(), Vec::default(), &config)
Expand Down

0 comments on commit e357a10

Please sign in to comment.