def test_point_in_polygon_2(self): """Test if point is in polygon.""" polygon = Polygon([ Point(30.0, -30.0), Point(30.0, -25.0), Point(35.0, -25.0), Point(30.0, -30.0), ]) # 1. Outside point = Point(20.0, -40.0) assert not polygon.is_inside(point) # 2. Inside point = Point(31.0, -28.0) assert polygon.is_inside(point) # 3. Inside point = Point(30.0, -28.0) assert polygon.is_inside(point) # 4. Inside point = Point(30.0, -25.0) assert polygon.is_inside(point) # 5. Outside point = Point(34.0, -29.0) assert not polygon.is_inside(point) # 6. Invalid point assert not polygon.is_inside(None)
def test_distance_to_polygon_6(self): """Test calculating distance to polygon.""" mock_polygon = Polygon([ Point(-30.0, 179.0), Point(-30.0, -179.5), Point(-29.5, -179.5), Point(-29.5, 179.0), Point(-30.0, 179.0), ]) home_coordinates = (-29.8, -177.0) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_polygon) self.assertAlmostEqual(distance, 241.2, 1) home_coordinates = (-29.9, 178.0) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_polygon) self.assertAlmostEqual(distance, 96.4, 1) home_coordinates = (-29.0, -179.8) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_polygon) self.assertAlmostEqual(distance, 55.6, 1) home_coordinates = (-29.0, 179.8) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_polygon) self.assertAlmostEqual(distance, 55.6, 1)
def test_polygon_equality(self): """Test points.""" polygon1 = Polygon([ Point(30.0, 30.0), Point(30.0, 35.0), Point(35.0, 35.0), Point(35.0, 30.0), Point(30.0, 30.0), ]) polygon2 = Polygon([ Point(30.0, 30.0), Point(30.0, 35.0), Point(35.0, 35.0), Point(35.0, 30.0), Point(30.0, 30.0), ]) assert polygon1 == polygon2
def _create_polygon_single(polygon_data: Tuple) -> List[Polygon]: """Create polygon from provided tuple of coordinates.""" if len(polygon_data) % 2 != 0: # Not even number of coordinates - chop last entry. polygon_data = polygon_data[0:len(polygon_data) - 1] points = [] for i in range(0, len(polygon_data), 2): points.append(Point(polygon_data[i], polygon_data[i + 1])) return [Polygon(points)]
def test_distance_to_polygon_4(self): """Test calculating distance to polygon.""" home_coordinates = (30.0, 151.3) mock_polygon = Polygon([ Point(30.0, 151.0), Point(30.0, 151.5), Point(30.5, 151.5), Point(30.5, 151.0), Point(30.0, 151.0), ]) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_polygon) self.assertAlmostEqual(distance, 0.0, 1)
def test_extract_coordinates_from_polygon(self): """Test extracting coordinates from polygon.""" mock_polygon = Polygon([ Point(-30.0, 151.0), Point(-30.0, 151.5), Point(-30.5, 151.5), Point(-30.5, 151.0), Point(-30.0, 151.0), ]) latitude, longitude = GeoRssDistanceHelper.extract_coordinates( mock_polygon) self.assertAlmostEqual(latitude, -30.2, 1) self.assertAlmostEqual(longitude, 151.2, 1)
def test_polygon(self): """Test polygon.""" polygon = Polygon([ Point(-30.1, 150.1), Point(-30.2, 150.2), Point(-30.4, 150.4), Point(-30.8, 150.8), Point(-30.1, 150.1), ]) assert len(polygon.points) == 5 assert polygon.centroid.latitude == -30.32 assert polygon.centroid.longitude == 150.32 assert (repr(polygon) == "<Polygon(centroid=" "<Point(latitude=-30.32, longitude=150.32)>)>")
def test_feed_entry_features(): """Test feed entry filtering by feature.""" point = Point(0.0, 0.0) polygon = Polygon([point, point]) bounding_box = BoundingBox(point, point) feed_item = MockFeedItem( None, [point, polygon, point, polygon, polygon, bounding_box]) # 1. Include all feed_entry = MockSimpleFeedEntry(None, feed_item, [Point, Polygon, BoundingBox]) assert len(feed_entry.geometries) == 6 # 2. Exclude points feed_entry = MockSimpleFeedEntry(None, feed_item, [Polygon, BoundingBox]) assert len(feed_entry.geometries) == 4 # 3. Exclude polygons feed_entry = MockSimpleFeedEntry(None, feed_item, [Point, BoundingBox]) assert len(feed_entry.geometries) == 3 # 4. Exclude bounding boxes feed_entry = MockSimpleFeedEntry(None, feed_item, [Point, Polygon]) assert len(feed_entry.geometries) == 5 # 5. Exclude all feed_entry = MockSimpleFeedEntry(None, feed_item, []) assert len(feed_entry.geometries) == 0
def test_point_in_polygon_1(self): """Test if point is in polygon.""" polygon = Polygon([ Point(30.0, 30.0), Point(30.0, 35.0), Point(35.0, 35.0), Point(30.0, 30.0) ]) # 1. Outside point = Point(20.0, 20.0) assert not polygon.is_inside(point) # 2. Inside point = Point(31.0, 32.0) assert polygon.is_inside(point) # 3. Inside point = Point(30.0, 32.0) assert polygon.is_inside(point) # 4. Inside point = Point(30.0, 35.0) assert polygon.is_inside(point) # 5. Outside point = Point(34.0, 31.0) assert not polygon.is_inside(point)