Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to polyline #9

Merged
merged 3 commits into from Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions 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 Cargo.toml
Expand Up @@ -25,6 +25,7 @@ shapefile = { version = "0.2", features = ["geo-types"] }
topojson = "0.2.0"
float-cmp = "0.8.0"
wkt = "0.6.0"
polyline = "0.6.0"

[[bin]]
name = "echomap"
Expand Down
1 change: 1 addition & 0 deletions fixtures/input.polyline.txt
@@ -0,0 +1 @@
_p~iF~ps|U_ulLnnqC_mqNvxq`@
39 changes: 38 additions & 1 deletion src/main.rs
Expand Up @@ -8,6 +8,7 @@ use console::Term;
use geo_types::{Geometry, Point};
use geojson::{self, GeoJson};
use indicatif::ProgressBar;
use polyline::decode_polyline;
use rstar::RTree;
use topojson::{to_geojson, TopoJson};
use wkt::{conversion::try_into_geometry, Wkt};
Expand All @@ -22,6 +23,7 @@ enum InputFormat {
Csv,
Shapefile,
Wkt,
Polyline,
}

impl FromStr for InputFormat {
Expand All @@ -34,6 +36,7 @@ impl FromStr for InputFormat {
"csv" => Ok(InputFormat::Csv),
"shp" => Ok(InputFormat::Shapefile),
"wkt" => Ok(InputFormat::Wkt),
"polyline" => Ok(InputFormat::Polyline),
_ => Err(()),
}
}
Expand Down Expand Up @@ -180,6 +183,18 @@ fn handle_wkt(input_str: String, simplification: f64, is_area: bool) -> Vec<Grid
.collect()
}

fn handle_polyline(input_str: String, precision: &str, simplification: f64) -> Vec<GridGeom<f64>> {
let precision: u32 = precision
.parse()
.expect("Precision has to be defined for polyline format");
let lines = decode_polyline(&input_str, precision).unwrap();
allevo marked this conversation as resolved.
Show resolved Hide resolved
GridGeom::vec_from_geom(
geo_types::Geometry::LineString(lines),
simplification,
false,
)
}

fn main() {
let matches = App::new("echomap")
.version("0.4.0")
Expand All @@ -194,7 +209,7 @@ fn main() {
.long("format")
.value_name("FORMAT")
.help("Input file format (tries to infer from file extension by default)")
.possible_values(&["geojson", "topojson", "csv", "shp"])
.possible_values(&["geojson", "topojson", "csv", "shp", "polyline"])
.default_value_if("INPUT", Some("-"), "geojson")
.takes_value(true))
.arg(Arg::with_name("lon")
Expand Down Expand Up @@ -227,6 +242,11 @@ fn main() {
.help("Proportion of removable points to remove (0-1 or 0%-100%)")
.takes_value(true)
.default_value(&"0.01"))
.arg(Arg::with_name("precision")
allevo marked this conversation as resolved.
Show resolved Hide resolved
.long("precision")
.help("Precision value for polyline parsing")
.required_if("format", "polyline")
.takes_value(true))
.arg(Arg::with_name("area")
.short("a")
.long("area")
Expand Down Expand Up @@ -283,6 +303,11 @@ fn main() {
simplification,
matches.is_present("area"),
),
InputFormat::Polyline => handle_polyline(
read_input_to_string(matches.value_of("INPUT").unwrap()),
matches.value_of("precision").unwrap(),
simplification,
),
};

// Create a combined LineString for bounds calculation
Expand Down Expand Up @@ -356,4 +381,16 @@ mod test {
]
);
}

#[test]
fn test_handle_polyline() {
let input_str = include_str!("../fixtures/input.polyline.txt").to_string();
assert_eq!(
handle_polyline(input_str, "5", 0.),
vec![
GridGeom::Line(Line::new((-120.2, 38.5), (-120.95, 40.7))),
GridGeom::Line(Line::new((-120.95, 40.7), (-126.453, 43.252)))
]
);
}
}