Coverage for gws-app/gws/base/shape/_test.py: 100%
142 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
1import gws
2import gws.test.util as u
3import gws.base.shape as shape
4import gws.lib.crs as crs
5import shapely.wkt
6import shapely.wkb
7import geoalchemy2.shape
10def test_from_wkt():
11 default_crs = crs.WGS84
12 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
13 assert shape.from_wkt(wkt, default_crs).geom == shapely.wkt.loads(wkt)
14 assert shape.from_wkt(wkt, default_crs).crs == default_crs
15 assert shape.from_wkt(wkt, default_crs).type == 'polygon'
16 assert not shape.from_wkt(wkt, default_crs).x
17 assert not shape.from_wkt(wkt, default_crs).y
20def test_from_wkt_raises():
21 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
22 default_crs = None
23 with u.raises(Exception):
24 shape.from_wkt(wkt, default_crs)
27def test_from_ewkt():
28 ewkt = 'SRID=4326;POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
29 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
30 assert shape.from_wkt(ewkt).geom == shapely.wkt.loads(wkt)
31 assert shape.from_wkt(ewkt).crs.epsg == 'EPSG:4326'
32 assert shape.from_wkt(ewkt).type == 'polygon'
33 assert not shape.from_wkt(ewkt).x
34 assert not shape.from_wkt(ewkt).y
37def test_from_wkb():
38 default_crs = crs.WGS84
39 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
40 wkb = shapely.wkb.dumps(shapely.from_wkt(wkt))
41 assert shape.from_wkb(wkb, default_crs).geom == shapely.wkb.loads(wkb)
42 assert shape.from_wkb(wkb, default_crs).crs == default_crs
43 assert shape.from_wkb(wkb, default_crs).type == 'polygon'
44 assert not shape.from_wkb(wkb, default_crs).x
45 assert not shape.from_wkb(wkb, default_crs).y
48def test_from_wkb_raises():
49 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
50 wkb = shapely.wkb.dumps(shapely.from_wkt(wkt))
51 default_crs = None
52 with u.raises(Exception):
53 shape.from_wkb(wkb, default_crs)
56def test_from_ewkb():
57 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
58 ewkb = shapely.wkb.dumps(shapely.from_wkt(wkt), srid=4326)
59 assert shape.from_wkb(ewkb).geom == shapely.wkb.loads(ewkb)
60 assert shape.from_wkb(ewkb).crs.epsg == 'EPSG:4326'
61 assert shape.from_wkb(ewkb).type == 'polygon'
62 assert not shape.from_wkb(ewkb).x
63 assert not shape.from_wkb(ewkb).y
66WKB_HEX = (
67 "0103000000010000000500000000000000000000000000000000000000000000000000F03F000000000000F03F"
68 "000000000000F03F00000000000000000000000000000000000000000000"
69)
70EWKB_HEX = (
71 "0103000020E6100000010000000500000000000000000000000000000000000000000000000000F03F00000000"
72 "0000F03F000000000000F03F00000000000000000000000000000000000000000000"
73)
76def test_from_wkb_hex():
77 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
78 wkb_hex = shapely.wkb.dumps(shapely.from_wkt(wkt), hex=True)
79 default_crs = crs.WGS84
80 assert shape.from_wkb_hex(wkb_hex, default_crs).geom == shapely.wkb.loads(bytes.fromhex(wkb_hex))
81 assert shape.from_wkb_hex(wkb_hex, default_crs).crs == default_crs
82 assert shape.from_wkb_hex(wkb_hex, default_crs).type == 'polygon'
83 assert not shape.from_wkb_hex(wkb_hex, default_crs).x
84 assert not shape.from_wkb_hex(wkb_hex, default_crs).y
87def test_from_wkb_hex_raises():
88 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
89 wkb_hex = shapely.wkb.dumps(shapely.from_wkt(wkt), hex=True)
90 default_crs = None
91 with u.raises(Exception):
92 shape.from_wkb_hex(wkb_hex, default_crs)
95def test_from_ewkb_hex():
96 wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
97 ewkb_hex = shapely.wkb.dumps(shapely.from_wkt(wkt), hex=True, srid=4326)
98 assert shape.from_wkb_hex(ewkb_hex).geom == shapely.wkb.loads(bytes.fromhex(ewkb_hex))
99 assert shape.from_wkb_hex(ewkb_hex).crs.epsg == 'EPSG:4326'
100 assert shape.from_wkb_hex(ewkb_hex).type == 'polygon'
101 assert not shape.from_wkb_hex(ewkb_hex).x
102 assert not shape.from_wkb_hex(ewkb_hex).y
105# def test_from_wkb_element():
106# wkt = 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'
107# default_crs = crs.WGS84
108# wkb_hex = shapely.wkb.dumps(shapely.from_wkt(wkt), hex=True, srid=4326)
109# shp= shapely.wkb.loads(bytes.fromhex(wkb_hex))
110# element = geoalchemy2.shape.from_shape(shp)
111# assert shape.from_wkb_element(element, default_crs).geom == shp
112# assert not shape.from_wkb_element(element).crs.epsg
113# assert shape.from_wkb_element(element).type == 'polygon'
114# assert not shape.from_wkb_element(element).x
115# assert not shape.from_wkb_element(element).y
117def test_from_geojson():
118 geojson = {"type": "Polygon",
119 "coordinates": [
120 [
121 [0.0, 0.0],
122 [0.0, 1.0],
123 [1.0, 1.0],
124 [1.0, 0.0],
125 [0.0, 0.0]
126 ]
127 ]}
128 geojson_str = '''{"type": "Polygon",
129 "coordinates": [
130 [
131 [0.0, 0.0],
132 [1.0, 0.0],
133 [1.0, 1.0],
134 [0.0, 1.0],
135 [0.0, 0.0]
136 ]
137 ]}'''
138 default_crs = crs.WGS84
139 assert shape.from_geojson(geojson, default_crs).geom == shapely.from_geojson(geojson_str)
140 assert shape.from_geojson(geojson, default_crs).crs.epsg == 'EPSG:4326'
141 assert shape.from_geojson(geojson, default_crs).type == 'polygon'
142 assert not shape.from_geojson(geojson, default_crs).x
143 assert not shape.from_geojson(geojson, default_crs).y
146def test_from_geojson_xy():
147 geojson = {"type": "Polygon",
148 "coordinates": [
149 [
150 [0.0, 0.0],
151 [0.0, 1.0],
152 [1.0, 1.0],
153 [1.0, 0.0],
154 [0.0, 0.0]
155 ]
156 ]}
157 geojson_str = '''{"type": "Polygon",
158 "coordinates": [
159 [
160 [0.0, 0.0],
161 [0.0, 1.0],
162 [1.0, 1.0],
163 [1.0, 0.0],
164 [0.0, 0.0]
165 ]
166 ]}'''
167 default_crs = crs.WGS84
168 assert shape.from_geojson(geojson, default_crs, always_xy=True).geom == shapely.from_geojson(geojson_str)
169 assert shape.from_geojson(geojson, default_crs).crs.epsg == 'EPSG:4326'
170 assert shape.from_geojson(geojson, default_crs).type == 'polygon'
171 assert not shape.from_geojson(geojson, default_crs).x
172 assert not shape.from_geojson(geojson, default_crs).y
175def test_from_props():
176 geojson = {"type": "Polygon",
177 "coordinates": [
178 [
179 [0.0, 0.0],
180 [0.0, 1.0],
181 [1.0, 1.0],
182 [1.0, 0.0],
183 [0.0, 0.0]
184 ]
185 ]}
186 geojson_str = '''{"type": "Polygon",
187 "coordinates": [
188 [
189 [0.0, 0.0],
190 [0.0, 1.0],
191 [1.0, 1.0],
192 [1.0, 0.0],
193 [0.0, 0.0]
194 ]
195 ]}'''
196 d = {'crs': '4326', 'geometry': geojson}
197 data = gws.Data(d)
198 props = gws.Props(data)
199 assert shape.from_props(props).geom == shapely.from_geojson(geojson_str)
200 assert shape.from_props(props).crs.epsg == 'EPSG:4326'
201 assert shape.from_props(props).type == 'polygon'
202 assert not shape.from_props(props).x
203 assert not shape.from_props(props).y
206def test_from_props_raises():
207 d = {'crs': None}
208 data = gws.Data(d)
209 props = gws.Props(data)
210 with u.raises(Exception):
211 shape.from_props(props)
214def test_from_dict():
215 geojson = {"type": "Polygon",
216 "coordinates": [
217 [
218 [0.0, 0.0],
219 [0.0, 1.0],
220 [1.0, 1.0],
221 [1.0, 0.0],
222 [0.0, 0.0]
223 ]
224 ]}
225 geojson_str = '''{"type": "Polygon",
226 "coordinates": [
227 [
228 [0.0, 0.0],
229 [0.0, 1.0],
230 [1.0, 1.0],
231 [1.0, 0.0],
232 [0.0, 0.0]
233 ]
234 ]}'''
235 d = {'crs': '4326',
236 'geometry': geojson}
237 assert shape.from_dict(d).geom == shapely.from_geojson(geojson_str)
238 assert shape.from_dict(d).crs.epsg == 'EPSG:4326'
239 assert shape.from_dict(d).type == 'polygon'
240 assert not shape.from_dict(d).x
241 assert not shape.from_dict(d).y
244def test_from_dict_raises():
245 d = {'crs': None}
246 with u.raises(Exception):
247 shape.from_dict(d)
250def test_from_from_bounds():
251 geojson_str = '''{"type": "Polygon",
252 "coordinates": [
253 [
254 [1.0, 0.0],
255 [1.0, 1.0],
256 [0.0, 1.0],
257 [0.0, 0.0],
258 [1.0, 0.0]
259 ]
260 ]}'''
261 c = crs.WGS84
262 extent = gws.Extent((0.0, 0.0, 1.0, 1.0))
263 bounds = gws.Bounds(crs=c, extent=extent)
264 assert shape.from_bounds(bounds).geom == shapely.from_geojson(geojson_str)
265 assert shape.from_bounds(bounds).crs.epsg == 'EPSG:4326'
266 assert shape.from_bounds(bounds).type == 'polygon'
267 assert not shape.from_bounds(bounds).x
268 assert not shape.from_bounds(bounds).y
271def test_from_xy():
272 geojson_str = '''{"type": "Point",
273 "coordinates": [1.0, 1.0]}'''
274 c = crs.WGS84
275 assert shape.from_xy(1.0, 1.0, c).geom == shapely.from_geojson(geojson_str)
276 assert shape.from_xy(1.0, 1.0, c).crs.epsg == 'EPSG:4326'
277 assert shape.from_xy(1.0, 1.0, c).type == 'point'
278 assert shape.from_xy(1.0, 1.0, c).x == 1.0
279 assert shape.from_xy(1.0, 1.0, c).y == 1.0