示例#1
0
文件: Writer.cpp 项目: dakcarto/PDAL
bool Writer::WriteBlock(PointBuffer const& buffer)
{

    boost::uint8_t* point_data;
    boost::uint32_t point_data_length;
    boost::uint32_t schema_byte_size;


    bool pack = getOptions().getValueOrDefault<bool>("pack_ignored_fields", true);
    if (pack)
        PackPointData(buffer, &point_data, point_data_length, schema_byte_size);
    else
    {
        point_data = buffer.getData(0);
        point_data_length = buffer.getSchema().getByteSize() * buffer.getNumPoints();
    }

    bool doKDTree = getOptions().getValueOrDefault<bool>("kdtreeify", true);
    if (doKDTree)
    {
        // kdtreeify(buffer, &point_data, point_data_length, schema_byte_size);
    }

    std::string block_table = getOptions().getValueOrThrow<std::string>("block_table");

    // // Pluck the block id out of the first point in the buffer
    pdal::Schema const& schema = buffer.getSchema();
    Dimension const& blockDim = schema.getDimension("BlockID");

    m_block_id  = buffer.getField<boost::int32_t>(blockDim, 0);
    m_obj_id = getOptions().getValueOrThrow<boost::int32_t>("pc_id");
    m_num_points = static_cast<boost::int64_t>(buffer.getNumPoints());
    // if (m_type == DATABASE_POSTGRESQL)
   //  {
   //      std::string cloud_column = getOptions().getValueOrDefault<std::string>("cloud_column", "id");
   //      bool is3d = getOptions().getValueOrDefault<bool>("is3d", false);
   // 
   // 
   //      std::vector<boost::uint8_t> block_data;
   //      for (boost::uint32_t i = 0; i < point_data_length; ++i)
   //      {
   //          block_data.push_back(point_data[i]);
   //      }
   // 
   //      if (pack)
   //          delete point_data;
   //      m_block_bytes.str("");
   //      Utils::binary_to_hex_stream(point_data, m_block_bytes, 0, point_data_length);
   //      m_block_data = m_block_bytes.str();
   //      //std::cout << "hex: " << hex.substr(0, 30) << std::endl;
   //      m_srid = getOptions().getValueOrDefault<boost::uint32_t>("srid", 4326);
   // 
   //      boost::uint32_t precision(9);
   //      pdal::Bounds<double> bounds = buffer.calculateBounds(3);
   //      // m_extent.str("");
   //      m_extent = bounds.toWKT(precision); // polygons are only 2d, not cubes
   //      // m_bbox.str("");
   //      m_bbox = bounds.toBox(precision, 3);
   //      log()->get(logDEBUG) << "extent: " << m_extent << std::endl;
   //      log()->get(logDEBUG) << "bbox: " << m_bbox << std::endl;
   // 
   //      if (!m_block_statement)
   //      {
   //          // m_block_statement = (m_session->prepare <<   m_block_insert_query.str(), \
   //          //                                                      ::soci::use(m_obj_id, "obj_id"), \
   //          //                                                      ::soci::use(m_block_id, "block_id"), \
   //          //                                                      ::soci::use(m_num_points, "num_points"), \
   //          //                                                      ::soci::use(m_block_bytes.str(),"hex"), \
   //          //                                                      ::soci::use(m_extent.str(), "extent"), \
   //          //                                                      ::soci::use(m_srid, "srid"), \
   //          //                                                      ::soci::use(m_bbox.str(), "bbox"));
   //          m_block_statement = new ::soci::statement(*m_session);
   // 
   //          m_block_statement->exchange(::soci::use(m_obj_id, "obj_id"));
   //          m_block_statement->exchange(::soci::use(m_block_id, "block_id"));
   //          m_block_statement->exchange(::soci::use(m_num_points, "num_points"));
   //          m_block_statement->exchange(::soci::use(m_block_data,"hex"));
   //          m_block_statement->exchange(::soci::use(m_extent, "extent"));
   //          m_block_statement->exchange(::soci::use(m_srid, "srid"));
   //          m_block_statement->exchange(::soci::use(m_bbox, "bbox"));
   //          m_block_statement->alloc();
   //          m_block_statement->prepare(m_block_insert_query.str());
   //          m_block_statement->define_and_bind();
   // 
   //      }
   //      // ::soci::statement st = (m_session->prepare <<    m_block_insert_query.str(), \
   //      //                                                      ::soci::use(m_obj_id, "obj_id"), \
   //      //                                                      ::soci::use(m_block_id, "block_id"), \
   //      //                                                      ::soci::use(m_num_points, "num_points"), \
   //      //                                                      ::soci::use(m_block_bytes.str(),"hex"), \
   //      //                                                      ::soci::use(m_extent.str(), "extent"), \
   //      //                                                      ::soci::use(m_srid, "srid"), \
   //      //                                                      ::soci::use(m_bbox.str(), "bbox"));
   //      try
   //      {
   //          m_block_statement->execute(true);
   //      }
   //      catch (std::exception const& e)
   //      {
   //          std::ostringstream oss;
   //          oss << "Insert query failed with error '" << e.what() << "'";
   //          m_session->rollback();
   //          throw pdal_error(oss.str());
   //      }
   // 
   //  }

    return true;
}
示例#2
0
bool Writer::WriteBlock(PointBuffer const& buffer)
{
    bool bUsePartition = m_block_table_partition_column.size() != 0;

    // Pluck the block id out of the first point in the buffer
    pdal::Schema const& schema = buffer.getSchema();
    Dimension const& blockDim = schema.getDimension("BlockID");

    boost::int32_t block_id  = buffer.getField<boost::int32_t>(blockDim, 0);

    std::ostringstream oss;
    std::ostringstream partition;

    if (bUsePartition)
    {
        partition << "," << m_block_table_partition_column;
    }

    oss << "INSERT INTO "<< m_block_table_name <<
        "(OBJ_ID, BLK_ID, NUM_POINTS, POINTS,   "
        "PCBLK_MIN_RES, BLK_EXTENT, PCBLK_MAX_RES, NUM_UNSORTED_POINTS, PT_SORT_DIM";
    if (bUsePartition)
        oss << partition.str();
    oss << ") "
        "VALUES ( :1, :2, :3, :4, 1, mdsys.sdo_geometry(:5, :6, null,:7, :8)"
        ", 1, 0, 1";
    if (bUsePartition)
        oss << ", :9";

    oss <<")";

    // TODO: If gotdata == false below, this memory probably leaks --mloskot
    OCILobLocator** locator =(OCILobLocator**) VSIMalloc(sizeof(OCILobLocator*) * 1);

    Statement statement = Statement(m_connection->CreateStatement(oss.str().c_str()));


    long* p_pc_id = (long*) malloc(1 * sizeof(long));
    p_pc_id[0] = m_pc_id;

    long* p_result_id = (long*) malloc(1 * sizeof(long));
    p_result_id[0] = (long)block_id;

    long* p_num_points = (long*) malloc(1 * sizeof(long));
    p_num_points[0] = (long)buffer.getNumPoints();

    // std::cout << "point count on write: " << buffer.getNumPoints() << std::endl;


    // :1
    statement->Bind(&m_pc_id);

    // :2
    statement->Bind(p_result_id);

    // :3
    statement->Bind(p_num_points);

    // :4
    statement->Define(locator, 1);


    // std::vector<liblas::uint8_t> data;
    // bool gotdata = GetResultData(result, reader, data, 3);
    // if (! gotdata) throw std::runtime_error("unable to fetch point data byte array");
    // boost::uint8_t* point_data = buffer.getData(0);
    boost::uint8_t* point_data;
    boost::uint32_t point_data_length;
    boost::uint32_t schema_byte_size;
    
    bool pack = getOptions().getValueOrDefault<bool>("pack_ignored_fields", true);
    if (pack)
        PackPointData(buffer, &point_data, point_data_length, schema_byte_size);
    else
    {
        point_data = buffer.getData(0);
        point_data_length = buffer.getSchema().getByteSize() * buffer.getNumPoints();
    }

    // statement->Bind((char*)point_data,(long)(buffer.getSchema().getByteSize()*buffer.getNumPoints()));
    statement->Bind((char*)point_data,(long)(point_data_length));

    // :5
    long* p_gtype = (long*) malloc(1 * sizeof(long));
    p_gtype[0] = m_gtype;

    statement->Bind(p_gtype);

    // :6
    long* p_srid  = 0;


    if (m_srid != 0)
    {
        p_srid = (long*) malloc(1 * sizeof(long));
        p_srid[0] = m_srid;
    }
    statement->Bind(p_srid);

    // :7
    OCIArray* sdo_elem_info=0;
    m_connection->CreateType(&sdo_elem_info, m_connection->GetElemInfoType());
    SetElements(statement, sdo_elem_info);
    statement->Bind(&sdo_elem_info, m_connection->GetElemInfoType());

    // :8
    OCIArray* sdo_ordinates=0;
    m_connection->CreateType(&sdo_ordinates, m_connection->GetOrdinateType());

    // x0, x1, y0, y1, z0, z1, bUse3d
    pdal::Bounds<double> bounds = CalculateBounds(buffer);
    SetOrdinates(statement, sdo_ordinates, bounds);
    statement->Bind(&sdo_ordinates, m_connection->GetOrdinateType());

    // :9
    long* p_partition_d = 0;
    if (bUsePartition)
    {
        p_partition_d = (long*) malloc(1 * sizeof(long));
        p_partition_d[0] = m_block_table_partition_value;
        statement->Bind(p_partition_d);
    }

    try
    {
        statement->Execute();
    }
    catch (std::runtime_error const& e)
    {
        std::ostringstream oss;
        oss << "Failed to insert block # into '" << m_block_table_name << "' table. Does the table exist? "  << std::endl << e.what() << std::endl;
        throw std::runtime_error(oss.str());
    }

    oss.str("");


    OWStatement::Free(locator, 1);

    if (p_pc_id != 0) free(p_pc_id);
    if (p_result_id != 0) free(p_result_id);
    if (p_num_points != 0) free(p_num_points);
    if (p_gtype != 0) free(p_gtype);
    if (p_srid != 0) free(p_srid);
    if (p_partition_d != 0) free(p_partition_d);

    m_connection->DestroyType(&sdo_elem_info);
    m_connection->DestroyType(&sdo_ordinates);




    return true;
}