osgEarth 2.1.1
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Attributes

osgEarth::DiskCache Class Reference

Inheritance diagram for osgEarth::DiskCache:
Collaboration diagram for osgEarth::DiskCache:

List of all members.

Classes

struct  LayerProperties

Public Member Functions

 DiskCache (const DiskCacheOptions &options=DiskCacheOptions())
 DiskCache (const DiskCache &rhs, const osg::CopyOp &op=osg::CopyOp::DEEP_COPY_ALL)
 META_Object (osgEarth, DiskCache)
std::string getPath () const
virtual bool isCached (const TileKey &key, const CacheSpec &spec) const
virtual std::string getFilename (const TileKey &key, const CacheSpec &spec) const
virtual bool getImage (const TileKey &key, const CacheSpec &spec, osg::ref_ptr< const osg::Image > &out_image)
virtual void setImage (const TileKey &key, const CacheSpec &spec, const osg::Image *image)
virtual void storeProperties (const CacheSpec &spec, const Profile *profile, unsigned int tileSize)
virtual bool loadProperties (const std::string &cacheId, CacheSpec &out_spec, osg::ref_ptr< const Profile > &out_profile, unsigned int &out_tileSize)

Protected Types

typedef std::map< std::string,
LayerProperties
LayerPropertiesCache

Protected Member Functions

std::string getTMSPath (const std::string &cacheId) const

Protected Attributes

LayerPropertiesCache _layerPropertiesCache
bool _writeWorldFilesOverride

Private Attributes

DiskCacheOptions _options

Detailed Description

Base class for any cache that stores tile files to disk

Definition at line 377 of file Caching.


Member Typedef Documentation

typedef std::map< std::string, LayerProperties > osgEarth::DiskCache::LayerPropertiesCache [protected]

Definition at line 436 of file Caching.


Constructor & Destructor Documentation

DiskCache::DiskCache ( const DiskCacheOptions options = DiskCacheOptions())

Definition at line 86 of file Caching.cpp.

                                                      :
Cache( options ),
_options( options )
{
    setName( "tilecache" );
    _writeWorldFilesOverride = getenv("OSGEARTH_WRITE_WORLD_FILES") != 0L;
}
DiskCache::DiskCache ( const DiskCache rhs,
const osg::CopyOp &  op = osg::CopyOp::DEEP_COPY_ALL 
)

Definition at line 94 of file Caching.cpp.


Member Function Documentation

std::string DiskCache::getFilename ( const TileKey key,
const CacheSpec spec 
) const [virtual]

Gets the filename to cache to for the given TileKey

Reimplemented in osgEarth::TMSCache.

Definition at line 124 of file Caching.cpp.

{
        unsigned int level, x, y;
    level = key.getLevelOfDetail() +1;
    key.getTileXY( x, y );

    unsigned int numCols, numRows;
    key.getProfile()->getNumTiles(level, numCols, numRows);

    // need to invert the y-tile index
    y = numRows - y - 1;

    char buf[2048];
    sprintf( buf, "%s/%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.%s",
        getPath().c_str(),
        spec.cacheId().c_str(),
        level,
        (x / 1000000),
        (x / 1000) % 1000,
        (x % 1000),
        (y / 1000000),
        (y / 1000) % 1000,
        (y % 1000),
        spec.format().c_str());

    return buf;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool DiskCache::getImage ( const TileKey key,
const CacheSpec spec,
osg::ref_ptr< const osg::Image > &  out_image 
) [virtual]

Gets the cached image for the given TileKey

Implements osgEarth::Cache.

Definition at line 153 of file Caching.cpp.

{
        std::string filename = getFilename(key, spec);

    //If the path doesn't contain a zip file, check to see that it actually exists on disk
    if (!osgEarth::isZipPath(filename))
    {
        if (!osgDB::fileExists(filename)) 
            return false;
    }

    {
        Threading::ScopedReadLock lock(s_mutex);
        out_image = osgDB::readImageFile( filename );
    }

    return out_image.valid();
}

Here is the call graph for this function:

std::string DiskCache::getPath ( ) const

Gets the path of this DiskCache

Definition at line 112 of file Caching.cpp.

{
    if ( _options.path().empty() || _refURI.empty() )
        return _options.path();
    else
    {
        if (osgDB::containsServerAddress( _refURI )) return _options.path();
        return osgEarth::getFullPath( _refURI, _options.path() );
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

std::string DiskCache::getTMSPath ( const std::string &  cacheId) const [protected]

Definition at line 272 of file Caching.cpp.

{
    return getPath() + std::string("/") + cacheId + std::string("/tms.xml");
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool DiskCache::isCached ( const TileKey key,
const CacheSpec spec 
) const [virtual]

Gets whether the given TileKey is cached or not

Reimplemented from osgEarth::Cache.

Definition at line 104 of file Caching.cpp.

{
        //Check to see if the file for this key exists
        std::string filename = getFilename( key, spec );
    return osgDB::fileExists(filename);
}

Here is the call graph for this function:

bool DiskCache::loadProperties ( const std::string &  cacheId,
CacheSpec out_spec,
osg::ref_ptr< const Profile > &  out_profile,
unsigned int &  out_tileSize 
) [virtual]

Loads the TileMap for the given layer.

Reimplemented from osgEarth::Cache.

Definition at line 289 of file Caching.cpp.

{
        //Try to get it from the cache.
        LayerPropertiesCache::const_iterator i = _layerPropertiesCache.find(cacheId);
        if ( i != _layerPropertiesCache.end())
        {
        out_spec = CacheSpec(cacheId, i->second._format);
                out_tileSize = i->second._tile_size;
        out_profile = i->second._profile.get();
        return true;
        }

        osg::ref_ptr<TileMap> tileMap;

        std::string path = getTMSPath( cacheId );
        OE_INFO << LC << "Metadata file is " << path << std::endl;

        if (osgDB::fileExists( path ) )
        {
                osg::ref_ptr<TileMap> tileMap = TileMapReaderWriter::read(path, NULL);
                if (tileMap.valid())
                {
                        LayerProperties props;
                        props._format = tileMap->getFormat().getExtension();
                        props._tile_size = tileMap->getFormat().getWidth();
                        props._profile = tileMap->createProfile();

                        _layerPropertiesCache[cacheId] = props;

            out_spec = CacheSpec( cacheId, props._format );
            out_profile = props._profile.get();
            out_tileSize = props._tile_size;
            return true;
                }
        else
        {
            OE_WARN << LC << "Failed to load cache metadata from " << path << std::endl;
        }
        }
        return false;
}

Here is the call graph for this function:

osgEarth::DiskCache::META_Object ( osgEarth  ,
DiskCache   
)
void DiskCache::setImage ( const TileKey key,
const CacheSpec spec,
const osg::Image *  image 
) [virtual]

Sets the cached image for the given TileKey

Implements osgEarth::Cache.

Definition at line 176 of file Caching.cpp.

{
        std::string filename = getFilename( key, spec );
    std::string path = osgDB::getFilePath(filename);
        std::string extension = spec.format();

        //If the format is empty, see if we can glean an extension from the image filename
        if (extension.empty())
        {
                if (!image->getFileName().empty())
                {
                        extension = osgDB::getFileExtension( image->getFileName() );
                }
        }

        //If the format is STILL empty, just use PNG
        if (extension.empty())
        {
                extension = "png";
        }

    // serialize cache writes.
    Threading::ScopedWriteLock lock(s_mutex);

    //If the path doesn't currently exist or we can't create the path, don't cache the file
    if (!osgDB::fileExists(path) && !osgEarth::isZipPath(path) && !osgDB::makeDirectory(path))
    {
        OE_WARN << LC << "Couldn't create path " << path << std::endl;
    }

    std::string ext = osgDB::getFileExtension(filename);

    if ( _options.writeWorldFiles() == true || _writeWorldFilesOverride )
    {
        //Write out the world file along side the image
        double minx, miny, maxx, maxy;
        key.getExtent().getBounds(minx, miny, maxx, maxy);

        std::string baseFilename = osgDB::getNameLessExtension(filename);

        /*
        Determine the correct extension for the file type.  Typically, world file extensions
        consist of the first letter of the extension, followed by the third, then the letter "w".
        For instance a jpg file's world file would be a jgw file.
        */
        std::string worldFileExt = "wld";
        if (ext.size() >= 3)
        {
            worldFileExt[0] = ext[0];
            worldFileExt[1] = ext[2];
            worldFileExt[2] = 'w';
        }
        std::string worldFileName = baseFilename + std::string(".") + worldFileExt;
        std::ofstream worldFile;
        worldFile.open(worldFileName.c_str());

        double x_units_per_pixel = (maxx - minx) / (double)image->s();
        double y_units_per_pixel = -(maxy - miny) / (double)image->t();
        worldFile << std::fixed << std::setprecision(10)
            //X direction units per pixel
            << x_units_per_pixel << std::endl
            //Rotation about the y axis, in our case 0
            << "0" << std::endl
            //Rotation about the x axis, in our case 0
            << "0" << std::endl
            //Y direction units per pixel, typically negative
            << y_units_per_pixel << std::endl
            //X coordinate of the center of the upper left pixel
            << minx + 0.5 * x_units_per_pixel << std::endl
            //Y coordinate of the upper left pixel
            << maxy + 0.5 * y_units_per_pixel;
        worldFile.close();
    }

    bool writingJpeg = (ext == "jpg" || ext == "jpeg");

        osg::ref_ptr<osgDB::ReaderWriter::Options> op = new osgDB::ReaderWriter::Options();
        op->setOptionString(_options.imageWriterPluginOptions().value());

        //If we are trying to write a non RGB image to JPEG, convert it to RGB before we write it
    if ((image->getPixelFormat() != GL_RGB) && writingJpeg)
    {
                //Take a reference so the converted image will be deleted
                osg::ref_ptr<osg::Image> rgb = ImageUtils::convertToRGB8( image );
                if (rgb.valid())
                {
                        osgDB::writeImageFile(*rgb.get(), filename, op.get());
                }
    }
    else
    {
        osgDB::writeImageFile(*image, filename, op.get());
    }
}

Here is the call graph for this function:

void DiskCache::storeProperties ( const CacheSpec spec,
const Profile profile,
unsigned int  tileSize 
) [virtual]

Store the TileMap for the given profile.

Reimplemented from osgEarth::Cache.

Definition at line 277 of file Caching.cpp.

{
        osg::ref_ptr<TileMap> tileMap = TileMap::create("", profile, spec.format(), tile_size, tile_size);
    tileMap->setTitle( spec.name() );
        std::string path = getTMSPath( spec.cacheId() );
    OE_DEBUG << LC << "Writing TMS file to  " << path << std::endl;
        TileMapReaderWriter::write( tileMap.get(), path );
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 437 of file Caching.

Reimplemented from osgEarth::Cache.

Reimplemented in osgEarth::TMSCache.

Definition at line 441 of file Caching.

Definition at line 438 of file Caching.


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