1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2021 Artem Pavlenko
*
* 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
*
*****************************************************************************/
#ifndef RASTER_FEATURESET_HPP
#define RASTER_FEATURESET_HPP
#include "raster_datasource.hpp"
#include "raster_info.hpp"
// mapnik
#include <mapnik/feature.hpp>
#include <mapnik/debug.hpp>
// stl
#include <vector>
// boost
#include <boost/utility.hpp>
class single_file_policy
{
raster_info info_;
public:
class const_iterator
{
enum iterator_e {start,end};
bool status_;
const single_file_policy* p_;
public:
explicit const_iterator(const single_file_policy* p)
: status_(start),
p_(p) {}
const_iterator()
: status_(end) {}
const_iterator(const const_iterator& other)
: status_(other.status_),
p_(other.p_) {}
const_iterator& operator++()
{
status_ = end;
return *this;
}
const raster_info& operator*() const
{
return p_->info_;
}
const raster_info* operator->() const
{
return &(p_->info_);
}
bool operator!=(const const_iterator& itr)
{
return status_ != itr.status_;
}
};
explicit single_file_policy(const raster_info& info)
: info_(info) {}
const_iterator begin()
{
return const_iterator(this);
}
const_iterator query(const box2d<double>& box)
{
if (box.intersects(info_.envelope()))
{
return begin();
}
return end();
}
const_iterator end()
{
return const_iterator();
}
inline int img_width(int reader_width) const
{
return reader_width;
}
inline int img_height(int reader_height) const
{
return reader_height;
}
inline box2d<double> transform(box2d<double> &) const
{
return box2d<double>(0, 0, 0, 0);
}
};
class tiled_file_policy
{
public:
using const_iterator = std::vector<raster_info>::const_iterator;
tiled_file_policy(std::string const& file,
std::string const& format,
unsigned tile_size,
box2d<double> const& extent,
box2d<double> const& bbox,
unsigned width,
unsigned height)
{
double lox = extent.minx();
double loy = extent.miny();
int max_x = int(std::ceil(double(width) / double(tile_size)));
int max_y = int(std::ceil(double(height) / double(tile_size)));
double pixel_x = extent.width() / double(width);
double pixel_y = extent.height() / double(height);
MAPNIK_LOG_DEBUG(raster) << "tiled_file_policy: Raster Plugin PIXEL SIZE("<< pixel_x << "," << pixel_y << ")";
box2d<double> e = bbox.intersect(extent);
for (int x = 0; x < max_x; ++x)
{
for (int y = 0; y < max_y; ++y)
{
double x0 = lox + x * tile_size * pixel_x;
double y0 = loy + y * tile_size * pixel_y;
double x1 = x0 + tile_size * pixel_x;
double y1 = y0 + tile_size * pixel_y;
if (e.intersects(box2d<double>(x0, y0, x1, y1)))
{
box2d<double> tile_box = e.intersect(box2d<double>(x0,y0,x1,y1));
raster_info info(file,format,tile_box,tile_size,tile_size);
infos_.push_back(info);
}
}
}
MAPNIK_LOG_DEBUG(raster) << "tiled_file_policy: Raster Plugin INFO SIZE=" << infos_.size() << " " << file;
}
const_iterator begin()
{
return infos_.begin();
}
const_iterator end()
{
return infos_.end();
}
inline int img_width(int reader_width) const
{
return reader_width;
}
inline int img_height(int reader_height) const
{
return reader_height;
}
inline box2d<double> transform(box2d<double>&) const
{
return box2d<double>(0, 0, 0, 0);
}
private:
std::vector<raster_info> infos_;
};
class tiled_multi_file_policy
{
public:
using const_iterator = std::vector<raster_info>::const_iterator;
tiled_multi_file_policy(std::string const& file_pattern,
std::string const& format,
unsigned tile_size,
box2d<double> extent,
box2d<double> bbox,
unsigned width,
unsigned height,
unsigned tile_stride)
: image_width_(width),
image_height_(height),
tile_size_(tile_size),
tile_stride_(tile_stride)
{
double lox = extent.minx();
double loy = extent.miny();
//int max_x = int(std::ceil(double(width) / double(tile_size)));
//int max_y = int(std::ceil(double(height) / double(tile_size)));
double pixel_x = extent.width() / double(width);
double pixel_y = extent.height() / double(height);
MAPNIK_LOG_DEBUG(raster) << "tiled_multi_file_policy: Raster Plugin PIXEL SIZE(" << pixel_x << "," << pixel_y << ")";
// intersection of query with extent => new query
box2d<double> e = bbox.intersect(extent);
const int x_min = int(std::floor((e.minx() - lox) / (tile_size * pixel_x)));
const int y_min = int(std::floor((e.miny() - loy) / (tile_size * pixel_y)));
const int x_max = int(std::ceil((e.maxx() - lox) / (tile_size * pixel_x)));
const int y_max = int(std::ceil((e.maxy() - loy) / (tile_size * pixel_y)));
for (int x = x_min; x < x_max; ++x)
{
for (int y = y_min; y < y_max; ++y)
{
// x0, y0, x1, y1 => projection-space image coordinates.
double x0 = lox + x*tile_size*pixel_x;
double y0 = loy + y*tile_size*pixel_y;
double x1 = x0 + tile_size*pixel_x;
double y1 = y0 + tile_size*pixel_y;
// check if it intersects the query
if (e.intersects(box2d<double>(x0,y0,x1,y1)))
{
// tile_box => intersection of tile with query in projection-space.
box2d<double> tile_box = e.intersect(box2d<double>(x0,y0,x1,y1));
std::string file = interpolate(file_pattern, x, y);
raster_info info(file,format,tile_box,tile_size,tile_size);
infos_.push_back(info);
}
}
}
MAPNIK_LOG_DEBUG(raster) << "tiled_multi_file_policy: Raster Plugin INFO SIZE=" << infos_.size() << " " << file_pattern;
}
const_iterator begin()
{
return infos_.begin();
}
const_iterator end()
{
return infos_.end();
}
inline int img_width(int) const
{
return image_width_;
}
inline int img_height(int) const
{
return image_height_;
}
inline box2d<double> transform(box2d<double>& box) const
{
int x_offset = int(std::floor(box.minx() / tile_size_));
int y_offset = int(std::floor(box.miny() / tile_size_));
box2d<double> rem(x_offset * tile_size_,
y_offset * tile_size_,
x_offset * tile_size_,
y_offset * tile_size_);
box.init(box.minx() - rem.minx(),
box.miny() - rem.miny(),
box.maxx() - rem.maxx(),
box.maxy() - rem.maxy());
return rem;
}
private:
std::string interpolate(std::string const& pattern, int x, int y) const;
unsigned int image_width_, image_height_, tile_size_, tile_stride_;
std::vector<raster_info> infos_;
};
template <typename LookupPolicy>
class raster_featureset : public mapnik::Featureset
{
using iterator_type = typename LookupPolicy::const_iterator;
public:
raster_featureset(LookupPolicy const& policy,
box2d<double> const& exttent,
mapnik::query const& q);
virtual ~raster_featureset();
mapnik::feature_ptr next();
private:
LookupPolicy policy_;
mapnik::value_integer feature_id_;
mapnik::context_ptr ctx_;
mapnik::box2d<double> extent_;
mapnik::box2d<double> bbox_;
iterator_type curIter_;
iterator_type endIter_;
double filter_factor_;
};
#endif // RASTER_FEATURESET_HPP
|