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 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.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_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_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 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_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_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_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_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_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_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_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_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": {}, "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_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_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_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_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_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_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(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_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_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_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_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 setUp(self): self.square = Polygon([(0, 0), (0, 1), (1, 1), (1, 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))