def test_equally_spaced_points_4(self): p1 = geo.Point(0, 0, 10) p2 = geo.Point(0, 0, 7) points = p1.equally_spaced_points(p2, 1) self.assertEqual(points, [p1, geo.Point(0, 0, 9), geo.Point(0, 0, 8), p2])
def test_same_points(self): self.assertEqual( geo.Point(*utils.get_middle_point(1.2, -1.4, 1.2, -1.4)), geo.Point(1.2, -1.4)) self.assertEqual( geo.Point(*utils.get_middle_point(-150.33, 22.1, -150.33, 22.1)), geo.Point(-150.33, 22.1))
def test_single_segment(self): line = geo.Line([ geo.Point(0., 0.00899322029302, 0.), geo.Point(0.03344582378948, -0.00936927115925, 4.24264069) ]) line = line.resample_to_num_points(7) self.assertEqual(len(line), 7)
def test_differnet_point(self): self.assertEqual( geo.Point(*utils.get_middle_point(0, 0, 0.2, -0.2)), geo.Point(0.1, -0.1), ) self.assertEqual(geo.Point(*utils.get_middle_point(20, 40, 20.02, 40)), geo.Point(20.01, 40))
def test_point_wkt2d(self): pt = geo.Point(13.5, 17.8) self.assertEqual('POINT(13.5 17.8)', pt.wkt2d) # Test a point with depth; the 2d wkt should be the same pt = geo.Point(13.5, 17.8, 1.5) self.assertEqual('POINT(13.5 17.8)', pt.wkt2d)
def test_mesh_spacing_uniformness(self): MESH_SPACING = 10 tl = geo.Point(60, 60) tr = geo.Point(70, 60) bottom_line = [geo.Point(lon, 58) for lon in xrange(70, 59, -1)] poly = geo.Polygon([tl, tr] + bottom_line) mesh = poly.discretize(mesh_spacing=MESH_SPACING) self.assertIsInstance(mesh, geo.Mesh) mesh = list(mesh) for i, point in enumerate(mesh): if i == len(mesh) - 1: # the point is last in the mesh break next_point = mesh[i + 1] if next_point.longitude < point.longitude: # this is the next row (down along the meridian). # let's check that the new row stands exactly # mesh spacing kilometers below the previous one. self.assertAlmostEqual( point.distance(geo.Point(point.longitude, next_point.latitude)), MESH_SPACING, places=4 ) continue dist = point.distance(next_point) self.assertAlmostEqual(MESH_SPACING, dist, places=4)
def test_wkt(self): expected_wkt = ( 'POLYGON((-1.111111 2.222222, -3.333333 4.444444, ' '5.555555 -6.666666, -1.111111 2.222222))' ) poly = polygon.Polygon( [geo.Point(-1.111111, 2.222222), geo.Point(-3.333333, 4.444444), geo.Point(5.555555, -6.666666)] ) self.assertEqual(expected_wkt, poly.wkt)
def test_resample_2(self): """ Line made of 3 points (aligned in the same direction) equally spaced (spacing equal to 10 km). The resampled line contains 2 points (with spacing of 30 km) consistent with the number of points as predicted by n = round(20 / 30) + 1. """ p1 = geo.Point(0.0, 0.0) p2 = geo.Point(0.0, 0.089932202939476777) p3 = geo.Point(0.0, 0.1798644058789465) self.assertEqual(2, len(geo.Line([p1, p2, p3]).resample(30.0)))
def test_equally_spaced_points_3(self): """ Corner case where the end point is equal to the start point. In this situation we have just one point (the start/end point). """ p1 = geo.Point(0.0, 0.0) p2 = geo.Point(0.0, 0.0) points = p1.equally_spaced_points(p2, 10.0) self.assertEqual(1, len(points)) self.assertEqual(p1, points[0]) self.assertEqual(p2, points[0])
def test_equally_spaced_points_1(self): p1 = geo.Point(0.0, 0.0) p2 = geo.Point(0.190775520815, 0.190774854966) points = p1.equally_spaced_points(p2, 10.0) self.assertEqual(4, len(points)) self.assertEqual(p1, points[0]) # first point is the start point self.assertEqual(p2, points[3]) # last point is the end point expected = geo.Point(0.0635916966572, 0.0635916574897, 0.0) self.assertEqual(expected, points[1]) expected = geo.Point(0.127183510817, 0.127183275812, 0.0) self.assertEqual(expected, points[2])
def test_equally_spaced_points_2(self): p1 = geo.Point(0.0, 0.0, 0.0) p2 = geo.Point(0.134898484431, 0.134898249018, 21.2132034356) points = p1.equally_spaced_points(p2, 10.0) self.assertEqual(4, len(points)) self.assertEqual(p1, points[0]) # first point is the start point self.assertEqual(p2, points[3]) # last point is the end point expected = geo.Point(0.0449661107016, 0.0449660968538, 7.07106781187) self.assertEqual(expected, points[1]) expected = geo.Point(0.0899322629466, 0.0899321798598, 14.1421356237) self.assertEqual(expected, points[2])
def test_cutting_corners(self): p1 = geo.Point(0., 0.) p2 = p1.point_at(1, 0, 1) p3 = p2.point_at(1, 0, 179) p4 = p3.point_at(5, 0, 90) line = geo.Line([p1, p2, p3, p4]).resample_to_num_points(3) self.assertEqual(len(line), 3)
def test_mesh_depth(self): p = geo.Point(0.5, -0.5) mesh = geo.Mesh(numpy.array([0.5, 0.5, 0.5, 0.5]), numpy.array([-0.5, -0.5, -0.5, -0.5]), numpy.array([0., 1., 2., 3.])) distances = p.distance_to_mesh(mesh) ed = [0, 1, 2, 3] numpy.testing.assert_array_almost_equal(distances, ed)
def test_points_close_to_edges(self): # Test points close to the edges: # Note that any point which lies directly on a meridian (with a # longitude component of -10 or 10, in this case) intersects but is not # contained by the polygon. This is because meridians are great circle # arcs themselves. This test illustrates the difference in behavior # between North/South lines and East/West lines. # [North, South, East, West] points = [ geo.Point(0, 10), geo.Point(0, -10.0), geo.Point(9.9999999, 0), geo.Point(-9.9999999, 0), ] mesh = geo.Mesh.from_points_list(points) self.assertTrue(self.poly.intersects(mesh).all())
def test_point_depth(self): p = geo.Point(0, 0, 10) mesh = geo.Mesh(numpy.array([0.1, 0.2, 0.3, 0.4]), numpy.array([0., 0., 0., 0.]), depths=None) distances = p.distance_to_mesh(mesh) ed = [14.95470217, 24.38385672, 34.82510666, 45.58826465] numpy.testing.assert_array_almost_equal(distances, ed)
def test_resample_4(self): """ When resampling a line with a single point, the result is a one point line with the same point. """ p1 = geo.Point(0.0, 0.0) self.assertEqual(geo.Line([p1]), geo.Line([p1]).resample(10.0))
def test_points_close_to_corners(self): # The same boundary conditions apply here (as noted in the test above). points = [ geo.Point(-9.999999, 10), geo.Point(9.999999, 10), geo.Point(-9.999999, 9.999999), geo.Point(9.999999, 9.999999), geo.Point(-9.999999, -9.99999), geo.Point(-9.999999, 9.999999), geo.Point(-9.999999, -10), geo.Point(9.999999, -10), ] mesh = geo.Mesh.from_points_list(points) self.assertTrue(self.poly.intersects(mesh).all())
def test_clockwise(self): poly = polygon.Polygon([geo.Point(0, 0), geo.Point(0, 1), geo.Point(1, 0.5)]) dilated = poly.dilate(20) elons = [ 0.0804399, 0.0645005, 0.0479561, 0.0309619, 0.0136773, -0.0037356, -0.0211136, -0.0382935, -0.0551142, -0.0714181, -0.0870522, -0.1018698, -0.1157321, -0.1285089, -0.1400805, -0.1503383, -0.1591861, -0.1665409, -0.1723339, -0.1765105, -0.1790318, -0.1798739, -0.1799013, -0.1790601, -0.1765397, -0.1723637, -0.1665713, -0.1592168, -0.1503691, -0.1401112, -0.1285392, -0.1157618, -0.1018986, -0.0870797, -0.0714441, -0.0551384, -0.0383156, -0.0211335, -0.0037531, 0.0136624, 0.0309498, 0.0479468, 0.0644941, 0.0804365, 1.0804671, 1.0955589, 1.1097657, 1.1229560, 1.1350074, 1.1458085, 1.1552592, 1.1632720, 1.1697727, 1.1747011, 1.1780116, 1.1796735, 1.1796714, 1.1780054, 1.1746910, 1.1697588, 1.1632545, 1.1552383, 1.1457846, 1.1349808, 1.1229271, 1.1097349, 1.0955266, 1.0804337 ] elats = [ -0.1608776, -0.1679056, -0.1733589, -0.1771863, -0.1793519, -0.1798355, -0.1786324, -0.1757539, -0.1712271, -0.1650944, -0.1574134, -0.1482560, -0.1377081, -0.1258688, -0.1128490, -0.0987708, -0.0837663, -0.0679761, -0.0515485, -0.0346374, -0.0174015, -0.0000025, 0.9999975, 1.0173956, 1.0346306, 1.0515410, 1.0679680, 1.0837578, 1.0987621, 1.1128403, 1.1258603, 1.1377001, 1.1482485, 1.1574067, 1.1650887, 1.1712225, 1.1757506, 1.1786305, 1.1798351, 1.1793533, 1.1771894, 1.1733639, 1.1679125, 1.1608864, 0.6608661, 0.6523872, 0.6424971, 0.6312873, 0.6188616, 0.6053351, 0.5908330, 0.5754898, 0.5594475, 0.5428546, 0.5258649, 0.5086357, 0.4913265, 0.4740977, 0.4571088, 0.4405171, 0.4244763, 0.4091349, 0.3946351, 0.3811110, 0.3686880, 0.3574810, 0.3475939, 0.3391182 ] ebbox = [-0.17990133, 1.17967345, 1.17983512, -0.17983547] numpy.testing.assert_allclose(dilated.lons, elons, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated.lats, elats, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated._bbox, ebbox) self.assertIs(dilated._projection, poly._projection) self.assertEqual(len(dilated._polygon2d.boundary.coords), len(elons) + 1)
def test_fewer_points(self): points = [geo.Point(i / 10., 0) for i in xrange(13)] line = geo.Line(points).resample_to_num_points(2) expected_points = [points[0], points[-1]] self.assertEqual(line.points, expected_points) line = geo.Line(points).resample_to_num_points(4) expected_points = points[::4] self.assertEqual(line.points, expected_points)
def test_resample(self): p1 = geo.Point(0.0, 0.0, 0.0) p2 = geo.Point(0.0, 0.127183341091, 14.1421356237) p3 = geo.Point(0.134899286793, 0.262081472606, 35.3553390593) resampled = geo.Line([p1, p2, p3]).resample(10.0) p1 = geo.Point(0.0, 0.0, 0.0) p2 = geo.Point(0.0, 0.0635916705456, 7.07106781187) p3 = geo.Point(0.0, 0.127183341091, 14.1421356237) p4 = geo.Point(0.0449662998195, 0.172149398777, 21.2132034356) p5 = geo.Point(0.0899327195183, 0.217115442616, 28.2842712475) p6 = geo.Point(0.134899286793, 0.262081472606, 35.3553390593) expected = geo.Line([p1, p2, p3, p4, p5, p6]) self.assertEqual(expected, resampled)
def test_point_depth(self): p = geo.Point(0, 0, 10) mesh = geo.Mesh(numpy.array([0.1, 0.2, 0.3, 0.4]), numpy.array([0., 0., 0., 0.]), depths=None) closer = p.closer_than(mesh, 30) numpy.testing.assert_array_equal(closer, [1, 1, 0, 0]) closer = p.closer_than(mesh, 35) numpy.testing.assert_array_equal(closer, [1, 1, 1, 0]) closer = p.closer_than(mesh, 15) numpy.testing.assert_array_equal(closer, [1, 0, 0, 0])
def test_mesh_depth(self): p = geo.Point(0.5, -0.5) mesh = geo.Mesh(numpy.array([0.5, 0.5, 0.5, 0.5]), numpy.array([-0.5, -0.5, -0.5, -0.5]), numpy.array([0., 1., 2., 3.])) closer = p.closer_than(mesh, 0.1) numpy.testing.assert_array_equal(closer, [1, 0, 0, 0]) closer = p.closer_than(mesh, 1.5) numpy.testing.assert_array_equal(closer, [1, 1, 0, 0]) closer = p.closer_than(mesh, 3) numpy.testing.assert_array_equal(closer, [1, 1, 1, 1])
def test_both_depths(self): p = geo.Point(3, 7, 9) mesh = geo.Mesh(numpy.array([2.9, 2.9, 3., 3., 3.1, 3.1]), numpy.array([7., 7.1, 6.9, 7.1, 6.8, 7.2]), numpy.array([20., 30., 10., 20., 40., 50.])) distances = p.distance_to_mesh(mesh) ed = [ 15.58225761, 26.19968783, 11.16436819, 15.64107148, 39.71688472, 47.93043417 ] numpy.testing.assert_array_almost_equal(distances, ed)
def test_no_depths(self): p = geo.Point(20, 30) mesh = geo.Mesh(numpy.array([[18., 19., 20., 21., 22.]] * 3), numpy.array([[29] * 5, [30] * 5, [31] * 5]), depths=None) closer = p.closer_than(mesh, 120) self.assertEqual(closer.dtype, bool) ec = [[0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0]] numpy.testing.assert_array_equal(closer, ec) closer = p.closer_than(mesh, 100) ec = [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]] numpy.testing.assert_array_equal(closer, ec)
def test_both_depths(self): p = geo.Point(3, 7, 9) mesh = geo.Mesh(numpy.array([2.9, 2.9, 3., 3., 3.1, 3.1]), numpy.array([7., 7.1, 6.9, 7.1, 6.8, 7.2]), numpy.array([20., 30., 10., 20., 40., 50.])) closer = p.closer_than(mesh, 20) numpy.testing.assert_array_equal(closer, [1, 0, 1, 1, 0, 0]) closer = p.closer_than(mesh, 40) numpy.testing.assert_array_equal(closer, [1, 1, 1, 1, 1, 0]) closer = p.closer_than(mesh, 10) numpy.testing.assert_array_equal(closer, [0, 0, 0, 0, 0, 0]) closer = p.closer_than(mesh, 60) numpy.testing.assert_array_equal(closer, [1, 1, 1, 1, 1, 1])
def test_counterclockwise(self): poly = polygon.Polygon([geo.Point(5, 6), geo.Point(4, 6), geo.Point(4, 5)]) dilated = poly.dilate(20) elons = [ 5.1280424, 4.1280406, 4.1149304, 4.1007112, 4.0855200, 4.0695036, 4.0528165, 4.0356198, 4.0180793, 4.0003644, 3.9826460, 3.9650949, 3.9478807, 3.9311693, 3.9151220, 3.8998938, 3.8856315, 3.8724729, 3.8605451, 3.8499631, 3.8408293, 3.8332319, 3.8272443, 3.8229245, 3.8203143, 3.8194390, 3.8191353, 3.8199981, 3.8225928, 3.8268944, 3.8328618, 3.8404377, 3.8495493, 3.8601090, 3.8720154, 3.8851539, 3.8993981, 3.9146108, 3.9306458, 3.9473485, 3.9645582, 3.9821092, 3.9998325, 5.0001675, 5.0178827, 5.0354259, 5.0526283, 5.0693244, 5.0853535, 5.1005616, 5.1148024, 5.1279389, 5.1398448, 5.1504059, 5.1595205, 5.1671011, 5.1730751, 5.1773852, 5.1799900, 5.1808646, 5.1800010, 5.1774074, 5.1731090, 5.1671473, 5.1595797, 5.1504790, 5.1399327 ] elats = [ 5.8729890, 4.8731997, 4.8612916, 4.8507226, 4.8415948, 4.8339963, 4.8280004, 4.8236650, 4.8210319, 4.8201265, 4.8209575, 4.8235169, 4.8277800, 4.8337058, 4.8412369, 4.8503008, 4.8608099, 4.8726630, 4.8857454, 4.8999311, 4.9150831, 4.9310551, 4.9476930, 4.9648361, 4.9823190, 4.9999728, 5.9999728, 6.0175942, 6.0350466, 6.0521621, 6.0687761, 6.0847286, 6.0998661, 6.1140429, 6.1271226, 6.1389792, 6.1494986, 6.1585795, 6.1661344, 6.1720906, 6.1763909, 6.1789936, 6.1798739, 6.1798739, 6.1789944, 6.1763940, 6.1720977, 6.1661467, 6.1585984, 6.1495254, 6.1390149, 6.1271681, 6.1140988, 6.0999329, 6.0848065, 6.0688651, 6.0522620, 6.0351568, 6.0177140, 6.0001013, 5.9824878, 5.9650430, 5.9479344, 5.9313265, 5.9153789, 5.9002447, 5.8860693 ] ebbox = [3.81913534, 5.18086464, 6.17987385, 4.82012646] numpy.testing.assert_allclose(dilated.lons, elons, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated.lats, elats, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated._bbox, ebbox) self.assertIs(dilated._projection, poly._projection) self.assertEqual(len(dilated._polygon2d.boundary.coords), len(elons) + 1)
def test_simple(self): points = [geo.Point(0, 0), geo.Point(0.1, 0.3)] line = geo.Line(points).resample_to_num_points(3) expected_points = [ geo.Point(0, 0), geo.Point(0.05, 0.15), geo.Point(0.1, 0.3) ] self.assertEqual(line.points, expected_points) line = geo.Line(points).resample_to_num_points(4) expected_points = [ geo.Point(0, 0), geo.Point(0.0333333, 0.1), geo.Point(0.0666666, 0.2), geo.Point(0.1, 0.3) ] self.assertEqual(line.points, expected_points)
def test_no_depths(self): p = geo.Point(20, 30) mesh = geo.Mesh(numpy.array([[ 18., 19., 20., ]] * 3), numpy.array([[29.] * 3, [30.] * 3, [31.] * 3]), depths=None) distances = p.distance_to_mesh(mesh, with_depths=False) ed = [[223.21812393, 147.4109544, 111.19492664], [192.59281778, 96.29732568, 0], [221.53723588, 146.77568123, 111.19492664]] numpy.testing.assert_array_almost_equal(distances, ed)
def test_remove_adjacent_duplicates(self): p1 = geo.Point(0.0, 0.0, 0.0) p2 = geo.Point(0.0, 1.0, 0.0) p3 = geo.Point(0.0, 1.0, 0.0) p4 = geo.Point(0.0, 2.0, 0.0) p5 = geo.Point(0.0, 3.0, 0.0) p6 = geo.Point(0.0, 3.0, 0.0) expected = [p1, p2, p4, p5] self.assertEquals(expected, geo.Line([p1, p2, p3, p4, p5, p6]).points)
def test_valid_points(self): points = [geo.Point(170, -10), geo.Point(170, 10), geo.Point(176, 0), geo.Point(-170, -5), geo.Point(-175, -10), geo.Point(-178, -6)] poly = geo.Polygon(points) self.assertEqual(len(poly.lons), 6) self.assertEqual(len(poly.lats), 6) self.assertEqual(list(poly.lons), [170, 170, 176, -170, -175, -178]) self.assertEqual(list(poly.lats), [-10, 10, 0, -5, -10, -6]) self.assertEqual(poly.lons.dtype, 'float') self.assertEqual(poly.lats.dtype, 'float')