Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] check coordinate validity #843

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

This comment was marked as outdated.

.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