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_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_bounding_box_1(self): """Test bounding box.""" bbox = BoundingBox(Point(-30.0, 148.0), Point(-28.0, 150.0)) assert (repr(bbox) == "<BoundingBox(bottom_left=" "<Point(latitude=-30.0, longitude=148.0)>, " "top_right=" "<Point(latitude=-28.0, longitude=150.0)>)>") assert bbox.centroid.latitude == -29.0 assert bbox.centroid.longitude == 149.0 assert bbox.is_inside(Point(-29.5, 148.1)) assert not bbox.is_inside(Point(-29.5, 147.9))
def test_bounding_box_3(self): """Test bounding box.""" bbox = BoundingBox(Point(-5.0, 175.0), Point(5.0, -175.0)) assert (repr(bbox) == "<BoundingBox(bottom_left=" "<Point(latitude=-5.0, longitude=175.0)>, " "top_right=" "<Point(latitude=5.0, longitude=-175.0)>)>") assert bbox.centroid.latitude == 0.0 assert bbox.centroid.longitude == 180.0 assert bbox.is_inside(Point(-2.5, -179.0)) assert not bbox.is_inside(Point(2.5, -170.0))
def _create_bbox_multiple(bbox: List) -> List[BoundingBox]: """Create multiple bboxes from provided list of coordinates.""" bounding_boxes = [] for entry in bbox: if len(entry) == 4: bounding_boxes.append( BoundingBox(Point(entry[2], entry[0]), Point(entry[3], entry[1]))) else: _LOGGER.warning("Insufficient data for " "bounding box: %s", entry) return bounding_boxes
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_distance_to_point(self): """Test calculating distance to point.""" home_coordinates = (-31.0, 150.0) mock_point = Point(-30.0, 151.0) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_point) self.assertAlmostEqual(distance, 146.8, 1)
def test_extract_coordinates_from_point(self): """Test extracting coordinates from point.""" mock_point = Point(-30.0, 151.0) latitude, longitude = GeoRssDistanceHelper.extract_coordinates( mock_point) assert latitude == -30.0 assert longitude == 151.0
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 _geometry_geo_long_lat(self) -> Optional[List[Point]]: """Check for geo:long and geo:lat tags.""" # <geo:long>119.948006</geo:long> # <geo:lat>-23.126413</geo:lat> lat = self._attribute([XML_TAG_GEO_LAT]) long = self._attribute([XML_TAG_GEO_LONG]) if long and lat: return [Point(lat, long)] return None
def test_distance_to_bbox_2(self): """Test calculating distance to bounding box.""" mock_bbox = BoundingBox(Point(5.0, 175.0), Point(15.0, -175.0)) # 1. inside home_coordinates = (5.0, 176.0) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 0.0, 1) # 2. above-left home_coordinates = (20.0, 170.0) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 768.1, 1) # 3. above-right home_coordinates = (20.0, -170.0) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 768.1, 1)
def _geometry_geo_point(self) -> Optional[List[Point]]: """Check for geo:Point tag.""" # <geo:Point xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> # <geo:lat>38.3728</geo:lat> # <geo:long>15.7213</geo:long> # </geo:Point> point = self._attribute([XML_TAG_GEO_POINT]) if point: lat = point.get(XML_TAG_GEO_LAT) long = point.get(XML_TAG_GEO_LONG) if long and lat: return [Point(lat, long)] return None
def _geometry_georss_where(self) -> Optional[List[Geometry]]: """Check for georss:where tag.""" where = self._attribute([XML_TAG_GEORSS_WHERE]) if where: # Point: # <georss:where> # <gml:Point> # <gml:pos>44.11 -66.23</gml:pos> # </gml:Point> # </georss:where> pos = self._attribute_in_structure( where, [XML_TAG_GML_POINT, XML_TAG_GML_POS]) if pos: return [Point(pos[0], pos[1])] # Polygon: # <georss:where> # <gml:Polygon> # <gml:exterior> # <gml:LinearRing> # <gml:posList> # -71.106216 42.366661 # -71.105576 42.367104 # -71.104378 42.367134 # -71.103729 42.366249 # -71.098793 42.363331 # -71.101028 42.362541 # -71.106865 42.366123 # -71.106216 42.366661 # </gml:posList> # </gml:LinearRing> # </gml:exterior> # </gml:Polygon> # </georss:where> pos_list = self._attribute_in_structure( where, [ XML_TAG_GML_POLYGON, XML_TAG_GML_EXTERIOR, XML_TAG_GML_LINEAR_RING, XML_TAG_GML_POS_LIST, ], ) if pos_list: return self._create_polygon(pos_list) return None
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)
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 _create_georss_point_multiple(point: List) -> List[Point]: """Create multiple points from provided coordinates.""" points = [] for entry in point: points.append(Point(entry[0], entry[1])) return points
def _create_georss_point_single(point: Tuple) -> List[Point]: """Create single point from provided coordinates.""" return [Point(point[0], point[1])]
def test_distance_to_bbox_1(self): """Test calculating distance to bounding box.""" home_coordinates = (20.0, 20.0) # 1. inside mock_bbox = BoundingBox(Point(10.0, 10.0), Point(30.0, 30.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 0.0, 1) # 2. above-left mock_bbox = BoundingBox(Point(10.0, 25.0), Point(15.0, 30.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 768.1, 1) # 3. above mock_bbox = BoundingBox(Point(10.0, 15.0), Point(15.0, 25.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 556.0, 1) # 4. above-right mock_bbox = BoundingBox(Point(10.0, 10.0), Point(15.0, 15.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 768.1, 1) # 5. left mock_bbox = BoundingBox(Point(15.0, 25.0), Point(25.0, 30.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 522.4, 1) # 6. right mock_bbox = BoundingBox(Point(15.0, 10.0), Point(25.0, 15.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 522.4, 1) # 7. below-left mock_bbox = BoundingBox(Point(25.0, 25.0), Point(30.0, 30.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 756.8, 1) # 8. below mock_bbox = BoundingBox(Point(25.0, 15.0), Point(30.0, 25.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 556.0, 1) # 9. below-right mock_bbox = BoundingBox(Point(25.0, 10.0), Point(30.0, 15.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 756.8, 1) # special case home_coordinates = (-20.0, -20.0) mock_bbox = BoundingBox(Point(-30.0, -15.0), Point(-25.0, -10.0)) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_bbox) self.assertAlmostEqual(distance, 756.8, 1)
def _create_bbox_single(bbox: Tuple) -> List[BoundingBox]: """Create single bbox from provided tuple of coordinates.""" return [BoundingBox(Point(bbox[2], bbox[0]), Point(bbox[3], bbox[1]))]
def test_point(self): """Test point.""" point = Point(-37.1234, 149.2345) assert point.latitude == -37.1234 assert point.longitude == 149.2345 assert repr(point) == "<Point(latitude=-37.1234, longitude=149.2345)>"
def test_point_equality(self): """Test points.""" point1 = Point(10.0, 15.0) point2 = Point(10.0, 15.0) assert point1 == point2
def test_bounding_box_equality(self): """Test points.""" bbox1 = BoundingBox(Point(10.0, 10.0), Point(20.0, 20.0)) bbox2 = BoundingBox(Point(10.0, 10.0), Point(20.0, 20.0)) assert bbox1 == bbox2