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
140
141
142
143
144
145
146
147
148
149
|
import math
from unittest import skipIf
from unittest.mock import patch
from django.contrib.gis.geos import GEOSGeometry, LineString
from django.contrib.gis.geos import prototypes as capi
from django.contrib.gis.geos.coordseq import GEOSCoordSeq
from django.contrib.gis.geos.libgeos import geos_version_tuple
from django.test import SimpleTestCase
class GEOSCoordSeqTest(SimpleTestCase):
def test_getitem(self):
coord_seq = LineString([(x, x) for x in range(2)]).coord_seq
for i in (0, 1):
with self.subTest(i):
self.assertEqual(coord_seq[i], (i, i))
for i in (-3, 10):
msg = f"Invalid GEOS Geometry index: {i}"
with self.subTest(i):
with self.assertRaisesMessage(IndexError, msg):
coord_seq[i]
@skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
def test_has_m(self):
geom = GEOSGeometry("POINT ZM (1 2 3 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
self.assertIs(coord_seq.hasm, True)
geom = GEOSGeometry("POINT Z (1 2 3)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
self.assertIs(coord_seq.hasm, False)
geom = GEOSGeometry("POINT M (1 2 3)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
self.assertIs(coord_seq.hasm, True)
@skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
def test_get_set_m(self):
geom = GEOSGeometry("POINT ZM (1 2 3 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
self.assertEqual(coord_seq.tuple, (1, 2, 3, 4))
self.assertEqual(coord_seq.getM(0), 4)
coord_seq.setM(0, 10)
self.assertEqual(coord_seq.tuple, (1, 2, 3, 10))
self.assertEqual(coord_seq.getM(0), 10)
geom = GEOSGeometry("POINT M (1 2 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
self.assertEqual(coord_seq.tuple, (1, 2, 4))
self.assertEqual(coord_seq.getM(0), 4)
coord_seq.setM(0, 10)
self.assertEqual(coord_seq.tuple, (1, 2, 10))
self.assertEqual(coord_seq.getM(0), 10)
self.assertIs(math.isnan(coord_seq.getZ(0)), True)
@skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
def test_setitem(self):
geom = GEOSGeometry("POINT ZM (1 2 3 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
coord_seq[0] = (10, 20, 30, 40)
self.assertEqual(coord_seq.tuple, (10, 20, 30, 40))
geom = GEOSGeometry("POINT M (1 2 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
coord_seq[0] = (10, 20, 40)
self.assertEqual(coord_seq.tuple, (10, 20, 40))
self.assertEqual(coord_seq.getM(0), 40)
self.assertIs(math.isnan(coord_seq.getZ(0)), True)
@skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
def test_kml_m_dimension(self):
geom = GEOSGeometry("POINT ZM (1 2 3 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
self.assertEqual(coord_seq.kml, "<coordinates>1.0,2.0,3.0</coordinates>")
geom = GEOSGeometry("POINT M (1 2 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
self.assertEqual(coord_seq.kml, "<coordinates>1.0,2.0,0</coordinates>")
@skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
def test_clone_m_dimension(self):
geom = GEOSGeometry("POINT ZM (1 2 3 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
clone = coord_seq.clone()
self.assertEqual(clone.tuple, (1, 2, 3, 4))
self.assertIs(clone.hasz, True)
self.assertIs(clone.hasm, True)
geom = GEOSGeometry("POINT M (1 2 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
clone = coord_seq.clone()
self.assertEqual(clone.tuple, (1, 2, 4))
self.assertIs(clone.hasz, False)
self.assertIs(clone.hasm, True)
@skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
def test_dims(self):
geom = GEOSGeometry("POINT ZM (1 2 3 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
self.assertEqual(coord_seq.dims, 4)
geom = GEOSGeometry("POINT M (1 2 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
self.assertEqual(coord_seq.dims, 3)
geom = GEOSGeometry("POINT Z (1 2 3)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
self.assertEqual(coord_seq.dims, 3)
geom = GEOSGeometry("POINT (1 2)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
self.assertEqual(coord_seq.dims, 2)
def test_size(self):
geom = GEOSGeometry("POINT (1 2)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
self.assertEqual(coord_seq.size, 1)
geom = GEOSGeometry("POINT M (1 2 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
self.assertEqual(coord_seq.size, 1)
@skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
def test_iscounterclockwise(self):
geom = GEOSGeometry("LINEARRING ZM (0 0 3 0, 1 0 0 2, 0 1 1 3, 0 0 3 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
self.assertEqual(
coord_seq.tuple,
(
(0.0, 0.0, 3.0, 0.0),
(1.0, 0.0, 0.0, 2.0),
(0.0, 1.0, 1.0, 3.0),
(0.0, 0.0, 3.0, 4.0),
),
)
self.assertIs(coord_seq.is_counterclockwise, True)
def test_m_support_error(self):
geom = GEOSGeometry("POINT M (1 2 4)")
coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
msg = "GEOSCoordSeq with an M dimension requires GEOS 3.14+."
# mock geos_version_tuple to be 3.13.13
with patch(
"django.contrib.gis.geos.coordseq.geos_version_tuple",
return_value=(3, 13, 13),
):
with self.assertRaisesMessage(NotImplementedError, msg):
coord_seq.hasm
|