-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_point.py
139 lines (109 loc) · 4.37 KB
/
test_point.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
138
139
from . import unittest, numpy
from shapely.geometry import Point, asPoint
from shapely.geos import DimensionError
class LineStringTestCase(unittest.TestCase):
def test_point(self):
# Test 2D points
p = Point(1.0, 2.0)
self.assertEqual(p.x, 1.0)
self.assertEqual(p.y, 2.0)
self.assertEqual(p.coords[:], [(1.0, 2.0)])
self.assertEqual(str(p), p.wkt)
self.assertFalse(p.has_z)
with self.assertRaises(DimensionError):
p.z
# Check 3D
p = Point(1.0, 2.0, 3.0)
self.assertEqual(p.coords[:], [(1.0, 2.0, 3.0)])
self.assertEqual(str(p), p.wkt)
self.assertTrue(p.has_z)
self.assertEqual(p.z, 3.0)
# From coordinate sequence
p = Point((3.0, 4.0))
self.assertEqual(p.coords[:], [(3.0, 4.0)])
# From another point
q = Point(p)
self.assertEqual(q.coords[:], [(3.0, 4.0)])
# Coordinate access
self.assertEqual(p.x, 3.0)
self.assertEqual(p.y, 4.0)
self.assertEqual(tuple(p.coords), ((3.0, 4.0),))
self.assertEqual(p.coords[0], (3.0, 4.0))
with self.assertRaises(IndexError): # index out of range
p.coords[1]
# Bounds
self.assertEqual(p.bounds, (3.0, 4.0, 3.0, 4.0))
# Geo interface
self.assertEqual(p.__geo_interface__,
{'type': 'Point', 'coordinates': (3.0, 4.0)})
# Modify coordinates
p.coords = (2.0, 1.0)
self.assertEqual(p.__geo_interface__,
{'type': 'Point', 'coordinates': (2.0, 1.0)})
# Alternate method
p.coords = ((0.0, 0.0),)
self.assertEqual(p.__geo_interface__,
{'type': 'Point', 'coordinates': (0.0, 0.0)})
# Adapt a coordinate list to a point
coords = [3.0, 4.0]
pa = asPoint(coords)
self.assertEqual(pa.coords[0], (3.0, 4.0))
self.assertEqual(pa.distance(p), 5.0)
# Move the coordinates and watch the distance change
coords[0] = 1.0
self.assertEqual(pa.coords[0], (1.0, 4.0))
self.assertAlmostEqual(pa.distance(p), 4.123105625617661)
# Test Non-operability of Null geometry
p_null = Point()
self.assertEqual(p_null.wkt, 'GEOMETRYCOLLECTION EMPTY')
self.assertEqual(p_null.coords[:], [])
self.assertEqual(p_null.area, 0.0)
# Check that we can set coordinates of a null geometry
p_null.coords = (1, 2)
self.assertEqual(p_null.coords[:], [(1.0, 2.0)])
@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
p = Point(array([1.0, 2.0]))
self.assertEqual(p.coords[:], [(1.0, 2.0)])
# Adapt a Numpy array to a point
a = array([1.0, 2.0])
pa = asPoint(a)
assert_array_equal(pa.context, array([1.0, 2.0]))
self.assertEqual(pa.coords[:], [(1.0, 2.0)])
# Now, the inverse
self.assertEqual(pa.__array_interface__,
pa.context.__array_interface__)
pas = asarray(pa)
assert_array_equal(pas, array([1.0, 2.0]))
# Adapt a coordinate list to a point
coords = [3.0, 4.0]
pa = asPoint(coords)
coords[0] = 1.0
# Now, the inverse (again?)
self.assertIsNotNone(pa.__array_interface__)
pas = asarray(pa)
assert_array_equal(pas, array([1.0, 4.0]))
# From Array.txt
p = Point(0.0, 0.0, 1.0)
coords = p.coords[0]
self.assertEqual(coords, (0.0, 0.0, 1.0))
self.assertIsNotNone(p.ctypes)
# Convert to Numpy array, passing through Python sequence
a = asarray(coords)
self.assertEqual(a.ndim, 1)
self.assertEqual(a.size, 3)
self.assertEqual(a.shape, (3,))
# Convert to Numpy array, passing through a ctypes array
b = asarray(p)
self.assertEqual(b.size, 3)
self.assertEqual(b.shape, (3,))
assert_array_equal(b, array([0.0, 0.0, 1.0]))
# Make a point from a Numpy array
a = asarray([1.0, 1.0, 0.0])
p = Point(*list(a))
self.assertEqual(p.coords[:], [(1.0, 1.0, 0.0)])
def test_suite():
return unittest.TestLoader().loadTestsFromTestCase(LineStringTestCase)