def test_distance_to_polygon(self): """Test calculating distance to point.""" home_coordinates = [-31.0, 150.0] 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, 110.6, 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 _create_polygon(coordinates): """Create a polygon from the provided coordinates.""" if coordinates: if len(coordinates) % 2 != 0: # Not even number of coordinates - chop last entry. coordinates = coordinates[0:len(coordinates) - 1] points = [] for i in range(0, len(coordinates), 2): points.append(Point(coordinates[i], coordinates[i + 1])) return Polygon(points) return None
def geometry(self) -> Optional[Geometry]: """Return the geometry of this feed item.""" # <georss:point>-0.5 119.8</georss:point> point = self._attribute([XML_TAG_GEORSS_POINT]) if point: return Point(point[0], point[1]) # GML 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) # <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) # <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) # <georss:polygon> # -34.937663524 148.597260613 # -34.9377026399999 148.597169138 # -34.9377002169999 148.59708737 # -34.9376945989999 148.59705595 # -34.9376863529999 148.596955098 # -34.937663524 148.597260613 # </georss:polygon> polygon = self._attribute([XML_TAG_GEORSS_POLYGON]) if polygon: # For now, only supporting the first polygon. if isinstance(polygon, list) and isinstance(polygon[0], list): polygon = polygon[0] return self._create_polygon(polygon) # None of the above return None
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)>"