Coverage for gws-app / gws / gis / gdalx / _test / vector_test.py: 100%
75 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-03 10:12 +0100
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-03 10:12 +0100
1"""Tests for GDAL vector data handling."""
3import gws
4import gws.test.util as u
5import gws.gis.gdalx as gdalx
6import gws.base.shape
7import gws.lib.datetimex as datetimex
8import gws.lib.crs
9import gws.lib.osx as osx
12def test_shp():
13 cols = dict(
14 c_date=gws.AttributeType.date,
15 c_float=gws.AttributeType.float,
16 c_int=gws.AttributeType.int,
17 c_str=gws.AttributeType.str,
18 )
19 crs = gws.lib.crs.require(25833)
21 recs_a = []
23 for i in range(1, 10):
24 rec = gws.FeatureRecord()
25 rec.attributes = dict(
26 c_date=datetimex.parse(f'2021-04-0{i}'),
27 c_float=i / 10,
28 c_int=i,
29 c_str=f'~D|∂|ü|Ю~{i}',
30 )
31 rec.shape = gws.base.shape.from_xy(i * 1000, i * 2000, crs)
32 recs_a.append(rec)
34 with u.temp_dir_in_base_dir() as d:
35 with gdalx.open_vector(f'{d}/shape.shp', 'w') as ds:
36 la = ds.create_layer('', cols, gws.GeometryType.point, crs)
37 la.insert(recs_a)
38 dbf = open(f'{d}/shape.dbf', 'rb').read()
39 assert recs_a[0].attributes['c_str'].encode('utf8') in dbf
41 with gdalx.open_vector(f'{d}/shape.shp', 'r') as ds:
42 la = ds.layer(0)
43 assert la is not None
44 recs_b = la.get_all()
46 assert [r.attributes for r in recs_a] == [r.attributes for r in recs_b]
47 assert [r.shape.to_ewkt() for r in recs_a] == [r.shape.to_ewkt() for r in recs_b]
50def test_shp_with_encoding():
51 cols = dict(
52 name=gws.AttributeType.str,
53 )
55 recs_a = []
57 names = [f'~D|ä|ü|ß~']
59 for name in names:
60 rec = gws.FeatureRecord()
61 rec.attributes = dict(name=name)
62 recs_a.append(rec)
64 with u.temp_dir_in_base_dir() as d:
65 with gdalx.open_vector(f'{d}/shape_iso.shp', 'w', encoding='ISO-8859-1') as ds:
66 la = ds.create_layer('', cols)
67 la.insert(recs_a)
69 dbf = open(f'{d}/shape_iso.dbf', 'rb').read()
70 assert names[0].encode('ISO-8859-1') in dbf
72 # NB: "encoding" should only be passed when no "cpg" file is present
73 osx.unlink(f'{d}/shape_iso.cpg')
75 with gdalx.open_vector(f'{d}/shape_iso.shp', 'r', encoding='ISO-8859-1') as ds:
76 la = ds.layer(0)
77 assert [r.attributes for r in recs_a] == [r.attributes for r in la.get_all()]
79 with gdalx.open_vector(f'{d}/shape_utf.shp', 'w', encoding='utf8') as ds:
80 la = ds.create_layer('', cols)
81 la.insert(recs_a)
83 dbf = open(f'{d}/shape_utf.dbf', 'rb').read()
84 assert names[0].encode('utf8') in dbf
86 # NB: "encoding" should only be passed when no "cpg" file is present
87 osx.unlink(f'{d}/shape_utf.cpg')
89 with gdalx.open_vector(f'{d}/shape_utf.shp', 'r', encoding='UTF8') as ds:
90 la = ds.layer(0)
91 assert [r.attributes for r in recs_a] == [r.attributes for r in la.get_all()]
94def test_gpkg():
95 cols = dict(
96 c_date=gws.AttributeType.date,
97 c_float=gws.AttributeType.float,
98 c_int=gws.AttributeType.int,
99 c_str=gws.AttributeType.str,
100 )
101 crs = gws.lib.crs.require(25833)
103 recs_a = []
105 for i in range(1, 10):
106 rec = gws.FeatureRecord()
107 rec.attributes = dict(
108 c_date=datetimex.parse(f'2021-04-0{i}'),
109 c_float=i / 10,
110 c_int=i,
111 c_str=f'~D|∂|ü|Ю~{i}',
112 )
113 rec.shape = gws.base.shape.from_xy(i * 1000, i * 2000, crs)
114 recs_a.append(rec)
116 with u.temp_dir_in_base_dir() as d:
117 path = f'{d}/data.gpkg'
118 with gdalx.open_vector(path, 'w') as ds:
119 la = ds.create_layer('', cols, gws.GeometryType.point, crs)
120 la.insert(recs_a)
122 with gdalx.open_vector(path, 'r') as ds:
123 la = ds.layer(0)
124 assert la is not None
125 recs_b = la.get_all()
127 assert [r.attributes for r in recs_a] == [r.attributes for r in recs_b]
128 assert [r.shape.to_ewkt() for r in recs_a] == [r.shape.to_ewkt() for r in recs_b]