Coverage for gws-app/gws/lib/gml/_test/parser_test.py: 100%
68 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
1"""Tests for the parser module"""
3import gws
4import gws.lib.crs
5import gws.lib.gml.parser as parser
6import gws.test.util as u
7import gws.lib.xmlx
10def test_parse_envelope_wgs():
11 box = gws.lib.xmlx.from_string("""
12 <root xmlns:gml="http://www.opengis.net/gml">
13 <gml:Box>
14 <gml:coordinates>1,2 3,4</gml:coordinates>
15 </gml:Box>
16 </root>
17 """)
18 wgs = gws.lib.crs.WGS84
19 p = parser.parse_envelope(box.findfirst(), wgs, False)
20 assert p.crs == wgs
21 assert p.extent == (2.0, 1.0, 4.0, 3.0)
24def test_parse_envelope():
25 box = gws.lib.xmlx.from_string("""
26 <root xmlns:gml="http://www.opengis.net/gml">
27 <gml:Box srsName="EPSG:3857">
28 <gml:coordinates>1,2 3,4</gml:coordinates>
29 </gml:Box>
30 </root>
31 """)
32 wm = gws.lib.crs.WEBMERCATOR
33 p = parser.parse_envelope(box.findfirst())
34 assert p.crs == wm
35 assert p.extent == (1.0, 2.0, 3.0, 4.0)
38def test_parse_envelope_always_xy():
39 box = gws.lib.xmlx.from_string("""
40 <root xmlns:gml="http://www.opengis.net/gml">
41 <gml:Envelope srsDimension="2">
42 <gml:lowerCorner>1 2</gml:lowerCorner>
43 <gml:upperCorner>3 4</gml:upperCorner>
44 </gml:Envelope>
45 </root>
46 """)
47 wgs = gws.lib.crs.WGS84
49 p = parser.parse_envelope(box.findfirst(), wgs)
50 assert p.crs == wgs
51 assert p.extent == (2.0, 1.0, 4.0, 3.0)
54def test_parse_envelope_gml3():
55 box = gws.lib.xmlx.from_string("""
56 <root xmlns:gml="http://www.opengis.net/gml">
57 <gml:Envelope srsDimension="2" srsName="EPSG:3857">
58 <gml:lowerCorner>1 2</gml:lowerCorner>
59 <gml:upperCorner>3 4</gml:upperCorner>
60 </gml:Envelope>
61 </root>
62 """)
63 wm = gws.lib.crs.WEBMERCATOR
64 p = parser.parse_envelope(box.findfirst())
65 assert p.crs == wm
66 assert p.extent == (1.0, 2.0, 3.0, 4.0)
69def test_parse_envelope_no_crs():
70 box = gws.lib.xmlx.from_string("""
71 <root xmlns:gml="http://www.opengis.net/gml">
72 <gml:Box>
73 <gml:coordinates>1,2 3,4</gml:coordinates>
74 </gml:Box>
75 </root>
76 """)
77 with u.raises(Exception):
78 parser.parse_envelope(box.findfirst())
81def test_parse_envelope_no_envelope():
82 box = gws.lib.xmlx.from_string("""
83 <root xmlns:gml="http://www.opengis.net/gml">
84 <gml:Foo srsName="EPSG:3857">
85 <gml:coordinates>1,2 3,4</gml:coordinates>
86 </gml:Foo>
87 </root>
88 """)
89 with u.raises(Exception):
90 parser.parse_envelope(box.findfirst())
93def test_is_geometry_element():
94 element = gws.lib.xmlx.from_string("""
95 <root xmlns:gml="http://www.opengis.net/gml">
96 <gml:Point>
97 <gml:coordinates>100,200</gml:coordinates>
98 </gml:Point>
99 </root>
100 """)
101 assert parser.is_geometry_element(element.findfirst())
104def test_is_not_geometry_element():
105 element = gws.lib.xmlx.from_string("""
106 <root xmlns:gml="http://www.opengis.net/gml">
107 <gml:point>
108 <gml:coordinates>100,200</gml:coordinates>
109 </gml:point>
110 </root>
111 """)
112 assert not parser.is_geometry_element(element)
115##########
116def test_parse_shape():
117 shape = gws.lib.xmlx.from_string("""
118 <root xmlns:gml="http://www.opengis.net/gml">
119 <gml:Point srsName="EPSG:3857">
120 <gml:coordinates>100,200</gml:coordinates>
121 </gml:Point>
122 </root>
123 """)
124 p = parser.parse_shape(shape.findfirst())
125 assert p.x == 100 and p.y == 200
128def test_parse_shape_no_crs():
129 shape = gws.lib.xmlx.from_string("""
130 <root xmlns:gml="http://www.opengis.net/gml">
131 <gml:Point>
132 <gml:coordinates>100,200</gml:coordinates>
133 </gml:Point>
134 </root>
135 """)
136 with u.raises(Exception):
137 parser.parse_shape(shape.findfirst())
140def test_parse_shape_wgs():
141 shape = gws.lib.xmlx.from_string("""
142 <root xmlns:gml="http://www.opengis.net/gml">
143 <gml:Point>
144 <gml:coordinates>100,200</gml:coordinates>
145 </gml:Point>
146 </root>
147 """)
148 wgs = gws.lib.crs.WGS84
149 p = parser.parse_shape(shape.findfirst(), wgs)
150 assert p.x == 200 and p.y == 100
153def test_parse_shape_always_xy():
154 shape = gws.lib.xmlx.from_string("""
155 <root xmlns:gml="http://www.opengis.net/gml">
156 <gml:Point>
157 <gml:coordinates>100,200</gml:coordinates>
158 </gml:Point>
159 </root>
160 """)
161 wgs = gws.lib.crs.WGS84
162 p = parser.parse_shape(shape.findfirst(), wgs, always_xy=True)
163 assert p.x == 100 and p.y == 200
166def test_parse_geometry():
167 shape = gws.lib.xmlx.from_string("""
168 <root xmlns:gml="http://www.opengis.net/gml">
169 <gml:Polygon>
170 <gml:outerBoundaryIs>
171 <gml:LinearRing>
172 <gml:coordinates>0,0 100,0 100,100 0,100 0,0</gml:coordinates>
173 </gml:LinearRing>
174 </gml:outerBoundaryIs>
175 </gml:Polygon>
176 </root>
177 """)
178 assert parser.parse_geometry(shape.findfirst()) == {
179 'coordinates': [
180 [[0.0, 0.0], [100.0, 0.0], [100.0, 100.0], [0.0, 100.0], [0.0, 0.0]],
181 ],
182 'type': 'Polygon',
183 }
186def test_parse_geometry_unknown_type():
187 shape = gws.lib.xmlx.from_string("""
188 <root xmlns:gml="http://www.opengis.net/gml">
189 <gml:Points>
190 <gml:pointMember>
191 <gml:Point>
192 <gml:coordinates>100.0, 0.0</gml:coordinates>
193 </gml:Point>
194 </gml:pointMember>
195 <gml:pointMember>
196 <gml:Point>
197 <gml:coordinates>101.0, 1.0</gml:coordinates>
198 </gml:Point>
199 </gml:pointMember>
200 </gml:Points>
201 </root>
202 """)
203 with u.raises(Exception):
204 parser.parse_geometry(shape.findfirst())