def test_degenerate_shapes(self): # Test all degenerate shapes (points, lines) have area zero points = sgeo.MultiPoint([(4, 59), (6, 59), (6, 61), (4, 61)]) line = sgeo.LineString([(4, 59), (6, 59), (6, 61), (4, 61)]) ring = sgeo.LinearRing([(4, 59), (6, 59), (6, 61), (4, 61)]) self.assertEqual(utils.GeometryArea(points), 0) self.assertEqual(utils.GeometryArea(line), 0) self.assertEqual(utils.GeometryArea(ring), 0)
def test_complex_polygons(self): square = sgeo.Polygon([(4, 59), (6, 59), (6, 61), (4, 61)]) hole = sgeo.Polygon([(5, 60), (5.1, 60.1), (5, 60.2)]) square_with_hole = sgeo.Polygon(square.exterior, [hole.exterior]) multipoly = sgeo.MultiPolygon([square_with_hole, hole]) self.assertAlmostEqual( utils.GeometryArea(square_with_hole), utils.GeometryArea(square) - utils.GeometryArea(hole), 7) self.assertAlmostEqual(utils.GeometryArea(multipoly), 24699.71, 2)
def test_area_geojson_geocollection_and_merge(self): expected_area = 3535 expected_real_area = expected_area * 2 / 3. with open(os.path.join(TEST_DIR, 'test_geocollection.json'), 'r') as fd: multigeo = json.load(fd) area = utils.GeometryArea(multigeo) self.assertAlmostEqual(area, expected_area, 0) area = utils.GeometryArea(multigeo, merge_geometries=True) self.assertAlmostEqual(area, expected_real_area, 0)
def test_area_missouri(self): # The real Missouri area is 180530 km2. Make sure we are within 0.25% official_area = 180530 with open(os.path.join(TEST_DIR, 'missouri.json'), 'r') as fd: missouri = json.load(fd) poly = sgeo.Polygon(missouri['geometries'][0]['coordinates'][0][0]) area = utils.GeometryArea(poly) self.assertTrue(np.abs(area - official_area) < 0.0025 * official_area)
def test_remove_small_holes(self): # Test all degenerate shapes (points, lines) have area zero # Use a small sw=quare centered around 60degree latitude, so radius is half # on the longitude square = sgeo.Polygon([(4, 59), (6, 59), (6, 61), (4, 61)]) hole = sgeo.Polygon([(5, 60), (5.01, 60.01), (5, 60.02)]) square_with_hole = sgeo.Polygon([(4, 59), (6, 59), (6, 61), (4, 61)], [[(5, 60), (5.01, 60.01), (5, 60.02)]]) square_cleaned = utils.PolyWithoutSmallHoles(square_with_hole, min_hole_area_km2=0.62) multipoly = sgeo.MultiPolygon([square_with_hole, hole]) multipoly_cleaned = utils.PolyWithoutSmallHoles(multipoly, min_hole_area_km2=0.62) area = utils.GeometryArea(square) area_with_hole = utils.GeometryArea(square_with_hole) self.assertAlmostEqual(area - area_with_hole, 0.617, 3) self.assertEqual(square, square_cleaned) self.assertEqual(multipoly_cleaned, sgeo.MultiPolygon([square, hole]))
def assertAlmostSamePolygon(self, poly1, poly2, tol_km2=0.001): self.assertTrue(utils.GeometryArea(poly1.difference(poly2)) < tol_km2) self.assertTrue(utils.GeometryArea(poly2.difference(poly1)) < tol_km2)
def test_area_geojson_ppa(self): expected_area = 130.98 with open(os.path.join(TEST_DIR, 'ppa_record_0.json'), 'r') as fd: ppa = json.load(fd) area = utils.GeometryArea(ppa['zone']['features'][0]['geometry']) self.assertAlmostEqual(area, expected_area, 2)
def test_area_geojson_missouri(self): official_area = 180530 with open(os.path.join(TEST_DIR, 'missouri.json'), 'r') as fd: missouri = json.load(fd) area = utils.GeometryArea(missouri) self.assertTrue(np.abs(area - official_area) < 0.0025 * official_area)
def test_area_simple_square(self): # Use a small sw=quare centered around 60degree latitude, so radius is half # on the longitude square = sgeo.Polygon([(4, 59), (6, 59), (6, 61), (4, 61)]) area = utils.GeometryArea(square) self.assertAlmostEqual(area, 24699.71, 2)