def test_left_right_not_parallel(self): """ Tests the function is_rectangle for a polygon whose left and right sides are not parallel. """ # left side crooked polygon = ( (0.0, 0.0), (25.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0) ) self.assertEqual(shapes.is_rectangle(polygon), False) # right side crooked polygon = ( (0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (25.0, 0.0), (0.0, 0.0) ) self.assertEqual(shapes.is_rectangle(polygon), False)
def test_no_width(self): """ Tests the function is_rectangle for a polygon whose bottom or top is a point. """ # top is point polygon = ( (0.0, 0.0), (0.0, 50.0), (0.0, 50.0), (50.0, 0.0), (0.0, 0.0) ) self.assertEqual(shapes.is_rectangle(polygon), False) # bottom is point polygon = ( (0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (0.0, 0.0), (0.0, 0.0) ) self.assertEqual(shapes.is_rectangle(polygon), False)
def test_top_bottom_not_parallel(self): """ Tests the function is_rectangle for a polygon whose top and bottom sides are not parallel. """ # top crooked polygon = ( (0.0, 0.0), (0.0, 25.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0) ) self.assertEqual(shapes.is_rectangle(polygon), False) # botton crooked polygon = ( (0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 25.0), (0.0, 0.0) ) self.assertEqual(shapes.is_rectangle(polygon), False)
def test_no_height(self): """ Tests the function is_rectangle for a polygon whose side is a point. """ # left side is point polygon = ((0.0, 0.0), (0.0, 0.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) self.assertEqual(shapes.is_rectangle(polygon), False) # right side is point polygon = ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 50.0), (0.0, 0.0)) self.assertEqual(shapes.is_rectangle(polygon), False)
def test_at_equator(self): """ Tests the function convert_circle_to_rectangle at the Equator. This makes it easier to calculate expected values. """ coord = (-78.0, 0) radius_m = 10000 square = shapes.convert_circle_to_rectangle(coord, radius_m) # check that the shape is correct self.assertEqual(shapes.is_rectangle(square[0]), True, 'Shape is not a rectangle') # check that the rectangle center is at the original point bounds = Bounds(*square.extent) self.assertAlmostEqual(square.centroid[0], coord[0], places=6, msg='The center longitude is incorrect') self.assertAlmostEqual(square.centroid[1], coord[1], places=6, msg='The center latitude is incorrect') # check that rectangle has the correct area area_in_sq_m = bounds.center_width_m * bounds.height_m actual_sq_km = units.sq_meters_to_sq_km(area_in_sq_m) expected_sq_km = (2 * radius_m / 1000)**2 self.assertAlmostEqual(actual_sq_km, expected_sq_km, delta=0.001) # check that the rectangle contains the circle radius_in_degrees = units.convert_meters_to_degrees(radius_m, 'down') point = Point(coord) circle = point.buffer(radius_in_degrees) self.assertTrue(square.contains(circle))
def test_for_counterclockwise(self): """ Tests the function is_rectangle for a counterclockwise rectangle. """ rectangle = (((0.0, 0.0), (50.0, 0.0), (50.0, 50.0), (0.0, 50.0), (0.0, 0.0))) self.assertEqual(shapes.is_rectangle(rectangle), True)
def test_less_than_four_segments(self): """ Tests the function is_rectangle for fewer than four line segments. """ # a triangle coords = ((0.0, 0.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) self.assertEqual(shapes.is_rectangle(coords), False)
def test_more_than_four_segments(self): """ Tests the function is_rectangle for more than four line segments. """ # a pentagon coords = ((0.0, 0.0), (0.0, 50.0), (25.0, 75.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) self.assertEqual(shapes.is_rectangle(coords), False)
def test_not_closed(self): """ Tests the function is_rectangle for a series of coordinates that are not closed. """ coords = ( (0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 1.0) # doesn't match first point ) self.assertEqual(shapes.is_rectangle(coords), False)
def shape(self): """ Returns a string indicating the feature type. If the feature type is not a Point, Polygon, or Multipolygon, returns 'Other'. """ if isinstance(self.geom, Point): if self.has_buffer(): return 'Circle' else: return 'Point' elif isinstance(self.geom, Polygon): if shapes.is_rectangle(self.geom.exterior_ring): return 'Rectangle' else: return 'Polygon' elif isinstance(self.geom, MultiPolygon): return 'MultiPolygon' else: return 'Other'