2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02: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,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* 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
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2011-05-17 01:41:34 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// mapnik
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2015-01-22 18:39:37 +01:00
|
|
|
#include <mapnik/image.hpp>
|
2013-01-04 18:23:06 +01:00
|
|
|
#include <mapnik/raster.hpp>
|
2014-08-28 11:29:04 +02:00
|
|
|
#include <mapnik/view_transform.hpp>
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/image_reader.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2014-11-26 12:22:36 +01:00
|
|
|
#include <mapnik/util/variant.hpp>
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2011-10-12 02:08:03 +02:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2013-06-21 21:00:48 +02:00
|
|
|
#include <boost/format.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2009-10-12 05:58:11 +02:00
|
|
|
|
2011-10-23 16:31:25 +02:00
|
|
|
#include "raster_featureset.hpp"
|
|
|
|
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::query;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::image_reader;
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::feature_ptr;
|
2015-01-22 03:31:02 +01:00
|
|
|
using mapnik::image_rgba8;
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::raster;
|
2011-05-17 01:41:34 +02:00
|
|
|
using mapnik::feature_factory;
|
2007-03-16 11:11:37 +01:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
template <typename LookupPolicy>
|
2006-09-12 16:29:22 +02:00
|
|
|
raster_featureset<LookupPolicy>::raster_featureset(LookupPolicy const& policy,
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> const& extent,
|
2006-09-12 16:29:22 +02:00
|
|
|
query const& q)
|
2011-11-14 04:37:50 +01:00
|
|
|
: policy_(policy),
|
|
|
|
feature_id_(1),
|
2013-09-20 15:00:11 +02:00
|
|
|
ctx_(std::make_shared<mapnik::context_type>()),
|
2011-11-14 04:37:50 +01:00
|
|
|
extent_(extent),
|
|
|
|
bbox_(q.get_bbox()),
|
|
|
|
curIter_(policy_.begin()),
|
2017-06-13 16:17:54 +02:00
|
|
|
endIter_(policy_.end()),
|
|
|
|
filter_factor_(q.get_filter_factor())
|
2011-10-23 16:31:25 +02:00
|
|
|
{
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
template <typename LookupPolicy>
|
2011-10-23 16:31:25 +02:00
|
|
|
raster_featureset<LookupPolicy>::~raster_featureset()
|
|
|
|
{
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
template <typename LookupPolicy>
|
2005-09-21 22:27:37 +02:00
|
|
|
feature_ptr raster_featureset<LookupPolicy>::next()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2011-10-23 16:31:25 +02:00
|
|
|
if (curIter_ != endIter_)
|
|
|
|
{
|
2012-01-16 15:51:31 +01:00
|
|
|
feature_ptr feature(feature_factory::create(ctx_,feature_id_++));
|
2012-02-02 02:37:35 +01:00
|
|
|
|
2011-10-23 16:31:25 +02:00
|
|
|
try
|
|
|
|
{
|
2013-04-19 10:50:50 +02:00
|
|
|
std::unique_ptr<image_reader> reader(mapnik::get_image_reader(curIter_->file(),curIter_->format()));
|
2009-04-10 14:05:31 +02:00
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(raster) << "raster_featureset: Reader=" << curIter_->format() << "," << curIter_->file()
|
|
|
|
<< ",size(" << curIter_->width() << "," << curIter_->height() << ")";
|
2011-10-12 02:08:03 +02:00
|
|
|
|
2011-10-23 16:31:25 +02:00
|
|
|
if (reader.get())
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2011-10-23 16:31:25 +02:00
|
|
|
int image_width = policy_.img_width(reader->width());
|
|
|
|
int image_height = policy_.img_height(reader->height());
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-23 16:31:25 +02:00
|
|
|
if (image_width > 0 && image_height > 0)
|
|
|
|
{
|
2014-08-28 11:17:15 +02:00
|
|
|
mapnik::view_transform t(image_width, image_height, extent_, 0, 0);
|
2011-10-23 16:31:25 +02:00
|
|
|
box2d<double> intersect = bbox_.intersect(curIter_->envelope());
|
|
|
|
box2d<double> ext = t.forward(intersect);
|
|
|
|
box2d<double> rem = policy_.transform(ext);
|
2017-05-10 14:33:29 +02:00
|
|
|
// select minimum raster containing whole ext
|
|
|
|
int x_off = static_cast<int>(std::floor(ext.minx()));
|
|
|
|
int y_off = static_cast<int>(std::floor(ext.miny()));
|
|
|
|
int end_x = static_cast<int>(std::ceil(ext.maxx()));
|
|
|
|
int end_y = static_cast<int>(std::ceil(ext.maxy()));
|
2011-10-23 16:31:25 +02:00
|
|
|
|
2017-05-10 14:33:29 +02:00
|
|
|
// clip to available data
|
2017-05-12 15:37:12 +02:00
|
|
|
if (x_off >= image_width) x_off = image_width - 1;
|
2017-06-06 17:28:21 +02:00
|
|
|
if (y_off >= image_height) y_off = image_height - 1;
|
2017-05-10 14:33:29 +02:00
|
|
|
if (x_off < 0) x_off = 0;
|
|
|
|
if (y_off < 0) y_off = 0;
|
|
|
|
if (end_x > image_width) end_x = image_width;
|
|
|
|
if (end_y > image_height) end_y = image_height;
|
2014-08-28 11:17:15 +02:00
|
|
|
|
2017-05-10 14:33:29 +02:00
|
|
|
int width = end_x - x_off;
|
|
|
|
int height = end_y - y_off;
|
2017-05-12 15:37:12 +02:00
|
|
|
if (width < 1) width = 1;
|
|
|
|
if (height < 1) height = 1;
|
2017-05-10 14:33:29 +02:00
|
|
|
|
|
|
|
// calculate actual box2d of returned raster
|
|
|
|
box2d<double> feature_raster_extent(rem.minx() + x_off,
|
|
|
|
rem.miny() + y_off,
|
|
|
|
rem.maxx() + x_off + width,
|
|
|
|
rem.maxy() + y_off + height);
|
|
|
|
feature_raster_extent = t.backward(feature_raster_extent);
|
|
|
|
mapnik::image_any data = reader->read(x_off, y_off, width, height);
|
2017-06-13 16:17:54 +02:00
|
|
|
mapnik::raster_ptr raster = std::make_shared<mapnik::raster>(feature_raster_extent, intersect, std::move(data), filter_factor_);
|
2017-05-10 14:33:29 +02:00
|
|
|
feature->set_raster(raster);
|
2011-10-23 16:31:25 +02:00
|
|
|
}
|
2010-03-03 04:23:53 +01:00
|
|
|
}
|
2011-10-23 16:31:25 +02:00
|
|
|
}
|
|
|
|
catch (mapnik::image_reader_exception const& ex)
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_ERROR(raster) << "Raster Plugin: image reader exception caught: " << ex.what();
|
2011-10-31 18:43:56 +01:00
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_ERROR(raster) << "Raster Plugin: " << ex.what();
|
2011-10-23 16:31:25 +02:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_ERROR(raster) << "Raster Plugin: exception caught";
|
2011-10-23 16:31:25 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-23 16:31:25 +02:00
|
|
|
++curIter_;
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
return feature_ptr();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 16:31:25 +02:00
|
|
|
std::string tiled_multi_file_policy::interpolate(std::string const& pattern, int x, int y) const
|
2011-10-12 02:08:03 +02:00
|
|
|
{
|
2011-10-23 16:31:25 +02:00
|
|
|
// TODO: make from some sort of configurable interpolation
|
|
|
|
int tms_y = tile_stride_ * ((image_height_ / tile_size_) - y - 1);
|
|
|
|
int tms_x = tile_stride_ * x;
|
2014-10-01 03:35:07 +02:00
|
|
|
// TODO - optimize by avoiding boost::format
|
2011-10-23 16:31:25 +02:00
|
|
|
std::string xs = (boost::format("%03d/%03d/%03d") % (tms_x / 1000000) % ((tms_x / 1000) % 1000) % (tms_x % 1000)).str();
|
|
|
|
std::string ys = (boost::format("%03d/%03d/%03d") % (tms_y / 1000000) % ((tms_y / 1000) % 1000) % (tms_y % 1000)).str();
|
|
|
|
std::string rv(pattern);
|
|
|
|
boost::algorithm::replace_all(rv, "${x}", xs);
|
|
|
|
boost::algorithm::replace_all(rv, "${y}", ys);
|
|
|
|
return rv;
|
2011-10-12 02:08:03 +02:00
|
|
|
}
|
|
|
|
|
2005-09-21 22:27:37 +02:00
|
|
|
template class raster_featureset<single_file_policy>;
|
2009-04-10 14:05:31 +02:00
|
|
|
template class raster_featureset<tiled_file_policy>;
|
2011-10-12 02:08:03 +02:00
|
|
|
template class raster_featureset<tiled_multi_file_policy>;
|