Exemple #1
0
    def test_two_points_fail(self):
        coord_list = [
            Coordinate(37.099623, -8.469745),
            Coordinate(37.099623, -8.469745),
        ]

        with self.assertRaises(NotAPolygonError):
            in_coord = Coordinate(37.099623, -8.46974)
            is_pt_inside = in_coord.is_inside_polygon(coord_list)
 def _sort_dealers_by_distance(self, latitude, longitude):
     my_coord = Coordinate(latitude, longitude)
     sorted_dealers = defaultdict(list)
     # go through the dealers list and compute the distances
     for dealer in self._dataset['dealers']:
         dealer_coord = Coordinate(dealer['latitude'], dealer['longitude'])
         distance = my_coord.distance_to(dealer_coord)
         sorted_dealers[distance].append(dealer)
     sorted_dealers = OrderedDict(sorted(sorted_dealers.items()))
     sorted_dealers = [dealer for dealer in sorted_dealers.values()]
     return sorted_dealers
 def get_dealers_in_polygon_with_vehicle(self,
                                         coord_pair,
                                         model=None,
                                         fuel=None,
                                         transmission=None):
     res = []
     polygon = [Coordinate(*lat_lon_pair) for lat_lon_pair in coord_pair]
     for dealer in self._dataset['dealers']:
         if self._dealer_has_vehicle(dealer, model, fuel, transmission):
             dealer_coord = Coordinate(dealer['latitude'],
                                       dealer['longitude'])
             try:
                 if dealer_coord.is_inside_polygon(polygon):
                     res.append(dealer)
             except NotAPolygonError as e:
                 raise TestDriveError(str(e))
     return res
Exemple #4
0
    def test_points_in_triangle(self):
        coord_list = [
            Coordinate(37.096542, -8.473348),
            Coordinate(37.095747, -8.470210),
            Coordinate(37.097643, -8.470783)
        ]

        in_coord = Coordinate(37.097022, -8.471147)
        is_pt_inside = in_coord.is_inside_polygon(coord_list)
        self.assertTrue(is_pt_inside)

        out_coord = Coordinate(37.097598, -8.469701)
        is_pt_inside = out_coord.is_inside_polygon(coord_list)
        self.assertFalse(is_pt_inside)
Exemple #5
0
    def test_distance_from_texas_to_guatemala(self):
        """Test haversine distance."""
        # Make sure that Kurupt won't get a wrong result when planning his
        #   next trip :)
        texas = Coordinate(29.7630556, -95.3630556)  # Houston
        guatemala = Coordinate(14.628434, -90.522713)

        expected = 1754.502
        obtained = texas.distance_to(guatemala)
        self.assertEqual(expected, obtained, 'Wrong distance computed.')

        # Kurupt will also have to come back from his trip
        expected = 1754.502
        obtained = guatemala.distance_to(texas)
        self.assertEqual(expected, obtained, 'Wrong distance computed.')
Exemple #6
0
    def test_points_outside_of_polygon(self):

        coord_list = [
            Coordinate(31.000213, -87.584839),
            Coordinate(31.009629, -85.003052),
            Coordinate(30.726726, -84.838257),
            Coordinate(30.584962, -82.168579),
            Coordinate(30.73617, -81.476441),
            Coordinate(29.002375, -80.795288),
            Coordinate(26.896598, -79.938355),
            Coordinate(25.813738, -80.059204),
            Coordinate(24.93028, -80.454712),
            Coordinate(24.401135, -81.817017),
            Coordinate(24.700927, -81.959839),
            Coordinate(24.950203, -81.124878),
            Coordinate(26.0015, -82.014771),
            Coordinate(27.833247, -83.014527),
            Coordinate(28.8389, -82.871704),
            Coordinate(29.987293, -84.091187),
            Coordinate(29.539053, -85.134888),
            Coordinate(30.272352, -86.47522),
            Coordinate(30.281839, -87.628784),
        ]

        out_coord = Coordinate(24.311058, -81.17981)
        is_pt_inside = out_coord.is_inside_polygon(coord_list)
        self.assertFalse(is_pt_inside)

        out_coord = Coordinate(29.029276, -90.805666)
        is_pt_inside = out_coord.is_inside_polygon(coord_list)
        self.assertFalse(is_pt_inside)

        out_coord = Coordinate(25.159207, -79.916382)
        is_pt_inside = out_coord.is_inside_polygon(coord_list)
        self.assertFalse(is_pt_inside)

        out_coord = Coordinate(31.319856, -84.607544)
        is_pt_inside = out_coord.is_inside_polygon(coord_list)
        self.assertFalse(is_pt_inside)
Exemple #7
0
    def test_points_inside_of_polygon(self):
        coord_list = [
            Coordinate(31.000213, -87.584839),
            Coordinate(31.009629, -85.003052),
            Coordinate(30.726726, -84.838257),
            Coordinate(30.584962, -82.168579),
            Coordinate(30.73617, -81.476441),
            Coordinate(29.002375, -80.795288),
            Coordinate(26.896598, -79.938355),
            Coordinate(25.813738, -80.059204),
            Coordinate(24.93028, -80.454712),
            Coordinate(24.401135, -81.817017),
            Coordinate(24.700927, -81.959839),
            Coordinate(24.950203, -81.124878),
            Coordinate(26.0015, -82.014771),
            Coordinate(27.833247, -83.014527),
            Coordinate(28.8389, -82.871704),
            Coordinate(29.987293, -84.091187),
            Coordinate(29.539053, -85.134888),
            Coordinate(30.272352, -86.47522),
            Coordinate(30.281839, -87.628784),
        ]

        in_coord = Coordinate(30.82112, -87.255249)
        is_pt_inside = in_coord.is_inside_polygon(coord_list)
        self.assertTrue(is_pt_inside)

        in_coord = Coordinate(27.92065, -82.619019)
        is_pt_inside = in_coord.is_inside_polygon(coord_list)
        self.assertTrue(is_pt_inside)

        in_coord = Coordinate(25.853292, -80.223999)
        is_pt_inside = in_coord.is_inside_polygon(coord_list)
        self.assertTrue(is_pt_inside)

        in_coord = Coordinate(24.680963, -81.366577)
        is_pt_inside = in_coord.is_inside_polygon(coord_list)
        self.assertTrue(is_pt_inside)
Exemple #8
0
 def test_coord_is_not_dict(self):
     coord = Coordinate(1, 2)
     d = {'a': 'b'}
     self.assertNotEqual(coord, d)
Exemple #9
0
 def test_repeated_point_fail(self):
     coord_list = [
         Coordinate(31.000213, -87.584839),
         Coordinate(31.009629, -85.003052),
         Coordinate(30.726726, -84.838257),
         Coordinate(30.584962, -82.168579),
         Coordinate(30.73617, -81.476441),
         Coordinate(29.002375, -80.795288),
         Coordinate(26.896598, -79.938355),
         Coordinate(25.813738, -80.059204),
         Coordinate(24.93028, -80.454712),
         Coordinate(24.401135, -81.817017),
         Coordinate(24.700927, -81.959839),
         Coordinate(24.950203, -81.124878),
         Coordinate(26.0015, -82.014771),
         Coordinate(27.833247, -83.014527),
         Coordinate(28.8389, -82.871704),
         Coordinate(29.987293, -84.091187),
         Coordinate(29.539053, -85.134888),
         Coordinate(30.272352, -86.47522),
         Coordinate(30.281839, -87.628784),
         Coordinate(30.281839, -87.628784),
     ]
     with self.assertRaises(NotAPolygonError):
         out_coord = Coordinate(24.311058, -81.17981)
         is_pt_inside = out_coord.is_inside_polygon(coord_list)