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
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 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
|
|
|
*****************************************************************************/
|
2006-10-04 13:22:18 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/image_reader.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
|
|
|
|
2005-09-21 22:27:37 +02:00
|
|
|
#include "raster_featureset.hpp"
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-10-12 05:58:11 +02:00
|
|
|
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::query;
|
|
|
|
using mapnik::CoordTransform;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::image_reader;
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::feature_ptr;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::image_data_32;
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::raster;
|
|
|
|
|
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)
|
2007-03-16 11:11:37 +01:00
|
|
|
: policy_(policy),
|
|
|
|
id_(1),
|
2009-04-10 14:05:31 +02:00
|
|
|
extent_(extent),
|
|
|
|
bbox_(q.get_bbox()),
|
|
|
|
curIter_(policy_.begin()),
|
2007-03-16 11:11:37 +01:00
|
|
|
endIter_(policy_.end())
|
2005-06-14 17:06:59 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <typename LookupPolicy>
|
2005-09-21 22:27:37 +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
|
|
|
{
|
2007-03-16 11:11:37 +01:00
|
|
|
if (curIter_!=endIter_)
|
|
|
|
{
|
2007-04-30 22:08:49 +02:00
|
|
|
feature_ptr feature(new Feature(++id_));
|
2007-03-16 11:11:37 +01:00
|
|
|
try
|
|
|
|
{
|
2009-12-16 21:02:06 +01:00
|
|
|
std::auto_ptr<image_reader> reader(mapnik::get_image_reader(curIter_->file(),curIter_->format()));
|
2009-04-10 14:05:31 +02:00
|
|
|
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-19 00:15:59 +01:00
|
|
|
std::clog << "Raster Plugin: READER = " << curIter_->format() << " " << curIter_->file()
|
|
|
|
<< " size(" << curIter_->width() << "," << curIter_->height() << ")" << std::endl;
|
2009-04-10 14:05:31 +02:00
|
|
|
#endif
|
2007-03-16 11:11:37 +01:00
|
|
|
if (reader.get())
|
|
|
|
{
|
|
|
|
int image_width=reader->width();
|
|
|
|
int image_height=reader->height();
|
2008-01-07 11:20:05 +01:00
|
|
|
|
2007-03-16 11:11:37 +01:00
|
|
|
if (image_width>0 && image_height>0)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2009-04-10 14:05:31 +02:00
|
|
|
CoordTransform t(image_width,image_height,extent_,0,0);
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> intersect=bbox_.intersect(curIter_->envelope());
|
|
|
|
box2d<double> ext=t.forward(intersect);
|
2010-03-03 04:23:53 +01:00
|
|
|
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(x_off, y_off, x_off+width, y_off+height);
|
|
|
|
intersect = t.backward(feature_raster_extent);
|
|
|
|
|
|
|
|
image_data_32 image(width,height);
|
|
|
|
reader->read(x_off,y_off,image);
|
|
|
|
feature->set_raster(mapnik::raster_ptr(new raster(intersect,image)));
|
|
|
|
}
|
|
|
|
}
|
2007-03-16 11:11:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2010-11-19 00:15:59 +01:00
|
|
|
std::cerr << "Raster Plugin: exception caught" << std::endl;
|
2007-03-16 11:11:37 +01:00
|
|
|
}
|
2009-04-10 14:05:31 +02:00
|
|
|
|
2007-03-16 11:11:37 +01:00
|
|
|
++curIter_;
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
return feature_ptr();
|
2005-06-14 17:06:59 +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>;
|