2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2011-10-23 14:31:25 +00:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +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,
|
2005-06-14 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2011-05-16 23:41:34 +00:00
|
|
|
|
2006-10-04 11:22:18 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/image_reader.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
2011-05-16 23:41:34 +00:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2006-10-04 11:22:18 +00:00
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
// boost
|
2011-10-12 00:08:03 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2009-10-12 03:58:11 +00:00
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
#include "raster_featureset.hpp"
|
|
|
|
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::query;
|
|
|
|
using mapnik::CoordTransform;
|
2009-12-16 20:02:06 +00:00
|
|
|
using mapnik::image_reader;
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::feature_ptr;
|
2009-12-16 20:02:06 +00:00
|
|
|
using mapnik::image_data_32;
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::raster;
|
2011-05-16 23:41:34 +00:00
|
|
|
using mapnik::feature_factory;
|
2007-03-16 10:11:37 +00:00
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
template <typename LookupPolicy>
|
2006-09-12 14:29:22 +00:00
|
|
|
raster_featureset<LookupPolicy>::raster_featureset(LookupPolicy const& policy,
|
2009-12-16 20:02:06 +00:00
|
|
|
box2d<double> const& extent,
|
2006-09-12 14:29:22 +00:00
|
|
|
query const& q)
|
2011-11-14 03:37:50 +00:00
|
|
|
: policy_(policy),
|
|
|
|
feature_id_(1),
|
|
|
|
extent_(extent),
|
|
|
|
bbox_(q.get_bbox()),
|
|
|
|
curIter_(policy_.begin()),
|
|
|
|
endIter_(policy_.end())
|
2011-10-23 14:31:25 +00:00
|
|
|
{
|
2012-01-19 22:36:27 +00:00
|
|
|
ctx_ = boost::make_shared<mapnik::context_type>();
|
2011-10-23 14:31:25 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
|
|
|
template <typename LookupPolicy>
|
2011-10-23 14:31:25 +00:00
|
|
|
raster_featureset<LookupPolicy>::~raster_featureset()
|
|
|
|
{
|
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
|
|
|
template <typename LookupPolicy>
|
2005-09-21 20:27:37 +00:00
|
|
|
feature_ptr raster_featureset<LookupPolicy>::next()
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2011-10-23 14:31:25 +00:00
|
|
|
if (curIter_ != endIter_)
|
|
|
|
{
|
2012-01-16 14:51:31 +00:00
|
|
|
feature_ptr feature(feature_factory::create(ctx_,feature_id_++));
|
2012-02-02 01:37:35 +00:00
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::auto_ptr<image_reader> reader(mapnik::get_image_reader(curIter_->file(),curIter_->format()));
|
2009-04-10 12:05:31 +00:00
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-23 14:31:25 +00:00
|
|
|
std::clog << "Raster Plugin: READER = " << curIter_->format() << " " << curIter_->file()
|
|
|
|
<< " size(" << curIter_->width() << "," << curIter_->height() << ")" << std::endl;
|
2009-04-10 12:05:31 +00:00
|
|
|
#endif
|
2011-10-12 00:08:03 +00:00
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
if (reader.get())
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2011-10-23 14:31:25 +00:00
|
|
|
int image_width = policy_.img_width(reader->width());
|
|
|
|
int image_height = policy_.img_height(reader->height());
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
if (image_width > 0 && image_height > 0)
|
|
|
|
{
|
|
|
|
CoordTransform t(image_width, image_height, extent_, 0, 0);
|
|
|
|
box2d<double> intersect = bbox_.intersect(curIter_->envelope());
|
|
|
|
box2d<double> ext = t.forward(intersect);
|
|
|
|
box2d<double> rem = policy_.transform(ext);
|
|
|
|
if (ext.width() > 0.5 && ext.height() > 0.5 )
|
|
|
|
{
|
|
|
|
// select minimum raster containing whole ext
|
|
|
|
int x_off = static_cast<int>(floor(ext.minx()));
|
|
|
|
int y_off = static_cast<int>(floor(ext.miny()));
|
|
|
|
int end_x = static_cast<int>(ceil(ext.maxx()));
|
|
|
|
int end_y = static_cast<int>(ceil(ext.maxy()));
|
|
|
|
|
|
|
|
// clip to available data
|
|
|
|
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;
|
|
|
|
int width = end_x - x_off;
|
|
|
|
int height = end_y - y_off;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
intersect = t.backward(feature_raster_extent);
|
2010-03-03 03:23:53 +00:00
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
image_data_32 image(width,height);
|
|
|
|
reader->read(x_off, y_off, image);
|
|
|
|
feature->set_raster(boost::make_shared<raster>(intersect, image));
|
|
|
|
}
|
|
|
|
}
|
2010-03-03 03:23:53 +00:00
|
|
|
}
|
2011-10-23 14:31:25 +00:00
|
|
|
}
|
|
|
|
catch (mapnik::image_reader_exception const& ex)
|
|
|
|
{
|
2011-10-31 17:43:56 +00:00
|
|
|
std::cerr << "Raster Plugin: image reader exception caught: " << ex.what() << std::endl;
|
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
|
|
|
std::cerr << "Raster Plugin: " << ex.what() << std::endl;
|
2011-10-23 14:31:25 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << "Raster Plugin: exception caught" << std::endl;
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
++curIter_;
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
return feature_ptr();
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2011-10-23 14:31:25 +00:00
|
|
|
std::string tiled_multi_file_policy::interpolate(std::string const& pattern, int x, int y) const
|
2011-10-12 00:08:03 +00:00
|
|
|
{
|
2011-10-23 14:31:25 +00: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;
|
|
|
|
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 00:08:03 +00:00
|
|
|
}
|
|
|
|
|
2005-09-21 20:27:37 +00:00
|
|
|
template class raster_featureset<single_file_policy>;
|
2009-04-10 12:05:31 +00:00
|
|
|
template class raster_featureset<tiled_file_policy>;
|
2011-10-12 00:08:03 +00:00
|
|
|
template class raster_featureset<tiled_multi_file_policy>;
|