2009-12-16 20:02:06 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2009-12-16 20:02:06 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 10:49:16 +00:00
|
|
|
* Copyright (C) 2015 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
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2015-01-22 17:39:37 +00:00
|
|
|
#include <mapnik/image.hpp>
|
2009-12-16 20:02:06 +00:00
|
|
|
#include <mapnik/image_util.hpp>
|
2013-03-13 01:36:03 +00:00
|
|
|
#include <mapnik/query.hpp>
|
|
|
|
#include <mapnik/raster.hpp>
|
2013-01-04 17:23:06 +00:00
|
|
|
#include <mapnik/feature.hpp>
|
2011-05-16 23:41:34 +00:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
|
|
|
|
2014-01-28 23:05:10 +00:00
|
|
|
#include <cstring>
|
2009-12-16 20:02:06 +00:00
|
|
|
|
|
|
|
using mapnik::coord2d;
|
|
|
|
using mapnik::box2d;
|
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::query;
|
2011-05-16 23:41:34 +00:00
|
|
|
using mapnik::feature_factory;
|
2009-12-16 20:02:06 +00:00
|
|
|
|
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
rasterlite_featureset::rasterlite_featureset(void* dataset,
|
|
|
|
rasterlite_query q)
|
2011-11-14 03:37:50 +00:00
|
|
|
: dataset_(dataset),
|
|
|
|
gquery_(q),
|
2012-02-02 17:00:02 +00:00
|
|
|
first_(true),
|
2013-09-20 13:00:11 +00:00
|
|
|
ctx_(std::make_shared<mapnik::context_type>())
|
2009-12-16 20:02:06 +00:00
|
|
|
{
|
|
|
|
rasterliteSetBackgroundColor(dataset_, 255, 0, 255);
|
|
|
|
rasterliteSetTransparentColor(dataset_, 255, 0, 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
rasterlite_featureset::~rasterlite_featureset()
|
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Closing";
|
2009-12-16 20:02:06 +00:00
|
|
|
|
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;
|
2013-10-22 20:05:44 +00:00
|
|
|
MAPNIK_LOG_DEBUG(gdal) << "rasterlite_featureset: Next feature in Dataset=" << &dataset_;
|
2014-08-12 12:40:45 +00:00
|
|
|
return mapnik::util::apply_visitor(query_dispatch(*this), gquery_);
|
2010-11-14 14:58:29 +00:00
|
|
|
}
|
|
|
|
return feature_ptr();
|
2009-12-16 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
feature_ptr rasterlite_featureset::get_feature(mapnik::query const& q)
|
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Running get_feature";
|
2009-12-16 20:02:06 +00:00
|
|
|
|
2012-02-02 17:00:02 +00:00
|
|
|
feature_ptr feature(feature_factory::create(ctx_,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
|
|
|
|
2011-10-22 12:29:54 +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
|
|
|
|
2013-10-11 11:35:34 +00:00
|
|
|
const int width = static_cast<int>(std::get<0>(q.resolution()) * intersect.width() + 0.5);
|
|
|
|
const int height = static_cast<int>(std::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
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Raster extent=" << raster_extent;
|
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: View extent=" << q.get_bbox();
|
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Intersect extent=" << intersect;
|
2013-10-11 11:35:34 +00:00
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Query resolution=" << std::get<0>(q.resolution()) << "," << std::get<1>(q.resolution());
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Size=" << width << " " << height;
|
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Pixel Size=" << pixel_size;
|
2009-12-16 20:02:06 +00:00
|
|
|
|
2010-11-14 14:58:29 +00:00
|
|
|
if (width > 0 && height > 0)
|
|
|
|
{
|
2010-11-18 23:03:00 +00:00
|
|
|
int size = 0;
|
2011-10-22 12:29:54 +00:00
|
|
|
void* raster = 0;
|
2009-12-16 20:02:06 +00:00
|
|
|
|
2010-11-14 14:58:29 +00:00
|
|
|
if (rasterliteGetRawImageByRect(dataset_,
|
2011-10-22 12:29:54 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-01-22 02:31:02 +00:00
|
|
|
mapnik::image_rgba8 image(width,height);
|
2010-11-14 14:58:29 +00:00
|
|
|
unsigned char* raster_data = static_cast<unsigned char*>(raster);
|
2015-05-04 10:49:11 +00:00
|
|
|
std::memcpy(image.bytes(), raster_data, size);
|
2015-01-10 00:13:04 +00:00
|
|
|
feature->set_raster(std::make_shared<mapnik::raster>(intersect, std::move(image), 1.0));
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(rasterlite) << "rasterlite_featureset: Done";
|
2010-11-14 14:58:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-10 00:13:57 +00:00
|
|
|
MAPNIK_LOG_ERROR(rasterlite) << "Rasterlite Plugin: Error " << rasterliteGetLastError (dataset_);
|
2010-11-14 14:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-14 03:37:50 +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
|
|
|
}
|