Skip to content

Commit e50e6e3

Browse files
committed
chg: wkb encoding
1 parent 5d0b3db commit e50e6e3

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/gps_logger_parser/parser_base.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from contextlib import contextmanager
66

77
import geoarrow.pandas as _ # noqa: F401
8+
import geoarrow.pyarrow as ga
89
import pandas as pd
910
import pyarrow as pa
1011
import pyarrow.csv as pacsv
@@ -124,7 +125,7 @@ def get_harmonization_schema(self) -> dict:
124125
"Subclasses must implement get_harmonization_schema()"
125126
)
126127

127-
def as_table(self) -> pa.Table:
128+
def as_table(self, geometry_encoding: str = "wkb") -> pa.Table:
128129
# Build JSON array directly from row iteration to avoid materializing
129130
# both a list-of-dicts and a list-of-JSON-strings simultaneously
130131
original_json = pa.array(
@@ -142,6 +143,14 @@ def as_table(self) -> pa.Table:
142143

143144
table = pa.Table.from_pandas(harmonized_data, preserve_index=False)
144145

146+
if geometry_encoding == "wkb" and "geometry" in table.column_names:
147+
geometry_index = table.column_names.index("geometry")
148+
table = table.set_column(
149+
geometry_index,
150+
"geometry",
151+
ga.as_wkb(table.column("geometry")),
152+
)
153+
145154
table = table.append_column(
146155
"_original_data",
147156
original_json,
@@ -158,16 +167,16 @@ def as_table(self) -> pa.Table:
158167
)
159168
return table
160169

161-
def write_parquet(self, path: pathlib.Path, filename: str | None = None):
170+
def write_parquet(self, path: pathlib.Path, filename: str | None = None, **kwargs):
162171
if filename:
163172
filename = pathlib.Path(filename)
164173
else:
165174
filename = self.file._file_path.name
166175

167-
pq.write_table(self.as_table(), str(path / f"{filename}.parquet"))
176+
pq.write_table(self.as_table(**kwargs), str(path / f"{filename}.parquet"))
168177

169-
def write_csv(self, path):
170-
pacsv.write_csv(self.as_table(), str(path))
178+
def write_csv(self, path, **kwargs):
179+
pacsv.write_csv(self.as_table(**kwargs), str(path))
171180

172181

173182
class CSVParser(Parser):

src/gps_logger_parser/tests/test_parsers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pathlib
22

3+
import geoarrow.pyarrow as ga
34
import pyarrow as pa
45
import pyarrow.compute as pc
56
import pytest
@@ -84,6 +85,39 @@ def test_harmonizing(file, path, config):
8485
)
8586

8687

88+
gps_test_files = [
89+
(filename, TESTS_DATA_PATH / "files" / filename, conf)
90+
for filename, conf in CONFIG.get("files", {}).items()
91+
if conf.get("skip", False) is not True
92+
and conf.get("type", "").startswith("gps")
93+
and (TESTS_DATA_PATH / "files" / filename).exists()
94+
]
95+
96+
97+
@pytest.mark.timeout(10)
98+
@pytest.mark.parametrize("file,path,config", gps_test_files)
99+
def test_geometry_encoding_wkb(file, path, config):
100+
"""Test that as_table() produces WKB-encoded geometry by default."""
101+
parser_instance = detect_file(path)
102+
table = parser_instance.as_table()
103+
assert "geometry" in table.column_names
104+
geometry_type = table.schema.field("geometry").type
105+
assert isinstance(geometry_type, ga.GeometryExtensionType)
106+
assert geometry_type.encoding == ga.Encoding.WKB
107+
108+
109+
@pytest.mark.timeout(10)
110+
@pytest.mark.parametrize("file,path,config", gps_test_files)
111+
def test_geometry_encoding_geoarrow(file, path, config):
112+
"""Test that as_table(geometry_encoding='geoarrow') preserves native encoding."""
113+
parser_instance = detect_file(path)
114+
table = parser_instance.as_table(geometry_encoding="geoarrow")
115+
assert "geometry" in table.column_names
116+
geometry_type = table.schema.field("geometry").type
117+
assert isinstance(geometry_type, ga.GeometryExtensionType)
118+
assert geometry_type.encoding == ga.Encoding.GEOARROW
119+
120+
87121
# @pytest.mark.timeout(10)
88122
# @pytest.mark.parametrize("file,path,file_format", testdata_success)
89123
# def test_original_data_preserved(file, path, file_format):

0 commit comments

Comments
 (0)