Skip to content

Commit

Permalink
Handle some clipping edge cases. #127
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 23, 2022
1 parent 2123f31 commit ac5623a
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 424 deletions.
3 changes: 1 addition & 2 deletions streets_reader/src/extract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::HashMap;

use osm::{NodeID, OsmID, RelationID, WayID};

use abstutil::Tags;
use geom::{HashablePt2D, Pt2D};
use osm2streets::osm::{NodeID, OsmID, RelationID, WayID};
use osm2streets::{osm, Direction, RestrictionType};

use crate::osm_reader::{Node, Relation, Way};
Expand Down
3 changes: 2 additions & 1 deletion streets_reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ fn extract_osm(
streets.gps_bounds = doc.gps_bounds.clone().unwrap();

if let Some(pts) = clip_pts {
streets.boundary_polygon = Ring::new(streets.gps_bounds.convert(&pts))?.into_polygon();
streets.boundary_polygon =
Ring::deduping_new(streets.gps_bounds.convert(&pts))?.into_polygon();
doc.clip(&streets.boundary_polygon);
} else {
streets.boundary_polygon = streets.gps_bounds.to_bounds().get_rectangle();
Expand Down
12 changes: 9 additions & 3 deletions streets_reader/src/osm_reader/clip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use geom::{Distance, PolyLine, Polygon};

use osm2streets::osm;

use super::Document;

impl Document {
Expand All @@ -21,13 +23,17 @@ impl Document {
// For line-string ways (not areas), clip them to the boundary. way.pts and way.nodes
// become out-of-sync.
for (id, way) in &mut self.ways {
// TODO This could just be a cul-de-sac road
if way.pts[0] == *way.pts.last().unwrap() {
// Only clip roads. Areas need more work.
if !way.tags.has_any(vec![osm::HIGHWAY, "railway"]) {
continue;
}

// Careful. We use unchecked_new because we might be dealing with a loop, but we still
// need to dedupe, or we might have invalid line segments.
let mut way_pts = way.pts.clone();
way_pts.dedup();
let mut polylines =
clip_polyline_to_ring(PolyLine::unchecked_new(way.pts.clone()), boundary_polygon);
clip_polyline_to_ring(PolyLine::unchecked_new(way_pts), boundary_polygon);
// Usually there's just one result
if polylines.len() == 1 {
way.pts = polylines.pop().unwrap().into_points();
Expand Down
Loading

0 comments on commit ac5623a

Please sign in to comment.