Skip to content

Commit

Permalink
Merge pull request #2 from lenalebt/master
Browse files Browse the repository at this point in the history
fixes issues with multiple polygons in one file
  • Loading branch information
frafra authored Aug 3, 2024
2 parents b34cd2d + 471f08e commit 6da792e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
44 changes: 26 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ extern crate pest;
#[macro_use]
extern crate pest_derive;

use std::io::{self, Read};

use geojson::{Feature, GeoJson, Geometry, Value};

use pest::Parser;
use std::io::{self, Read};

#[derive(Parser)]
#[grammar = "poly.pest"]
pub struct PolyParser;


fn main() -> io::Result<()> {
let mut buffer = String::new();
let mut stdin = io::stdin();
stdin.read_to_string(&mut buffer)?;

let file = PolyParser::parse(Rule::file, &buffer)
.expect("unsuccessful parse")
.next().unwrap();

.next()
.unwrap();

let mut multipolygon: Vec<Vec<Vec<Vec<f64>>>> = Vec::new();
let mut current_polygon: Option<Vec<Vec<Vec<f64>>>> = None;

for file_pair in file.into_inner() {
match file_pair.as_rule() {
Rule::name => (),
Rule::ring => {
let mut subtract = false;
let mut points = vec![];
Expand All @@ -34,38 +34,46 @@ fn main() -> io::Result<()> {
Rule::name => (),
Rule::subtract => {
subtract = true;
},
}
Rule::point => {
let mut x: f64 = 0.0;
let mut y: f64 = 0.0;
for point_pair in polygon_pair.into_inner() {
match point_pair.as_rule() {
Rule::x => {
x = point_pair
.as_str().parse().unwrap();
},
x = point_pair.as_str().parse().unwrap();
}
Rule::y => {
y = point_pair
.as_str().parse().unwrap();
},
y = point_pair.as_str().parse().unwrap();
}
_ => unreachable!(),
}
}
points.push(vec![x, y]);
},
}
_ => unreachable!(),
}
}
if subtract {
multipolygon[0].push(points);
if let Some(ref mut polygon) = current_polygon {
polygon.push(points);
}
} else {
multipolygon.push(vec![points]);
if let Some(polygon) = current_polygon.take() {
multipolygon.push(polygon);
}
current_polygon = Some(vec![points]);
}
},
Rule::EOI => (),
}
Rule::EOI => {
if let Some(polygon) = current_polygon.take() {
multipolygon.push(polygon);
}
}
_ => unreachable!(),
}
}

let geometry = Geometry::new(Value::MultiPolygon(multipolygon));
let geojson = GeoJson::Feature(Feature {
bbox: None,
Expand Down
6 changes: 3 additions & 3 deletions src/poly.pest
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ name = { (!"\n" ~ ANY)+ }

separator = _{ (" " | "\t")+ }

decimal = _{ sign? ~ number ~ ("." ~ number ~ ("E" ~ sign ~ number)?)? }
decimal = _{ sign? ~ number ~ ("." ~ number)? ~ (("E" | "e") ~ sign? ~ number)? }
sign = _{ "+" | "-" }
number = _{ ASCII_DIGIT+ }

file = { SOI ~ (ring ~ NEWLINE)* ~ "END" ~ NEWLINE* ~ EOI }
file = { SOI ~ name ~ NEWLINE ~ (ring ~ NEWLINE)* ~ "END" ~ NEWLINE* ~ EOI }

ring = { (name ~ NEWLINE)? ~ subtract? ~ name ~ NEWLINE ~ (point ~ NEWLINE)* ~ "END" }
ring = { (name | (subtract ~ name)) ~ NEWLINE ~ (point ~ NEWLINE)* ~ "END" }
subtract = { "!" }
point = { separator? ~ x ~ separator ~ y}
x = { decimal }
Expand Down

0 comments on commit 6da792e

Please sign in to comment.