def setUp(self): self.point = Point((1.0, 2.0, 3.0), properties={"type": "apple", "color": (43,67,10)}) self.vertices = [(2.0, 9.0, 9.0), (4.0, 1.0, 9.0), (4.0, 1.0, 5.0), (2.0, 8.0, 0.0), (9.0, 8.0, 4.0), (1.0, 4.0, 6.0), (7.0, 3.0, 4.0), (2.0, 5.0, 3.0), (1.0, 6.0, 6.0), (8.0, 1.0, 0.0), (5.0, 5.0, 1.0), (4.0, 5.0, 7.0), (3.0, 3.0, 5.0), (9.0, 0.0, 9.0), (6.0, 3.0, 8.0), (4.0, 5.0, 7.0), (9.0, 9.0, 4.0), (1.0, 4.0, 7.0), (1.0, 7.0, 8.0), (9.0, 1.0, 6.0)] self.data = [99.0, 2.0, 60.0, 75.0, 71.0, 34.0, 1.0, 49.0, 4.0, 36.0, 47.0, 58.0, 65.0, 72.0, 4.0, 27.0, 52.0, 37.0, 95.0, 17.0] self.mp = Multipoint(self.vertices, data=self.data) self.line = Line(self.vertices) self.poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)]) self.poly3 = Polygon([(0.0, 8.0, 0.5), (0.0, 5.0, 0.8), (6.0, 1.0, 0.6)]) self.ring = Polygon([(2.0, 2.0), (4.0, 2.0), (3.0, 6.0)]) self.ringed_poly = Polygon([(0.0, 0.0), (10, 0.0), (10.0, 10.0), (0.0, 10.0)], subs=[self.ring]) self.unitsquare = Polygon([(0.0,0.0), (1.0,0.0), (1.0,1.0), (0.0,1.0)]) return
def setUp(self): self.point = Point((1.0, 2.0, 3.0), properties={"type": "apple", "color": (43,67,10)}) self.vertices = [(2.0, 9.0, 9.0), (4.0, 1.0, 9.0), (4.0, 1.0, 5.0), (2.0, 8.0, 0.0), (9.0, 8.0, 4.0), (1.0, 4.0, 6.0), (7.0, 3.0, 4.0), (2.0, 5.0, 3.0), (1.0, 6.0, 6.0), (8.0, 1.0, 0.0), (5.0, 5.0, 1.0), (4.0, 5.0, 7.0), (3.0, 3.0, 5.0), (9.0, 0.0, 9.0), (6.0, 3.0, 8.0), (4.0, 5.0, 7.0), (9.0, 9.0, 4.0), (1.0, 4.0, 7.0), (1.0, 7.0, 8.0), (9.0, 1.0, 6.0)] self.data = [99.0, 2.0, 60.0, 75.0, 71.0, 34.0, 1.0, 49.0, 4.0, 36.0, 47.0, 58.0, 65.0, 72.0, 4.0, 27.0, 52.0, 37.0, 95.0, 17.0] self.mp = Multipoint(self.vertices, data=self.data) self.line = Line(self.vertices) self.poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)]) self.poly3 = Polygon([(0.0, 8.0, 0.5), (0.0, 5.0, 0.8), (6.0, 1.0, 0.6)]) self.ring = Polygon([(2.0, 2.0), (4.0, 2.0), (3.0, 6.0)]) self.ringed_poly = Polygon([(0.0, 0.0), (10, 0.0), (10.0, 10.0), (0.0, 10.0)], subs=[self.ring]) self.unitsquare = Polygon([(0.0,0.0), (1.0,0.0), (1.0,1.0), (0.0,1.0)]) return
def test_poly_contains_polar(self): p = Polygon([(0, 80), (45, 80), (90, 80), (135, 80), (180, 80), (225, 80), (270, 80), (315, 80)], crs=SphericalEarth) self.assertTrue(p.contains(Point((45, 85), crs=SphericalEarth))) self.assertFalse(p.contains(Point((45, 75), crs=SphericalEarth))) return
def test_ringedpoly_perimeter(self): ring = Polygon([(2.0, 2.0), (4.0, 2.0), (3.0, 6.0)]) ringed_poly = Polygon([(0.0, 0.0), (10, 0.0), (10.0, 10.0), (0.0, 10.0)], subs=[ring]) self.assertEqual(round(ringed_poly.perimeter, 3), 50.246) return
def test_poly_contains_polar(self): p = Polygon([(0, 80), (45, 80), (90, 80), (135, 80), (180, 80), (225, 80), (270, 80), (315, 80)], crs=SphericalEarth) self.assertTrue(p.contains(Point((45, 85), crs=SphericalEarth))) self.assertFalse(p.contains(Point((45, 75), crs=SphericalEarth))) return
def test_ringedpoly_area(self): ring = Polygon([(2.0, 2.0), (4.0, 2.0), (3.0, 6.0)]) ringed_poly = Polygon([(0.0, 0.0), (10, 0.0), (10.0, 10.0), (0.0, 10.0)], subs=[ring]) self.assertEqual(ringed_poly.area, 100 - ring.area) return
def test_poly_extent_foreign_crs(self): poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)], crs=LonLatWGS84) poly3 = Polygon([(0.0, 8.0, 0.5), (0.0, 5.0, 0.8), (6.0, 1.0, 0.6)], crs=LonLatWGS84) x, y = zip(*poly.get_vertices(crs=NSIDCNorth)) self.assertEqual(poly.get_extent(NSIDCNorth), (min(x), max(x), min(y), max(y))) self.assertEqual(poly3.get_extent(NSIDCNorth), (min(x), max(x), min(y), max(y))) return
def setUp(self): self.points = [Point((1, 1), data={"species": "T. officianale"}, crs=LonLatWGS84), Point((3, 1), data={"species": "C. tectorum"}, crs=LonLatWGS84), Point((4, 3), data={"species": "M. alba"}, crs=LonLatWGS84), Point((2, 2), data={"species": "V. cracca"}, crs=LonLatWGS84)] self.multipoint = Multipoint([(1,1), (3,1), (4,3), (2,2)], data={"species": ["T. officianale", "C. tectorum", "M. alba", "V. cracca"]}, crs=LonLatWGS84) self.line = Line([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], properties={"geom_id": 27, "name": "test line"}, crs=LonLatWGS84) self.polygon = Polygon([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], crs=LonLatWGS84) self.points3 = [Point((1, 1, 0), crs=LonLatWGS84), Point((3, 1, 3), crs=LonLatWGS84), Point((4, 3, 2), crs=LonLatWGS84), Point((2, 2, -1), crs=LonLatWGS84)] self.line3 = Line([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) self.polygon3 = Polygon([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) testfiles = ["points.shp", "line.shp", "polygon.shp"] if any(not exists(join(TMPDATA, "shapefiles/", fnm)) for fnm in testfiles): self.saveTestData() return
def setUp(self): self.points = [Point((1, 1), properties={"species": "T. officianale"}, crs=LonLatWGS84), Point((3, 1), properties={"species": "C. tectorum"}, crs=LonLatWGS84), Point((4, 3), properties={"species": "M. alba"}, crs=LonLatWGS84), Point((2, 2), properties={"species": "V. cracca"}, crs=LonLatWGS84)] self.multipoint = Multipoint([(1,1), (3,1), (4,3), (2,2)], data={"species": ["T. officianale", "C. tectorum", "M. alba", "V. cracca"]}, crs=LonLatWGS84) self.line = Line([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], properties={"geom_id": 27, "name": "test line"}, crs=LonLatWGS84) self.polygon = Polygon([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], crs=LonLatWGS84) self.points3 = [Point((1, 1, 0), crs=LonLatWGS84), Point((3, 1, 3), crs=LonLatWGS84), Point((4, 3, 2), crs=LonLatWGS84), Point((2, 2, -1), crs=LonLatWGS84)] self.line3 = Line([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) self.polygon3 = Polygon([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) testfiles = ["points.shp", "line.shp", "polygon.shp"] if any(not exists(join(TMPDATA, "shapefiles/", fnm)) for fnm in testfiles): self.saveTestData() return
def test_poly_contains3(self): # test some hard cases diamond = Polygon([(0,0), (1,1), (2,0), (1, -1)]) self.assertFalse(diamond.contains(Point((2, 1)))) self.assertTrue(diamond.contains(Point((1, 0)))) self.assertFalse(diamond.contains(Point((2.5, 0)))) self.assertFalse(diamond.contains(Point((2, -1)))) return
def test_polygon_write(self): p = Polygon([[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0]], crs=LonLatWGS84) s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806") ans = """{ "properties": {}, "bbox": [100.0, 0.0, 101.0, 1.0], "geometry": { "type": "Polygon", "coordinates": [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] }, "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "type": "Feature" }""" self.verifyJSON(s, ans) return
def test_polygon_write(self): p = Polygon([[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0]], crs=LonLatWGS84) s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806") ans = """{ "properties": {}, "geometry": { "type": "Polygon", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "coordinates": [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] }, "type": "Feature" }""" self.verifyJson(s, ans) return
def test_polygon_add(self): polyA = Polygon([(1, 2), (2, 3), (5, 4)], crs=SphericalEarth) polyB = Polygon([(3, 4), (4, 5), (6, 5)], crs=LonLatWGS84) res = polyA + polyB self.assertTrue(isinstance(res, Multipolygon)) self.assertEqual(len(res), 2) self.assertEqual(res.crs, SphericalEarth) return
def test_poly_rotate(self): poly = Polygon([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]) rot45 = poly.rotate(45, (0.5, 0.5)) self.assertTrue(np.allclose(rot45.coords(), np.array([[ 0.5, 1.20710678, 0.5, -0.20710678], [-0.20710678, 0.5, 1.20710678, 0.5 ]]))) rot90 = poly.rotate(90, (0.0, 0.0)) self.assertTrue(np.allclose(rot90.coords(), np.array([[0.0, 0.0, -1.0, -1.0], [0.0, 1.0, 1.0, 0.0]]))) return
def test_poly_contains4(self): # hippie star theta = np.linspace(0, 2 * np.pi, 361)[:-1] r = 10 * np.sin(theta * 8) + 15 x = np.cos(theta) * r + 25 y = np.sin(theta) * r + 25 polygon = Polygon(zip(x, y)) # causes naive cross-product methods to fail pt = Point((28.75, 25.625)) self.assertTrue(polygon.contains(pt)) return
def test_poly_contains4(self): # hippie star theta = np.linspace(0, 2*np.pi, 361)[:-1] r = 10*np.sin(theta*8) + 15 x = np.cos(theta) * r + 25 y = np.sin(theta) * r + 25 polygon = Polygon(zip(x, y)) # causes naive cross-product methods to fail pt = Point((28.75, 25.625)) self.assertTrue(polygon.contains(pt)) return
def test_poly_intersection(self): # test polygons formed exactly as in test_line_intersection2, except # the rings are implicitly closed # ----- # | --x-- # | . . | # --x-- | # ----- poly0 = Polygon([(0.0, 0.0), (3.0, 0.0), (3.0, 3.0), (0.0, 3.0)]) poly1 = Polygon([(1.0, 4.0), (-2.0, 4.0), (-2.0, 1.0), (1.0, 1.0)]) self.assertTrue(poly0.intersects(poly1)) self.assertEqual(poly0.intersections(poly1), Multipoint([(0.0, 1.0), (1.0, 3.0)])) return
def test_poly_intersection(self): # test polygons formed exactly as in test_line_intersection2, except # the rings are implicitly closed # ----- # | --x-- # | . . | # --x-- | # ----- poly0 = Polygon([(0.0, 0.0), (3.0, 0.0), (3.0, 3.0), (0.0, 3.0)]) poly1 = Polygon([(1.0, 4.0), (-2.0, 4.0), (-2.0, 1.0), (1.0, 1.0)]) self.assertTrue(poly0.intersects(poly1)) self.assertEqual(poly0.intersections(poly1), Multipoint([(0.0, 1.0), (1.0, 3.0)])) return
def test_poly_rotate(self): poly = Polygon([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]) rot45 = poly.rotate(45, (0.5, 0.5)) self.assertTrue( np.allclose( rot45.coords(), np.array([[0.5, 1.20710678, 0.5, -0.20710678], [-0.20710678, 0.5, 1.20710678, 0.5]]))) rot90 = poly.rotate(90, (0.0, 0.0)) self.assertTrue( np.allclose( rot90.coords(), np.array([[0.0, 0.0, -1.0, -1.0], [0.0, 1.0, 1.0, 0.0]]))) return
def test_multipoint_within_polygon(self): np.random.seed(42) x = (np.random.random(100) - 0.5) * 180.0 y = (np.random.random(100) - 0.5) * 30.0 xp = [-80, -50, 20, 35, 55, -45, -60] yp = [0, -10, -8, -17, 15, 18, 12] poly = Polygon(zip(xp, yp), crs=LonLatWGS84) mp = Multipoint(zip(x, y), crs=LonLatWGS84) subset = mp.within_polygon(poly) excluded = [pt for pt in mp if pt not in subset] self.assertTrue(all(poly.contains(pt) for pt in subset)) self.assertFalse(any(poly.contains(pt) for pt in excluded)) return
def test_multipoint_within_polygon(self): np.random.seed(42) x = (np.random.random(100) - 0.5) * 180.0 y = (np.random.random(100) - 0.5) * 30.0 xp = [-80, -50, 20, 35, 55, -45, -60] yp = [0, -10, -8, -17, 15, 18, 12] poly = Polygon(zip(xp, yp), crs=LonLatWGS84) mp = Multipoint(zip(x, y), crs=LonLatWGS84) subset = mp.within_polygon(poly) excluded = [pt for pt in mp if pt not in subset] self.assertTrue(all(poly.contains(pt) for pt in subset)) self.assertFalse(any(poly.contains(pt) for pt in excluded)) return
def test_poly_centroid2(self): poly = Polygon([(0,0), (1,0), (2,0.5), (1,1), (0,1)], properties={"name": "features1"}) c = poly.centroid self.assertAlmostEqual(c.x, 7/9) self.assertEqual(c.y, 0.5) self.assertEqual(c.properties, poly.properties) return
def test_area_compute_pi(self): r = np.linspace(0, 2 * np.pi, 10000) x = np.cos(r) y = np.sin(r) kp = Polygon(zip(x, y)) self.assertAlmostEqual(kp.area, np.pi, places=6) return
def test_polygon_write(self): p = Polygon([[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]) s = self.asJsonBuffer(p, urn="urn:ogc:def:crs:EPSG::5806") ans = """{ "bbox": [ [ 100.0, 101.0 ], [ 0.0, 1.0 ] ], "properties": {}, "id": [ 0, 1, 2, 3, 4 ], "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] }, "type": "Feature" }""" self.verifyJson(s.read(), ans) return
def test_featurecollection2geometry(self): path = os.path.join(TESTDATA, "geojson_input/featurecollection.json") features = vector.read_geojson(path) ans0 = Point((102.0, 0.5), properties={"prop0": "value0"}, crs=self.default_crs) self.assertEqual(features[0], ans0) ans1 = Line([(102.0, 0.0), (103.0, 1.0), (104.0, 0.0), (105.0, 1.0)], properties={ "prop0": "value0", "prop1": 0.0 }, crs=self.default_crs) self.assertEqual(features[1], ans1) ans2 = Polygon([(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)], properties={ "prop0": "value0", "prop1": { "this": "that" } }, crs=self.default_crs) self.assertEqual(features[2], ans2) return
def test_poly_contains1(self): # trivial cases pt0 = Point((-0.5, 0.92)) unitsquare = Polygon([(0.0,0.0), (1.0,0.0), (1.0,1.0), (0.0,1.0)]) self.assertFalse(unitsquare.contains(pt0)) pt1 = Point((0.125, 0.875)) self.assertTrue(unitsquare.contains(pt1)) x = np.arange(-4, 5) y = (x)**2 line = Line([(x_,y_) for x_,y_ in zip(x, y)], crs=Cartesian) bbox = Polygon([(-2.5, 2.5), (2.5, 2.5), (2.5, -2.5), (-2.5, -2.5)], crs=Cartesian) self.assertEqual(list(filter(bbox.contains, line)), [Point((-1, 1)), Point((0, 0)), Point((1, 1))]) return
def test_poly(self): x = np.arange(5) y = x**2 poly = Polygon(list(zip(x, y))) self.assertEqual( poly.__geo_interface__, { "type": "Polygon", "bbox": (0, 0, 4, 16), "coordinates": [list(zip(x, y))] })
def test_multipolygon_within_poly(self): np.random.seed(49) multipolygon = \ Multipolygon([[np.array([[0,0],[3,0],[3,3],[0,3]]) + np.random.randint(-50, 50, (1, 2))] for _ in range(50)]) poly = Polygon([(-30, -40), (12, -30), (8, 22), (-10, 50)]) within = multipolygon.within(poly) self.assertEqual(len(within), 8) return
def test_multiline_touching_poly(self): np.random.seed(49) multiline = Multiline([ 10 * np.random.rand(10, 2) + np.random.randint(-50, 50, (1, 2)) for _ in range(50) ]) poly = Polygon([(-30, -40), (12, -30), (8, 22), (-10, 50)]) touching = multiline.touching(poly) self.assertEqual(len(touching), 12) return
def test_multipolygon_touching_poly(self): np.random.seed(49) multipolygon = \ Multipolygon([[np.array([[0,0],[3,0],[3,3],[0,3]]) + np.random.randint(-50, 50, (1, 2))] for _ in range(50)]) poly = Polygon([(-30, -40), (12, -30), (8, 22), (-10, 50)]) touching = multipolygon.touching(poly) self.assertEqual(len(touching), 14) return
def test_multiline_within_poly(self): np.random.seed(49) multiline = Multiline([ 10 * np.random.rand(10, 2) + np.random.randint(-50, 50, (1, 2)) for _ in range(50) ]) poly = Polygon([(-30, -40), (12, -30), (8, 22), (-10, 50)]) within = multiline.within(poly) self.assertEqual(len(within), 8) return
def test_poly_contains1(self): # trivial cases pt0 = Point((-0.5, 0.92)) unitsquare = Polygon([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]) self.assertFalse(unitsquare.contains(pt0)) pt1 = Point((0.125, 0.875)) self.assertTrue(unitsquare.contains(pt1)) x = np.arange(-4, 5) y = (x)**2 line = Line([(x_, y_) for x_, y_ in zip(x, y)], crs=Cartesian) bbox = Polygon([(-2.5, 2.5), (2.5, 2.5), (2.5, -2.5), (-2.5, -2.5)], crs=Cartesian) self.assertEqual( list(filter(bbox.contains, line)), [Point((-1, 1)), Point( (0, 0)), Point((1, 1))]) return
def test_poly(self): x = np.arange(5.0) y = x**2 vertices_ring = _as_nested_lists(zip(x, y)) vertices_ring.append(vertices_ring[0]) poly = Polygon(list(zip(x, y))) self.assertEqual( poly.geomdict, { "type": "Polygon", "bbox": (0, 0, 4, 16), "coordinates": [vertices_ring] })
def test_poly_contains2(self): # trivial but more interesting case x = np.arange(-4, 5) y = (x)**2 line = Line([(x_, y_) for x_, y_ in zip(x, y)], crs=Cartesian) bbox = Polygon([(-2.5, 2.5), (2.5, 2.5), (2.5, -2.5), (-2.5, -2.5)], crs=Cartesian) self.assertEqual( list(filter(bbox.contains, line)), [Point((-1, 1)), Point( (0, 0)), Point((1, 1))])
def test_poly_extent_foreign_crs(self): poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)], crs=LonLatWGS84) poly3 = Polygon([(0.0, 8.0, 0.5), (0.0, 5.0, 0.8), (6.0, 1.0, 0.6)], crs=LonLatWGS84) x, y = zip(*poly.get_vertices(crs=NSIDCNorth)) self.assertEqual(poly.get_extent(NSIDCNorth), (min(x), max(x), min(y), max(y))) self.assertEqual(poly3.get_extent(NSIDCNorth), (min(x), max(x), min(y), max(y))) return
class TestAffineTransforms(unittest.TestCase): def setUp(self): self.square = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) return def test_translate(self): M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(1,0), (2,0), (1,1)])) Mans = np.array([[1, 0, 1], [0, 1, 0], [0, 0, 1]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_affine_transform(M) ans = np.array([[1, 0], [1, 1], [2, 1], [2, 0]]) self.assertTrue(np.allclose(translated_square.get_vertices(), ans)) return def test_rotate(self): s2 = math.sqrt(0.5) M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(0,0), (s2,s2), (-s2,s2)])) Mans = np.array([[s2, -s2, 0], [s2, s2, 0], [0, 0, 1]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_affine_transform(M) ans = np.array([[0, 0], [-s2, s2], [0, 2*s2], [s2, s2]]) self.assertTrue(np.allclose(translated_square.get_vertices(), ans)) return def test_stretch(self): M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(0,0), (2,0), (0,2)])) Mans = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 1]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_affine_transform(M) ans = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]) self.assertTrue(np.allclose(translated_square.get_vertices(), ans)) return
class TestAffineTransforms(unittest.TestCase): def setUp(self): self.square = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) return def test_translate(self): M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(1,0), (2,0), (1,1)])) Mans = np.array([[1, 0, 1], [0, 1, 0], [0, 0, 1]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_affine_transform(M) ans = np.array([[1, 0], [1, 1], [2, 1], [2, 0]]) self.assertTrue(np.allclose(translated_square.get_vertices(), ans)) return def test_rotate(self): s2 = math.sqrt(0.5) M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(0,0), (s2,s2), (-s2,s2)])) Mans = np.array([[s2, -s2, 0], [s2, s2, 0], [0, 0, 1]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_affine_transform(M) ans = np.array([[0, 0], [-s2, s2], [0, 2*s2], [s2, s2]]) self.assertTrue(np.allclose(translated_square.get_vertices(), ans)) return def test_stretch(self): M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(0,0), (2,0), (0,2)])) Mans = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 1]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_affine_transform(M) ans = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]) self.assertTrue(np.allclose(translated_square.get_vertices(), ans)) return
def test_poly_contains3(self): # test some hard cases diamond = Polygon([(0, 0), (1, 1), (2, 0), (1, -1)]) self.assertFalse(diamond.contains(Point((2, 1)))) self.assertTrue(diamond.contains(Point((1, 0)))) self.assertFalse(diamond.contains(Point((2.5, 0)))) self.assertFalse(diamond.contains(Point((2, -1)))) return
def test_poly_polar(self): p = Polygon([(0.0, 80.0), (30.0, 80.0), (60.0, 80.0), (90.0, 80.0), (120.0, 80.0), (150.0, 80.0), (180.0, 80.0), (-150.0, 80.0), (-120.0, 80.0), (-90.0, 80.0), (-60.0, 80.0), (-30.0, 80.0)], crs=SphericalEarth) self.assertTrue(p.ispolar()) p = Polygon([(0.0, 85.0, 0.0), (90.0, 85.0, 0.0), (180.0, 85.0, 0.0), (-90.0, 85.0, 0.0)], crs=SphericalEarth) self.assertTrue(p.ispolar()) p = Polygon([(45.0, 30.0), (40.0, 25.0), (45.0, 20.0), (35.0, 25.0)], crs=SphericalEarth) self.assertFalse(p.ispolar()) p = Polygon([(-80, 0), (-50, -10), (20, -8), (35, -17), (55, 15), (-45, 18), (-60, 12)], crs=LonLatWGS84) self.assertFalse(p.ispolar()) p = Polygon([(45.0, 30.0), (40.0, 25.0), (45.0, 20.0), (35.0, 25.0)], crs=Cartesian) self.assertRaises(CRSError, p.ispolar) return
def test_hashing(self): np.random.seed(49) vertices = [(np.random.random(), np.random.random()) for i in range(1000)] line0 = Line(vertices, crs=SphericalEarth) line1 = Line(vertices, crs=LonLatWGS84) mp = Multipoint(vertices, crs=SphericalEarth) line2 = Line(vertices, crs=SphericalEarth) poly = Polygon(vertices, crs=SphericalEarth) point = Point(vertices[0], crs=SphericalEarth) self.assertEqual(hash(line0), hash(line2)) self.assertNotEqual(hash(line0), hash(line1)) self.assertNotEqual(hash(line0), hash(mp)) self.assertNotEqual(hash(line0), hash(poly)) self.assertEqual(hash(point), hash(mp[0])) return
def test_bbox_geographical(self): for crs in (SphericalEarth, LonLatWGS84): poly = Polygon([(179, -1), (-179, -1), (-179, 1), (179, 1)], crs=crs) self.assertEqual(poly.bbox, (179, -1, -179, 1))
def test_poly_extent(self): poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)]) poly3 = Polygon([(0.0, 8.0, 0.5), (0.0, 5.0, 0.8), (6.0, 1.0, 0.6)]) self.assertEqual(poly.extent(), (0.0, 6.0, 1.0, 8.0)) self.assertEqual(poly3.extent(), (0.0, 6.0, 1.0, 8.0)) return
class TestGeometry(unittest.TestCase): def setUp(self): self.point = Point((1.0, 2.0, 3.0), data={"color":(43,67,10)}, properties="apple") self.vertices = [(2.0, 9.0, 9.0), (4.0, 1.0, 9.0), (4.0, 1.0, 5.0), (2.0, 8.0, 0.0), (9.0, 8.0, 4.0), (1.0, 4.0, 6.0), (7.0, 3.0, 4.0), (2.0, 5.0, 3.0), (1.0, 6.0, 6.0), (8.0, 1.0, 0.0), (5.0, 5.0, 1.0), (4.0, 5.0, 7.0), (3.0, 3.0, 5.0), (9.0, 0.0, 9.0), (6.0, 3.0, 8.0), (4.0, 5.0, 7.0), (9.0, 9.0, 4.0), (1.0, 4.0, 7.0), (1.0, 7.0, 8.0), (9.0, 1.0, 6.0)] self.data = [99.0, 2.0, 60.0, 75.0, 71.0, 34.0, 1.0, 49.0, 4.0, 36.0, 47.0, 58.0, 65.0, 72.0, 4.0, 27.0, 52.0, 37.0, 95.0, 17.0] self.mp = Multipoint(self.vertices, data=self.data) self.line = Line(self.vertices, data=self.data) self.poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)]) self.ring = Polygon([(2.0, 2.0), (4.0, 2.0), (3.0, 6.0)]) self.ringed_poly = Polygon([(0.0, 0.0), (10, 0.0), (10.0, 10.0), (0.0, 10.0)], subs=[self.ring]) self.unitsquare = Polygon([(0.0,0.0), (1.0,0.0), (1.0,1.0), (0.0,1.0)]) return def test_point_equality(self): pt1 = Point((3.0, 4.0)) pt2 = Point((3.0, 4.0, 5.0)) pt3 = Point((3.0, 4.0, 5.0), data={"species":"T. officianale", "density":"high"}) self.assertFalse(pt1 == pt2) self.assertFalse(pt1 == pt3) self.assertFalse(pt2 == pt3) return def test_point_vertex(self): self.assertEqual(self.point.get_vertex(), (1.0, 2.0, 3.0)) return def test_point_coordsxy(self): self.assertEqual(self.point.coordsxy(), (1.0, 2.0)) self.assertEqual(self.point[0], 1.0) self.assertEqual(self.point[1], 2.0) return def test_point_azimuth(self): point = Point((1.0, 2.0)) other = Point((2.0, 3.0)) self.assertEqual(point.azimuth(other), 0.25*180) other = Point((0.0, 3.0)) self.assertEqual(point.azimuth(other), 1.75*180) other = Point((0.0, 1.0)) self.assertEqual(point.azimuth(other), 1.25*180) other = Point((2.0, 1.0)) self.assertEqual(point.azimuth(other), 0.75*180) other = Point((1.0, 3.0)) self.assertEqual(point.azimuth(other), 0.0) other = Point((1.0, 1.0)) self.assertEqual(point.azimuth(other), 180.0) return def test_point_azimuth2(self): point = Point((5.0, 2.0)) other = Point((5.0, 2.0)) self.assertTrue(np.isnan(point.azimuth(other))) return def test_point_azimuth3(self): """ Verify with: printf "0 -1000000\n100000 -900000" | proj +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 +y_0=0 +units=m +datum=WGS84 +no_defs -I -s | tr '\n' ' ' | invgeod +ellps=WGS84 -f "%.6f" """ point = Point((0.0, -10e5), crs=NSIDCNorth) other = Point((1e5, -9e5), crs=NSIDCNorth) self.assertAlmostEqual(point.azimuth(other), 45.036973, places=6) return def test_point_shift(self): point = Point((-3.0, 5.0, 2.5), data={"color":(43,67,10)}, properties="apple") point.shift((4.0, -3.0, 0.5)) self.assertEqual(self.point, point) return def test_nearest_to(self): self.assertEqual(self.mp.nearest_point_to(self.point), self.mp[12]) return def test_empty_multipoint(self): mp = Multipoint([], crs=LonLatWGS84) self.assertEqual(len(mp), 0) return def test_multipoint_zip_init(self): x = range(-10, 10) y = [_x**2 for _x in x] Line(zip(x, y)) return def test_multipoint_shift(self): vertices = [(a-1,b+2,c-0.5) for (a,b,c) in self.vertices] mp = Multipoint(vertices, data=self.data) mp.shift((1, -2, 0.5)) self.assertEqual(mp, self.mp) return def test_multipoint_subset(self): ss1 = self.mp._subset(range(2,7)) ss2 = self.line._subset(range(2,7)) self.assertTrue(isinstance(ss1, Multipoint)) self.assertTrue(isinstance(ss2, Line)) return def test_multipoint_get(self): self.assertEqual(self.mp[0], Point(self.vertices[0], data=self.mp.data[0], properties=self.mp.properties)) return def test_multipoint_set(self): mp1 = Multipoint([(3.0, 3.0), (5.0, 1.0), (3.0, 1.0), (4.0, 4.0), (0.0, 1.0)], data=["rankin", "corbet", "arviat", "severn", "churchill"]) mp2 = Multipoint([(3.0, 3.0), (5.0, 1.0), (4.0, 5.0), (4.0, 4.0), (0.0, 1.0)], data=["rankin", "corbet", "umiujaq", "severn", "churchill"]) mp1[2] = (4.0, 5.0) self.assertNotEqual(mp1, mp2) mp1[2] = Point((4.0, 5.0), data=["umiujaq"]) self.assertEqual(mp1, mp2) return def test_multipoint_iterator(self): mp = Multipoint([(3.0, 3.0), (5.0, 1.0), (3.0, 1.0), (4.0, 4.0), (0.0, 1.0)], data=["rankin", "corbet", "arviat", "severn", "churchill"]) for i, pt in enumerate(mp): self.assertEqual(mp[i], pt) return def test_multipoint_get_data_fields(self): mp = Multipoint([(3.0, 3.0), (5.0, 1.0), (3.0, 1.0), (4.0, 4.0), (0.0, 1.0)], data={"location": ["rankin", "corbet", "arviat", "severn", "churchill"]}) pt = mp[3] self.assertEqual(pt.data.fields, ("location",)) self.assertEqual(pt.data[0], ("severn",)) return def test_multipoint_slicing(self): submp = Multipoint(self.vertices[5:10], data=self.data[5:10]) self.assertEqual(self.mp[5:10], submp) submp = Multipoint(self.vertices[5:], data=self.data[5:]) self.assertEqual(self.mp[5:], submp) return def test_multipoint_negative_index(self): self.assertEqual(self.mp[len(self.mp)-1], self.mp[-1]) return def test_multipoint_bbox(self): bbox = (1.0, 0.0, 9.0, 9.0) self.assertEqual(self.mp.bbox, bbox) return def test_multipoint_bbox_overlap(self): self.assertTrue(self.mp._bbox_overlap(self.poly)) return def test_multipoint_within_radius(self): vertices = [(float(x),float(y)) for x in range(-10,11) for y in range(-10,11)] ans = [v for v in vertices if math.sqrt(v[0]**2 + v[1]**2) <= 5.0] mp = Multipoint(vertices) sub = mp.within_radius(Point((0,0)), 5.0) self.assertEqual(sub, Multipoint(ans)) return def test_multipoint_within_bbox(self): vertices = [(float(x),float(y)) for x in range(-10,11) for y in range(-10,11)] ans = [v for v in vertices if (-5.0<=v[0]<=5.0) and (-4.0<=v[1]<=6.0)] mp = Multipoint(vertices) sub = mp.within_bbox((-5.0, -4.0, 5.0, 6.0)) self.assertEqual(sub, Multipoint(ans)) return def test_multipoint_convex_hull(self): vertices = [(953, 198), (986, 271), (937, 305), (934, 464), (967, 595), (965, 704), (800, 407), (782, 322), (863, 979), (637, 689), (254, 944), (330, 745), (363, 646), (27, 990), (127, 696), (286, 352), (436, 205), (88, 254), (187, 85)] mp = Multipoint(vertices) ch = mp.convex_hull() hull_vertices = [(187, 85), (953, 198), (986, 271), (965, 704), (863, 979), (27, 990), (88, 254)] self.assertEqual(ch.vertices, hull_vertices) return def test_multipoint_convex_hull2(self): vertices = [(-158, 175), (-179, 230), (-404, -390), (259, -79), (32, 144), (-59, 355), (402, 301), (239, 159), (-421, 172), (-482, 26), (2, -499), (134, -72), (-412, -12), (476, 235), (-412, 40), (-198, -256), (314, 331), (431, -492), (325, -415), (-400, -491)] mp = Multipoint(vertices) ch = mp.convex_hull() hull_vertices = [(2, -499), (431, -492), (476, 235), (402, 301), (314, 331), (-59, 355), (-421, 172), (-482, 26), (-400, -491)] self.assertEqual(ch.vertices, hull_vertices) return def test_connected_multipoint_shortest_distance_to(self): line = Line([(0.0, 0.0), (2.0, 2.0), (5.0, 4.0)]) dist = line.shortest_distance_to(Point((0.0, 2.0))) self.assertTrue(abs(dist - math.sqrt(2)) < 1e-10) return def test_connected_multipoint_shortest_distance_to2(self): line = Line([(127.0, -35.0), (132.0, -28.0), (142.0, -29.0)], crs=LonLatWGS84) dist = line.shortest_distance_to(Point((98.0, -7.0), crs=LonLatWGS84)) self.assertAlmostEqual(dist, 4257313.5324397, places=6) return def test_connected_multipoint_nearest_on_boundary(self): line = Line([(0.0, 0.0), (2.0, 2.0), (5.0, 4.0)]) npt = line.nearest_on_boundary(Point((0.0, 2.0))) self.assertEqual(npt, Point((1.0, 1.0))) return def assertPointAlmostEqual(self, a, b): for (a_, b_) in zip(a.vertex, b.vertex): self.assertAlmostEqual(a_, b_, places=5) self.assertEqual(a.data, b.data) self.assertEqual(a.properties, b.properties) self.assertEqual(a._crs, b._crs) return def test_connected_multipoint_nearest_on_boundary2(self): line = Line([(-40, 0.0), (35, 0.0)], crs=LonLatWGS84) npt = line.nearest_on_boundary(Point((30.0, 80.0), crs=LonLatWGS84)) self.assertPointAlmostEqual(npt, Point((30.0, 0.0), crs=LonLatWGS84)) return # def test_connected_multipoint_nearest_on_boundary3(self): # # This is the test that tends to break naive root finding schemes # line = Line([(-40, 0.0), (35, 0.0)], crs=LonLatWGS84) # npt = line.nearest_on_boundary(Point((30.0, 1e-8), crs=LonLatWGS84)) # self.assertPointAlmostEqual(npt, Point((30.0, 0.0), crs=LonLatWGS84)) # return # def test_connected_multipoint_nearest_on_boundary4(self): # line = Line([(-20.0, 32.0), (-26.0, 43.0), (-38.0, 39.0)], crs=LonLatWGS84) # npt = line.nearest_on_boundary(Point((-34.0, 52.0), crs=LonLatWGS84)) # self.assertPointAlmostEqual(npt, Point((-27.98347, 42.456316), crs=LonLatWGS84)) # return def test_line_append2d(self): ln0 = Line([(3.0, 3.0), (5.0, 1.0), (3.0, 1.0)]) ln1 = Line([(3.0, 3.0), (5.0, 1.0), (3.0, 1.0), (0.0, 1.0)]) ln0.append(Point((0.0, 1.0))) self.assertEqual(ln0, ln1) return def test_line_append3d(self): ln0 = Line([(3.0, 3.0, 2.0), (5.0, 1.0, 0.0), (3.0, 1.0, 5.0)]) ln1 = Line([(3.0, 3.0, 2.0), (5.0, 1.0, 0.0), (3.0, 1.0, 5.0), (0.0, 1.0, 3.0)]) ln0.append(Point((0.0, 1.0, 3.0))) self.assertEqual(ln0, ln1) return def test_line_extend(self): ln0a = Line([(3.0, 3.0, 2.0), (5.0, 1.0, 0.0), (3.0, 1.0, 5.0)]) ln0b = Line([(4.0, 4.0, 6.0), (0.0, 1.0, 3.0)]) ln1 = Line([(3.0, 3.0, 2.0), (5.0, 1.0, 0.0), (3.0, 1.0, 5.0), (4.0, 4.0, 6.0), (0.0, 1.0, 3.0)]) ln0a.extend(ln0b) self.assertEqual(ln0a, ln1) def test_pop(self): ln = Multipoint([(3.0, 3.0, 2.0), (5.0, 1.0, 0.0), (3.0, 1.0, 5.0), (4.0, 4.0, 6.0), (0.0, 1.0, 3.0)], data=["red", "green", "blue", "chartreuse", "aquamarine"]) lnresult = Multipoint([(3.0, 3.0, 2.0), (5.0, 1.0, 0.0), (3.0, 1.0, 5.0), (0.0, 1.0, 3.0)], data=["red", "green", "blue", "aquamarine"]) pt = ln.pop(3) ptresult = Point((4.0, 4.0, 6.0), data="chartreuse") self.assertEqual(pt, ptresult) self.assertEqual(ln, lnresult) return def test_line_intersection(self): line0 = Line([(0.0, 0.0), (3.0, 3.0)]) line1 = Line([(0.0, 3.0), (3.0, 0.0)]) self.assertTrue(line0.intersects(line1)) self.assertEqual(line0.intersections(line1), Multipoint([(1.5, 1.5)])) return def test_line_intersection_horizontal(self): line0 = Line([(-2.5, 2.5), (2.5, 2.5)]) line1 = Line([(0.0, 0.0), (1.0, 5.0)]) self.assertTrue(line0.intersects(line1)) self.assertEqual(line0.intersections(line1), Multipoint([(0.5, 2.5)])) return def test_line_intersection_vertical(self): line0 = Line([(2.5, 2.5), (2.5, -2.5)]) line1 = Line([(1.5, 2.5), (3.5, -2.5)]) self.assertTrue(line0.intersects(line1)) self.assertEqual(line0.intersections(line1), Multipoint([(2.5, 0.0)])) return def test_poly_clockwise(self): p = Polygon([(0,0), (0,1), (1,1), (1,0)]) self.assertTrue(p.isclockwise()) return def test_poly_counterclockwise(self): p = Polygon([(0,0), (1,0), (1,1), (0,1)]) self.assertFalse(p.isclockwise()) return def test_poly_extents(self): self.assertEqual(self.poly.get_extents(), (0.0, 6.0, 1.0, 8.0)) return def test_poly_length(self): self.assertEqual(self.poly.length, 19.430647008220866) return def test_poly_contains1(self): # trivial case pt0 = Point((-0.5, 0.92)) pt1 = Point((0.125, 0.875)) self.assertFalse(self.unitsquare.contains(pt0)) self.assertTrue(self.unitsquare.contains(pt1)) return def test_poly_contains2(self): # trivial but more interesting case x = np.arange(-4, 5) y = (x)**2 line = Line([(x_,y_) for x_,y_ in zip(x, y)], crs=Cartesian) bbox = Polygon([(-2.5, 2.5), (2.5, 2.5), (2.5, -2.5), (-2.5, -2.5)], crs=Cartesian) self.assertEqual(list(filter(bbox.contains, line)), [Point((-1, 1)), Point((0, 0)), Point((1, 1))]) def test_poly_contains3(self): # test some hard cases diamond = Polygon([(0,0), (1,1), (2,0), (1, -1)]) self.assertFalse(diamond.contains(Point((2, 1)))) self.assertTrue(diamond.contains(Point((1, 0)))) self.assertFalse(diamond.contains(Point((2.5, 0)))) self.assertFalse(diamond.contains(Point((2, -1)))) return def test_poly_contains4(self): # case where point is on an edge (should return true) square = Polygon([(0,0), (1,0), (1,1), (0,1)]) pt = Point([0.5, 0]) self.assertTrue(square.contains(pt)) return def test_poly_contains5(self): # hippie star theta = np.linspace(0, 2*np.pi, 361)[:-1] r = 10*np.sin(theta*8) + 15 x = np.cos(theta) * r + 25 y = np.sin(theta) * r + 25 polygon = Polygon(zip(x, y)) # causes naive cross-product methods to fail pt = Point((28.75, 25.625)) self.assertTrue(polygon.contains(pt)) return def test_poly_getitem(self): poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0), (7.0, 2.0), (5.0, 4.0)]) sub = poly[:3] self.assertEqual(sub, Line([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)])) return def test_poly_getitem2(self): poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0), (7.0, 2.0), (5.0, 4.0)]) sub = poly[:4:2] self.assertEqual(sub, Line([(0.0, 8.0), (6.0, 1.0)])) return def test_poly_getitem3(self): poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0), (7.0, 2.0), (5.0, 4.0)]) sub = poly[:] self.assertEqual(sub, poly) return def test_poly_centroid(self): poly = Polygon([(0,0), (1,0), (1,1), (0,1)], properties={"name": "features1"}) c = poly.centroid self.assertEqual(c.x, 0.5) self.assertEqual(c.y, 0.5) self.assertEqual(c.properties, poly.properties) return def test_poly_centroid2(self): poly = Polygon([(0,0), (1,0), (2,0.5), (1,1), (0,1)], properties={"name": "features1"}) c = poly.centroid self.assertAlmostEqual(c.x, 7/9) self.assertEqual(c.y, 0.5) self.assertEqual(c.properties, poly.properties) return def test_ringedpoly_perimeter(self): self.assertEqual(round(self.ringed_poly.perimeter, 3), 50.246) return def test_ringedpoly_area(self): self.assertEqual(self.ringed_poly.area, 100 - self.ring.area) return def test_area_compute_pi(self): r = np.linspace(0, 2*np.pi, 10000) x = np.cos(r) y = np.sin(r) kp = Polygon(zip(x,y)) self.assertAlmostEqual(kp.area, np.pi, places=6) return def test_segments(self): v = self.vertices self.assertEqual([tuple(a.vertices) for a in self.line.segments], [(v[i], v[i+1]) for i in range(len(self.vertices)-1)]) return def test_within_distance(self): line = Line([(0,0), (1,1), (3,1)]) pt = Point((1,1.5)) self.assertTrue(line.within_distance(pt, 0.6)) self.assertFalse(line.within_distance(pt, 0.4)) return def test_walk_cartesian(self): start = Point((-3, -4), crs=Cartesian) dest = start.walk(5.0, math.atan(3.0/4.0), radians=True) self.assertAlmostEqual(dest.x, 0.0) self.assertAlmostEqual(dest.y, 0.0) return def test_walk(self): start = Point((-123.1, 49.25), crs=LonLatWGS84) dest = start.walk(1e5, 80.0) self.assertAlmostEqual(dest.x, -121.743196, places=6) self.assertAlmostEqual(dest.y, 49.398187, places=6) return def test_walk_albers(self): AlaskaAlbers = Proj4CRS("+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 " "+x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs", "+ellps=GRS80") start = Point((-2658638, 2443580), crs=AlaskaAlbers) dest = start.walk(4500, 195.0) self.assertAlmostEqual(dest.x, -2662670.889, places=3) self.assertAlmostEqual(dest.y, 2441551.155, places=3) return def test_subsection_cartesian(self): line = Line([(0.0, 0.0), (1.0, 2.0), (3.0, -2.0), (4.0, -1.0), (4.0, 3.0), (3.0, 2.0)]) points = line.subsection(20) ans = [Point(v) for v in [(0.0, 0.0), (0.318619234003536, 0.637238468007072), (0.637238468007072, 1.274476936014144), (0.9558577020106079, 1.9117154040212159), (1.274476936014144, 1.4510461279717122), (1.59309617001768, 0.8138076599646402), (1.911715404021216, 0.17656919195756826), (2.230334638024752, -0.4606692760495037), (2.5489538720282883, -1.0979077440565757), (2.867573106031824, -1.7351462120636478), (3.294395938694146, -1.7056040613058538), (3.7981771815888177, -1.2018228184111823), (4.0, -0.5729663008226373), (4.0, 0.13948796534818164), (4.0, 0.8519422315190006), (4.0, 1.5643964976898195), (4.0, 2.2768507638606383), (4.0, 2.989305030031457), (3.5037812428946715, 2.503781242894671), (3.0, 2.0)]] for a,b in zip(points, ans): self.assertPointAlmostEqual(a, b) return def test_subsection_lonlat(self): line = Line([(0, 40), (120, 40)], crs=LonLatWGS84) points = line.subsection(20) ans = [Point(v, crs=LonLatWGS84) for v in [(0, 40), (4.006549675732082, 43.200316625343305), (8.44359845345209, 46.2434129228378), (13.382442375999254, 49.09308515921458), (18.894149336762318, 51.705248417290484), (25.03918819127435, 54.027440893063556), (31.85052685770255, 55.99968253476488), (39.31083346558522, 57.55771841446013), (47.329401349484314, 58.6395037346357), (55.7308352362257, 59.194673757153645), (64.26916476377436, 59.19467375715364), (72.67059865051574, 58.639503734635674), (80.68916653441482, 57.557718414460105), (88.14947314229748, 55.999682534764844), (94.96081180872568, 54.02744089306352), (101.10585066323772, 51.705248417290456), (106.61755762400078, 49.09308515921457), (111.55640154654793, 46.24341292283779), (115.99345032426793, 43.2003166253433), (120, 40)]] for a,b in zip(points, ans): self.assertPointAlmostEqual(a, b) return def test_subsection_lonlat_precision(self): line = Line([(-20.247017, 79.683933), (-20.0993, 79.887917), (-19.13705, 80.048567), (-18.680467, 80.089333), (-17.451917, 80.14405), (-16.913233, 80.02715), (-16.631367, 80.022933), (-16.194067, 80.0168), (-15.915983, 80.020267), (-15.7763, 80.021283)], crs=LonLatWGS84) for n in range(2, 30): self.assertEqual(len(line.subsection(n)), n) return
class TestShapefile(unittest.TestCase): def setUp(self): self.points = [Point((1, 1), data={"species": "T. officianale"}, crs=LonLatWGS84), Point((3, 1), data={"species": "C. tectorum"}, crs=LonLatWGS84), Point((4, 3), data={"species": "M. alba"}, crs=LonLatWGS84), Point((2, 2), data={"species": "V. cracca"}, crs=LonLatWGS84)] self.multipoint = Multipoint([(1,1), (3,1), (4,3), (2,2)], data={"species": ["T. officianale", "C. tectorum", "M. alba", "V. cracca"]}, crs=LonLatWGS84) self.line = Line([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], crs=LonLatWGS84) self.polygon = Polygon([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], crs=LonLatWGS84) self.points3 = [Point((1, 1, 0), crs=LonLatWGS84), Point((3, 1, 3), crs=LonLatWGS84), Point((4, 3, 2), crs=LonLatWGS84), Point((2, 2, -1), crs=LonLatWGS84)] self.line3 = Line([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) self.polygon3 = Polygon([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) testfiles = ["points.shp", "line.shp", "polygon.shp"] if any(not exists(join(TESTDATA, "shapefiles/", fnm)) for fnm in testfiles): self.saveTestData() return def saveTestData(self): testfiles = [(self.multipoint, "points"), (self.line, "line"), (self.polygon, "polygon")] for (geom, fnm) in testfiles: geom.to_shapefile(os.path.join(TESTDATA, "shapefiles", fnm)) return def assertGeomEqual(self, this, that): self.assertTrue(np.all(this.get_vertices() == that.get_vertices())) self.assertEqual(this._crs, that._crs) return def test_writepoints(self): mp = Multipoint([p.vertex for p in self.points]) mp.to_shapefile("data/points_shp") return def test_writeline(self): self.line.to_shapefile("data/line_shp") return def test_writepoly(self): self.polygon.to_shapefile("data/polygon_shp") return def test_writepoints3(self): mp = Multipoint([p.vertex for p in self.points3]) mp.to_shapefile("data/pointsz_shp") return def test_writeline3(self): self.line3.to_shapefile("data/linez_shp") return def test_writepoly3(self): self.polygon3.to_shapefile("data/polygonz_shp") return def test_write_collection_points(self): mp = Multipoint([p.vertex for p in self.points]) mp0 = copy(mp) mp1 = copy(mp.shift((4, 2))) mp2 = copy(mp.shift((-2, 3))) shp.write_shapefile([mp0, mp1, mp2], "data/points_collection") return def test_write_collection_lines(self): line0 = copy(self.line) line1 = copy(self.line.shift((4, 2))) line2 = copy(self.line.shift((-2, 3))) shp.write_shapefile([line0, line1, line2], "data/line_collection") return def test_dbase_type(self): self.assertEqual(shp.property_field_type(1.0), "N") self.assertEqual(shp.property_field_type(1), "N") self.assertEqual(shp.property_field_type(np.float32(1.0)), "N") self.assertEqual(shp.property_field_type(np.int16(1)), "N") #self.assertEqual(shp.property_field_type(1.0), "O") #self.assertEqual(shp.property_field_type(1), "I") #self.assertEqual(shp.property_field_type(np.float32(1.0)), "O") #self.assertEqual(shp.property_field_type(np.int16(1)), "I") #self.assertEqual(shp.property_field_type(True), "L") #self.assertEqual(shp.property_field_type(False), "L") self.assertEqual(shp.property_field_type("pale ale"), "C") self.assertEqual(shp.property_field_type(datetime.date(1986, 8, 17)), "D") self.assertEqual(shp.property_field_type(datetime.datetime(2013, 5, 4, 20, 40, 21)), "@") return def test_read_points(self): shps = read_shapefile(os.path.join(TESTDATA, "newp")) mp = shps[0] self.assertEqual(mp.vertices, [(-14.612, 80.50906666666667), (-14.612, 80.50906666666667), (-14.612, 80.50906666666667), (-13.744733333333333, 80.28181666666667), (-13.744733333333333, 80.28181666666667), (-13.744733333333333, 80.28181666666667), (-11.002583333333334, 80.32173333333333), (-11.002583333333334, 80.32173333333333), (-11.002583333333334, 80.32173333333333), (-11.07225, 80.56316666666666), (-11.07225, 80.56316666666666)]) self.assertEqual(mp.data.getfield("meterno"), ['IMS1/1', 'IMS2/1', '5952/2', 'IMS4/1', '5953/2', '1963/13', 'IMS5/1', '5213/A', '2121/13', 'IMS3/1', '3613/2']) self.assertEqual(mp.data.getfield("depth_m"), [73, 143, 247, 86, 147, 250, 74, 142, 235, 150, 248]) return def test_read_multipoint_attributes(self): mp = read_shapefile(os.path.join(TESTDATA, "shapefiles", "points")) self.assertEqual(mp[0].data.getfield("species"), self.multipoint.data.getfield("species")) return def test_read_line(self): line = read_shapefile(os.path.join(TESTDATA, "shapefiles", "line"))[0] self.assertGeomEqual(line, self.line) return def test_read_polygon(self): polygon = read_shapefile(os.path.join(TESTDATA, "shapefiles", "polygon"))[0] self.assertGeomEqual(polygon, self.polygon) return
def test_poly_clockwise(self): p = Polygon([(0,0), (0,1), (1,1), (1,0)]) self.assertTrue(p.isclockwise()) return
class TestGeometryAnalysis(unittest.TestCase): """ Tests for analysis methods of geometrical objects """ def setUp(self): self.point = Point((1.0, 2.0, 3.0), properties={"type": "apple", "color": (43,67,10)}) self.vertices = [(2.0, 9.0, 9.0), (4.0, 1.0, 9.0), (4.0, 1.0, 5.0), (2.0, 8.0, 0.0), (9.0, 8.0, 4.0), (1.0, 4.0, 6.0), (7.0, 3.0, 4.0), (2.0, 5.0, 3.0), (1.0, 6.0, 6.0), (8.0, 1.0, 0.0), (5.0, 5.0, 1.0), (4.0, 5.0, 7.0), (3.0, 3.0, 5.0), (9.0, 0.0, 9.0), (6.0, 3.0, 8.0), (4.0, 5.0, 7.0), (9.0, 9.0, 4.0), (1.0, 4.0, 7.0), (1.0, 7.0, 8.0), (9.0, 1.0, 6.0)] self.data = [99.0, 2.0, 60.0, 75.0, 71.0, 34.0, 1.0, 49.0, 4.0, 36.0, 47.0, 58.0, 65.0, 72.0, 4.0, 27.0, 52.0, 37.0, 95.0, 17.0] self.mp = Multipoint(self.vertices, data=self.data) self.line = Line(self.vertices) self.poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)]) self.poly3 = Polygon([(0.0, 8.0, 0.5), (0.0, 5.0, 0.8), (6.0, 1.0, 0.6)]) self.ring = Polygon([(2.0, 2.0), (4.0, 2.0), (3.0, 6.0)]) self.ringed_poly = Polygon([(0.0, 0.0), (10, 0.0), (10.0, 10.0), (0.0, 10.0)], subs=[self.ring]) self.unitsquare = Polygon([(0.0,0.0), (1.0,0.0), (1.0,1.0), (0.0,1.0)]) return def test_within_distance(self): line = Line([(0,0), (1,1), (3,1)]) pt = Point((1,1.5)) self.assertTrue(line.within_distance(pt, 0.6)) self.assertFalse(line.within_distance(pt, 0.4)) return def test_walk_cartesian(self): start = Point((-3, -4), crs=Cartesian) dest = start.walk(5.0, 90-math.atan2(4.0, 3.0)*180/math.pi) self.assertAlmostEqual(dest.x, 0.0) self.assertAlmostEqual(dest.y, 0.0) return def test_walk(self): start = Point((-123.1, 49.25), crs=LonLatWGS84) dest = start.walk(1e5, 80.0) self.assertAlmostEqual(dest.x, -121.743196, places=6) self.assertAlmostEqual(dest.y, 49.398187, places=6) return def test_walk_albers_geodetic(self): AlaskaAlbers = ProjectedCRS("+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 " "+x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs", "+ellps=GRS80") start = Point((-2658638, 2443580), crs=AlaskaAlbers) dest = start.walk(4500, 195.0, projected=False) self.assertAlmostEqual(dest.x, -2662670.889, places=3) self.assertAlmostEqual(dest.y, 2441551.155, places=3) def test_walk_albers_projected(self): AlaskaAlbers = ProjectedCRS("+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 " "+x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs", "+ellps=GRS80") start = Point((-2658638, 2443580), crs=AlaskaAlbers) dest = start.walk(4500, 195.0) self.assertAlmostEqual(dest.x, -2659802.686, places=3) self.assertAlmostEqual(dest.y, 2439233.334, places=3) return def test_point_azimuth(self): point = Point((1.0, 2.0)) other = Point((2.0, 3.0)) self.assertEqual(point.azimuth(other), 0.25*180) other = Point((0.0, 3.0)) self.assertEqual(point.azimuth(other), -0.25*180) other = Point((0.0, 1.0)) self.assertEqual(point.azimuth(other), -0.75*180) other = Point((2.0, 1.0)) self.assertEqual(point.azimuth(other), 0.75*180) other = Point((1.0, 3.0)) self.assertEqual(point.azimuth(other), 0.0) other = Point((1.0, 1.0)) self.assertEqual(point.azimuth(other), -180.0) return def test_point_azimuth2(self): point = Point((5.0, 2.0)) other = Point((5.0, 2.0)) self.assertTrue(np.isnan(point.azimuth(other))) return def test_point_azimuth3(self): """ Verify with: printf "0 -1000000\n100000 -900000" | proj +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 +y_0=0 +units=m +datum=WGS84 +no_defs -I -s | tr '\n' ' ' | invgeod +ellps=WGS84 -f "%.6f" """ point = Point((0.0, -10e5), crs=NSIDCNorth) other = Point((1e5, -9e5), crs=NSIDCNorth) self.assertAlmostEqual(point.azimuth(other, projected=False), 45.036973, places=6) return def test_point_shift_inplace(self): point = Point((-3.0, 5.0, 2.5), properties={"type": "apple", "color":(43,67,10)}) point.shift((4.0, -3.0, 0.5), inplace=True) self.assertEqual(self.point, point) return def test_point_shift(self): point = Point((-3.0, 5.0, 2.5), properties={"type": "apple", "color":(43,67,10)}) point_shifted = point.shift((4.0, -3.0, 0.5)) self.assertEqual(self.point, point_shifted) return def test_nearest_to(self): self.assertEqual(self.mp.nearest_vertex_to(self.point), 12) return def test_multipoint_shift_inplace(self): vertices = [(a-1,b+2,c-0.5) for (a,b,c) in self.vertices] mp = Multipoint(vertices, data=self.data) mp.shift((1, -2, 0.5), inplace=True) self.assertEqual(mp, self.mp) def test_multipoint_shift(self): vertices = [(a-1,b+2,c-0.5) for (a,b,c) in self.vertices] mp = Multipoint(vertices, data=self.data) mp_shifted = mp.shift((1, -2, 0.5)) self.assertEqual(mp_shifted, self.mp) return def test_multipoint_bbox(self): bbox = (1.0, 0.0, 9.0, 9.0) self.assertEqual(self.mp.bbox, bbox) return def test_multiline_bbox(self): geom = Multiline([[(1,2), (3,4), (3,2)], [(6,8),(2,6),(3,0)], [(-3,-4), (7, -1), (3, 2), (2, -3)]], crs=LonLatWGS84) self.assertEqual(geom.bbox, (-3, -4, 7, 8)) return def test_multipolygon_bbox(self): geom = Multipolygon([[[(1,2), (3,4), (3,2)]], [[(6,8),(2,6),(3,0)]], [[(-3,-4), (7, -1), (3, 2), (2, -3)]]], crs=LonLatWGS84) self.assertEqual(geom.bbox, (-3, -4, 7, 8)) return def test_multipoint_bbox_overlap(self): self.assertTrue(self.mp._bbox_overlap(self.poly)) return def test_multipoint_within_radius(self): vertices = [(float(x),float(y)) for x in range(-10,11) for y in range(-10,11)] ans = [v for v in vertices if math.sqrt(v[0]**2 + v[1]**2) < 5.0] mp = Multipoint(vertices) sub = mp.within_radius(Point((0,0)), 5.0) self.assertEqual(sub, Multipoint(ans)) return def test_multipoint_within_bbox(self): vertices = [(float(x),float(y)) for x in range(-10,11) for y in range(-10,11)] ans = [v for v in vertices if (-5.0<v[0]<5.0) and (-4.0<v[1]<6.0)] mp = Multipoint(vertices) sub = mp.within_bbox((-5.0, -4.0, 5.0, 6.0)) self.assertEqual(sub, Multipoint(ans)) return def test_multipoint_within_polygon(self): np.random.seed(42) x = (np.random.random(100) - 0.5) * 180.0 y = (np.random.random(100) - 0.5) * 30.0 xp = [-80, -50, 20, 35, 55, -45, -60] yp = [0, -10, -8, -17, 15, 18, 12] poly = Polygon(zip(xp, yp), crs=LonLatWGS84) mp = Multipoint(zip(x, y), crs=LonLatWGS84) subset = mp.within_polygon(poly) excluded = [pt for pt in mp if pt not in subset] self.assertTrue(all(poly.contains(pt) for pt in subset)) self.assertFalse(any(poly.contains(pt) for pt in excluded)) return def test_multipoint_convex_hull(self): vertices = [(953, 198), (986, 271), (937, 305), (934, 464), (967, 595), (965, 704), (800, 407), (782, 322), (863, 979), (637, 689), (254, 944), (330, 745), (363, 646), (27, 990), (127, 696), (286, 352), (436, 205), (88, 254), (187, 85)] mp = Multipoint(vertices) ch = mp.convex_hull() hull_vertices = [(187, 85), (953, 198), (986, 271), (965, 704), (863, 979), (27, 990), (88, 254)] self.assertTrue(np.all(np.equal(ch.vertices, hull_vertices))) return def test_multipoint_convex_hull2(self): vertices = [(-158, 175), (-179, 230), (-404, -390), (259, -79), (32, 144), (-59, 355), (402, 301), (239, 159), (-421, 172), (-482, 26), (2, -499), (134, -72), (-412, -12), (476, 235), (-412, 40), (-198, -256), (314, 331), (431, -492), (325, -415), (-400, -491)] mp = Multipoint(vertices) ch = mp.convex_hull() hull_vertices = [(2, -499), (431, -492), (476, 235), (402, 301), (314, 331), (-59, 355), (-421, 172), (-482, 26), (-400, -491)] self.assertTrue(np.all(np.equal(ch.vertices, hull_vertices))) return def test_connected_multipoint_shortest_distance_to(self): line = Line([(0.0, 0.0), (2.0, 2.0), (5.0, 4.0)]) dist = line.shortest_distance_to(Point((0.0, 2.0))) self.assertTrue(abs(dist - math.sqrt(2)) < 1e-10) return def test_connected_multipoint_shortest_distance_to2(self): line = Line([(127.0, -35.0), (132.0, -28.0), (142.0, -29.0)], crs=LonLatWGS84) dist = line.shortest_distance_to(Point((98.0, -7.0), crs=LonLatWGS84)) self.assertAlmostEqual(dist, 4257313.5324397, places=6) return def test_connected_multipoint_nearest_on_boundary(self): line = Line([(0.0, 0.0), (2.0, 2.0), (5.0, 4.0)]) npt = line.nearest_on_boundary(Point((0.0, 2.0))) self.assertEqual(npt, Point((1.0, 1.0))) return def assertPointAlmostEqual(self, a, b): for (a_, b_) in zip(a.vertex, b.vertex): self.assertAlmostEqual(a_, b_, places=5) self.assertEqual(a.properties, b.properties) self.assertEqual(a.crs, b.crs) return def test_connected_multipoint_nearest_on_boundary2(self): line = Line([(-40, 0.0), (35, 0.0)], crs=LonLatWGS84) npt = line.nearest_on_boundary(Point((30.0, 80.0), crs=LonLatWGS84)) self.assertPointAlmostEqual(npt, Point((30.0, 0.0), crs=LonLatWGS84)) return def test_connected_multipoint_nearest_on_boundary3(self): # This is the test that tends to break naive root finding schemes line = Line([(-40, 0.0), (35, 0.0)], crs=LonLatWGS84) npt = line.nearest_on_boundary(Point((30.0, 1e-8), crs=LonLatWGS84)) self.assertPointAlmostEqual(npt, Point((30.0, 0.0), crs=LonLatWGS84)) return def test_connected_multipoint_nearest_on_boundary4(self): line = Line([(-20.0, 32.0), (-26.0, 43.0), (-38.0, 39.0)], crs=LonLatWGS84) npt = line.nearest_on_boundary(Point((-34.0, 52.0), crs=LonLatWGS84)) self.assertPointAlmostEqual(npt, Point((-27.98347, 42.456316), crs=LonLatWGS84)) return def test_line_intersection(self): line0 = Line([(0.0, 0.0), (3.0, 3.0)]) line1 = Line([(0.0, 3.0), (3.0, 0.0)]) self.assertTrue(line0.intersects(line1)) self.assertEqual(line0.intersections(line1), Multipoint([(1.5, 1.5)])) return def test_line_intersection_horizontal(self): line0 = Line([(-2.5, 2.5), (2.5, 2.5)]) line1 = Line([(0.0, 0.0), (1.0, 5.0)]) self.assertTrue(line0.intersects(line1)) self.assertEqual(line0.intersections(line1), Multipoint([(0.5, 2.5)])) return def test_line_intersection_vertical(self): line0 = Line([(2.5, 2.5), (2.5, -2.5)]) line1 = Line([(1.5, 2.5), (3.5, -2.5)]) self.assertTrue(line0.intersects(line1)) self.assertEqual(line0.intersections(line1), Multipoint([(2.5, 0.0)])) return def test_intersection_polygons(self): poly0 = Polygon([(0, 0), (2, 0), (3, 1), (2, 1), (2, 2), (1, 0)]) poly1 = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)]) self.assertTrue(poly0.intersects(poly1)) return def test_line_intersects_geographical1(self): line1 = Line([(-40.0, 36.0), (-38.0, 36.5)], crs=SphericalEarth) line2 = Line([(-39.0, 34.0), (-39.0, 37.5)], crs=SphericalEarth) self.assertTrue(line1.intersects(line2)) return def test_line_intersects_geographical2(self): line1 = Line([(-40.0, 36.0), (-38.0, 36.5)], crs=SphericalEarth) line2 = Line([(-42.0, 34.0), (-41.0, 37.5)], crs=SphericalEarth) self.assertFalse(line1.intersects(line2)) return def test_poly_clockwise(self): p = Polygon([(0,0), (0,1), (1,1), (1,0)]) self.assertTrue(p.isclockwise()) return def test_poly_counterclockwise(self): p = Polygon([(0,0), (1,0), (1,1), (0,1)]) self.assertFalse(p.isclockwise()) return def test_poly_polar(self): p = Polygon([(0.0, 80.0), (30.0, 80.0), (60.0, 80.0), (90.0, 80.0), (120.0, 80.0), (150.0, 80.0), (180.0, 80.0), (-150.0, 80.0), (-120.0, 80.0), (-90.0, 80.0), (-60.0, 80.0), (-30.0, 80.0)], crs=SphericalEarth) self.assertTrue(p.ispolar()) p = Polygon([(0.0, 85.0, 0.0), (90.0, 85.0, 0.0), (180.0, 85.0, 0.0), (-90.0, 85.0, 0.0)], crs=SphericalEarth) self.assertTrue(p.ispolar()) p = Polygon([(45.0, 30.0), (40.0, 25.0), (45.0, 20.0), (35.0, 25.0)], crs=SphericalEarth) self.assertFalse(p.ispolar()) p = Polygon([(-80, 0), (-50, -10), (20, -8), (35, -17), (55, 15), (-45, 18), (-60, 12)], crs=LonLatWGS84) self.assertFalse(p.ispolar()) p = Polygon([(45.0, 30.0), (40.0, 25.0), (45.0, 20.0), (35.0, 25.0)], crs=Cartesian) self.assertRaises(CRSError, p.ispolar) return def test_poly_extent(self): self.assertEqual(self.poly.get_extent(), (0.0, 6.0, 1.0, 8.0)) self.assertEqual(self.poly3.get_extent(), (0.0, 6.0, 1.0, 8.0)) return def test_poly_extent_foreign_crs(self): poly = Polygon([(0.0, 8.0), (0.0, 5.0), (6.0, 1.0)], crs=LonLatWGS84) poly3 = Polygon([(0.0, 8.0, 0.5), (0.0, 5.0, 0.8), (6.0, 1.0, 0.6)], crs=LonLatWGS84) x, y = zip(*poly.get_vertices(crs=NSIDCNorth)) self.assertEqual(poly.get_extent(NSIDCNorth), (min(x), max(x), min(y), max(y))) self.assertEqual(poly3.get_extent(NSIDCNorth), (min(x), max(x), min(y), max(y))) return def test_poly_length(self): self.assertEqual(self.poly.perimeter, 19.430647008220866) return def test_poly_contains1(self): # trivial cases pt0 = Point((-0.5, 0.92)) self.assertFalse(self.unitsquare.contains(pt0)) pt1 = Point((0.125, 0.875)) self.assertTrue(self.unitsquare.contains(pt1)) x = np.arange(-4, 5) y = (x)**2 line = Line([(x_,y_) for x_,y_ in zip(x, y)], crs=Cartesian) bbox = Polygon([(-2.5, 2.5), (2.5, 2.5), (2.5, -2.5), (-2.5, -2.5)], crs=Cartesian) self.assertEqual(list(filter(bbox.contains, line)), [Point((-1, 1)), Point((0, 0)), Point((1, 1))]) return def test_poly_contains2(self): # test some hard cases diamond = Polygon([(0,0), (1,1), (2,0), (1, -1)]) self.assertFalse(diamond.contains(Point((2, 1)))) self.assertTrue(diamond.contains(Point((1, 0)))) self.assertFalse(diamond.contains(Point((2.5, 0)))) self.assertFalse(diamond.contains(Point((0, -1)))) self.assertFalse(diamond.contains(Point((2, -1)))) return #def test_poly_contains3(self): # # case where point is on an edge (should return true) # square = Polygon([(0,0), (1,0), (1,1), (0,1)]) # self.assertTrue(square.contains(Point([0.5, 0]))) # self.assertTrue(square.contains(Point([0, 0.5]))) # return def test_poly_contains4(self): # hippie star theta = np.linspace(0, 2*np.pi, 361)[:-1] r = 10*np.sin(theta*8) + 15 x = np.cos(theta) * r + 25 y = np.sin(theta) * r + 25 polygon = Polygon(zip(x, y)) # causes naive cross-product methods to fail pt = Point((28.75, 25.625)) self.assertTrue(polygon.contains(pt)) return def test_poly_centroid(self): poly = Polygon([(0,0), (1,0), (1,1), (0,1)], properties={"name": "features1"}) c = poly.centroid self.assertEqual(c.x, 0.5) self.assertEqual(c.y, 0.5) self.assertEqual(c.properties, poly.properties) return def test_poly_centroid2(self): poly = Polygon([(0,0), (1,0), (2,0.5), (1,1), (0,1)], properties={"name": "features1"}) c = poly.centroid self.assertAlmostEqual(c.x, 7/9) self.assertEqual(c.y, 0.5) self.assertEqual(c.properties, poly.properties) return def test_ringedpoly_perimeter(self): self.assertEqual(round(self.ringed_poly.perimeter, 3), 50.246) return def test_ringedpoly_area(self): self.assertEqual(self.ringed_poly.area, 100 - self.ring.area) return def test_area_compute_pi(self): r = np.linspace(0, 2*np.pi, 10000) x = np.cos(r) y = np.sin(r) kp = Polygon(zip(x,y)) self.assertAlmostEqual(kp.area, np.pi, places=6) return def test_to_points_cartesian(self): line = Line([(0.0, 0.0), (4.0, 3.0), (1.0, 7.0)]) points = line.to_points(1.0) ans = [(0., 0.), (0.8, 0.6), (1.6, 1.2), (2.4, 1.8), (3.2, 2.4), (4., 3.), (3.4, 3.8), (2.8, 4.6), (2.2, 5.4), (1.6, 6.2), (1., 7.)] self.assertEqual(len(points), len(ans)) for pt, vert in zip(points, ans): self.assertAlmostEqual(pt.x, vert[0]) self.assertAlmostEqual(pt.y, vert[1]) return def test_to_points_lonlat(self): line = Line([(0.0, 38.0), (-10.5, 33.0), (-6.0, 35.0)], crs=LonLatWGS84) points = line.to_points(100000.0) ans = [( 0. , 38. ), ( -1.00809817, 37.58554833), ( -2.01066416, 37.17113146), ( -3.00781084, 36.7567488 ), ( -3.99964867, 36.34239982), ( -4.98628577, 35.92808398), ( -5.96782797, 35.51380078), ( -6.94437893, 35.09954973), ( -7.91604017, 34.68533037), ( -8.88291117, 34.27114226), ( -9.84508939, 33.85698498), (-10.80267038, 33.44285814), (-10.09466286, 33.19083929), ( -9.15505703, 33.62895663), ( -8.21064326, 34.0669835 ), ( -7.26131724, 34.5049191 ), ( -6.30697252, 34.94276264)] self.assertEqual(len(points), len(ans)) for pt, vert in zip(points, ans): self.assertAlmostEqual(pt.x, vert[0]) self.assertAlmostEqual(pt.y, vert[1]) return def test_to_npoints_cartesian(self): line = Line([(0.0, 0.0), (1.0, 2.0), (3.0, -2.0), (4.0, -1.0), (4.0, 3.0), (3.0, 2.0)]) points = line.to_npoints(20) ans = [Point(v) for v in [(0.0, 0.0), (0.318619234003536, 0.637238468007072), (0.637238468007072, 1.274476936014144), (0.9558577020106079, 1.9117154040212159), (1.274476936014144, 1.4510461279717122), (1.59309617001768, 0.8138076599646402), (1.911715404021216, 0.17656919195756826), (2.230334638024752, -0.4606692760495037), (2.5489538720282883, -1.0979077440565757), (2.867573106031824, -1.7351462120636478), (3.294395938694146, -1.7056040613058538), (3.7981771815888177, -1.2018228184111823), (4.0, -0.5729663008226373), (4.0, 0.13948796534818164), (4.0, 0.8519422315190006), (4.0, 1.5643964976898195), (4.0, 2.2768507638606383), (4.0, 2.989305030031457), (3.5037812428946715, 2.503781242894671), (3.0, 2.0)]] for a,b in zip(points, ans): self.assertPointAlmostEqual(a, b) return def test_to_npoints_lonlat(self): line = Line([(0, 40), (120, 40)], crs=LonLatWGS84) points = line.to_npoints(20) ans = [Point(v, crs=LonLatWGS84) for v in [(0, 40), (4.006549675732082, 43.200316625343305), (8.44359845345209, 46.2434129228378), (13.382442375999254, 49.09308515921458), (18.894149336762318, 51.705248417290484), (25.03918819127435, 54.027440893063556), (31.85052685770255, 55.99968253476488), (39.31083346558522, 57.55771841446013), (47.329401349484314, 58.6395037346357), (55.7308352362257, 59.194673757153645), (64.26916476377436, 59.19467375715364), (72.67059865051574, 58.639503734635674), (80.68916653441482, 57.557718414460105), (88.14947314229748, 55.999682534764844), (94.96081180872568, 54.02744089306352), (101.10585066323772, 51.705248417290456), (106.61755762400078, 49.09308515921457), (111.55640154654793, 46.24341292283779), (115.99345032426793, 43.2003166253433), (120, 40)]] for a,b in zip(points, ans): self.assertPointAlmostEqual(a, b) return def test_to_npoints_lonlat_precision(self): line = Line([(-20.247017, 79.683933), (-20.0993, 79.887917), (-19.13705, 80.048567), (-18.680467, 80.089333), (-17.451917, 80.14405), (-16.913233, 80.02715), (-16.631367, 80.022933), (-16.194067, 80.0168), (-15.915983, 80.020267), (-15.7763, 80.021283)], crs=LonLatWGS84) for n in range(2, 30): self.assertEqual(len(line.to_npoints(n)), n) return
def test_poly_contains3(self): # case where point is on an edge (should return true) square = Polygon([(0,0), (1,0), (1,1), (0,1)]) self.assertTrue(square.contains(Point([0.5, 0]))) self.assertTrue(square.contains(Point([0, 0.5]))) return
def setUp(self): self.square = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) return
def setUp(self): self.square = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) return
class TestShapefile(unittest.TestCase): def setUp(self): self.points = [Point((1, 1), data={"species": "T. officianale"}, crs=LonLatWGS84), Point((3, 1), data={"species": "C. tectorum"}, crs=LonLatWGS84), Point((4, 3), data={"species": "M. alba"}, crs=LonLatWGS84), Point((2, 2), data={"species": "V. cracca"}, crs=LonLatWGS84)] self.multipoint = Multipoint([(1,1), (3,1), (4,3), (2,2)], data={"species": ["T. officianale", "C. tectorum", "M. alba", "V. cracca"]}, crs=LonLatWGS84) self.line = Line([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], properties={"geom_id": 27, "name": "test line"}, crs=LonLatWGS84) self.polygon = Polygon([(1.0,5.0),(5.0,5.0),(5.0,1.0),(3.0,3.0),(1.0,1.0)], crs=LonLatWGS84) self.points3 = [Point((1, 1, 0), crs=LonLatWGS84), Point((3, 1, 3), crs=LonLatWGS84), Point((4, 3, 2), crs=LonLatWGS84), Point((2, 2, -1), crs=LonLatWGS84)] self.line3 = Line([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) self.polygon3 = Polygon([(1,5,2),(5,5,-1),(5,1,3),(3,3,1),(1,1,0)], crs=LonLatWGS84) testfiles = ["points.shp", "line.shp", "polygon.shp"] if any(not exists(join(TMPDATA, "shapefiles/", fnm)) for fnm in testfiles): self.saveTestData() return def saveTestData(self): testfiles = [(self.multipoint, "points"), (self.line, "line"), (self.polygon, "polygon")] os.makedirs(os.path.join(TMPDATA, "shapefiles")) for (geom, fnm) in testfiles: geom.to_shapefile(os.path.join(TMPDATA, "shapefiles", fnm)) return def assertGeomEqual(self, this, that): self.assertTrue(np.all(this.get_vertices() == that.get_vertices())) try: self.assertEqual(this.crs.get_proj4(), that.crs.get_proj4()) except AttributeError: print("warning: crs equality not established") return def test_writepoint(self): point = self.points[0] point.to_shapefile(os.path.join(TESTDIR, "data/point")) for fnm in ("point.shx", "point.shx", "point.dbf", "point.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_writepoints(self): points = self.points shp.write_shapefile(os.path.join(TESTDIR, "data/points.shp"), *points) for fnm in ("points.shx", "points.shx", "points.dbf", "points.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_writemultipoint(self): mp = Multipoint(self.points) mp.to_shapefile(os.path.join(TESTDIR, "data/multipoint")) for fnm in ("multipoint.shx", "multipoint.shx", "multipoint.dbf", "multipoint.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_writeline(self): self.line.to_shapefile(os.path.join(TESTDIR, "data/line")) for fnm in ("line.shx", "line.shx", "line.dbf", "line.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_writepoly(self): self.polygon.to_shapefile(os.path.join(TESTDIR, "data/polygon")) for fnm in ("polygon.shx", "polygon.shx", "polygon.dbf", "polygon.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_writepoints3(self): mp = Multipoint(self.points3) mp.to_shapefile(os.path.join(TESTDIR, "data/multipointz")) for fnm in ("multipointz.shx", "multipointz.shx", "multipointz.dbf", "multipointz.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_writeline3(self): self.line3.to_shapefile(os.path.join(TESTDIR, "data/linez")) for fnm in ("linez.shx", "linez.shx", "linez.dbf", "linez.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_writepoly3(self): self.polygon3.to_shapefile(os.path.join(TESTDIR, "data/polygonz")) for fnm in ("polygonz.shx", "polygonz.shx", "polygonz.dbf", "polygonz.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_write_collection_multipoint(self): mp = Multipoint([p.vertex for p in self.points]) mp0 = copy(mp) mp1 = copy(mp.shift((4, 2))) mp2 = copy(mp.shift((-2, 3))) shp.write_shapefile(os.path.join(TESTDIR, "data/mp_collection.shp"), mp0, mp1, mp2) for fnm in ("mp_collection.shx", "mp_collection.shx", "mp_collection.dbf", "mp_collection.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_write_collection_lines(self): line0 = copy(self.line) line1 = copy(self.line.shift((4, 2))) line2 = copy(self.line.shift((-2, 3))) shp.write_shapefile(os.path.join(TESTDIR, "data/line_collection.shp"), line0, line1, line2) for fnm in ("line_collection.shx", "line_collection.shx", "line_collection.dbf", "line_collection.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) return def test_read_points(self): points = read_shapefile(os.path.join(TESTDATA, "shp_input", "points")) self.assertEqual(len(points), 4) pt = points[0] self.assertTrue("+proj=lonlat" in pt.crs.get_proj4()) self.assertTrue("+a=6378137.0" in pt.crs.get_proj4()) self.assertTrue("+f=0.00335281" in pt.crs.get_proj4()) mp = Multipoint(points) self.assertEqual(mp.d["species"], ['T. officianale', 'C. tectorum', 'M. alba', 'V. cracca']) self.assertEqual(mp.d["ID"], ['0', '1', '2', '3']) self.assertEqual(mp.coordinates, ((1.0, 3.0, 4.0, 2.0), (1.0, 1.0, 3.0, 2.0))) def test_read_line(self): line = read_shapefile(os.path.join(TESTDATA, "shp_input", "line"))[0] self.assertTrue("+proj=lonlat" in line.crs.get_proj4()) self.assertTrue("+a=6378137.0" in line.crs.get_proj4()) self.assertTrue("+f=0.00335281" in line.crs.get_proj4()) self.assertEqual(line.coordinates, ((1.0, 5.0, 5.0, 3.0, 1.0), (5.0, 5.0, 1.0, 3.0, 1.0))) return def test_read_polygon(self): polygon = read_shapefile(os.path.join(TESTDATA, "shp_input", "polygon"))[0] self.assertTrue("+proj=lonlat" in polygon.crs.get_proj4()) self.assertTrue("+a=6378137.0" in polygon.crs.get_proj4()) self.assertTrue("+f=0.00335281" in polygon.crs.get_proj4()) self.assertEqual(polygon.coordinates, ((1.0, 5.0, 5.0, 3.0, 1.0), (5.0, 5.0, 1.0, 3.0, 1.0))) return def test_read_points_newp(self): # Read a multipoint with a projected cooridnate system newp = read_shapefile(os.path.join(TESTDATA, "shp_input", "newp_nsidc_north")) proj4 = ('+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 ' '+y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs') for part in proj4.split(): self.assertTrue(part[:8] in newp[0].crs.get_proj4()) coords = list(zip(*[pt.vertex[:2] for pt in newp])) self.assertEqual(coords, [(521236.8297444395, 521236.8297444395, 521236.8297444395, 547490.4452879033, 547490.4452879033, 547490.4452879033, 587584.1578033275, 587584.1578033275, 587584.1578033275, 571828.4918982167, 571828.4918982167), (-888853.1384770898, -888853.1384770898, -888853.1384770898, -902049.3617542256, -902049.3617542256, -902049.3617542256, -871214.0673764511, -871214.0673764511, -871214.0673764511, -850080.914674058, -850080.914674058)]) meterno = [pt.properties["meterno"] for pt in newp] self.assertEqual(meterno, ['IMS1/1', 'IMS2/1', '5952/2', 'IMS4/1', '5953/2', '1963/13', 'IMS5/1', '5213/A', '2121/13', 'IMS3/1', '3613/2']) depth = [pt.properties["depth_m"] for pt in newp] self.assertEqual(depth, ['73', '143', '247', '86', '147', '250', '74', '142', '235', '150', '248']) return
class TestAffineTransforms(unittest.TestCase): def setUp(self): self.square = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) return def test_translate_point(self): pt = Point((0, 0), crs=Cartesian) with self.assertRaises(ValueError): newpt = pt.apply_transform(np.array([[0, 0, 1, 0], [0, 0, 2, 0]])) newpt = pt.apply_transform(np.array([[0, 0, 1], [0, 0, 2]])) self.assertEqual(newpt.x, 1.0) self.assertEqual(newpt.y, 2.0) return def test_translate_point_3(self): pt = Point((0, 0, 3), crs=Cartesian) newpt = pt.apply_transform(np.array([[0, 0, 1], [0, 0, 2]])) self.assertEqual(newpt.x, 1.0) self.assertEqual(newpt.y, 2.0) self.assertEqual(newpt.z, 3.0) return def test_transform_multiline(self): g = Multiline([[(0,0), (1,1), (1,2)], [(-3,2), (-2,-1), (0,1)]]) pi = math.pi gnew = g.apply_transform(np.array([[math.cos(0.5*pi), -math.sin(0.5*pi), 0], [math.sin(0.5*pi), math.cos(0.5*pi), 0]])) l1, l2 = gnew.vertices() self.assertTrue(np.allclose(l1, np.array([(0,0), (-1,1), (-2,1)]))) self.assertTrue(np.allclose(l2, np.array([(-2,-3), (1,-2), (-1,0)]))) gnew2 = gnew.apply_transform(np.array([[2, 0, 0], [0, -3, 0]])) l1, l2 = gnew2.vertices() self.assertTrue(np.allclose(l1, np.array([(0,0), (-2,-3), (-4,-3)]))) self.assertTrue(np.allclose(l2, np.array([(-4,9), (2,6), (-2,0)]))) return def test_translate_multipoint(self): M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(1,0), (2,0), (1,1)])) Mans = np.array([[1, 0, 1], [0, 1, 0]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_transform(M) ans = np.array([[1, 0], [1, 1], [2, 1], [2, 0]]) self.assertTrue(np.allclose(translated_square.vertices(), ans)) return def test_rotate_multipoint(self): s2 = math.sqrt(0.5) M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(0,0), (s2,s2), (-s2,s2)])) Mans = np.array([[s2, -s2, 0], [s2, s2, 0]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_transform(M) ans = np.array([[0, 0], [-s2, s2], [0, 2*s2], [s2, s2]]) self.assertTrue(np.allclose(translated_square.vertices(), ans)) return def test_stretch_multipoint(self): M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]), Multipoint([(0,0), (2,0), (0,2)])) Mans = np.array([[2, 0, 0], [0, 2, 0]]) self.assertTrue(np.allclose(M, Mans)) translated_square = self.square.apply_transform(M) ans = np.array([[0, 0], [0, 2], [2, 2], [2, 0]]) self.assertTrue(np.allclose(translated_square.vertices(), ans)) return
def test_intersection_polygons(self): poly0 = Polygon([(0, 0), (2, 0), (3, 1), (2, 1), (2, 2), (1, 0)]) poly1 = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)]) self.assertTrue(poly0.intersects(poly1)) return
def test_poly_output(self): p = Polygon([(4, 2), (3, 5), (3, 2), (7, 3)]) sp = shapely.geometry.shape(p.geomdict) self.assertEqual(p.bbox(), sp.bounds) return
def test_poly_output(self): p = Polygon([(4, 2), (3, 5), (3, 2), (7, 3)]) sp = shapely.geometry.shape(p.geomdict) self.assertEqual(p.bbox(), sp.bounds) return
def test_poly_counterclockwise(self): p = Polygon([(0,0), (1,0), (1,1), (0,1)]) self.assertFalse(p.isclockwise()) return