@@ -62,16 +62,44 @@ def test_geobuf():
6262 print (str2geojson2str (json .dumps (geojson ), indent = True , sort_keys = True ))
6363
6464 # precision: 6(default), 7, 8(recommand), 9
65+ encoder = Encoder ()
66+ assert encoder .max_precision () == 10 ** 6
67+ assert not encoder .only_xy ()
68+ assert encoder .round_z () is None
69+
6570 encoder = Encoder (max_precision = int (10 ** 8 ))
71+ assert encoder .max_precision () == 10 ** 8
6672 encoded = encoder .encode (geojson = json .dumps (geojson ))
6773 print ("encoded pbf bytes" )
6874 print (pbf_decode (encoded ))
6975
76+ encoder = Encoder (round_z = 3 )
77+ assert encoder .round_z () == 3
78+
79+ with pytest .raises (Exception ) as excinfo :
80+ Encoder (max_precision = int (10 ** 10 )) # uint32_t overflow
81+ assert "incompatible constructor arguments" in repr (excinfo )
82+
7083 decoder = Decoder ()
7184 geojson_text = decoder .decode (encoded , indent = True )
7285 print (geojson_text )
7386
7487
88+ def test_geobuf_roundz ():
89+ f = geojson .Feature ().from_rapidjson (sample_geojson ())
90+ llas0 = geojson .Feature ().from_geobuf (f .to_geobuf ()).to_numpy ()
91+
92+ llas = geojson .Feature ().from_geobuf (f .to_geobuf (precision = 3 )).to_numpy ()
93+ assert np .all (np .round (llas , 3 ) == llas )
94+
95+ llas = geojson .Feature ().from_geobuf (f .to_geobuf (only_xy = True )).to_numpy ()
96+ assert np .all (llas [:, 2 ] == np .zeros (len (llas )))
97+
98+ llas = geojson .Feature ().from_geobuf (f .to_geobuf (round_z = 3 )).to_numpy ()
99+ assert np .all (np .round (llas0 [:, :2 ], 8 ) == llas [:, :2 ])
100+ assert np .all (np .round (llas0 [:, 2 ], 3 ) == llas [:, 2 ])
101+
102+
75103def test_rapidjson_empty ():
76104 assert not bool (rapidjson ())
77105 assert not bool (rapidjson ([]))
@@ -247,6 +275,28 @@ def test_rapidjson_sort_dump():
247275 assert obj6 == obj5
248276
249277
278+ def test_rapidjson_round ():
279+ arr = rapidjson ([1.23456 , [3.2 , 2.345 ]])
280+ assert arr () == [1.23456 , [3.2 , 2.345 ]]
281+ arr .round ()
282+ assert arr () == [1.235 , [3.2 , 2.345 ]]
283+ arr .round (precision = 2 )
284+ assert arr () == [1.24 , [3.2 , 2.35 ]]
285+
286+ obj = rapidjson (sample_geojson ())
287+ assert obj () == sample_geojson ()
288+ assert (
289+ obj .clone ().round (precision = 1 ).dumps (sort_keys = True )
290+ == '{"geometry":{"coordinates":[[120.4,31.4,1.1],[120.3,31.3,2.2],[120.4,31.2,3.3],[120.7,31.3,4.4]],"extra_key":"extra_value","type":"LineString"},"my_key":"my_value","properties":{"dict":{"key":42,"value":3.1},"double":3.1,"int":42,"int2":-101,"list":["a","list","is","a","list"],"string":"string"},"type":"Feature"}' # noqa
291+ )
292+ assert (
293+ obj .clone ()
294+ .round (precision = 1 , skip_keys = ["geometry" ])
295+ .dumps (sort_keys = True ) # noqa
296+ == '{"geometry":{"coordinates":[[120.40317479950272,31.416966084052177,1.111111],[120.28451900911591,31.30578266928819,2.22],[120.35592249359615,31.21781895672254,3.3333333333333],[120.67093786630113,31.299502266522722,4.4]],"extra_key":"extra_value","type":"LineString"},"my_key":"my_value","properties":{"dict":{"key":42,"value":3.1},"double":3.1,"int":42,"int2":-101,"list":["a","list","is","a","list"],"string":"string"},"type":"Feature"}' # noqa
297+ )
298+
299+
250300def test_geojson_point ():
251301 # as_numpy
252302 g1 = geojson .Point ()
0 commit comments