mapnik/plugins/input/rasterlite/rasterlite_datasource.cpp

205 lines
5.6 KiB
C++
Raw Normal View History

2009-12-16 21:02:06 +01:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2007 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// $Id$
#include "rasterlite_datasource.hpp"
#include "rasterlite_featureset.hpp"
// boost
#include <boost/filesystem/operations.hpp>
// mapnik
#include <mapnik/ptree_helpers.hpp>
#include <mapnik/geom_util.hpp>
using mapnik::datasource;
using mapnik::parameters;
DATASOURCE_PLUGIN(rasterlite_datasource)
using mapnik::box2d;
using mapnik::coord2d;
using mapnik::query;
using mapnik::featureset_ptr;
using mapnik::layer_descriptor;
using mapnik::datasource_exception;
/*
* Opens a GDALDataset and returns a pointer to it.
* Caller is responsible for calling GDALClose on it
*/
inline void *rasterlite_datasource::open_dataset() const
{
2010-11-14 15:58:29 +01:00
void *dataset = rasterliteOpen (dataset_name_.c_str(), table_name_.c_str());
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
if (! dataset) throw datasource_exception("Rasterlite Plugin: Error opening dataset");
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
if (rasterliteIsError (dataset))
{
std::string error (rasterliteGetLastError(dataset));
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
rasterliteClose (dataset);
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
throw datasource_exception(error);
}
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
return dataset;
2009-12-16 21:02:06 +01:00
}
rasterlite_datasource::rasterlite_datasource(parameters const& params, bool bind)
2010-11-14 15:58:29 +01:00
: datasource(params),
desc_(*params.get<std::string>("type"),"utf-8")
2009-12-16 21:02:06 +01:00
{
#ifdef MAPNIK_DEBUG
2010-11-14 15:58:29 +01:00
std::clog << "\nRasterlite Plugin: Initializing...\n";
2009-12-16 21:02:06 +01:00
#endif
2010-11-14 15:58:29 +01:00
boost::optional<std::string> file = params.get<std::string>("file");
if (!file) throw datasource_exception("missing <file> parameter");
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
boost::optional<std::string> table = params.get<std::string>("table");
if (!table) throw datasource_exception("missing <table> parameter");
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
table_name_ = *table;
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
boost::optional<std::string> base = params.get<std::string>("base");
if (base)
dataset_name_ = *base + "/" + *file;
else
dataset_name_ = *file;
2010-11-14 15:58:29 +01:00
if (bind)
{
this->bind();
}
}
void rasterlite_datasource::bind() const
{
2010-11-14 15:58:29 +01:00
if (is_bound_) return;
2010-11-14 15:58:29 +01:00
if (!boost::filesystem::exists(dataset_name_)) throw datasource_exception(dataset_name_ + " does not exist");
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
void *dataset = open_dataset();
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
double x0, y0, x1, y1;
if (rasterliteGetExtent (dataset, &x0, &y0, &x1, &y1) != RASTERLITE_OK)
{
std::string error (rasterliteGetLastError(dataset));
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
rasterliteClose (dataset);
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
throw datasource_exception(error);
}
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
extent_.init(x0,y0,x1,y1);
2009-12-16 21:02:06 +01:00
#ifdef MAPNIK_DEBUG
2010-11-14 15:58:29 +01:00
int srid, auth_srid;
const char *auth_name;
const char *ref_sys_name;
const char *proj4text;
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
int tile_count;
double pixel_x_size, pixel_y_size;
int levels = rasterliteGetLevels (dataset);
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
if (rasterliteGetSrid(dataset, &srid, &auth_name, &auth_srid, &ref_sys_name, &proj4text) != RASTERLITE_OK)
{
std::string error (rasterliteGetLastError(dataset));
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
rasterliteClose (dataset);
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
throw datasource_exception(error);
}
std::clog << "Rasterlite Plugin: Data Source=" << rasterliteGetTablePrefix(dataset) << "\n";
std::clog << "Rasterlite Plugin: SRID=" << srid << "\n";
std::clog << "Rasterlite Plugin: Authority=" << auth_name << "\n";
std::clog << "Rasterlite Plugin: AuthSRID=" << auth_srid << "\n";
std::clog << "Rasterlite Plugin: RefSys Name=" << ref_sys_name << "\n";
std::clog << "Rasterlite Plugin: Proj4Text=" << proj4text << "\n";
std::clog << "Rasterlite Plugin: Extent(" << x0 << "," << y0 << " " << x1 << "," << y1 << ")\n";
std::clog << "Rasterlite Plugin: Levels=" << levels << "\n";
2009-12-16 21:02:06 +01:00
2010-11-14 15:58:29 +01:00
for (int i = 0; i < levels; i++)
{
if (rasterliteGetResolution(dataset, i, &pixel_x_size, &pixel_y_size, &tile_count) == RASTERLITE_OK)
{
std::clog << "Rasterlite Plugin: Level=" << i
<< " x=" << pixel_x_size << " y=" << pixel_y_size << " tiles=" << tile_count << "\n";
}
}
2009-12-16 21:02:06 +01:00
#endif
2010-11-14 15:58:29 +01:00
rasterliteClose(dataset);
2010-11-14 15:58:29 +01:00
is_bound_ = true;
2009-12-16 21:02:06 +01:00
}
2010-11-14 15:58:29 +01:00
rasterlite_datasource::~rasterlite_datasource()
2009-12-16 21:02:06 +01:00
{
}
std::string rasterlite_datasource::name()
{
2010-11-14 15:58:29 +01:00
return "rasterlite";
}
int rasterlite_datasource::type() const
{
return datasource::Raster;
2009-12-16 21:02:06 +01:00
}
box2d<double> rasterlite_datasource::envelope() const
{
2010-11-14 15:58:29 +01:00
if (!is_bound_) bind();
2010-11-14 15:58:29 +01:00
return extent_;
2009-12-16 21:02:06 +01:00
}
layer_descriptor rasterlite_datasource::get_descriptor() const
{
2010-11-14 15:58:29 +01:00
return desc_;
2009-12-16 21:02:06 +01:00
}
featureset_ptr rasterlite_datasource::features(query const& q) const
{
2010-11-14 15:58:29 +01:00
if (!is_bound_) bind();
2010-11-14 15:58:29 +01:00
rasterlite_query gq = q;
return featureset_ptr(new rasterlite_featureset(open_dataset(), gq));
2009-12-16 21:02:06 +01:00
}
featureset_ptr rasterlite_datasource::features_at_point(coord2d const& pt) const
{
2010-11-14 15:58:29 +01:00
if (!is_bound_) bind();
2010-11-14 15:58:29 +01:00
rasterlite_query gq = pt;
return featureset_ptr(new rasterlite_featureset(open_dataset(), gq));
2009-12-16 21:02:06 +01:00
}
2010-11-13 21:21:21 +01:00