def test_get_width_at_latitude(self): """ Tests method for finding the width of the bounding box at the given latitude, in meters. """ bounds = Bounds(n_lat=50.0, s_lat=0.0, e_lng=50.0, w_lng=0.0) width = bounds.get_width_at_latitude_m(25.0) self.assertAlmostEqual(width, 5017040, delta=5)
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_buffer(self): """ Tests method for buffering the bounds. """ bounds = Bounds(n_lat=28.0, s_lat=26.0, e_lng=0.0, w_lng=-2.0) old_height = bounds.height_m old_width = bounds.center_width_m buffer_m = 10 bounds.buffer(buffer_m) actual_height = bounds.height_m actual_width = bounds.center_width_m expected_height = old_height + (buffer_m * 2) expected_width = old_width + (buffer_m * 2) self.assertAlmostEqual(actual_height, expected_height, delta=0.001) self.assertAlmostEqual(actual_width, expected_width, delta=0.001)
def test_center_width(self): """ Tests method for finding the average width of the bounding box in meters. """ bounds = Bounds(n_lat=50.0, s_lat=0.0, e_lng=25.0, w_lng=0.0) expected = vincenty((25.0, 0), (25.0, 25.0)).meters self.assertEqual(bounds.center_width_m, expected)
def convert_circle_to_rectangle(point, radius_m): """ (tuple(int or float, int or float), int or float) -> GEOSGeometry Polygon Takes a circle as a (lng, lat) tuple and a radius in meters. Returns the smallest rectangular Polygon that encompasses the circle. """ # reverse the lng, lat order to convert to geopy Point format center = reverse_coordinate_order(point) # create a geopy coordinate calculator for the given distance calculator = vincenty(meters=radius_m) # calculate points at the given distance in the cardinal directions n_pt = calculator.destination(point=center, bearing=0) s_pt = calculator.destination(point=center, bearing=180) e_pt = calculator.destination(point=center, bearing=90) w_pt = calculator.destination(point=center, bearing=270) bounds = Bounds(n_lat=n_pt.latitude, s_lat=s_pt.latitude, e_lng=e_pt.longitude, w_lng=w_pt.longitude) return bounds.bounding_box
def create_buffered_bounds(polygon, buffer_m): """ Takes a GEOSGeometry Polygon and a buffer distance in meters. Returns a Bounds object representing the polygon bounds buffered by the specified distance. """ assert (isinstance(polygon, Polygon)) return Bounds(*polygon.extent).buffer(buffer_m)
def create_buffered_bounds(polygon, buffer_m): """ Takes a GEOSGeometry Polygon and a buffer distance in meters. Returns a Bounds object representing the polygon bounds buffered by the specified distance. """ if not isinstance(polygon, Polygon): # pragma: no cover raise TypeError('object is not a Polygon') return Bounds(*polygon.extent).buffer(buffer_m)
def test_default_corners(self): """ Tests the methods for getting corners using the default values for a new Bounds instance. """ bounds = Bounds() points = (bounds.sw_corner, bounds.nw_corner, bounds.ne_corner, bounds.se_corner) self.assertEqual(points, ((180.0, 90.0), (180.0, -90.0), (-180.0, -90.0), (-180.0, 90.0)))
def test_height(self): """ Tests method for finding the height of the bounding box in meters. """ bounds = Bounds(n_lat=50.0, s_lat=0.0, e_lng=25.0, w_lng=0.0) self.assertAlmostEqual(bounds.height_m, 5540850, delta=5)