mapnik/plugins/input/rasterlite/rasterlite_featureset.cpp

168 lines
5.2 KiB
C++
Raw Normal View History

2009-12-16 20:02:06 +00:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
2009-12-16 20:02:06 +00:00
*
* 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
*
*****************************************************************************/
#include "rasterlite_featureset.hpp"
// mapnik
#include <mapnik/image_util.hpp>
#include <mapnik/feature_factory.hpp>
// boost
#include <boost/make_shared.hpp>
2009-12-16 20:02:06 +00:00
using mapnik::query;
using mapnik::coord2d;
using mapnik::box2d;
using mapnik::Feature;
using mapnik::feature_ptr;
using mapnik::CoordTransform;
using mapnik::geometry_type;
2009-12-16 20:02:06 +00:00
using mapnik::query;
using mapnik::feature_factory;
2009-12-16 20:02:06 +00:00
rasterlite_featureset::rasterlite_featureset(void* dataset, rasterlite_query q)
: dataset_(dataset),
gquery_(q),
first_(true)
2009-12-16 20:02:06 +00:00
{
rasterliteSetBackgroundColor(dataset_, 255, 0, 255);
rasterliteSetTransparentColor(dataset_, 255, 0, 255);
}
rasterlite_featureset::~rasterlite_featureset()
{
#ifdef MAPNIK_DEBUG
2010-11-18 23:03:00 +00:00
std::clog << "Rasterlite Plugin: closing dataset" << std::endl;
2009-12-16 20:02:06 +00:00
#endif
2010-11-14 14:58:29 +00:00
rasterliteClose(dataset_);
2009-12-16 20:02:06 +00:00
}
feature_ptr rasterlite_featureset::next()
{
2010-11-14 14:58:29 +00:00
if (first_)
{
first_ = false;
query *q = boost::get<query>(&gquery_);
if (q)
{
2010-11-14 14:58:29 +00:00
return get_feature(*q);
}
else
{
2010-11-14 14:58:29 +00:00
coord2d *p = boost::get<coord2d>(&gquery_);
if (p)
{
2010-11-14 14:58:29 +00:00
return get_feature_at_point(*p);
}
}
// should never reach here
}
return feature_ptr();
2009-12-16 20:02:06 +00:00
}
feature_ptr rasterlite_featureset::get_feature(mapnik::query const& q)
{
#ifdef MAPNIK_DEBUG
2010-11-18 23:03:00 +00:00
std::clog << "Rasterlite Plugin: get_feature" << std::endl;
2009-12-16 20:02:06 +00:00
#endif
feature_ptr feature(feature_factory::create(1));
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
double x0, y0, x1, y1;
rasterliteGetExtent (dataset_, &x0, &y0, &x1, &y1);
2009-12-16 20:02:06 +00:00
box2d<double> raster_extent(x0, y0, x1, y1);
2010-11-14 14:58:29 +00:00
box2d<double> intersect = raster_extent.intersect(q.get_bbox());
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
const int width = static_cast<int>(boost::get<0>(q.resolution()) * intersect.width() + 0.5);
const int height = static_cast<int>(boost::get<0>(q.resolution()) * intersect.height() + 0.5);
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
const double pixel_size = (intersect.width() >= intersect.height()) ?
(intersect.width() / (double) width) : (intersect.height() / (double) height);
2009-12-16 20:02:06 +00:00
#ifdef MAPNIK_DEBUG
2010-11-18 23:03:00 +00:00
std::clog << "Rasterlite Plugin: Raster extent=" << raster_extent << std::endl;
std::clog << "Rasterlite Plugin: View extent=" << q.get_bbox() << std::endl;
std::clog << "Rasterlite Plugin: Intersect extent=" << intersect << std::endl;
std::clog << "Rasterlite Plugin: Query resolution="
<< boost::get<0>(q.resolution()) << "," << boost::get<1>(q.resolution()) << std::endl;
2010-11-18 23:03:00 +00:00
std::clog << "Rasterlite Plugin: Size=" << width << " " << height << std::endl;
std::clog << "Rasterlite Plugin: Pixel Size=" << pixel_size << std::endl;
2009-12-16 20:02:06 +00:00
#endif
2010-11-14 14:58:29 +00:00
if (width > 0 && height > 0)
{
2010-11-18 23:03:00 +00:00
int size = 0;
void* raster = 0;
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
if (rasterliteGetRawImageByRect(dataset_,
intersect.minx(),
intersect.miny(),
intersect.maxx(),
intersect.maxy(),
pixel_size,
width,
height,
GAIA_RGBA_ARRAY,
&raster,
&size) == RASTERLITE_OK)
2010-11-14 14:58:29 +00:00
{
if (size > 0)
{
mapnik::image_data_32 image(width, height);
image.set(0xffffffff);
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
unsigned char* raster_data = static_cast<unsigned char*>(raster);
unsigned char* image_data = image.getBytes();
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
memcpy (image_data, raster_data, size);
2009-12-16 20:02:06 +00:00
feature->set_raster(boost::make_shared<mapnik::raster>(intersect,image));
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
free (raster);
2009-12-16 20:02:06 +00:00
#ifdef MAPNIK_DEBUG
2010-11-18 23:03:00 +00:00
std::clog << "Rasterlite Plugin: done" << std::endl;
2009-12-16 20:02:06 +00:00
#endif
2010-11-14 14:58:29 +00:00
}
else
{
2009-12-16 20:02:06 +00:00
#ifdef MAPNIK_DEBUG
2010-11-18 23:03:00 +00:00
std::clog << "Rasterlite Plugin: error=" << rasterliteGetLastError (dataset_) << std::endl;
2009-12-16 20:02:06 +00:00
#endif
2010-11-14 14:58:29 +00:00
}
}
2009-12-16 20:02:06 +00:00
2010-11-14 14:58:29 +00:00
return feature;
}
return feature_ptr();
2009-12-16 20:02:06 +00:00
}
feature_ptr rasterlite_featureset::get_feature_at_point(mapnik::coord2d const& pt)
{
2010-11-14 14:58:29 +00:00
return feature_ptr();
2009-12-16 20:02:06 +00:00
}