-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_linestring.py
137 lines (104 loc) · 4.83 KB
/
test_linestring.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from . import unittest, numpy
from shapely.geos import lgeos
from shapely.geometry import LineString, asLineString, Point, LinearRing
class LineStringTestCase(unittest.TestCase):
def test_linestring(self):
# From coordinate tuples
line = LineString(((1.0, 2.0), (3.0, 4.0)))
self.assertEqual(len(line.coords), 2)
self.assertEqual(line.coords[:], [(1.0, 2.0), (3.0, 4.0)])
# From Points
line2 = LineString((Point(1.0, 2.0), Point(3.0, 4.0)))
self.assertEqual(len(line2.coords), 2)
self.assertEqual(line2.coords[:], [(1.0, 2.0), (3.0, 4.0)])
# From mix of tuples and Points
line3 = LineString((Point(1.0, 2.0), (2.0, 3.0), Point(3.0, 4.0)))
self.assertEqual(len(line3.coords), 3)
self.assertEqual(line3.coords[:], [(1.0, 2.0), (2.0, 3.0), (3.0, 4.0)])
# Bounds
self.assertEqual(line.bounds, (1.0, 2.0, 3.0, 4.0))
# Coordinate access
self.assertEqual(tuple(line.coords), ((1.0, 2.0), (3.0, 4.0)))
self.assertEqual(line.coords[0], (1.0, 2.0))
self.assertEqual(line.coords[1], (3.0, 4.0))
with self.assertRaises(IndexError):
line.coords[2] # index out of range
# Geo interface
self.assertEqual(line.__geo_interface__,
{'type': 'LineString',
'coordinates': ((1.0, 2.0), (3.0, 4.0))})
# Coordinate modification
line.coords = ((-1.0, -1.0), (1.0, 1.0))
self.assertEqual(line.__geo_interface__,
{'type': 'LineString',
'coordinates': ((-1.0, -1.0), (1.0, 1.0))})
# Adapt a coordinate list to a line string
coords = [[5.0, 6.0], [7.0, 8.0]]
la = asLineString(coords)
self.assertEqual(la.coords[:], [(5.0, 6.0), (7.0, 8.0)])
# Test Non-operability of Null geometry
l_null = LineString()
self.assertEqual(l_null.wkt, 'GEOMETRYCOLLECTION EMPTY')
self.assertEqual(l_null.length, 0.0)
# Check that we can set coordinates of a null geometry
l_null.coords = [(0, 0), (1, 1)]
self.assertAlmostEqual(l_null.length, 1.4142135623730951)
def test_from_linestring(self):
line = LineString(((1.0, 2.0), (3.0, 4.0)))
copy = LineString(line)
self.assertEqual(len(copy.coords), 2)
self.assertEqual(copy.coords[:], [(1.0, 2.0), (3.0, 4.0)])
self.assertEqual('LineString',
lgeos.GEOSGeomType(copy._geom).decode('ascii'))
def test_from_linestring_z(self):
coords = [(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)]
line = LineString(coords)
copy = LineString(line)
self.assertEqual(len(copy.coords), 2)
self.assertEqual(copy.coords[:], coords)
self.assertEqual('LineString',
lgeos.GEOSGeomType(copy._geom).decode('ascii'))
def test_from_linearring(self):
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
ring = LinearRing(coords)
copy = LineString(ring)
self.assertEqual(len(copy.coords), 4)
self.assertEqual(copy.coords[:], coords)
self.assertEqual('LineString',
lgeos.GEOSGeomType(copy._geom).decode('ascii'))
@unittest.skipIf(not numpy, 'Numpy required')
def test_numpy(self):
from numpy import array, asarray
from numpy.testing import assert_array_equal
# Construct from a numpy array
line = LineString(array([[0.0, 0.0], [1.0, 2.0]]))
self.assertEqual(len(line.coords), 2)
self.assertEqual(line.coords[:], [(0.0, 0.0), (1.0, 2.0)])
line = LineString(((1.0, 2.0), (3.0, 4.0)))
la = asarray(line)
expected = array([[1.0, 2.0], [3.0, 4.0]])
assert_array_equal(la, expected)
# Coordinate sequences can be adapted as well
la = asarray(line.coords)
assert_array_equal(la, expected)
# Adapt a Numpy array to a line string
a = array([[1.0, 2.0], [3.0, 4.0]])
la = asLineString(a)
assert_array_equal(la.context, a)
self.assertEqual(la.coords[:], [(1.0, 2.0), (3.0, 4.0)])
# Now, the inverse
self.assertEqual(la.__array_interface__,
la.context.__array_interface__)
pas = asarray(la)
assert_array_equal(pas, array([[1.0, 2.0], [3.0, 4.0]]))
# From Array.txt
a = asarray([[0.0, 0.0], [2.0, 2.0], [1.0, 1.0]])
line = LineString(a)
self.assertEqual(line.coords[:], [(0.0, 0.0), (2.0, 2.0), (1.0, 1.0)])
data = line.ctypes
self.assertEqual(data[0], 0.0)
self.assertEqual(data[5], 1.0)
b = asarray(line)
assert_array_equal(b, array([[0., 0.], [2., 2.], [1., 1.]]))
def test_suite():
return unittest.TestLoader().loadTestsFromTestCase(LineStringTestCase)