Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into elm-maximum
Browse files Browse the repository at this point in the history
  • Loading branch information
hobu committed Nov 8, 2019
2 parents 25dac52 + 1c69629 commit ab24bcd
Show file tree
Hide file tree
Showing 6 changed files with 564 additions and 571 deletions.
69 changes: 24 additions & 45 deletions plugins/python/filters/PythonFilter.cpp
Expand Up @@ -94,87 +94,66 @@ void PythonFilter::addArgs(ProgramArgs& args)

void PythonFilter::addDimensions(PointLayoutPtr layout)
{
for (const std::string& s : m_args->m_addDimensions) {
for (const std::string& s : m_args->m_addDimensions)
{
StringList spec = Utils::split(s, '=');
Utils::trim(spec[0]);
if (spec.size() == 2)
{
Utils::trim(spec[1]);
auto type = pdal::Dimension::type(spec[1]);
if (type == pdal::Dimension::Type::None)
throwError("Invalid dimension type specified '" + spec[1] + "'. See "
"documentation for valid dimension types");
throwError("Invalid dimension type specified '" + spec[1] +
"'. See documentation for valid dimension types");
layout->registerOrAssignDim(spec[0], type);
} else if (spec.size() == 1){
layout->registerOrAssignDim(s, pdal::Dimension::Type::Double);
} else {
throwError("Invalid dimension specified '" + s + "'. Need <dimension> or <dimension>=<data_type>.");
}
else if (spec.size() == 1)
layout->registerOrAssignDim(s, pdal::Dimension::Type::Double);
else
throwError("Invalid dimension specified '" + s +
"'. Need <dimension> or <dimension>=<data_type>.");
}
}


void PythonFilter::prepared(PointTableRef table)
{
if (m_args->m_source.size() && m_args->m_scriptFile.size())
throwError("Can't set both 'source' and 'script' options.");
if (!m_args->m_source.size() && !m_args->m_scriptFile.size())
throwError("Must set one of 'source' and 'script' options.");
}


void PythonFilter::ready(PointTableRef table)
{
if (m_args->m_source.empty())
m_args->m_source = FileUtils::readFileIntoString(m_args->m_scriptFile);
std::ostream *out = log()->getLogStream();
plang::EnvironmentPtr env = plang::Environment::get();
env->set_stdout(out);
m_script = new plang::Script(m_args->m_source, m_args->m_module,
m_args->m_function);

m_pythonMethod = new plang::Invocation(*m_script);
m_pythonMethod->compile();
m_totalMetadata = table.metadata();
m_script.reset(new plang::Script(m_args->m_source, m_args->m_module,
m_args->m_function));
m_pythonMethod.reset(new plang::Invocation(*m_script, table.metadata(),
m_args->m_pdalargs.dump(1)));
}


PointViewSet PythonFilter::run(PointViewPtr view)
{
log()->get(LogLevel::Debug5) << "filters.python " << *m_script <<
" processing " << view->size() << " points." << std::endl;
m_pythonMethod->resetArguments();
m_pythonMethod->begin(*view, m_totalMetadata);

if (!m_args->m_pdalargs.empty())
{
std::ostringstream args;
args << m_args->m_pdalargs;
m_pythonMethod->setKWargs(args.str());
}
m_pythonMethod->execute();
m_pythonMethod->execute(view, getMetadata());

PointViewSet viewSet;

if (m_pythonMethod->hasOutputVariable("Mask"))
{
PointViewPtr outview = view->makeNew();

size_t arrSize(0);
void *pydata = m_pythonMethod->extractResult("Mask",
Dimension::Type::Unsigned8, arrSize);
char *ok = (char *)pydata;
for (PointId idx = 0; idx < arrSize; ++idx)
if (*ok++)
outview->appendPoint(*view, idx);

viewSet.insert(outview);
}
else
{
m_pythonMethod->end(*view, getMetadata());
viewSet.insert(view);
}
viewSet.insert(view);
return viewSet;
}


void PythonFilter::done(PointTableRef table)
{
static_cast<plang::Environment*>(plang::Environment::get())->reset_stdout();
delete m_pythonMethod;
delete m_script;
}

} // namespace pdal
6 changes: 3 additions & 3 deletions plugins/python/filters/PythonFilter.hpp
Expand Up @@ -57,13 +57,13 @@ class PDAL_DLL PythonFilter : public Filter

virtual void addArgs(ProgramArgs& args);
virtual void addDimensions(PointLayoutPtr layout);
virtual void prepared(PointTableRef table);
virtual void ready(PointTableRef table);
virtual PointViewSet run(PointViewPtr view);
virtual void done(PointTableRef table);

plang::Script* m_script;
plang::Invocation *m_pythonMethod;
MetadataNode m_totalMetadata;
std::unique_ptr<plang::Script> m_script;
std::unique_ptr<plang::Invocation> m_pythonMethod;

struct Args;
std::unique_ptr<Args> m_args;
Expand Down
7 changes: 4 additions & 3 deletions plugins/python/plang/Environment.cpp
Expand Up @@ -227,6 +227,7 @@ std::string getTraceback()
return mssg.str();
}

// Returns a new reference.
PyObject *fromMetadata(MetadataNode m)
{
std::string name = m.name();
Expand All @@ -246,11 +247,11 @@ PyObject *fromMetadata(MetadataNode m)
PyDict_SetItemString(data, "name", PyUnicode_FromString(name.data()));
PyDict_SetItemString(data, "value", PyUnicode_FromString(value.data()));
PyDict_SetItemString(data, "type", PyUnicode_FromString(type.data()));
PyDict_SetItemString(data, "description", PyUnicode_FromString(description.data()));
PyDict_SetItemString(data, "description",
PyUnicode_FromString(description.data()));

if (children.size())
{
PyDict_SetItemString(data, "children", submeta);
}
return data;
}

Expand Down

0 comments on commit ab24bcd

Please sign in to comment.