Ejemplo n.º 1
0
int TranslateKernel::execute()
{
    // setting common options for each stage propagates the debug flag and
    // verbosity level
    Options readerOptions, filterOptions, writerOptions;
    setCommonOptions(readerOptions);
    setCommonOptions(filterOptions);
    setCommonOptions(writerOptions);

    m_manager = std::unique_ptr<PipelineManager>(new PipelineManager);

    if (!m_readerType.empty())
    {
        m_manager->addReader(m_readerType);
    }
    else
    {
        StageFactory factory;
        std::string driver = factory.inferReaderDriver(m_inputFile);

        if (driver.empty())
            throw app_runtime_error("Cannot determine input file type of " +
                                    m_inputFile);
        m_manager->addReader(driver);
    }

    if (m_manager == NULL)
        throw pdal_error("Error making pipeline\n");

    Stage* reader = m_manager->getStage();

    if (reader == NULL)
        throw pdal_error("Error getting reader\n");

    readerOptions.add("filename", m_inputFile);
    reader->setOptions(readerOptions);

    Stage* stage = reader;

    // add each filter provided on the command-line, updating the stage pointer
    for (auto const f : m_filterType)
    {
        std::string filter_name(f);

        if (!Utils::startsWith(f, "filters."))
            filter_name.insert(0, "filters.");

        Stage* filter = &(m_manager->addFilter(filter_name));

        if (filter == NULL)
        {
            std::ostringstream oss;
            oss << "Unable to add filter " << filter_name << ".  Filter "
              "is invalid or plugin could not be loaded.  Check "
              "'pdal --drivers'.";
            throw pdal_error("Error getting filter\n");
        }

        filter->setOptions(filterOptions);
        filter->setInput(*stage);
        stage = filter;
    }

    if (!m_writerType.empty())
    {
        m_manager->addWriter(m_writerType);
    }
    else
    {
        StageFactory factory;
        std::string driver = factory.inferWriterDriver(m_outputFile);

        if (driver.empty())
            throw app_runtime_error("Cannot determine output file type of " +
                                    m_outputFile);
        Options options = factory.inferWriterOptionsChanges(m_outputFile);
        writerOptions += options;
        m_manager->addWriter(driver);
    }

    Stage* writer = m_manager->getStage();

    if (writer == NULL)
        throw pdal_error("Error getting writer\n");

    writerOptions.add("filename", m_outputFile);
    writer->setOptions(writerOptions);
    writer->setInput(*stage);

    // be sure to recurse through any extra stage options provided by the user
    applyExtraStageOptionsRecursive(writer);

    m_manager->execute();

    if (m_pipelineOutput.size() > 0)
        PipelineWriter::writePipeline(m_manager->getStage(), m_pipelineOutput);

    return 0;
}
Ejemplo n.º 2
0
void TIndexKernel::mergeFile()
{
    using namespace gdal;

    std::ostringstream out;

    if (!openDataset(m_idxFilename))
    {
        std::ostringstream out;
        out << "Couldn't open index dataset file '" << m_idxFilename << "'.";
        throw pdal_error(out.str());
    }
    if (!openLayer(m_layerName))
    {
        std::ostringstream out;
        out << "Couldn't open layer '" << m_layerName <<
            "' in output file '" << m_idxFilename << "'.";
        throw pdal_error(out.str());
    }

    FieldIndexes indexes = getFields();

    SpatialRef outSrs(m_tgtSrsString);
    if (!outSrs)
        throw pdal_error("Couldn't interpret target SRS string.");

    if (!m_wkt.empty())
    {
        Geometry g(m_wkt, outSrs);

        if (!g)
            throw pdal_error("Couldn't interpret geometry filter string.");
        OGR_L_SetSpatialFilter(m_layer, g.get());
    }

    std::vector<FileInfo> files;

    // Docs are bad here.  You need this call even if you haven't read anything
    // or nothing happens.
    OGR_L_ResetReading(m_layer);
    while (true)
    {
        OGRFeatureH feature = OGR_L_GetNextFeature(m_layer);
        if (!feature)
            break;

        FileInfo fileInfo;
        fileInfo.m_filename =
            OGR_F_GetFieldAsString(feature, indexes.m_filename);
        fileInfo.m_srs =
            OGR_F_GetFieldAsString(feature, indexes.m_srs);
        files.push_back(fileInfo);

        OGR_F_Destroy(feature);
    }

    StageFactory factory;

    MergeFilter merge;

    Options cropOptions;
    if (!m_bounds.empty())
        cropOptions.add("bounds", m_bounds);
    else
        cropOptions.add("polygon", m_wkt);

    for (auto f : files)
    {
        Stage *premerge = NULL;
        std::string driver = factory.inferReaderDriver(f.m_filename);
        Stage *reader = factory.createStage(driver);
        if (!reader)
        {
            out << "Unable to create reader for file '" << f.m_filename << "'.";
            throw pdal_error(out.str());
        }
        Options readerOptions;
        readerOptions.add("filename", f.m_filename);
        reader->setOptions(readerOptions);
        premerge = reader;

        if (m_tgtSrsString != f.m_srs)
        {
            Stage *repro = factory.createStage("filters.reprojection");
            repro->setInput(*reader);
            Options reproOptions;
            reproOptions.add("out_srs", m_tgtSrsString);
            reproOptions.add("in_srs", f.m_srs);
            repro->setOptions(reproOptions);
            premerge = repro;
        }

        // WKT is set, even if we're using a bounding box for fitering, so
        // can be used as a test here.
        if (!m_wkt.empty())
        {
            Stage *crop = factory.createStage("filters.crop");
            crop->setOptions(cropOptions);
            crop->setInput(*premerge);
            premerge = crop;
        }

        merge.setInput(*premerge);
    }

    std::string driver = factory.inferWriterDriver(m_filespec);
    Options factoryOptions = factory.inferWriterOptionsChanges(m_filespec);
    Stage *writer = factory.createStage(driver);
    if (!writer)
    {
        out << "Unable to create reader for file '" << m_filespec << "'.";
        throw pdal_error(out.str());
    }
    writer->setInput(merge);

    applyExtraStageOptionsRecursive(writer);

    Options writerOptions(factoryOptions);
    setCommonOptions(writerOptions);

    writerOptions.add("filename", m_filespec);
    writerOptions.add("offset_x", "auto");
    writerOptions.add("offset_y", "auto");
    writerOptions.add("offset_z", "auto");
    writer->addConditionalOptions(writerOptions);

    PointTable table;
    writer->prepare(table);
    writer->execute(table);
}