Skip to content

Commit

Permalink
Speed up tests for finding roads inside a boundary polygon. #67
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 31, 2024
1 parent 4adf4f0 commit e013d99
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions backend/src/neighbourhood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::collections::{BTreeMap, BTreeSet};

use anyhow::Result;
use geo::{
Area, Contains, Distance, Euclidean, Intersects, Length, LineInterpolatePoint, LineLocatePoint,
LineString, Point, Polygon,
Area, Distance, Euclidean, Length, LineInterpolatePoint, LineLocatePoint, LineString, Point,
Polygon, PreparedGeometry, Relate,
};
use geojson::FeatureCollection;
use web_time::Instant;
Expand Down Expand Up @@ -45,12 +45,14 @@ impl Neighbourhood {
let t1 = Instant::now();
let bbox = buffer_aabb(aabb(&boundary_polygon), 50.0);

let prepared_boundary = PreparedGeometry::from(boundary_polygon.clone());

let mut interior_roads = BTreeSet::new();
let mut crosses = BTreeMap::new();
for obj in map.closest_road.locate_in_envelope_intersecting(&bbox) {
let r = &map.roads[obj.data.0];

match line_in_polygon(&r.linestring, &boundary_polygon) {
match line_in_polygon(&r.linestring, &boundary_polygon, &prepared_boundary) {
LineInPolygon::Inside => {
interior_roads.insert(r.id);
}
Expand Down Expand Up @@ -217,13 +219,20 @@ enum LineInPolygon {
Outside,
}

fn line_in_polygon(linestring: &LineString, polygon: &Polygon) -> LineInPolygon {
fn line_in_polygon(
linestring: &LineString,
polygon: &Polygon,
prepared_polygon: &PreparedGeometry,
) -> LineInPolygon {
// TODO Reconsider rewriting all of this logic based on clip_linestring_to_polygon
if polygon.contains(linestring) {

let matrix = prepared_polygon.relate(linestring);

if matrix.is_within() {
return double_check_line_in_polygon(linestring, polygon);
}

if !polygon.intersects(linestring) {
if !matrix.is_intersects() {
return LineInPolygon::Outside;
}

Expand Down

0 comments on commit e013d99

Please sign in to comment.