Skip to content

Commit

Permalink
checkpoint - working on DB system
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgerlek committed May 5, 2015
1 parent 2c2a2f8 commit cd80c8d
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 756 deletions.
24 changes: 14 additions & 10 deletions plugins/rialto/CMakeLists.txt
Expand Up @@ -4,30 +4,34 @@

include (${PDAL_CMAKE_DIR}/sqlite.cmake)

set(srcsF
set(file_srcs
io/RialtoWriter.cpp
io/RialtoFileWriter.cpp)
io/RialtoFileWriter.cpp
)

set(incsF
set(file_incs
io/RialtoWriter.hpp
io/RialtoFileWriter.hpp
)
)

set(srcsD
set(db_srcs
io/RialtoWriter.cpp
io/RialtoDbWriter.cpp)
io/RialtoDbWriter.cpp
io/RialtoDb.cpp
)

set(incsD
set(db_incs
io/RialtoWriter.hpp
io/RialtoDbWriter.hpp
io/RialtoDb.hpp
)

PDAL_ADD_PLUGIN(dbwriter_libname writer rialtodb
FILES "${srcsD}" "${incsD}"
FILES "${db_srcs}" "${db_incs}"
LINK_WITH ${SQLITE3_LIBRARY})

PDAL_ADD_PLUGIN(filewriter_libname writer rialtofile
FILES "${srcsF}" "${incsF}")
FILES "${file_srcs}" "${file_incs}")

#
# Rialto tests
Expand All @@ -38,7 +42,7 @@ if(BUILD_RIALTO_TESTS)
LINK_WITH ${filewriter_libname}
)
PDAL_ADD_TEST(rialtodbtest
FILES test/RialtoDb.cpp test/RialtoDbTest.cpp test/RialtoDbWriterTest.cpp
FILES test/RialtoDbWriterTest.cpp
LINK_WITH ${dbwriter_libname}
)
endif()
190 changes: 190 additions & 0 deletions plugins/rialto/io/RialtoDb.cpp
@@ -0,0 +1,190 @@
/******************************************************************************
* Copyright (c) 2015, RadiantBlue Technologies, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/

#include "RialtoDb.hpp"

#include <pdal/BufferReader.hpp>
#include <pdal/Dimension.hpp>
#include <pdal/Options.hpp>
#include <pdal/pdal_error.hpp>
#include <pdal/pdal_types.hpp>
#include <pdal/PointTable.hpp>
#include <pdal/PointView.hpp>
#include <pdal/util/Bounds.hpp>
#include <pdal/util/FileUtils.hpp>

#include <cstdint>

#include <pdal/../../plugins/sqlite/io/SQLiteCommon.hpp> // TODO: fix path


namespace rialtosupport
{

namespace
{

} // anonymous namespace


RialtoDb::RialtoDb(const std::string& connection) :
m_connection(connection),
m_srid(4326)
{
}


RialtoDb::~RialtoDb()
{
log()->get(LogLevel::Debug) << "~RialtoDB" << std::endl;
}


void RialtoDb::open(bool writable)
{
m_log = std::shared_ptr<pdal::Log>(new pdal::Log("RialtoDB", "stdout"));
m_log->setLevel(LogLevel::Debug);

log()->get(LogLevel::Debug) << "RialtoDB::open()" << std::endl;

m_session = std::unique_ptr<SQLite>(new SQLite(m_connection, m_log));
m_session->connect(writable);

#if 0
m_session->spatialite();

const bool hasSpatialite = m_session->doesTableExist("geometry_columns");
if (!hasSpatialite)
{
std::ostringstream oss;
oss << "SELECT InitSpatialMetadata()";
m_session->execute(oss.str());
}
#endif

createTileSetsTable();
createTilesTable();
}


void RialtoDb::close()
{
// TODO
}


void RialtoDb::createTileSetsTable()
{
if (m_session->doesTableExist("TileSets")) return;

std::ostringstream oss1;
std::ostringstream oss2;

oss1 << "CREATE TABLE TileSets("
<< "tile_set_id INTEGER PRIMARY KEY AUTOINCREMENT,"
<< "name VARCHAR(64)," // TODO
<< "minx DOUBLE,"
<< "miny DOUBLE,"
<< "maxx DOUBLE,"
<< "maxy DOUBLE,"
<< "maxLevels INTEGER"
<< ")";

m_session->execute(oss1.str());

#if 0
oss2 << "SELECT AddGeometryColumn('TileSets', 'extent', "
<< m_srid << ", "
<< "'POLYGON', 'XY')";
m_session->execute(oss2.str());
#endif
}


void RialtoDb::createTilesTable()
{
if (m_session->doesTableExist("Tile")) return;

std::ostringstream oss1;

oss1 << "CREATE TABLE Tiles("
<< "tile_id INTEGER PRIMARY KEY AUTOINCREMENT,"
<< "tile_set_id INTEGER, "
<< "level INTEGER,"
<< "x DOUBLE,"
<< "y DOUBLE,"
<< "points BLOB, "
<< "FOREIGN KEY(tile_set_id) REFERENCES TileSets(tile_set_id)"
<< ")";

m_session->execute(oss1.str());

#if 0
std::ostringstream oss2;
oss2 << "SELECT AddGeometryColumn('Tiles', 'extent', "
<< m_srid << ", "
<< "'POLYGON', 'XY')";
m_session->execute(oss2.str());
#endif
}


uint32_t RialtoDb::addTileSet(const std::string& name)
{
std::ostringstream oss;
oss << "INSERT INTO TileSets "
<< "(name, minx, miny, maxx, maxy, maxLevels) "
<< "VALUES (?, ?, ?, ?, ?, ?)";

records rs;
row r;

r.push_back(column(name));
r.push_back(column(22));
r.push_back(column(33));
r.push_back(column(44));
r.push_back(column(55));
r.push_back(column(66));
rs.push_back(r);

// m_session->insert(oss.str(), rs);

long id = 0;//m_session->last_row_id();
// log()->get(LogLevel::Debug) << "inserted id=" << id << std::endl;

return id;
}


} // namespace rialtosupport
100 changes: 80 additions & 20 deletions plugins/rialto/test/RialtoDb.hpp → plugins/rialto/io/RialtoDb.hpp
Expand Up @@ -43,7 +43,7 @@

// A Rialto database contains two tables:
//
// DataSets
// TileSets
// id (PK)
// name
// bounds
Expand All @@ -52,26 +52,55 @@
//
// Tiles
// id (PK)
// dataset id (FK)
// yileset id (FK)
// x, y, level

namespace pdal {
class Log;
class SQLite;

}

namespace rialtosupport
{
class SQLite;

using namespace pdal;



class PDAL_DLL RialtoDb
{
public:
enum Mode {
Invalid,
Create,
Write,
Read
enum DataType {
Uint8, Uint16, Uint32, Uint64,
Int8, Int16, Int32, Int64,
Float32, Float64
};


// note we always use EPSG:4325
struct TileSetInfo {
std::string name; // aka filename
uint32_t maxLevel;
uint32_t numCols;
uint32_t numRows;
double xmin, ymin, xmax, ymax; // tile set extents (not extents of actual data)
uint32_t numDimensions;
};

struct DimensionInfo {
DataType datatype;
std::string name;
double minimum;
double mean;
double maximum;
};

struct TileInfo {
uint32_t level;
uint32_t x; // col
uint32_t y; // row
};

// modes:
// Create: creates a new database and sets up the required tables
// it is an error if the db already exists
Expand All @@ -80,30 +109,61 @@ class PDAL_DLL RialtoDb
// it is also an error if the db doesn't have the required tables
// Read: opens an existing database
// it is an error if the db doesn't already exist
RialtoDb(const std::string& path, Mode mode);
RialtoDb(const std::string& connection);

~RialtoDb();

void open(bool writable=false);

void close();

// adds a tile set to the database
//
// construct a reader for the file, and send it to a db writer
// (will also create the reproj and stats filters)
// returns id of new data set
uint32_t addDataSet(const std::string& filename);
uint32_t addTileSet(const std::string& filename);

// remove a tile set from the database
void deleteTileSet(uint32_t tileSetId);

// get list all the tile sets in the database, as a list of its
std::vector<uint32_t> getTileSetIds();

std::vector<uint32_t> getDataSetIds();
std::string getDataSetInfo(uint32_t dataSetId);
// get info about a specific tile set
TileSetInfo getTileSetInfo(uint32_t tileSetId);

std::vector<uint32_t> getTileIds(/*bbox, levels*/);
void getTileInfo(uint32_t tileId);
// get info about one of the dimensions of a tile set
DimensionInfo getDimensionInfo(uint32_t tileSetId, uint32_t dimension);

int foo();
// get info about a tile
TileInfo getTileInfo(uint32_t tileId);

// query for all the points of a tile set, bounded by bbox region
// returns a pipeline made up of a BufferReader and a CropFilter
Stage* query(uint32_t tileSetId,
double minx, double miny,
double max, double maxy,
uint32_t minLevel, uint32_t maxLevel);

private:
pdal::Writer* buildPipeline();
void executePipeline(pdal::Writer*);
// query for all the tiles of a tile set, bounded by bbox region
std::vector<uint32_t> getTileSets(uint32_t tileSetId,
double minx, double miny,
double max, double maxy,
uint32_t minLevel, uint32_t maxLevel);

void createTileSetsTable();
void createTilesTable();

void query();

SQLite* m_sqlite;
Mode m_mode;
LogPtr log() const { return m_log; }

std::string m_connection;
std::unique_ptr<SQLite> m_session;
LogPtr m_log;
int m_srid;

RialtoDb& operator=(const RialtoDb&); // not implemented
RialtoDb(const RialtoDb&); // not implemented
Expand Down

0 comments on commit cd80c8d

Please sign in to comment.