2007-04-25 21:15:38 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
//$Id$
|
|
|
|
|
|
|
|
#include "gdal_featureset.hpp"
|
|
|
|
#include <gdal_priv.h>
|
|
|
|
|
2009-12-07 07:45:27 +01:00
|
|
|
// boost
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
2007-04-25 21:15:38 +02:00
|
|
|
using mapnik::query;
|
2009-09-27 19:23:09 +02:00
|
|
|
using mapnik::coord2d;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2007-04-25 21:15:38 +02:00
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::CoordTransform;
|
2009-09-27 19:23:09 +02:00
|
|
|
using mapnik::point_impl;
|
|
|
|
using mapnik::geometry2d;
|
2007-04-25 21:15:38 +02:00
|
|
|
|
2009-09-28 18:56:01 +02:00
|
|
|
|
2009-09-27 19:23:09 +02:00
|
|
|
gdal_featureset::gdal_featureset(GDALDataset & dataset, int band, gdal_query q)
|
2007-04-25 21:15:38 +02:00
|
|
|
: dataset_(dataset),
|
2009-09-27 19:23:09 +02:00
|
|
|
band_(band),
|
|
|
|
gquery_(q),
|
2007-04-25 21:15:38 +02:00
|
|
|
first_(true) {}
|
|
|
|
|
2009-09-27 19:23:09 +02:00
|
|
|
gdal_featureset::~gdal_featureset()
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: closing dataset = " << &dataset_ << "\n";
|
2009-09-27 19:23:09 +02:00
|
|
|
#endif
|
|
|
|
GDALClose(&dataset_);
|
|
|
|
}
|
2007-04-25 21:15:38 +02:00
|
|
|
|
|
|
|
feature_ptr gdal_featureset::next()
|
|
|
|
{
|
|
|
|
if (first_)
|
|
|
|
{
|
|
|
|
first_ = false;
|
2009-09-27 19:23:09 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-10-12 07:25:36 +02:00
|
|
|
std::clog << "GDAL Plugin: featureset, dataset = " << &dataset_ << "\n";
|
2009-09-27 19:23:09 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
|
2009-09-27 19:23:09 +02:00
|
|
|
query *q = boost::get<query>(&gquery_);
|
|
|
|
if(q) {
|
|
|
|
return get_feature(*q);
|
|
|
|
} else {
|
|
|
|
coord2d *p = boost::get<coord2d>(&gquery_);
|
|
|
|
if(p) {
|
|
|
|
return get_feature_at_point(*p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// should never reach here
|
|
|
|
}
|
|
|
|
return feature_ptr();
|
|
|
|
}
|
2009-05-15 12:31:49 +02:00
|
|
|
|
2009-09-27 19:23:09 +02:00
|
|
|
feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
|
|
|
|
{
|
|
|
|
feature_ptr feature(new Feature(1));
|
|
|
|
|
|
|
|
GDALRasterBand * red = 0;
|
|
|
|
GDALRasterBand * green = 0;
|
|
|
|
GDALRasterBand * blue = 0;
|
|
|
|
GDALRasterBand * alpha = 0;
|
|
|
|
GDALRasterBand * grey = 0;
|
|
|
|
|
|
|
|
int nbands = dataset_.GetRasterCount();
|
2009-10-12 07:25:36 +02:00
|
|
|
|
|
|
|
unsigned raster_width = dataset_.GetRasterXSize();
|
|
|
|
unsigned raster_height = dataset_.GetRasterYSize();
|
|
|
|
|
|
|
|
// TODO - pull from class attributes...
|
2009-09-27 19:23:09 +02:00
|
|
|
double tr[6];
|
|
|
|
dataset_.GetGeoTransform(tr);
|
|
|
|
double dx = tr[1];
|
|
|
|
double dy = tr[5];
|
2009-10-12 07:25:36 +02:00
|
|
|
double x0 = tr[0]; // minx
|
|
|
|
double y0 = tr[3]; // miny
|
|
|
|
double x1 = tr[0] + raster_width * dx + raster_height * tr[2]; // maxx
|
|
|
|
double y1 = tr[3] + raster_width * tr[4] + raster_height * dy; // maxy
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> raster_extent(x0,y0,x1,y1);
|
2009-10-12 07:25:36 +02:00
|
|
|
|
|
|
|
CoordTransform t(raster_width,raster_height,raster_extent,0,0);
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> intersect = raster_extent.intersect(q.get_bbox());
|
|
|
|
box2d<double> box = t.forward(intersect);
|
2009-09-27 19:23:09 +02:00
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
// TODO: error check this further...
|
|
|
|
float x_off_f = (intersect.minx()-raster_extent.minx()) / fabs(dx);
|
|
|
|
float y_off_f = (raster_extent.maxy()-intersect.maxy()) / fabs(dy);
|
|
|
|
|
|
|
|
if (x_off_f < 0)
|
|
|
|
{
|
|
|
|
x_off_f = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y_off_f < 0)
|
|
|
|
{
|
|
|
|
y_off_f = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x_off = static_cast<int>(x_off_f);
|
|
|
|
int y_off = static_cast<int>(y_off_f);
|
2009-05-15 12:31:49 +02:00
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
int width = int(box.width() + 0.5);
|
|
|
|
int height = int(box.height() + 0.5);
|
|
|
|
|
2009-05-15 12:31:49 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Raster extent=" << raster_extent << "\n";
|
|
|
|
std::clog << "GDAL Plugin: View extent=" << intersect << "\n";
|
|
|
|
std::clog << "GDAL Plugin: Query resolution=" << q.resolution() << "\n";
|
|
|
|
std::clog << boost::format("GDAL Plugin: StartX=%d StartY=%d Width=%d Height=%d \n") % x_off % y_off % width % height;
|
2009-05-15 12:31:49 +02:00
|
|
|
#endif
|
|
|
|
|
2009-09-27 19:23:09 +02:00
|
|
|
if (width > 0 && height > 0)
|
|
|
|
{
|
2009-10-12 07:25:36 +02:00
|
|
|
int im_width = int(q.resolution() * intersect.width() + 0.5);
|
|
|
|
int im_height = int(q.resolution() * intersect.height() + 0.5);
|
2009-09-27 19:23:09 +02:00
|
|
|
|
2009-10-22 18:58:03 +02:00
|
|
|
// case where we need to avoid upsampling so that the
|
|
|
|
// image can be later scaled within raster_symbolizer
|
|
|
|
if (im_width >= width || im_height >= height)
|
|
|
|
{
|
|
|
|
im_width = width;
|
|
|
|
im_height = height;
|
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
mapnik::image_data_32 image(im_width, im_height);
|
2009-09-27 19:23:09 +02:00
|
|
|
image.set(0xffffffff);
|
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Image Size=(" << im_width << "," << im_height << ")\n";
|
|
|
|
std::clog << "GDAL Plugin: Reading band " << band_ << "\n";
|
2009-09-27 19:23:09 +02:00
|
|
|
#endif
|
2009-09-28 18:56:01 +02:00
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
if (band_>0) // we are querying a single band
|
2009-09-27 19:23:09 +02:00
|
|
|
{
|
|
|
|
float *imageData = (float*)image.getBytes();
|
|
|
|
GDALRasterBand * band = dataset_.GetRasterBand(band_);
|
2009-10-12 07:25:36 +02:00
|
|
|
band->RasterIO(GF_Read, x_off, y_off, width, height,
|
2009-09-27 19:23:09 +02:00
|
|
|
imageData, image.width(), image.height(),
|
|
|
|
GDT_Float32, 0, 0);
|
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersect,image)));
|
2009-09-27 19:23:09 +02:00
|
|
|
}
|
2009-09-28 18:56:01 +02:00
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
else // working with all bands
|
2007-04-25 21:15:38 +02:00
|
|
|
{
|
2009-05-15 12:31:49 +02:00
|
|
|
for (int i = 0; i < nbands; ++i)
|
|
|
|
{
|
|
|
|
GDALRasterBand * band = dataset_.GetRasterBand(i+1);
|
|
|
|
|
2009-09-28 18:56:01 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-10-12 07:25:36 +02:00
|
|
|
get_overview_meta(band);
|
2009-05-15 12:31:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
GDALColorInterp color_interp = band->GetColorInterpretation();
|
|
|
|
switch (color_interp)
|
|
|
|
{
|
|
|
|
case GCI_RedBand:
|
|
|
|
red = band;
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Found red band" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
break;
|
|
|
|
case GCI_GreenBand:
|
|
|
|
green = band;
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Found green band" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
break;
|
|
|
|
case GCI_BlueBand:
|
|
|
|
blue = band;
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Found blue band" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
break;
|
|
|
|
case GCI_AlphaBand:
|
|
|
|
alpha = band;
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Found alpha band" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
break;
|
|
|
|
case GCI_GrayIndex:
|
|
|
|
grey = band;
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Found gray band" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
break;
|
|
|
|
case GCI_PaletteIndex:
|
2009-09-28 18:56:01 +02:00
|
|
|
{
|
2009-05-15 12:31:49 +02:00
|
|
|
grey = band;
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Found gray band, and colortable..." << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
GDALColorTable *color_table = band->GetColorTable();
|
2009-09-27 19:23:09 +02:00
|
|
|
|
2009-05-15 12:31:49 +02:00
|
|
|
if ( color_table)
|
|
|
|
{
|
|
|
|
int count = color_table->GetColorEntryCount();
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Color Table count = " << count << "\n";
|
2009-05-15 12:31:49 +02:00
|
|
|
#endif
|
|
|
|
for ( int i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
const GDALColorEntry *ce = color_table->GetColorEntry ( i );
|
|
|
|
if (!ce ) continue;
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Color entry RGB (" << ce->c1 << "," <<ce->c2 << "," << ce->c3 << ")\n";
|
2009-05-15 12:31:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
2009-09-28 18:56:01 +02:00
|
|
|
}
|
2009-05-15 12:31:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GCI_Undefined:
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: Found undefined band (assumming gray band)" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
grey = band;
|
2009-09-28 18:56:01 +02:00
|
|
|
break;
|
2009-05-15 12:31:49 +02:00
|
|
|
default:
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: band type unknown!" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
2009-05-15 12:31:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-27 19:23:09 +02:00
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
if (red && green && blue)
|
2007-04-25 21:15:38 +02:00
|
|
|
{
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: processing rgb bands..." << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
|
|
|
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());
|
2007-04-25 21:15:38 +02:00
|
|
|
}
|
|
|
|
else if (grey)
|
|
|
|
{
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: processing gray band..." << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
|
|
|
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());
|
2007-04-25 21:15:38 +02:00
|
|
|
}
|
2009-05-15 12:31:49 +02:00
|
|
|
|
|
|
|
if (alpha)
|
|
|
|
{
|
2009-10-12 07:25:36 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: processing alpha band..." << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
#endif
|
|
|
|
alpha->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 3,
|
|
|
|
image.width(),image.height(),GDT_Byte,4,4*image.width());
|
2009-05-15 12:31:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 07:25:36 +02:00
|
|
|
feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersect,image)));
|
2009-09-27 19:23:09 +02:00
|
|
|
}
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
return feature_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
feature_ptr gdal_featureset::get_feature_at_point(mapnik::coord2d const& pt)
|
|
|
|
{
|
|
|
|
if (band_>0) {
|
|
|
|
|
|
|
|
unsigned raster_xsize = dataset_.GetRasterXSize();
|
|
|
|
unsigned raster_ysize = dataset_.GetRasterYSize();
|
|
|
|
|
|
|
|
double gt[6];
|
|
|
|
dataset_.GetGeoTransform(gt);
|
|
|
|
|
|
|
|
double det = gt[1]*gt[5] - gt[2]*gt[4];
|
|
|
|
// 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;
|
2009-09-28 18:56:01 +02:00
|
|
|
unsigned x = det2/det, y = det1/det;
|
2009-09-27 19:23:09 +02:00
|
|
|
|
|
|
|
if(0<=x && x<raster_xsize && 0<=y && y<raster_ysize) {
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << boost::format("GDAL Plugin: pt.x=%f pt.y=%f\n") % pt.x % pt.y;
|
|
|
|
std::clog << boost::format("GDAL Plugin: x=%f y=%f\n") % x % y;
|
2009-09-27 19:23:09 +02:00
|
|
|
#endif
|
|
|
|
GDALRasterBand * band = dataset_.GetRasterBand(band_);
|
|
|
|
int hasNoData;
|
|
|
|
double nodata = band->GetNoDataValue(&hasNoData);
|
|
|
|
double value;
|
|
|
|
band->RasterIO(GF_Read, x, y, 1, 1, &value, 1, 1, GDT_Float64, 0, 0);
|
|
|
|
if(!hasNoData || value!=nodata) {
|
|
|
|
// construct feature
|
|
|
|
feature_ptr feature(new Feature(1));
|
|
|
|
geometry2d * point = new point_impl;
|
|
|
|
point->move_to(pt.x, pt.y);
|
|
|
|
feature->add_geometry(point);
|
|
|
|
(*feature)["value"] = value;
|
|
|
|
return feature;
|
|
|
|
}
|
2007-04-25 21:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return feature_ptr();
|
|
|
|
}
|
2009-10-12 07:25:36 +02:00
|
|
|
|
|
|
|
void gdal_featureset::get_overview_meta(GDALRasterBand * band)
|
|
|
|
{
|
|
|
|
int band_overviews = band->GetOverviewCount();
|
|
|
|
if (band_overviews > 0)
|
|
|
|
{
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: "<< band_overviews << " overviews found!" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
for (int b = 0; b < band_overviews; b++)
|
|
|
|
{
|
|
|
|
GDALRasterBand * overview = band->GetOverview (b);
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << boost::format("GDAL Plugin: Overview=%d Width=%d Height=%d \n")
|
2009-10-12 07:25:36 +02:00
|
|
|
% b % overview->GetXSize() % overview->GetYSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << "GDAL Plugin: No overviews found!" << "\n";
|
2009-10-12 07:25:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int bsx,bsy;
|
|
|
|
double scale;
|
|
|
|
band->GetBlockSize(&bsx,&bsy);
|
|
|
|
scale = band->GetScale();
|
2009-12-16 21:02:06 +01:00
|
|
|
std::clog << boost::format("GDAL Plugin: Block=%dx%d Scale=%f Type=%s Color=%s \n") % bsx % bsy % scale
|
2009-10-12 07:25:36 +02:00
|
|
|
% GDALGetDataTypeName(band->GetRasterDataType())
|
|
|
|
% GDALGetColorInterpretationName(band->GetColorInterpretation());
|
|
|
|
}
|