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.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_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), -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), 45.036973, places=6) return def test_point_shift_inplace(self): point = Point((-3.0, 5.0, 2.5), data={"color": (43, 67, 10)}, properties="apple") 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), data={"color": (43, 67, 10)}, properties="apple") 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_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_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_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_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.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_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_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_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_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 = 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, -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 TestGeometry(unittest.TestCase): """ Tests for manipulating Geometry objects (indexing, iteration, equality, etc.) """ 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_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), properties={"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_add(self): ptA = Point((1, 2), crs=SphericalEarth) ptB = Point((3, 4), crs=LonLatWGS84) res = ptA + ptB self.assertTrue(isinstance(res, Multipoint)) self.assertEqual(len(res), 2) self.assertEqual(res.crs, SphericalEarth) return def test_line_add(self): lineA = Line([(1, 2), (2, 3)], crs=SphericalEarth) lineB = Line([(3, 4), (4, 5)], crs=LonLatWGS84) res = lineA + lineB self.assertTrue(isinstance(res, Multiline)) self.assertEqual(len(res), 2) self.assertEqual(res.crs, SphericalEarth) 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_empty_multipoint(self): mp = Multipoint([], crs=LonLatWGS84) self.assertEqual(mp.vertices.rank, 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_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): point = Point(self.vertices[0], properties={"value": 99.0}) self.assertEqual(self.mp[0], point) 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), properties={"value": "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.properties, {"location":"severn"}) return def test_multipoint_slicing1(self): submp = Multipoint(self.vertices[5:10], data=self.data[5:10]) self.assertEqual(self.mp[5:10], submp) def test_multipoint_slicing2(self): submp = Multipoint(self.vertices[5:], data=self.data[5:]) self.assertEqual(self.mp[5:], submp) return def test_multipoint_slicing3(self): mp = Multipoint(np.arange(214).reshape([107, 2])) self.assertEqual(len(mp[::10]), 11) return def test_multipoint_negative_index(self): self.assertEqual(self.mp[len(self.mp)-1], self.mp[-1]) 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_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_segments(self): v = self.vertices for i, seg in enumerate(self.line.segments): self.assertTrue(np.all(np.equal(seg.vertices[0], self.vertices[i]))) self.assertTrue(np.all(np.equal(seg.vertices[1], self.vertices[i+1]))) return
class TestGeometry(unittest.TestCase): """ Tests for manipulating Geometry objects (indexing, iteration, equality, etc.) """ 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_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), properties={"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_add(self): ptA = Point((1, 2), crs=SphericalEarth) ptB = Point((3, 4), crs=LonLatWGS84) res = ptA + ptB self.assertTrue(isinstance(res, Multipoint)) self.assertEqual(len(res), 2) self.assertEqual(res.crs, SphericalEarth) return def test_line_add(self): lineA = Line([(1, 2), (2, 3)], crs=SphericalEarth) lineB = Line([(3, 4), (4, 5)], crs=LonLatWGS84) res = lineA + lineB self.assertTrue(isinstance(res, Multiline)) self.assertEqual(len(res), 2) self.assertEqual(res.crs, SphericalEarth) 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_empty_multipoint(self): mp = Multipoint([], crs=LonLatWGS84) self.assertEqual(mp.vertices.rank, -1) 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_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): point = Point(self.vertices[0], properties={"value": 99.0}) self.assertEqual(self.mp[0], point) 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), properties={"value": "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.properties, {"location":"severn"}) return def test_multipoint_slicing1(self): submp = Multipoint(self.vertices[5:10], data=self.data[5:10]) self.assertEqual(self.mp[5:10], submp) def test_multipoint_slicing2(self): submp = Multipoint(self.vertices[5:], data=self.data[5:]) self.assertEqual(self.mp[5:], submp) return def test_multipoint_slicing3(self): mp = Multipoint(np.arange(214).reshape([107, 2])) self.assertEqual(len(mp[::10]), 11) return def test_multipoint_negative_index(self): self.assertEqual(self.mp[len(self.mp)-1], self.mp[-1]) 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_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_segments(self): v = self.vertices for i, seg in enumerate(self.line.segments): self.assertTrue(np.all(np.equal(seg.vertices[0], self.vertices[i]))) self.assertTrue(np.all(np.equal(seg.vertices[1], self.vertices[i+1]))) 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