-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_multilinestring.py
78 lines (59 loc) · 2.86 KB
/
test_multilinestring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from . import unittest, numpy
from shapely.geos import lgeos
from shapely.geometry import LineString, MultiLineString, asMultiLineString
from shapely.geometry.base import dump_coords
class MultiLineStringTestCase(unittest.TestCase):
def test_multilinestring(self):
# From coordinate tuples
geom = MultiLineString((((1.0, 2.0), (3.0, 4.0)),))
self.assertIsInstance(geom, MultiLineString)
self.assertEqual(len(geom.geoms), 1)
self.assertEqual(dump_coords(geom), [[(1.0, 2.0), (3.0, 4.0)]])
# From lines
a = LineString(((1.0, 2.0), (3.0, 4.0)))
ml = MultiLineString([a])
self.assertEqual(len(ml.geoms), 1)
self.assertEqual(dump_coords(ml), [[(1.0, 2.0), (3.0, 4.0)]])
# From another multi-line
ml2 = MultiLineString(ml)
self.assertEqual(len(ml2.geoms), 1)
self.assertEqual(dump_coords(ml2), [[(1.0, 2.0), (3.0, 4.0)]])
# Sub-geometry Access
geom = MultiLineString([(((0.0, 0.0), (1.0, 2.0)))])
self.assertIsInstance(geom[0], LineString)
self.assertEqual(dump_coords(geom[0]), [(0.0, 0.0), (1.0, 2.0)])
with self.assertRaises(IndexError): # index out of range
geom.geoms[1]
# Geo interface
self.assertEqual(geom.__geo_interface__,
{'type': 'MultiLineString',
'coordinates': (((0.0, 0.0), (1.0, 2.0)),)})
def test_from_multilinestring_z(self):
coords1 = [(0.0, 1.0, 2.0), (3.0, 4.0, 5.0)]
coords2 = [(6.0, 7.0, 8.0), (9.0, 10.0, 11.0)]
# From coordinate tuples
ml = MultiLineString([coords1, coords2])
copy = MultiLineString(ml)
self.assertIsInstance(copy, MultiLineString)
self.assertEqual('MultiLineString',
lgeos.GEOSGeomType(copy._geom).decode('ascii'))
self.assertEqual(len(copy.geoms), 2)
self.assertEqual(dump_coords(copy.geoms[0]), coords1)
self.assertEqual(dump_coords(copy.geoms[1]), coords2)
@unittest.skipIf(not numpy, 'Numpy required')
def test_numpy(self):
from numpy import array
from numpy.testing import assert_array_equal
# Construct from a numpy array
geom = MultiLineString([array(((0.0, 0.0), (1.0, 2.0)))])
self.assertIsInstance(geom, MultiLineString)
self.assertEqual(len(geom.geoms), 1)
self.assertEqual(dump_coords(geom), [[(0.0, 0.0), (1.0, 2.0)]])
# Adapt a sequence of Numpy arrays to a multilinestring
a = [array(((1.0, 2.0), (3.0, 4.0)))]
geoma = asMultiLineString(a)
assert_array_equal(geoma.context, [array([[1., 2.], [3., 4.]])])
self.assertEqual(dump_coords(geoma), [[(1.0, 2.0), (3.0, 4.0)]])
# TODO: is there an inverse?
def test_suite():
return unittest.TestLoader().loadTestsFromTestCase(MultiLineStringTestCase)