|
1 | 1 | use std::convert::{TryFrom, TryInto};
|
2 | 2 | use std::fs;
|
3 |
| -use std::io::{self, Read}; |
| 3 | +use std::io::{self, BufReader, Read}; |
4 | 4 | use std::str::FromStr;
|
5 | 5 |
|
6 | 6 | use anyhow::{self, Context, Result};
|
7 | 7 | use clap::{Arg, Command};
|
8 | 8 | use console::Term;
|
9 | 9 | use geo::{Geometry, Point};
|
10 |
| -use geojson::{self, GeoJson}; |
| 10 | +use geojson::{self, FeatureIterator, GeoJson}; |
11 | 11 | use indicatif::ProgressBar;
|
12 | 12 | use kml::{quick_collection, Kml};
|
13 | 13 | use polyline::decode_polyline;
|
@@ -113,14 +113,20 @@ pub fn process_geojson(gj: GeoJson, simplification: f64, is_area: bool) -> Vec<G
|
113 | 113 | }
|
114 | 114 |
|
115 | 115 | fn handle_geojson(
|
116 |
| - input_str: String, |
| 116 | + file_path: &str, |
117 | 117 | simplification: f64,
|
118 | 118 | is_area: bool,
|
119 | 119 | ) -> Result<Vec<GridGeom<f64>>> {
|
120 |
| - let gj: GeoJson = input_str |
121 |
| - .parse::<GeoJson>() |
122 |
| - .context("Unable to parse GeoJSON")?; |
123 |
| - Ok(process_geojson(gj, simplification, is_area)) |
| 120 | + Ok(match file_path { |
| 121 | + "-" => FeatureIterator::new(BufReader::new(io::stdin())) |
| 122 | + .filter_map(|f| f.ok()) |
| 123 | + .flat_map(|f| process_geojson(GeoJson::Feature(f), simplification, is_area)) |
| 124 | + .collect(), |
| 125 | + _ => FeatureIterator::new(BufReader::new(fs::File::open(file_path)?)) |
| 126 | + .filter_map(|f| f.ok()) |
| 127 | + .flat_map(|f| process_geojson(GeoJson::Feature(f), simplification, is_area)) |
| 128 | + .collect(), |
| 129 | + }) |
124 | 130 | }
|
125 | 131 |
|
126 | 132 | fn handle_topojson(
|
@@ -316,7 +322,7 @@ fn main() -> Result<()> {
|
316 | 322 |
|
317 | 323 | let geoms: Vec<GridGeom<f64>> = match file_format {
|
318 | 324 | InputFormat::GeoJson => handle_geojson(
|
319 |
| - read_input_to_string(matches.value_of("INPUT").unwrap())?, |
| 325 | + matches.value_of("INPUT").unwrap(), |
320 | 326 | simplification,
|
321 | 327 | matches.is_present("area"),
|
322 | 328 | ),
|
@@ -379,10 +385,10 @@ mod test {
|
379 | 385 |
|
380 | 386 | #[test]
|
381 | 387 | fn test_handle_geojson() {
|
382 |
| - let input_str = include_str!("../fixtures/input.geojson").to_string(); |
383 |
| - let outlines = handle_geojson(input_str.clone(), 0., false).unwrap(); |
| 388 | + let file_path = "../fixtures/input.geojson"; |
| 389 | + let outlines = handle_geojson(file_path, 0., false).unwrap(); |
384 | 390 | let lines = outlines.iter().filter(|g| matches!(g, GridGeom::Line(_)));
|
385 |
| - let areas = handle_geojson(input_str, 0., true).unwrap(); |
| 391 | + let areas = handle_geojson(file_path, 0., true).unwrap(); |
386 | 392 | let poly = areas.iter().filter(|g| matches!(g, GridGeom::Polygon(_)));
|
387 | 393 | assert_eq!(outlines.len(), 14);
|
388 | 394 | assert_eq!(lines.count(), 13);
|
|
0 commit comments