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_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_long_attribute_names(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", "description": "line for testing", "description_en": "Line for testing." }, crs=LonLatWGS84) with warnings.catch_warnings(): warnings.simplefilter("ignore") line.to_shapefile(os.path.join(TESTDIR, "data/line_truncated_attr")) for fnm in ("line_truncated_attr.shx", "line_truncated_attr.shx", "line_truncated_attr.dbf", "line_truncated_attr.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) line2 = read_shapefile( os.path.join(TESTDIR, "data", "line_truncated_attr"))[0] self.assertTrue("DESCRIPTIO" in line2.properties) self.assertTrue("DESCRIPTI2" in line2.properties) self.assertTrue("GEOM_ID" in line2.properties) self.assertTrue("NAME" in line2.properties)
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_write(self): p = Line([(100.0, 0.0), (101.0, 1.0)], crs=LonLatWGS84) s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806") ans = """{ "type": "Feature", "properties": {}, "geometry": { "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "coordinates": [ [ 100.0, 0.0 ], [ 101.0, 1.0 ] ], "type": "LineString" } }""" self.verifyJson(s, ans) 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_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 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_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 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 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_long_attribute_names(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", "description": "line for testing", "description_en": "Line for testing." }, crs=LonLatWGS84) with warnings.catch_warnings(): warnings.simplefilter("ignore") line.to_shapefile(os.path.join(TESTDIR, "data/line_truncated_attr")) for fnm in ("line_truncated_attr.shx", "line_truncated_attr.shx", "line_truncated_attr.dbf", "line_truncated_attr.prj"): self.assertTrue(os.path.isfile(os.path.join(TESTDIR, "data", fnm))) line2 = read_shapefile(os.path.join(TESTDIR, "data", "line_truncated_attr"))[0] self.assertTrue("DESCRIPTIO" in line2.properties) self.assertTrue("DESCRIPTI2" in line2.properties) self.assertTrue("GEOM_ID" in line2.properties) self.assertTrue("NAME" in line2.properties)
def test_line_output(self): p = Line([(4, 2), (3, 5), (3, 2), (7, 3)]) sp = shapely.geometry.shape(p.geomdict) x, y = p.coords() sx, sy = sp.xy self.assertTrue(np.all(x == np.array(sx))) self.assertTrue(np.all(y == np.array(sy))) 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_multipoint_subset(self): mp = Multipoint(self.vertices, data=self.data) line = Line(self.vertices) ss1 = mp._subset(range(2, 7)) ss2 = line._subset(range(2, 7)) self.assertTrue(isinstance(ss1, Multipoint)) self.assertTrue(isinstance(ss2, Line)) return
def test_multipoint_subset(self): mp = Multipoint(self.vertices, data=self.data) line = Line(self.vertices) ss1 = mp._subset(range(2,7)) ss2 = line._subset(range(2,7)) self.assertTrue(isinstance(ss1, Multipoint)) self.assertTrue(isinstance(ss2, Line)) return
def test_vertices_in_crs3(self): line = Line([(2.0, 34.0), (2.15, 34.2), (2.7, 34.1)], crs=SphericalEarth) UTM31N = Proj4CRS("+proj=utm +zone=31 +ellps=WGS84 " "+datum=WGS84 +units=m +no_defs", "+ellps=WGS84") self.assertEqual(line.get_coordinate_lists(UTM31N), ((407650.39665729366, 421687.71905896586, 472328.1095127584), (3762606.6598763773, 3784658.467084308, 3773284.485241791))) 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_line_intersection2(self): # test lines that have overlapping bounding boxes, but don't cross # ----- # | ----- # | | # ----- | # ----- line0 = Line([(0.0, 0.0), (3.0, 0.0), (3.0, 3.0), (0.0, 3.0)]) line1 = Line([(1.0, 4.0), (-2.0, 4.0), (-2.0, 1.0), (1.0, 1.0)]) self.assertFalse(line0.intersects(line1)) 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_vertices_in_crs3(self): line = Line([(2.0, 34.0), (2.15, 34.2), (2.7, 34.1)], crs=LonLatWGS84) UTM31N = ProjectedCRS( "+proj=utm +zone=31 +ellps=WGS84 " "+datum=WGS84 +units=m +no_defs", "UTM 31N (WGS 84)") ans = [[407650.39665729, 3762606.65987638], [421687.71905897, 3784658.46708431], [472328.10951276, 3773284.48524179]] for v0, v1 in zip(line.get_vertices(UTM31N), ans): self.assertTupleAlmostEqual(v0, v1, places=6) return
def test_vertices_in_crs3(self): line = Line([(2.0, 34.0), (2.15, 34.2), (2.7, 34.1)], crs=SphericalEarth) UTM31N = Proj4CRS( "+proj=utm +zone=31 +ellps=WGS84 " "+datum=WGS84 +units=m +no_defs", "+ellps=WGS84") self.assertEqual( line.get_coordinate_lists(UTM31N), ((407650.39665729366, 421687.71905896586, 472328.1095127584), (3762606.6598763773, 3784658.467084308, 3773284.485241791))) return
def test_vertices_in_crs3(self): line = Line([(2.0, 34.0), (2.15, 34.2), (2.7, 34.1)], crs=LonLatWGS84) UTM31N = ProjectedCRS("+proj=utm +zone=31 +ellps=WGS84 " "+datum=WGS84 +units=m +no_defs", "UTM 31N (WGS 84)") ans = [[407650.39665729, 3762606.65987638], [421687.71905897, 3784658.46708431], [472328.10951276, 3773284.48524179]] for v0, v1 in zip(line.vertices(UTM31N), ans): self.assertTupleAlmostEqual(v0, v1, places=6) return
def test_write_reproject(self): # tests whether coordinates are correctly reprojected to WGS84 lon/lat p = Line([(1e6, 1e6), (1.2e6, 1.4e6)], crs=WebMercator) s = p.as_geojson() ans = """{ "type": "Feature", "properties": {}, "geometry": { "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, "coordinates": [[8.983152841195214, 8.946573850543412], [10.779783409434257, 12.476624651238847]], "type": "LineString" } }""" self.verifyJson(s, ans) 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_line_write(self): p = Line([(100.0, 0.0), (101.0, 1.0)]) s = self.asJsonBuffer(p, urn="urn:ogc:def:crs:EPSG::5806") ans = """{ "type": "Feature", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "properties": {}, "id": [ 0, 1 ], "geometry": { "coordinates": [ [ 100.0, 0.0 ], [ 101.0, 1.0 ] ], "type": "LineString" }, "bbox": [ [ 100.0, 101.0 ], [ 0.0, 1.0 ] ] }""" 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_line_output(self): p = Line([(4, 2), (3, 5), (3, 2), (7, 3)]) sp = shapely.geometry.shape(p.geomdict) x, y = p.coordinates sx, sy = sp.xy self.assertEqual(x, tuple(sx)) self.assertEqual(y, tuple(sy)) 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_table_attribute_int(self): g = Line(zip(range(5), range(5, 0, -1)), data={ "a": range(5), "b": [a**2 for a in range(-2, 3)] }) self.assertEqual(g.d[3], {"a": 3, "b": 1}) return
def test_segments(self): line = Line(self.vertices) for i, seg in enumerate(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
def test_table_attribute_str(self): g = Line(zip(range(5), range(5, 0, -1)), data={ "a": range(5), "b": [a**2 for a in range(-2, 3)] }) self.assertEqual(g.d["a"], list(range(5))) self.assertEqual(g.d["b"], [a**2 for a in range(-2, 3)]) 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_multiline_touching_line(self): np.random.seed(49) multiline = Multiline([ 10 * np.random.rand(10, 2) + np.random.randint(-50, 50, (1, 2)) for _ in range(50) ]) line = Line([(-30, -40), (11, -30), (10, 22), (-10, 50)]) touching = multiline.touching(line) self.assertEqual(len(touching), 4) return
def test_multipolygon_touching_line(self): np.random.seed(49) multipolygon = \ Multipolygon([[np.array([[0,0],[10,0],[10,10],[0,10]]) + np.random.randint(-50, 50, (1, 2))] for _ in range(50)]) line = Line([(-40, -35), (-15, -30), (30, 5), (10, 32), (-15, 17)]) touching = multipolygon.touching(line) self.assertEqual(len(touching), 10) return
def test_add_route(self): route = Line([(np.random.random(), np.random.random()) for i in range(10)], properties={"name": "route0"}) g = vector.gpx.GPX() g.add_route(route) expected = self.Route( [self.Point(xy, {}, {}) for xy in route.vertices], {"name": "route0"}, {}) self.assertEqual(g.routes[0], expected) 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_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_line_intersects_geographical3(self): # checks to make sure geodesics are handled line1 = Line([(-50.0, 70.0), (50.0, 70.0)], crs=SphericalEarth) line2 = Line([(0.0, 71.0), (1.0, 89.0)], crs=SphericalEarth) self.assertTrue(line1.intersects(line2)) 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
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_line_intersects_geographical4(self): # checks that coordinates are normalized line1 = Line([(-10.0, 20.0), (-30.0, 20.0)], crs=SphericalEarth) line2 = Line([(340.0, 10.0), (340.0, 30.0)], crs=SphericalEarth) self.assertTrue(line1.intersects(line2)) 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_line_intersects_geographical4(self): # catches possible bugs in handling vertical segments on sweepline line1 = Line([(-50.0, 70.0), (50.0, 70.0)], crs=SphericalEarth) line2 = Line([(0.0, 71.0), (0.0, 89.0)], crs=SphericalEarth) self.assertTrue(line1.intersects(line2)) 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
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
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_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_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