www.pudn.com > dstile-0.2.rar > TileAccessor.cpp


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#include "exceptions.h"
#include "syskit.h"
#include "giskit.h"
#include "TileAccessor.h"

TileAccessor::TileAccessor() :
    m_outDrv(0),
    m_outDrvOpts(0),
    m_ext(),
    m_path()
{
}

TileAccessor::~TileAccessor() {
    if (m_outDrvOpts) {
	CSLDestroy(m_outDrvOpts);
	m_outDrvOpts = 0;
    }
}

void TileAccessor::SetOutputFormat(const string& format, const vector &opts) {
    m_outDrv = GetGDALDriverManager()->GetDriverByName(format.c_str());
    if (!m_outDrv) throw InvalidInput("Unable to set output format to '%s'.", format.c_str());
    if (m_outDrvOpts) {
        CSLDestroy(m_outDrvOpts);
        m_outDrvOpts = 0;
    }
    for (vector::const_iterator it = opts.begin(); it != opts.end(); ++it)
        m_outDrvOpts = CSLSetNameValue(m_outDrvOpts, it->substr(0, it->find('=')).c_str(), it->substr(it->find('=') + 1).c_str());
}

void TileAccessor::Write(int l, int x, int y, GDALDataset *t) {
    string loc = LocateTile(l, x, y);
    GDALDataset *tmp;
    int p = loc.rfind('/');

    if (p > 0) MakeDirs(loc.substr(0, p));
    tmp = m_outDrv->CreateCopy(loc.c_str(), t, 0, m_outDrvOpts, GDALDummyProgress, 0);
    if (!tmp) throw GDALException("Unable to create '%s'.", loc.c_str());
    delete tmp;
}

GDALDataset* TileAccessor::Read(int l, int x, int y, bool returnNullIfNoEnt) {
    string loc = LocateTile(l, x, y);
    GDALDataset *tile;
    
    if (returnNullIfNoEnt && access(loc.c_str(), R_OK)) return 0;
    tile = reinterpret_cast(GDALOpen(loc.c_str(), GA_ReadOnly));
    if (!tile) throw GDALException("Unable to open '%s'.", loc.c_str());
    return tile;
}

string TileAccessor::LocateTile(int l, int x, int y) {
    char tmp[256];
    int i1, i2, i3, i4;
	
    i1 = ((x >> 5) & 0x1F) | (((y >> 5) & 0x1F) << 5);
    i2 = ((x >> 10) & 0x1F) | (((y >> 10) & 0x1F) << 5);
    i3 = ((x >> 15) & 0x1F) | (((y >> 15) & 0x1F) << 5);
    i4 = ((x >> 20) & 0x1F) | (((y >> 20) & 0x1F) << 5);
    snprintf(tmp, sizeof(tmp), "%s%d/%03x/%03x/%03x/%03x/%02x%08x%08x%s", m_path.c_str(), l, i4, i3, i2, i1, l, x, y, m_ext.c_str());    
    if(wwcache){
    	y = (int)(m_l0ty*pow(2,l-1)-1-y);
    	snprintf(tmp, sizeof(tmp), "%s%d/%04d/%04d_%04d%s", m_path.c_str(), l, y,y,x, m_ext.c_str());
    }
    return tmp;
}

void TileAccessor::SetPath(const string& path) {
    if (path.size()) {
        if (path[path.size() - 1] == '/') m_path = path;
        else m_path = path + "/";
    } else m_path.clear();
}