Skip to content

Commit 9f639df

Browse files
authored
alternative fetches of drivers and options (#107)
1 parent 7a9678a commit 9f639df

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

pdal/drivers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from typing import Callable, ClassVar, FrozenSet, Mapping, Optional, Sequence, Type
55

66
from .pipeline import Filter, Reader, Stage, Writer
7+
from . import libpdalpython
8+
9+
import shlex
710

811
StreamableTypes: FrozenSet
912

@@ -59,6 +62,11 @@ def factory(self) -> Callable[..., Stage]:
5962

6063

6164
def inject_pdal_drivers() -> None:
65+
66+
# drivers = libpdalpython.getDrivers()
67+
#
68+
# options = libpdalpython.getOptions()
69+
6270
drivers = json.loads(
6371
subprocess.run(["pdal", "--drivers", "--showjson"], capture_output=True).stdout
6472
)
@@ -69,6 +77,19 @@ def inject_pdal_drivers() -> None:
6977
).stdout
7078
)
7179
)
80+
81+
command = 'pdal --options all --showjson'
82+
83+
p = subprocess.run(shlex.split(command),
84+
encoding='utf-8',
85+
capture_output=True)
86+
output, error = p.stdout, p.stderr
87+
if p.returncode:
88+
print (f'return code {p.returncode} error: "{error}"')
89+
if error:
90+
raise RuntimeError(f"Unable to run pdal --options with error '{error}'")
91+
92+
options = dict( json.loads( output ))
7293
streamable = []
7394
for d in drivers:
7495
name = d["name"]

pdal/libpdalpython.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <pybind11/pybind11.h>
22
#include <pybind11/stl.h>
33
#include <pybind11/numpy.h>
4+
#include <iostream>
45

56
#include <pdal/pdal_config.hpp>
67
#include <pdal/StageFactory.hpp>
@@ -27,6 +28,72 @@ namespace pdal {
2728
);
2829
};
2930

31+
std::vector<py::dict> getDrivers() {
32+
py::gil_scoped_acquire acquire;
33+
std::vector<py::dict> drivers;
34+
35+
pdal::StageFactory f(false);
36+
pdal::PluginManager<pdal::Stage>::loadAll();
37+
pdal::StringList stages = pdal::PluginManager<pdal::Stage>::names();
38+
39+
pdal::StageExtensions& extensions = pdal::PluginManager<pdal::Stage>::extensions();
40+
for (auto name : stages)
41+
{
42+
pdal::Stage *s = f.createStage(name);
43+
std::string description = pdal::PluginManager<Stage>::description(name);
44+
std::string link = pdal::PluginManager<Stage>::link(name);
45+
std::vector<std::string> extension_names = extensions.extensions(name);
46+
47+
py::dict d(
48+
"name"_a=name,
49+
"description"_a=description,
50+
"streamable"_a=s->pipelineStreamable(),
51+
"extensions"_a=extension_names
52+
);
53+
f.destroyStage(s);
54+
drivers.push_back(std::move(d));
55+
}
56+
return drivers;
57+
58+
};
59+
60+
std::vector<py::object> getOptions() {
61+
py::gil_scoped_acquire acquire;
62+
py::object json = py::module_::import("json");
63+
std::vector<py::object> stageOptions;
64+
65+
pdal::StageFactory f;
66+
pdal::PluginManager<pdal::Stage>::loadAll();
67+
pdal::StringList stages = pdal::PluginManager<pdal::Stage>::names();
68+
69+
for (auto name : stages)
70+
{
71+
if ( name == "filters.info" ) continue;
72+
pdal::Stage *s = f.createStage(name);
73+
pdal::ProgramArgs args;
74+
s->addAllArgs(args);
75+
std::ostringstream ostr;
76+
args.dump3(ostr);
77+
py::str pystring(ostr.str());
78+
pystring.attr("strip");
79+
80+
py::object j;
81+
82+
try {
83+
j = json.attr("loads")(pystring);
84+
} catch (py::error_already_set &e) {
85+
std::cout << "failed:" << name << "'" << ostr.str() << "'" <<std::endl;
86+
continue; // skip this one because we can't parse it
87+
}
88+
89+
f.destroyStage(s);
90+
py::list l = py::make_tuple( name, pystring);
91+
stageOptions.push_back(std::move(l));
92+
}
93+
return stageOptions;
94+
95+
};
96+
3097
std::vector<py::dict> getDimensions() {
3198
py::object np = py::module_::import("numpy");
3299
py::object dtype = np.attr("dtype");
@@ -172,6 +239,8 @@ namespace pdal {
172239
.def("_get_json", &Pipeline::getJson)
173240
.def("_del_executor", &Pipeline::delExecutor);
174241
m.def("getInfo", &getInfo);
242+
m.def("getDrivers", &getDrivers);
243+
m.def("getOptions", &getOptions);
175244
m.def("getDimensions", &getDimensions);
176245
m.def("infer_reader_driver", &StageFactory::inferReaderDriver);
177246
m.def("infer_writer_driver", &StageFactory::inferWriterDriver);

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
changes = fp.read()
1717

1818
setup(
19-
name="PDAL",
19+
name="python-pdal",
2020
version=version,
2121
description="Point cloud data processing",
2222
license="BSD",

0 commit comments

Comments
 (0)