Skip to content

Commit 27a1560

Browse files
mrtgenethobu
andauthored
Update filename handling in InferableTypeStage for filespec (#198)
* Update filename handling in InferableTypeStage for filespec * Validate 'path' in filename for InferableTypeStage Raise ValueError if 'path' is missing in filename dict * Enhance tests for pdal.Reader and pdal.Writer types Added assertions to validate reader and writer types for filespec format. * support and test full filespec definitions * conditionalize FileSpec support to PDAL 2.9+ * check headers for FileSpec --------- Co-authored-by: Howard Butler <hobu.inc@gmail.com>
1 parent 6b332fc commit 27a1560

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/pdal/pipeline.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
LogLevelFromPDAL = {v: k for k, v in LogLevelToPDAL.items()}
3434

3535

36+
HaveFileSpecSupport = False
37+
if libpdalpython.getInfo().major == 2 and \
38+
libpdalpython.getInfo().minor >= 9 or \
39+
libpdalpython.getInfo().major > 2:
40+
HaveFileSpecSupport = True
41+
42+
3643
class Pipeline(libpdalpython.Pipeline):
3744
def __init__(
3845
self,
@@ -217,7 +224,25 @@ def __or__(self, other: Union[Stage, Pipeline]) -> Pipeline:
217224
class InferableTypeStage(Stage):
218225
def __init__(self, filename: Optional[str] = None, **options: Any):
219226
if filename:
220-
options["filename"] = filename
227+
if isinstance(filename, dict):
228+
if "path" not in filename:
229+
raise ValueError(f"'path' is missing in the provided filespec: {filename}")
230+
if HaveFileSpecSupport:
231+
options["filename"] = filename
232+
else:
233+
# log that we can't pass FileSpec to PDAL
234+
# because the library version is too old
235+
msg = "PDAL library version is too old for FileSpec support. " \
236+
"Defaulting to using filename only"
237+
logging.info(msg)
238+
options["filename"] = filename["path"]
239+
240+
else:
241+
if HaveFileSpecSupport:
242+
filespec = {'path':str(filename)}
243+
options["filename"] = filespec
244+
else:
245+
options["filename"] = filename
221246
super().__init__(**options)
222247

223248
@property
@@ -226,7 +251,18 @@ def type(self) -> str:
226251
return super().type
227252
except KeyError:
228253
filename = self._options.get("filename")
229-
return str(self._infer_type(filename) if filename else "")
254+
if isinstance(filename, dict):
255+
if "path" not in filename:
256+
raise ValueError(f"'path' is missing in the provided filespec: {filename}")
257+
if HaveFileSpecSupport:
258+
path = filename.get('path')
259+
else:
260+
path = filename
261+
262+
else:
263+
path = str(filename)
264+
265+
return str(self._infer_type(path) if filename else "")
230266

231267
_infer_type = staticmethod(lambda filename: "")
232268

test/test_pipeline.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import sys
55

6-
from typing import List
76
from itertools import product
87
import numpy as np
98
import pytest
@@ -284,6 +283,20 @@ def test_infer_stage_type(self):
284283
assert pdal.Writer("foo.xxx").type == ""
285284
assert pdal.Reader().type == ""
286285
assert pdal.Writer().type == ""
286+
assert pdal.Reader({"path": "foo.las"}).type == "readers.las"
287+
assert pdal.Writer({"path": "foo.las"}).type == "writers.las"
288+
assert pdal.Reader({"path": "foo.xxx"}).type == ""
289+
assert pdal.Writer({"path": "foo.xxx"}).type == ""
290+
assert pdal.Reader({}).type == ""
291+
assert pdal.Writer({}).type == ""
292+
293+
def test_filespec(self):
294+
"""Can transit filespecs"""
295+
spec = {'path':'junk.las', 'headers':{'header1':'header_1', 'header2':'header_2'}, 'query':{'query1':'query_1', 'query2':'query_2'}}
296+
assert pdal.Reader(spec).type == "readers.las"
297+
assert pdal.Reader(spec).options['filename']['path'] == "junk.las"
298+
assert pdal.Reader(spec).options['filename']['headers']['header2'] == "header_2"
299+
assert pdal.Writer("foo.las").type == "writers.las"
287300

288301
def test_streamable(self):
289302
"""Can we distinguish streamable from non-streamable stages and pipeline"""

0 commit comments

Comments
 (0)