Skip to content

Commit ab101e6

Browse files
committed
fix tests readability
1 parent 37048c8 commit ab101e6

File tree

1 file changed

+66
-74
lines changed

1 file changed

+66
-74
lines changed

src/curves.rs

+66-74
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ impl Curve for PlanarLineStringCurve {
229229
/// Each [`Line`] made up of 2 [`Coord`]s.
230230
/// A spherical coordinates model takes 3 coordinates: longitude (`x`), latitude (`y`)
231231
/// and radius as const ([`geo::MEAN_EARTH_RADIUS`]).
232-
/// [`GeodesicLength`] for a Paris to New-York is about 5837.283 km, and
233-
/// [`HaversineLength`] for a Paris to New-York is about 5852.970 km.
232+
/// [`GeodesicLength`] for a Paris to New-York is about 5853.101 km, and
233+
/// [`HaversineLength`] for a Paris to New-York is about 5837.415 km.
234234
/// We've chosen here to stick with Haversine Formula.
235235
/// The computations are made along the Great Circle.
236236
/// When required, some methods use [geo::algorithm::densify_haversine::DensifyHaversine]
@@ -446,20 +446,13 @@ mod tests {
446446
pub use geo::line_string;
447447
use geo::point;
448448

449-
const PARIS_LON: f64 = 2.352565660016694;
450-
const PARIS_LAT: f64 = 48.85643268390663;
451-
const NEW_YORK_LON: f64 = -74.00599134051316;
452-
const NEW_YORK_LAT: f64 = 40.71274961837565;
453-
const LILLE_LON: f64 = 3.066667;
454-
const LILLE_LAT: f64 = 50.633333;
455-
const PERPIGNAN_LON: f64 = 2.8948332;
456-
const PERPIGNAN_LAT: f64 = 42.6886591;
457-
const BREST_LON: f64 = -4.486076;
458-
const BREST_LAT: f64 = 48.390394;
459-
const NANCY_LON: f64 = 6.184417;
460-
const NANCY_LAT: f64 = 48.692054;
461-
const REYKJAVIK_LON: f64 = -21.827774;
462-
const REYKJAVIK_LAT: f64 = 64.128288;
449+
const PARIS: geo::Coord = coord! {x: 2.35, y: 48.86};
450+
const NEW_YORK: geo::Coord = coord! {x: -74.01, y: 40.71};
451+
const LILLE: geo::Coord = coord! {x: 3.07, y: 50.63};
452+
const PERPIGNAN: geo::Coord = coord! {x: 2.89, y: 42.69};
453+
const BREST: geo::Coord = coord! {x: -4.49, y: 48.39};
454+
const NANCY: geo::Coord = coord! {x: 6.18, y: 48.69};
455+
const REYKJAVIK: geo::Coord = coord! {x: -21.83, y: 64.13};
463456

464457
#[test]
465458
fn planar_fragmented() {
@@ -573,39 +566,39 @@ mod tests {
573566
fn spherical_fragmented() {
574567
let framentation_max_length = 1.;
575568
let paris_to_new_york = SphericalLineStringCurve::new_fragmented(
576-
line_string![(x: PARIS_LON, y: PARIS_LAT), (x: NEW_YORK_LON, y: NEW_YORK_LAT)],
569+
line_string![PARIS, NEW_YORK],
577570
framentation_max_length,
578571
1.,
579572
);
580573

581-
assert_eq!(5837284, paris_to_new_york.len());
574+
assert_eq!(5837416, paris_to_new_york.len());
582575
assert_relative_eq!(
583576
framentation_max_length,
584577
paris_to_new_york[0].length(),
585-
epsilon = 1e-7
578+
epsilon = 1e-6
586579
);
587-
// 1e-7 means we lose 0.1 micrometer per segment
580+
// 1e-6 means we lose 1. micrometer per segment
588581
}
589582

590583
#[test]
591584
fn spherical_length() {
592585
let paris_to_new_york = SphericalLineStringCurve::new(
593-
line_string![(x: PARIS_LON, y: PARIS_LAT), (x: NEW_YORK_LON, y: NEW_YORK_LAT)],
586+
line_string![PARIS, NEW_YORK],
594587
1.,
595588
);
596-
assert_relative_eq!(5837283.441678336, paris_to_new_york.length()); // 5837283.441678336 using [`HaversineLength`] and 5852969.839293494 using [`GeodesicLength`]
589+
assert_eq!(5837415.205720471, paris_to_new_york.length()); // 5837415.205720471 using [`HaversineLength`] and 5853101.331803938 using [`GeodesicLength`]
597590

598591
let lille_to_perpignan = SphericalLineStringCurve::new(
599-
line_string![(x: LILLE_LON, y: LILLE_LAT), (x: PERPIGNAN_LON, y: PERPIGNAN_LAT)],
592+
line_string![LILLE, PERPIGNAN],
600593
1.,
601594
);
602-
assert_relative_eq!(883505.2931188548, lille_to_perpignan.length()); // 883505.2931188548 using [`HaversineLength`] and 883260.051153502 using [`GeodesicLength`]
595+
assert_eq!(882995.0489150163, lille_to_perpignan.length()); // 882995.0489150163 using [`HaversineLength`] and 882749.856002331 using [`GeodesicLength`]
603596

604597
let brest_to_nancy = SphericalLineStringCurve::new(
605-
line_string![(x: BREST_LON, y: BREST_LAT), (x: NANCY_LON, y: NANCY_LAT)],
598+
line_string![BREST, NANCY],
606599
1.,
607600
);
608-
assert_relative_eq!(785636.8730262491, brest_to_nancy.length()); // 785636.8730262491 using [`HaversineLength`] and 787994.4363866252 using [`GeodesicLength`]
601+
assert_eq!(785611.8752324395, brest_to_nancy.length()); // 785611.8752324395 using [`HaversineLength`] and 787969.3534391255 using [`GeodesicLength`]
609602
}
610603

611604
#[test]
@@ -646,65 +639,65 @@ mod tests {
646639
#[test]
647640
fn spherical_projection() {
648641
let mut paris_to_new_york = SphericalLineStringCurve::new(
649-
line_string![(x: PARIS_LON, y: PARIS_LAT), (x: NEW_YORK_LON, y: NEW_YORK_LAT)],
642+
line_string![PARIS, NEW_YORK],
650643
1.,
651644
);
652645

653646
// Point is located on the right (north) of the curve
654647
let projected = paris_to_new_york
655-
.project(point! {x: -6.705403880820967, y: 51.42135181702875})
648+
.project(point! {x: -6.71, y: 51.42})
656649
.unwrap();
657-
assert_eq!(701924.3809693493, projected.distance_along_curve);
658-
assert_eq!(-67157.93913531031, projected.offset);
650+
assert_eq!(701868.9818283478, projected.distance_along_curve);
651+
assert_eq!(-66665.6982638038, projected.offset);
659652

660653
// Point is located on the left (south) of the curve
661654
let projected = paris_to_new_york
662-
.project(point! {x: -12.250890759346419, y: 45.857650969554356})
655+
.project(point! {x: -12.25, y: 45.86})
663656
.unwrap();
664-
assert_eq!(963365.3768036617, projected.distance_along_curve);
665-
assert_eq!(625592.3211438804, projected.offset);
657+
assert_eq!(963056.4000812048, projected.distance_along_curve);
658+
assert_eq!(625574.8001320735, projected.offset);
666659

667660
// Same point, but with an offset from the curve
668661
paris_to_new_york.start_offset = 1000000.;
669662
let projected = paris_to_new_york
670-
.project(point! {x: -12.250890759346419, y: 45.857650969554356})
663+
.project(point! {x: -12.25, y: 45.86})
671664
.unwrap();
672-
assert_eq!(1963365.3768036617, projected.distance_along_curve);
673-
assert_eq!(625592.3211438804, projected.offset);
665+
assert_eq!(1963056.4000812047, projected.distance_along_curve);
666+
assert_eq!(625574.8001320735, projected.offset);
674667

675668
// ################################################################################
676669
let mut new_york_to_paris = SphericalLineStringCurve::new(
677-
line_string![(x: NEW_YORK_LON, y: NEW_YORK_LAT), (x: PARIS_LON, y: PARIS_LAT)],
670+
line_string![NEW_YORK, PARIS],
678671
1.,
679672
);
680673

681674
// Point is located on the left (north) of the curve
682675
let projected = new_york_to_paris
683-
.project(point! {x: -6.705403880820967, y: 51.42135181702875})
676+
.project(point! {x: -6.71, y: 51.42})
684677
.unwrap();
685-
assert_eq!(5135359.060708988, projected.distance_along_curve);
686-
assert_eq!(67157.93913531031, projected.offset);
678+
assert_eq!(5135546.223892125, projected.distance_along_curve);
679+
assert_eq!(66665.6982638038, projected.offset);
687680

688681
// Point is located on the right (south) of the curve
689682
let projected = new_york_to_paris
690-
.project(point! {x: -12.250890759346419, y: 45.857650969554356})
683+
.project(point! {x: -12.25, y: 45.86})
691684
.unwrap();
692-
assert_eq!(4873918.064874676, projected.distance_along_curve);
693-
assert_eq!(-625592.3211438811, projected.offset); // Note: result is weird -> distance should remain the same than the other way curve, difference is 0.7mm
685+
assert_eq!(4874358.805639268, projected.distance_along_curve);
686+
assert_eq!(-625574.8001320735, projected.offset);
694687

695688
// Same point, but with an offset from the curve
696689
new_york_to_paris.start_offset = 1000000.;
697690
let projected = new_york_to_paris
698-
.project(point! {x: -12.250890759346419, y: 45.857650969554356})
691+
.project(point! {x: -12.25, y: 45.86})
699692
.unwrap();
700-
assert_eq!(5873918.064874676, projected.distance_along_curve);
701-
assert_eq!(-625592.3211438811, projected.offset); // Note: same, difference is 0.7mm
693+
assert_eq!(5874358.805639268, projected.distance_along_curve);
694+
assert_eq!(-625574.8001320735, projected.offset);
702695
}
703696

704697
#[test]
705698
fn spherical_resolve() {
706699
let mut paris_to_new_york = SphericalLineStringCurve::new(
707-
line_string![(x: PARIS_LON, y: PARIS_LAT), (x: NEW_YORK_LON, y: NEW_YORK_LAT)],
700+
line_string![PARIS, NEW_YORK],
708701
1.,
709702
);
710703

@@ -713,20 +706,20 @@ mod tests {
713706
offset: 0.,
714707
};
715708
let paris_to_new_york_projection = paris_to_new_york.resolve(projection).unwrap();
716-
assert_eq!(paris_to_new_york_projection.x(), -11.113419713640527);
717-
assert_eq!(paris_to_new_york_projection.y(), 51.44320774918762);
709+
assert_eq!(-11.117252327440333, paris_to_new_york_projection.x());
710+
assert_eq!(51.44598457247205, paris_to_new_york_projection.y());
718711

719712
paris_to_new_york.start_offset = 300000.;
720713
let paris_to_new_york_projection = paris_to_new_york.resolve(projection).unwrap();
721-
assert_eq!(paris_to_new_york_projection.x(), -6.924269520648392);
722-
assert_eq!(paris_to_new_york_projection.y(), 50.83295845015173);
714+
assert_eq!(-6.927740203201038, paris_to_new_york_projection.x());
715+
assert_eq!(50.835999775637625, paris_to_new_york_projection.y());
723716

724717
projection.distance_along_curve = 8000000.;
725718
assert!(paris_to_new_york.resolve(projection).is_err());
726719

727-
// ################################################################################
720+
// Test on a linestring where only the longitude changes (latitude remains almost the same)
728721
let lille_to_perpignan = SphericalLineStringCurve::new(
729-
line_string![(x: LILLE_LON, y: LILLE_LAT), (x: PERPIGNAN_LON, y: PERPIGNAN_LAT)],
722+
line_string![LILLE, PERPIGNAN],
730723
1.,
731724
);
732725

@@ -735,12 +728,12 @@ mod tests {
735728
offset: 0.,
736729
};
737730
let lille_to_perpignan_p = lille_to_perpignan.resolve(projection).unwrap();
738-
assert_eq!(lille_to_perpignan_p.x(), 2.963285977058639);
739-
assert_eq!(lille_to_perpignan_p.y(), 46.13725407237963);
731+
assert_eq!(2.961652543888961, lille_to_perpignan_p.x());
732+
assert_eq!(46.13397259680868, lille_to_perpignan_p.y());
740733

741-
// ################################################################################
734+
// Test on a linestring where only the latitude changes (longitude remains almost the same)
742735
let brest_to_nancy = SphericalLineStringCurve::new(
743-
line_string![(x: BREST_LON, y: BREST_LAT), (x: NANCY_LON, y: NANCY_LAT)],
736+
line_string![BREST, NANCY],
744737
1.,
745738
);
746739

@@ -749,25 +742,25 @@ mod tests {
749742
offset: 0.,
750743
};
751744
let brest_to_nancy_p = brest_to_nancy.resolve(projection).unwrap();
752-
assert_eq!(brest_to_nancy_p.x(), 2.292338879356053);
753-
assert_eq!(brest_to_nancy_p.y(), 48.69669359753767);
745+
assert_eq!(2.288400083145514, brest_to_nancy_p.x());
746+
assert_eq!(48.69523589360801, brest_to_nancy_p.y());
754747
}
755748

756749
#[test]
757750
fn spherical_bbox() {
758751
let paris_to_new_york = SphericalLineStringCurve::new(
759-
line_string![(x: PARIS_LON, y: PARIS_LAT), (x: NEW_YORK_LON, y: NEW_YORK_LAT)],
752+
line_string![PARIS, NEW_YORK],
760753
1.,
761754
);
762755
let bbox = paris_to_new_york.bbox();
763756

764757
assert_eq!(
765758
bbox.min(),
766-
coord! {x: -75.00599134051316, y: 39.71274961837565}
759+
coord! {x: -75.01, y: 39.71}
767760
);
768761
assert_eq!(
769762
bbox.max(),
770-
coord! {x: 3.352565660016694, y: 49.85643268390663}
763+
coord! {x: 3.35, y: 49.86}
771764
);
772765
}
773766

@@ -777,40 +770,39 @@ mod tests {
777770

778771
// Intersection
779772
let paris_to_new_york = SphericalLineStringCurve::new(
780-
line_string![(x: PARIS_LON, y: PARIS_LAT), (x: NEW_YORK_LON, y: NEW_YORK_LAT)],
773+
line_string![PARIS, NEW_YORK],
781774
1.,
782775
);
783776
let segment = Line::new(
784-
coord! {x: -36.76627263796084, y: 69.72980545457074},
785-
coord! {x: -53.52127629098692, y: 15.34337895024332},
777+
coord! {x: -36.77, y: 69.73},
778+
coord! {x: -53.52, y: 15.34},
786779
);
787780
assert_eq!(
788-
paris_to_new_york.intersect_segment(segment),
789-
Some(point! {x: -42.500669938830555, y: 51.11605974559634})
781+
Some(point! {x: -42.50207557067806, y: 51.11700953497436}),
782+
paris_to_new_york.intersect_segment(segment)
790783
);
791784

792785
// No intersection
793786
let segment = Line::new(
794-
coord! {x: -88.45243862592235, y: 20.758717928501483},
795-
coord! {x:19.035989490700018, y: 41.32134615429521},
787+
coord! {x: -88.45, y: 20.76},
788+
coord! {x:19.04, y: 41.32},
796789
);
797790
assert!(paris_to_new_york.intersect_segment(segment).is_none());
798791

799-
// TODO: Collinear
800-
// Notes:
792+
// Collinear: not tested
801793
// - because of the haversine densification, the geometry is slightly different and includes more points
802794
// than before, thus creating intersection(s) point(s).
803795
// - is very rare in reality
804796

805797
// Multiple intersection
806798
let paris_to_reykjavik_to_new_york = SphericalLineStringCurve::new(
807-
line_string![(x: PARIS_LON, y: PARIS_LAT), (x: REYKJAVIK_LON, y: REYKJAVIK_LAT), (x: NEW_YORK_LON, y: NEW_YORK_LAT)],
799+
line_string![PARIS, REYKJAVIK, NEW_YORK],
808800
1.,
809801
);
810802

811803
let segment = Line::new(
812-
coord! {x: -70.77775907909825, y: 47.835409180411006},
813-
coord! {x: 9.293636086504506, y: 54.83039737996501},
804+
coord! {x: -70.78, y: 47.84},
805+
coord! {x: 9.29, y: 54.83},
814806
);
815807
assert!(paris_to_reykjavik_to_new_york
816808
.intersect_segment(segment)

0 commit comments

Comments
 (0)