Skip to content

Commit

Permalink
[feature] check coordinate validity
Browse files Browse the repository at this point in the history
  • Loading branch information
patochectp committed Feb 14, 2022
1 parent 9d80d2f commit 5102da9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
64 changes: 61 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl Collections {
let mut stop_area_ids_used: HashSet<String> = HashSet::new();
let mut equipments_used: HashSet<String> = HashSet::new();

let stop_locations = self
let mut stop_locations = self
.stop_locations
.take()
.into_iter()
Expand Down Expand Up @@ -330,7 +330,7 @@ impl Collections {
.collect::<Vec<_>>();
self.pathways = CollectionWithId::new(pathways)?;

let stop_points = self
let mut stop_points = self
.stop_points
.take()
.into_iter()
Expand Down Expand Up @@ -395,7 +395,7 @@ impl Collections {
})
.collect(),
)?;
let stop_areas = self
let mut stop_areas = self
.stop_areas
.take()
.into_iter()
Expand All @@ -416,6 +416,33 @@ impl Collections {
})
.collect::<Vec<_>>();

for sa in stop_areas.iter_mut().filter(|sa| !sa.coord.is_valid()) {
debug!(
"stop area {} geographic coordinates are not valid and set to (0, 0)",
sa.id
);
sa.coord = Coord::default();
}

for sp in stop_points.iter_mut().filter(|sp| !sp.coord.is_valid()) {
debug!(
"stop point {} geographic coordinates are not valid and set to (0, 0)",
sp.id
);
sp.coord = Coord::default();
}

for sl in stop_locations
.iter_mut()
.filter(|sl| sl.stop_type == StopType::StopEntrance && !sl.coord.is_valid())
{
debug!(
"stop access {} geographic coordinates are not valid and set to (0, 0)",
sl.id
);
sl.coord = Coord::default();
}

comments_used.extend(self.stop_time_comments.iter().filter_map(
|((vj_id, _), comment_id)| {
if vjs_used.contains(vj_id.as_str()) {
Expand Down Expand Up @@ -2221,4 +2248,35 @@ mod tests {
assert_eq!(2, vj.stop_times.len());
}
}

mod check_coord_integrity {
use crate::objects::Coord;

#[test]
fn check_valid_coord() {
let coord = Coord {
lon: 2.372987,
lat: 48.844746,
};
assert!(coord.is_valid());
}
}

#[test]
fn check_unvalid_lon() {
let coord = Coord {
lon: -182.,
lat: 48.844746,
};
assert!(!coord.is_valid());
}

#[test]
fn check_unvalid_lat() {
let coord = Coord {
lon: 2.372987,
lat: 91.,
};
assert!(!coord.is_valid());
}
}
4 changes: 4 additions & 0 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,10 @@ impl Coord {
2. * EARTH_RADIUS * f64::asin(f64::sqrt(x + y))
}

pub fn is_valid(&self) -> bool {
(self.lon >= -180. && self.lon <= 180.) && (self.lat >= -90. && self.lat <= 90.)
}

/// Returns a proxy object allowing to compute approximate
/// distances for cheap computation.
///
Expand Down

0 comments on commit 5102da9

Please sign in to comment.