2007-04-25 19:15:38 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2007-04-25 19:15:38 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-22 00:46:29 +00:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2007-04-25 19:15:38 +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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-05-16 22:17:50 +00:00
|
|
|
// mapnik
|
2011-11-08 11:50:56 +00:00
|
|
|
#include <mapnik/global.hpp>
|
2011-05-16 22:17:50 +00:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
|
|
|
|
2007-04-25 19:15:38 +00:00
|
|
|
#include "gdal_featureset.hpp"
|
|
|
|
#include <gdal_priv.h>
|
|
|
|
|
2009-12-07 06:45:27 +00:00
|
|
|
// boost
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
2007-04-25 19:15:38 +00:00
|
|
|
using mapnik::query;
|
2009-09-27 17:23:09 +00:00
|
|
|
using mapnik::coord2d;
|
2009-12-16 20:02:06 +00:00
|
|
|
using mapnik::box2d;
|
2007-04-25 19:15:38 +00:00
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::CoordTransform;
|
2010-11-03 13:19:15 +00:00
|
|
|
using mapnik::geometry_type;
|
2010-09-19 17:52:17 +00:00
|
|
|
using mapnik::datasource_exception;
|
2011-05-16 22:17:50 +00:00
|
|
|
using mapnik::feature_factory;
|
2007-04-25 19:15:38 +00:00
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
#ifdef _WINDOWS
|
2011-11-08 11:50:56 +00:00
|
|
|
using mapnik::rint;
|
2011-11-14 03:37:50 +00:00
|
|
|
#endif
|
2009-09-28 16:56:01 +00:00
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
gdal_featureset::gdal_featureset(GDALDataset& dataset,
|
|
|
|
int band,
|
|
|
|
gdal_query q,
|
|
|
|
mapnik::box2d<double> extent,
|
|
|
|
double width,
|
|
|
|
double height,
|
|
|
|
int nbands,
|
|
|
|
double dx,
|
|
|
|
double dy,
|
|
|
|
double filter_factor)
|
2011-11-14 03:37:50 +00:00
|
|
|
: dataset_(dataset),
|
2012-01-17 22:58:00 +00:00
|
|
|
ctx_(boost::make_shared<mapnik::context>()),
|
2011-11-14 03:37:50 +00:00
|
|
|
band_(band),
|
|
|
|
gquery_(q),
|
|
|
|
raster_extent_(extent),
|
|
|
|
raster_width_(width),
|
|
|
|
raster_height_(height),
|
|
|
|
dx_(dx),
|
|
|
|
dy_(dy),
|
|
|
|
nbands_(nbands),
|
|
|
|
filter_factor_(filter_factor),
|
2012-01-17 22:58:00 +00:00
|
|
|
first_(true)
|
2010-11-14 14:54:28 +00:00
|
|
|
{
|
2012-01-16 22:55:44 +00:00
|
|
|
ctx_->push("NODATA");
|
2010-11-14 14:54:28 +00:00
|
|
|
}
|
2007-04-25 19:15:38 +00:00
|
|
|
|
2010-05-30 03:23:59 +00:00
|
|
|
gdal_featureset::~gdal_featureset()
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: closing dataset = " << &dataset_ << std::endl;
|
2010-05-30 03:23:59 +00:00
|
|
|
#endif
|
2011-10-22 00:46:29 +00:00
|
|
|
|
2010-05-30 03:23:59 +00:00
|
|
|
GDALClose(&dataset_);
|
|
|
|
}
|
2007-04-25 19:15:38 +00:00
|
|
|
|
|
|
|
feature_ptr gdal_featureset::next()
|
|
|
|
{
|
2010-02-03 16:56:42 +00:00
|
|
|
if (first_)
|
|
|
|
{
|
2010-08-18 20:42:00 +00:00
|
|
|
first_ = false;
|
2010-05-30 03:23:59 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: featureset, dataset = " << &dataset_ << std::endl;
|
2010-05-30 03:23:59 +00:00
|
|
|
#endif
|
2009-05-15 10:31:49 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
query *q = boost::get<query>(&gquery_);
|
2011-10-22 00:46:29 +00:00
|
|
|
if (q)
|
|
|
|
{
|
2010-08-18 20:42:00 +00:00
|
|
|
return get_feature(*q);
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-18 20:42:00 +00:00
|
|
|
coord2d *p = boost::get<coord2d>(&gquery_);
|
2011-10-22 00:46:29 +00:00
|
|
|
if (p)
|
|
|
|
{
|
2010-08-18 20:42:00 +00:00
|
|
|
return get_feature_at_point(*p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// should never reach here
|
2010-02-03 16:56:42 +00:00
|
|
|
}
|
|
|
|
return feature_ptr();
|
2009-09-27 17:23:09 +00:00
|
|
|
}
|
2009-05-15 10:31:49 +00:00
|
|
|
|
2009-09-27 17:23:09 +00:00
|
|
|
feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
|
|
|
|
{
|
2012-01-16 22:55:44 +00:00
|
|
|
feature_ptr feature = feature_factory::create(ctx_,1);
|
|
|
|
|
2010-02-03 16:56:42 +00:00
|
|
|
GDALRasterBand * red = 0;
|
|
|
|
GDALRasterBand * green = 0;
|
|
|
|
GDALRasterBand * blue = 0;
|
|
|
|
GDALRasterBand * alpha = 0;
|
2011-11-14 03:37:50 +00:00
|
|
|
GDALRasterBand * grey = 0;
|
2012-01-16 22:55:44 +00:00
|
|
|
|
2010-09-18 19:19:27 +00:00
|
|
|
/*
|
2011-11-14 03:37:50 +00:00
|
|
|
double tr[6];
|
|
|
|
dataset_.GetGeoTransform(tr);
|
|
|
|
|
|
|
|
double dx = tr[1];
|
|
|
|
double dy = tr[5];
|
|
|
|
std::clog << "dx_: " << dx_ << " dx: " << dx << " dy_: " << dy_ << "dy: " << dy << "\n";
|
2010-09-18 19:19:27 +00:00
|
|
|
*/
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
CoordTransform t(raster_width_, raster_height_, raster_extent_, 0, 0);
|
2010-09-18 19:19:27 +00:00
|
|
|
box2d<double> intersect = raster_extent_.intersect(q.get_bbox());
|
2010-02-03 16:56:42 +00:00
|
|
|
box2d<double> box = t.forward(intersect);
|
2009-09-27 17:23:09 +00:00
|
|
|
|
2010-03-17 08:34:43 +00:00
|
|
|
//size of resized output pixel in source image domain
|
2011-10-22 00:46:29 +00:00
|
|
|
double margin_x = 1.0 / (fabs(dx_) * boost::get<0>(q.resolution()));
|
|
|
|
double margin_y = 1.0 / (fabs(dy_) * boost::get<1>(q.resolution()));
|
2010-03-17 08:34:43 +00:00
|
|
|
if (margin_x < 1)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-03-17 08:34:43 +00:00
|
|
|
margin_x = 1.0;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2010-03-17 08:34:43 +00:00
|
|
|
if (margin_y < 1)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-03-17 08:34:43 +00:00
|
|
|
margin_y = 1.0;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
|
|
|
|
2010-03-03 03:23:53 +00:00
|
|
|
//select minimum raster containing whole box
|
2010-03-17 08:34:43 +00:00
|
|
|
int x_off = rint(box.minx() - margin_x);
|
|
|
|
int y_off = rint(box.miny() - margin_y);
|
|
|
|
int end_x = rint(box.maxx() + margin_x);
|
|
|
|
int end_y = rint(box.maxy() + margin_y);
|
2011-10-22 00:46:29 +00:00
|
|
|
|
2010-03-03 03:23:53 +00:00
|
|
|
//clip to available data
|
|
|
|
if (x_off < 0)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-03-03 03:23:53 +00:00
|
|
|
x_off = 0;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2010-03-03 03:23:53 +00:00
|
|
|
if (y_off < 0)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-03-03 03:23:53 +00:00
|
|
|
y_off = 0;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2010-09-18 19:19:27 +00:00
|
|
|
if (end_x > (int)raster_width_)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-09-18 19:19:27 +00:00
|
|
|
end_x = raster_width_;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2010-09-18 19:19:27 +00:00
|
|
|
if (end_y > (int)raster_height_)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-09-18 19:19:27 +00:00
|
|
|
end_y = raster_height_;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2010-03-03 03:23:53 +00:00
|
|
|
int width = end_x - x_off;
|
|
|
|
int height = end_y - y_off;
|
2011-10-22 00:46:29 +00:00
|
|
|
|
2010-03-03 03:23:53 +00:00
|
|
|
// don't process almost invisible data
|
|
|
|
if (box.width() < 0.5)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-03-03 03:23:53 +00:00
|
|
|
width = 0;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2010-03-03 03:23:53 +00:00
|
|
|
if (box.height() < 0.5)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-03-03 03:23:53 +00:00
|
|
|
height = 0;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
|
|
|
|
2010-03-03 03:23:53 +00:00
|
|
|
//calculate actual box2d of returned raster
|
2011-10-22 00:46:29 +00:00
|
|
|
box2d<double> feature_raster_extent(x_off, y_off, x_off + width, y_off + height);
|
2010-03-03 03:23:53 +00:00
|
|
|
intersect = t.backward(feature_raster_extent);
|
2011-11-14 03:37:50 +00:00
|
|
|
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Raster extent=" << raster_extent_ << std::endl;
|
|
|
|
std::clog << "GDAL Plugin: View extent=" << intersect << std::endl;
|
|
|
|
std::clog << "GDAL Plugin: Query resolution=" << boost::get<0>(q.resolution()) << "," << boost::get<1>(q.resolution()) << std::endl;
|
|
|
|
std::clog << boost::format("GDAL Plugin: StartX=%d StartY=%d Width=%d Height=%d") % x_off % y_off % width % height << std::endl;
|
2009-05-15 10:31:49 +00:00
|
|
|
#endif
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-02-03 16:56:42 +00:00
|
|
|
if (width > 0 && height > 0)
|
|
|
|
{
|
2010-08-18 20:42:00 +00:00
|
|
|
double width_res = boost::get<0>(q.resolution());
|
|
|
|
double height_res = boost::get<1>(q.resolution());
|
|
|
|
int im_width = int(width_res * intersect.width() + 0.5);
|
|
|
|
int im_height = int(height_res * intersect.height() + 0.5);
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
// if layer-level filter_factor is set, apply it
|
|
|
|
if (filter_factor_)
|
|
|
|
{
|
|
|
|
im_width *= filter_factor_;
|
|
|
|
im_height *= filter_factor_;
|
2011-01-27 03:11:22 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "GDAL Plugin: applying layer filter_factor: " << filter_factor_ << "\n";
|
|
|
|
#endif
|
2010-08-18 20:42:00 +00:00
|
|
|
}
|
|
|
|
// otherwise respect symbolizer level factor applied to query, default of 1.0
|
|
|
|
else
|
|
|
|
{
|
|
|
|
double sym_downsample_factor = q.get_filter_factor();
|
|
|
|
im_width *= sym_downsample_factor;
|
|
|
|
im_height *= sym_downsample_factor;
|
|
|
|
}
|
2010-06-20 04:06:23 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
// case where we need to avoid upsampling so that the
|
2011-11-14 03:37:50 +00:00
|
|
|
// image can be later scaled within raster_symbolizer
|
2010-08-18 20:42:00 +00:00
|
|
|
if (im_width >= width || im_height >= height)
|
|
|
|
{
|
|
|
|
im_width = width;
|
|
|
|
im_height = height;
|
|
|
|
}
|
2009-05-15 10:31:49 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
if (im_width > 0 && im_height > 0)
|
|
|
|
{
|
|
|
|
mapnik::image_data_32 image(im_width, im_height);
|
2011-11-14 03:37:50 +00:00
|
|
|
image.set(0xffffffff);
|
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Image Size=(" << im_width << "," << im_height << ")" << std::endl;
|
|
|
|
std::clog << "GDAL Plugin: Reading band " << band_ << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
typedef std::vector<int,int> pallete;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-11-14 14:54:28 +00:00
|
|
|
if (band_ > 0) // we are querying a single band
|
2010-08-18 20:42:00 +00:00
|
|
|
{
|
2010-09-19 17:52:17 +00:00
|
|
|
if (band_ > nbands_)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2010-11-14 14:54:28 +00:00
|
|
|
throw datasource_exception((boost::format("GDAL Plugin: '%d' is an invalid band, dataset only has '%d' bands\n") % band_ % nbands_).str());
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2011-05-04 00:20:17 +00:00
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
float* imageData = (float*)image.getBytes();
|
2010-08-18 20:42:00 +00:00
|
|
|
GDALRasterBand * band = dataset_.GetRasterBand(band_);
|
2011-05-04 00:20:17 +00:00
|
|
|
int hasNoData;
|
|
|
|
double nodata = band->GetNoDataValue(&hasNoData);
|
2010-08-18 20:42:00 +00:00
|
|
|
band->RasterIO(GF_Read, x_off, y_off, width, height,
|
|
|
|
imageData, image.width(), image.height(),
|
|
|
|
GDT_Float32, 0, 0);
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-01-16 22:55:44 +00:00
|
|
|
feature->set_raster(boost::make_shared<mapnik::raster>(intersect,image));
|
2011-05-04 00:20:17 +00:00
|
|
|
if (hasNoData)
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2012-01-16 22:55:44 +00:00
|
|
|
feature->put("NODATA",nodata);
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2010-08-18 20:42:00 +00:00
|
|
|
}
|
|
|
|
else // working with all bands
|
|
|
|
{
|
2010-09-18 19:19:27 +00:00
|
|
|
for (int i = 0; i < nbands_; ++i)
|
2010-08-18 20:42:00 +00:00
|
|
|
{
|
2011-10-22 00:46:29 +00:00
|
|
|
GDALRasterBand * band = dataset_.GetRasterBand(i + 1);
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-22 00:46:29 +00:00
|
|
|
get_overview_meta(band);
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
GDALColorInterp color_interp = band->GetColorInterpretation();
|
|
|
|
switch (color_interp)
|
|
|
|
{
|
|
|
|
case GCI_RedBand:
|
|
|
|
red = band;
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Found red band" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case GCI_GreenBand:
|
|
|
|
green = band;
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Found green band" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case GCI_BlueBand:
|
|
|
|
blue = band;
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Found blue band" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case GCI_AlphaBand:
|
|
|
|
alpha = band;
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Found alpha band" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case GCI_GrayIndex:
|
|
|
|
grey = band;
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Found gray band" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case GCI_PaletteIndex:
|
|
|
|
{
|
|
|
|
grey = band;
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Found gray band, and colortable..." << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
GDALColorTable *color_table = band->GetColorTable();
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
if (color_table)
|
2010-08-18 20:42:00 +00:00
|
|
|
{
|
|
|
|
int count = color_table->GetColorEntryCount();
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Color Table count = " << count << std::endl;
|
2011-11-14 03:37:50 +00:00
|
|
|
#endif
|
2010-11-14 14:54:28 +00:00
|
|
|
for (int j = 0; j < count; j++)
|
2010-08-18 20:42:00 +00:00
|
|
|
{
|
2010-11-14 14:54:28 +00:00
|
|
|
const GDALColorEntry *ce = color_table->GetColorEntry (j);
|
|
|
|
if (! ce) continue;
|
2010-08-18 20:42:00 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-11-14 03:37:50 +00:00
|
|
|
std::clog << "GDAL Plugin: Color entry RGB (" << ce->c1 << "," <<ce->c2 << "," << ce->c3 << ")" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GCI_Undefined:
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: Found undefined band (assumming gray band)" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
grey = band;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: band type unknown!" << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
if (red && green && blue)
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: processing rgb bands..." << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
2011-03-11 21:28:18 +00:00
|
|
|
int hasNoData;
|
|
|
|
float nodata = red->GetNoDataValue(&hasNoData);
|
|
|
|
GDALColorTable *color_table = red->GetColorTable();
|
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
if (! alpha && hasNoData && ! color_table)
|
2011-03-11 21:28:18 +00:00
|
|
|
{
|
|
|
|
// first read the data in and create an alpha channel from the nodata values
|
2011-10-22 00:46:29 +00:00
|
|
|
float* imageData = (float*)image.getBytes();
|
2011-03-11 21:28:18 +00:00
|
|
|
red->RasterIO(GF_Read, x_off, y_off, width, height,
|
2011-10-22 00:46:29 +00:00
|
|
|
imageData, image.width(), image.height(),
|
|
|
|
GDT_Float32, 0, 0);
|
2011-03-11 21:28:18 +00:00
|
|
|
|
|
|
|
int len = image.width() * image.height();
|
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
for (int i = 0; i < len; ++i)
|
2011-03-11 21:28:18 +00:00
|
|
|
{
|
|
|
|
if (nodata == imageData[i])
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
|
|
|
*reinterpret_cast<unsigned *>(&imageData[i]) = 0;
|
|
|
|
}
|
2011-03-11 21:28:18 +00:00
|
|
|
else
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
|
|
|
*reinterpret_cast<unsigned *>(&imageData[i]) = 0xFFFFFFFF;
|
|
|
|
}
|
2011-03-11 21:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
red->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 0,
|
|
|
|
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
|
|
|
|
green->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 1,
|
|
|
|
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
|
|
|
|
blue->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 2,
|
|
|
|
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
|
2010-08-18 20:42:00 +00:00
|
|
|
}
|
|
|
|
else if (grey)
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: processing gray band..." << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
2011-03-11 21:28:18 +00:00
|
|
|
int hasNoData;
|
|
|
|
float nodata = grey->GetNoDataValue(&hasNoData);
|
2011-10-22 00:46:29 +00:00
|
|
|
GDALColorTable* color_table = grey->GetColorTable();
|
2011-03-11 21:28:18 +00:00
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
if (hasNoData && ! color_table)
|
2011-03-11 21:28:18 +00:00
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "\tno data value for layer: " << nodata << std::endl;
|
|
|
|
#endif
|
|
|
|
// first read the data in and create an alpha channel from the nodata values
|
2011-10-22 00:46:29 +00:00
|
|
|
float* imageData = (float*)image.getBytes();
|
2011-03-11 21:28:18 +00:00
|
|
|
grey->RasterIO(GF_Read, x_off, y_off, width, height,
|
2011-10-22 00:46:29 +00:00
|
|
|
imageData, image.width(), image.height(),
|
|
|
|
GDT_Float32, 0, 0);
|
2011-03-11 21:28:18 +00:00
|
|
|
|
|
|
|
int len = image.width() * image.height();
|
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
for (int i = 0; i < len; ++i)
|
2011-03-11 21:28:18 +00:00
|
|
|
{
|
|
|
|
if (nodata == imageData[i])
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
|
|
|
*reinterpret_cast<unsigned *>(&imageData[i]) = 0;
|
|
|
|
}
|
2011-03-11 21:28:18 +00:00
|
|
|
else
|
2011-10-22 00:46:29 +00:00
|
|
|
{
|
2011-03-11 21:28:18 +00:00
|
|
|
*reinterpret_cast<unsigned *> (&imageData[i]) = 0xFFFFFFFF;
|
2011-10-22 00:46:29 +00:00
|
|
|
}
|
2011-03-11 21:28:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
grey->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 0,
|
|
|
|
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
|
|
|
|
grey->RasterIO(GF_Read,x_off, y_off, width, height, image.getBytes() + 1,
|
|
|
|
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
|
|
|
|
grey->RasterIO(GF_Read,x_off, y_off, width, height, image.getBytes() + 2,
|
|
|
|
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
|
2011-01-27 03:11:22 +00:00
|
|
|
|
|
|
|
if (color_table)
|
|
|
|
{
|
2011-03-11 21:28:18 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "GDAL Plugin: Loading colour table..." << std::endl;
|
|
|
|
#endif
|
2011-01-27 03:11:22 +00:00
|
|
|
for (unsigned y = 0; y < image.height(); ++y)
|
|
|
|
{
|
|
|
|
unsigned int* row = image.getRow(y);
|
|
|
|
for (unsigned x = 0; x < image.width(); ++x)
|
|
|
|
{
|
|
|
|
unsigned value = row[x] & 0xff;
|
2011-10-22 00:46:29 +00:00
|
|
|
const GDALColorEntry *ce = color_table->GetColorEntry(value);
|
|
|
|
if (ce )
|
|
|
|
{
|
2011-01-27 03:11:22 +00:00
|
|
|
// TODO - big endian support
|
|
|
|
row[x] = (ce->c4 << 24)| (ce->c3 << 16) | (ce->c2 << 8) | (ce->c1) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-18 20:42:00 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: processing alpha band..." << std::endl;
|
2010-08-18 20:42:00 +00:00
|
|
|
#endif
|
2011-10-22 00:46:29 +00:00
|
|
|
alpha->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 3,
|
|
|
|
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
|
2010-08-18 20:42:00 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersect, image)));
|
2010-08-18 20:42:00 +00:00
|
|
|
}
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return feature_ptr();
|
2009-09-27 17:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
feature_ptr gdal_featureset::get_feature_at_point(mapnik::coord2d const& pt)
|
|
|
|
{
|
2010-11-14 14:54:28 +00:00
|
|
|
if (band_ > 0)
|
|
|
|
{
|
2010-08-18 20:42:00 +00:00
|
|
|
unsigned raster_xsize = dataset_.GetRasterXSize();
|
|
|
|
unsigned raster_ysize = dataset_.GetRasterYSize();
|
2009-09-27 17:23:09 +00:00
|
|
|
|
2010-08-18 20:42:00 +00:00
|
|
|
double gt[6];
|
|
|
|
dataset_.GetGeoTransform(gt);
|
2009-09-27 17:23:09 +00:00
|
|
|
|
2011-10-22 00:46:29 +00:00
|
|
|
double det = gt[1] * gt[5] - gt[2] * gt[4];
|
2010-08-18 20:42:00 +00:00
|
|
|
// subtract half a pixel width & height because gdal coord reference
|
|
|
|
// is the top-left corner of a pixel, not the center.
|
|
|
|
double X = pt.x - gt[0] - gt[1]/2;
|
|
|
|
double Y = pt.y - gt[3] - gt[5]/2;
|
|
|
|
double det1 = gt[1]*Y + gt[4]*X;
|
|
|
|
double det2 = gt[2]*Y + gt[5]*X;
|
|
|
|
unsigned x = det2/det, y = det1/det;
|
2009-09-27 17:23:09 +00:00
|
|
|
|
2010-11-14 14:54:28 +00:00
|
|
|
if (x < raster_xsize && y < raster_ysize)
|
|
|
|
{
|
2010-11-18 22:17:09 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << boost::format("GDAL Plugin: pt.x=%f pt.y=%f") % pt.x % pt.y << std::endl;
|
|
|
|
std::clog << boost::format("GDAL Plugin: x=%f y=%f") % x % y << std::endl;
|
2009-09-27 17:23:09 +00:00
|
|
|
#endif
|
2011-10-22 00:46:29 +00:00
|
|
|
GDALRasterBand* band = dataset_.GetRasterBand(band_);
|
2010-08-18 20:42:00 +00:00
|
|
|
int hasNoData;
|
|
|
|
double nodata = band->GetNoDataValue(&hasNoData);
|
|
|
|
double value;
|
|
|
|
band->RasterIO(GF_Read, x, y, 1, 1, &value, 1, 1, GDT_Float64, 0, 0);
|
2010-11-14 14:54:28 +00:00
|
|
|
|
|
|
|
if (! hasNoData || value != nodata)
|
|
|
|
{
|
2012-01-16 22:55:44 +00:00
|
|
|
// construct feature
|
|
|
|
feature_ptr feature = feature_factory::create(ctx_,1);
|
2010-11-03 13:19:15 +00:00
|
|
|
geometry_type * point = new geometry_type(mapnik::Point);
|
2010-08-18 20:42:00 +00:00
|
|
|
point->move_to(pt.x, pt.y);
|
|
|
|
feature->add_geometry(point);
|
2012-01-16 22:55:44 +00:00
|
|
|
feature->put("value",value);
|
2010-08-18 20:42:00 +00:00
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
}
|
2010-02-03 16:56:42 +00:00
|
|
|
}
|
|
|
|
return feature_ptr();
|
2007-04-25 19:15:38 +00:00
|
|
|
}
|
2009-10-12 05:25:36 +00:00
|
|
|
|
2010-12-20 19:17:56 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-22 00:46:29 +00:00
|
|
|
void gdal_featureset::get_overview_meta(GDALRasterBand* band)
|
2009-10-12 05:25:36 +00:00
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
int band_overviews = band->GetOverviewCount();
|
2009-10-12 05:25:36 +00:00
|
|
|
if (band_overviews > 0)
|
|
|
|
{
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << "GDAL Plugin: "<< band_overviews << " overviews found!" << std::endl;
|
|
|
|
|
2009-10-12 05:25:36 +00:00
|
|
|
for (int b = 0; b < band_overviews; b++)
|
|
|
|
{
|
2011-10-22 00:46:29 +00:00
|
|
|
GDALRasterBand * overview = band->GetOverview(b);
|
2010-11-18 22:17:09 +00:00
|
|
|
std::clog << boost::format("GDAL Plugin: Overview=%d Width=%d Height=%d")
|
|
|
|
% b % overview->GetXSize() % overview->GetYSize() << std::endl;
|
2009-10-12 05:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
std::clog << "GDAL Plugin: No overviews found!" << std::endl;
|
2009-10-12 05:25:36 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2009-10-12 05:25:36 +00:00
|
|
|
int bsx,bsy;
|
|
|
|
double scale;
|
2011-10-22 00:46:29 +00:00
|
|
|
band->GetBlockSize(&bsx, &bsy);
|
2009-10-12 05:25:36 +00:00
|
|
|
scale = band->GetScale();
|
2010-11-18 22:17:09 +00:00
|
|
|
|
|
|
|
std::clog << boost::format("GDAL Plugin: Block=%dx%d Scale=%f Type=%s Color=%s") % bsx % bsy % scale
|
2010-08-18 20:42:00 +00:00
|
|
|
% GDALGetDataTypeName(band->GetRasterDataType())
|
2011-11-14 03:37:50 +00:00
|
|
|
% GDALGetColorInterpretationName(band->GetColorInterpretation()) << std::endl;
|
2009-10-12 05:25:36 +00:00
|
|
|
}
|
2010-12-20 19:17:56 +00:00
|
|
|
#endif
|