osgEarth 2.1.1
Public Member Functions | Private Attributes

MBTilesSource Class Reference

Inheritance diagram for MBTilesSource:
Collaboration diagram for MBTilesSource:

List of all members.

Public Member Functions

 MBTilesSource (const TileSourceOptions &options)
void initialize (const std::string &referenceURI, const Profile *overrideProfile)
osg::Image * createImage (const TileKey &key, ProgressCallback *progress)
bool getMetaData (const std::string &key, std::string &value)
void computeLevels ()
virtual std::string getExtension () const

Private Attributes

const MBTilesOptions _options
sqlite3 * _database
unsigned int _minLevel
unsigned int _maxLevel
osg::ref_ptr< osgDB::ReaderWriter > _rw
std::string _tileFormat

Detailed Description

Definition at line 45 of file ReaderWriterMBTiles.cpp.


Constructor & Destructor Documentation

MBTilesSource::MBTilesSource ( const TileSourceOptions options) [inline]

Definition at line 48 of file ReaderWriterMBTiles.cpp.

                                                      :
      TileSource( options ),
      _options( options ),      
      _database( NULL ),
      _minLevel( 0 ),
      _maxLevel( 20 )
    {
    }

Member Function Documentation

void MBTilesSource::computeLevels ( ) [inline]

Definition at line 223 of file ReaderWriterMBTiles.cpp.

    {        
        sqlite3_stmt* select = NULL;
        std::string query = "SELECT min(zoom_level), max(zoom_level) from tiles";
        int rc = sqlite3_prepare_v2( _database, query.c_str(), -1, &select, 0L );
        if ( rc != SQLITE_OK )
        {
            OE_WARN << LC << "Failed to prepare SQL: " << query << "; " << sqlite3_errmsg(_database) << std::endl;
        }

        rc = sqlite3_step( select );
        if ( rc == SQLITE_ROW)
        {                     
            _minLevel = sqlite3_column_int( select, 0 );
            _maxLevel = sqlite3_column_int( select, 1 );
            //OE_NOTICE << "Min=" << _minLevel << " Max=" << _maxLevel << std::endl;
        }
        else
        {
            OE_DEBUG << LC << "SQL QUERY failed for " << query << ": " << std::endl;
        }

        sqlite3_finalize( select );        
    }
osg::Image* MBTilesSource::createImage ( const TileKey key,
ProgressCallback progress 
) [inline, virtual]

Creates an image for the given TileKey. The returned object is new and is the responsibility of the caller.

Implements osgEarth::TileSource.

Definition at line 119 of file ReaderWriterMBTiles.cpp.

    {             
        int z = key.getLevelOfDetail();
        int x = key.getTileX();
        int y = key.getTileY();

        if (z < _minLevel)
        {
            //Return an empty image to make it continue subdividing
            return ImageUtils::createEmptyImage();
        }

        if (z > _maxLevel)
        {
            //If we're at the max level, just return NULL
            return NULL;
        }

        unsigned int numRows, numCols;
        key.getProfile()->getNumTiles(key.getLevelOfDetail(), numCols, numRows);
        y  = numRows - y - 1;

        //Get the image
        sqlite3_stmt* select = NULL;
        std::string query = "SELECT tile_data from tiles where zoom_level = ? AND tile_column = ? AND tile_row = ?";
        int rc = sqlite3_prepare_v2( _database, query.c_str(), -1, &select, 0L );
        if ( rc != SQLITE_OK )
        {
            OE_WARN << LC << "Failed to prepare SQL: " << query << "; " << sqlite3_errmsg(_database) << std::endl;
            return false;
        }

        bool valid = true;        
        sqlite3_bind_int( select, 1, z );
        sqlite3_bind_int( select, 2, x );
        sqlite3_bind_int( select, 3, y );


        osg::Image* result = NULL;
        rc = sqlite3_step( select );
        if ( rc == SQLITE_ROW)
        {                     
            // the pointer returned from _blob gets freed internally by sqlite, supposedly
            const char* data = (const char*)sqlite3_column_blob( select, 0 );
            int imageBufLen = sqlite3_column_bytes( select, 0 );

            // deserialize the image from the buffer:
            std::string imageString( data, imageBufLen );
            std::stringstream imageBufStream( imageString );
            osgDB::ReaderWriter::ReadResult rr = _rw->readImage( imageBufStream );
            if (rr.validImage())
            {
                result = rr.takeImage();            
            }
        }
        else
        {
            OE_DEBUG << LC << "SQL QUERY failed for " << query << ": " << std::endl;
            valid = false;
        }

        sqlite3_finalize( select );
        return result;

    }

Here is the call graph for this function:

virtual std::string MBTilesSource::getExtension ( ) const [inline, virtual]

Gets the preferred extension for this TileSource

Reimplemented from osgEarth::TileSource.

Definition at line 249 of file ReaderWriterMBTiles.cpp.

    {
        return _tileFormat;
    }
bool MBTilesSource::getMetaData ( const std::string &  key,
std::string &  value 
) [inline]

Definition at line 186 of file ReaderWriterMBTiles.cpp.

    {
        //get the metadata
        sqlite3_stmt* select = NULL;
        std::string query = "SELECT value from metadata where name = ?";
        int rc = sqlite3_prepare_v2( _database, query.c_str(), -1, &select, 0L );
        if ( rc != SQLITE_OK )
        {
            OE_WARN << LC << "Failed to prepare SQL: " << query << "; " << sqlite3_errmsg(_database) << std::endl;
            return false;
        }


        bool valid = true;
        std::string keyStr = std::string( key );
        rc = sqlite3_bind_text( select, 1, keyStr.c_str(), keyStr.length(), SQLITE_STATIC );
        if (rc != SQLITE_OK )
        {
            OE_WARN << LC << "Failed to bind text: " << query << "; " << sqlite3_errmsg(_database) << std::endl;
            return false;
        }

        rc = sqlite3_step( select );
        if ( rc == SQLITE_ROW)
        {                     
            value = (char*)sqlite3_column_text( select, 0 );
        }
        else
        {
            OE_DEBUG << LC << "SQL QUERY failed for " << query << ": " << std::endl;
            valid = false;
        }

        sqlite3_finalize( select );
        return valid;
    }
void MBTilesSource::initialize ( const std::string &  referenceURI,
const Profile overrideProfile 
) [inline, virtual]

Initialize the TileSource. The profile should be computed and set here using setProfile()

Implements osgEarth::TileSource.

Definition at line 58 of file ReaderWriterMBTiles.cpp.

    {
        //Set the profile
        setProfile( osgEarth::Registry::instance()->getGlobalMercatorProfile() );

        //Open the database
        std::string filename = _options.filename().value();

        //Get the absolute filename
        if (!osgDB::containsServerAddress(filename))
        {
            filename = osgEarth::getFullPath(referenceURI, filename);
        }

        int flags = SQLITE_OPEN_READONLY;
        int rc = sqlite3_open_v2( filename.c_str(), &_database, flags, 0L );
        if ( rc != 0 )
        {
            OE_WARN << LC << "Failed to open database \"" << filename << "\": " << sqlite3_errmsg(_database) << std::endl;
            return;
        }

        //Print out some metadata
        std::string name, type, version, description, format;
        getMetaData( "name", name );
        getMetaData( "type", type);
        getMetaData( "version", version );
        getMetaData( "description", description );
        getMetaData( "format", format );
        OE_NOTICE << "name=" << name << std::endl
                  << "type=" << type << std::endl
                  << "version=" << version << std::endl
                  << "description=" << description << std::endl
                  << "format=" << format << std::endl;

        //Determine the tile format and get a reader writer for it.        
        if (_options.format().isSet())
        {
            //Get an explicitly defined format
            _tileFormat = _options.format().value();
        }
        else if (!format.empty())
        {
            //Try to get it from the database metadata
            _tileFormat = format;
        }
        else
        {
            //Assume it's PNG
            _tileFormat = "png";
        }

        OE_DEBUG << LC <<  "_tileFormat = " << _tileFormat << std::endl;

        //Get the ReaderWriter
        _rw = osgDB::Registry::instance()->getReaderWriterForExtension( _tileFormat );

        computeLevels();
    }    

Here is the call graph for this function:


Member Data Documentation

sqlite3* MBTilesSource::_database [private]

Definition at line 256 of file ReaderWriterMBTiles.cpp.

unsigned int MBTilesSource::_maxLevel [private]

Definition at line 258 of file ReaderWriterMBTiles.cpp.

unsigned int MBTilesSource::_minLevel [private]

Definition at line 257 of file ReaderWriterMBTiles.cpp.

Reimplemented from osgEarth::TileSource.

Definition at line 255 of file ReaderWriterMBTiles.cpp.

osg::ref_ptr<osgDB::ReaderWriter> MBTilesSource::_rw [private]

Definition at line 260 of file ReaderWriterMBTiles.cpp.

std::string MBTilesSource::_tileFormat [private]

Definition at line 261 of file ReaderWriterMBTiles.cpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines