def test_polygon_from_points(self): pts = [Point((x, y), data={"d": d}, properties={"p":i}, crs=LonLatWGS84) for i,((x,y),d) in enumerate(zip(self.vertices, self.data))] mp = Polygon(pts) ans = Polygon(self.vertices, data={"p":range(len(pts))}, crs=LonLatWGS84) self.assertEqual(mp, ans) return
def test_bbox_geographical(self): for crs in (SphericalEarth, LonLatWGS84): poly = Polygon([(179, -1), (-179, -1), (-179, 1), (179, 1)], crs=crs) bb = poly.bbox() self.assertEqual((bb[0], bb[2]), (179, -179)) self.assertAlmostEqual(bb[1], -1.000152297, places=8) self.assertAlmostEqual(bb[3], 1.000152297, places=8)
def test_merge_polygons(self): pg1 = Polygon([[(3, 4), (6, 4), (7, 6), (2, 5)]]) pg2 = Polygon([[(2, 4), (5, 4), (6, 6), (0, 5)]]) mp1 = Multipolygon([[[(0, 0), (9, 1), (5, 8)], [(1, 1), (6, 2), (3, 3)]], [[(3, 3), (4, 3), (4, 5), (4, 3)]]]) mp2 = Multipolygon([[[(1, 9), (6, 0), (9, 8)], [(4, 3), (4, 4), (5, 4)]], [[(6, 2), (7, 3), (8, 4), (7, 5)]]]) merged = Multipolygon.merge(pg1, mp1, pg2, mp2) self.assertEqual(len(merged), 6)
def test_polygon_from_points(self): x = range(-5, 5) y = [x_**2 for x_ in x] vertices = list(zip(x, y)) data = [x_ * y_ for (x_, y_) in vertices] pts = [ Point((x, y), properties={"p": i}, crs=LonLatWGS84) for i, ((x, y), d) in enumerate(zip(vertices, data)) ] poly = Polygon(pts, crs=LonLatWGS84) ans = Polygon(vertices, crs=LonLatWGS84) self.assertEqual(poly, ans)
def test_multipolygon_from_polygons(self): polys = [] for i in range(5): sub = [] for j in range(5): sub.append((2 * j + i, -1.5 * j + 2 * i)) polys.append(Polygon(sub, properties={"d": i * j})) g = multipart_from_singleparts(polys) self.assertEqual(g.d["d"], [0, 4, 8, 12, 16]) return
def test_multipolygon_from_polygons(self): polys = [] for i in range(5): sub = [] for j in range(5): sub.append((2 * j + i, -1.5 * j + 2 * i)) polys.append(Polygon(sub, properties={"d": i * j}, crs=LonLatWGS84)) g = Multipolygon(polys) self.assertEqual(g.d["d"], [0, 4, 8, 12, 16]) self.assertEqual(g.crs, LonLatWGS84)
def test_multipolygon_from_polygons_constructor(self): polys = [] for i in range(5): sub = [] for j in range(5): sub.append((2 * j + i, -1.5 * j + 2 * i)) polys.append(Polygon(sub, crs=LonLatWGS84)) g = Multipolygon(polys) for p, p_ in zip(g, polys): self.assertEqual(p.vertices(), p_.vertices()) self.assertEqual(g.crs, LonLatWGS84)
def benchmark(): θ = np.linspace(0, 2*np.pi, 361)[:-1] r = np.sin(θ*20) + 1.5 x = np.cos(θ) * r y = np.sin(θ) * r polygon = Polygon(zip(x, y)) line = Line([(-2,-3), (0, 3)]) for seg in polygon.segments: x = seg.intersections(line) bbox = Polygon([(-1, -1), (-1, 1), (1, 1), (1, -1)]) points = [] for pt in polygon: if bbox.contains(pt): points.append(pt) multipoint = Multipoint(points) hull = multipoint.convex_hull() print("bbox contains", len(multipoint), "out of", len(polygon), "vertices") return polygon, line, bbox, multipoint, hull
def benchmark(): θ = np.linspace(0, 2 * np.pi, 361)[:-1] r = np.sin(θ * 20) + 1.5 x = np.cos(θ) * r y = np.sin(θ) * r polygon = Polygon(zip(x, y)) line = Line([(-2, -3), (0, 3)]) for seg in polygon.segments: x = seg.intersections(line) bbox = Polygon([(-1, -1), (-1, 1), (1, 1), (1, -1)]) points = [] for pt in polygon: if bbox.contains(pt): points.append(pt) multipoint = Multipoint(points) hull = multipoint.convex_hull() print("bbox contains", len(multipoint), "out of", len(polygon), "vertices") return polygon, line, bbox, multipoint, hull
def test_area(self): for crs in (SphericalEarth, LonLatWGS84): poly0 = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)], crs=crs) poly1 = Polygon([(179, -1), (-179, -1), (-179, 1), (179, 1)], crs=crs) self.assertAlmostEqual(poly0.area, poly1.area)
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))