+ added a "shared" parameter to select if you want to open the dataset in shared mode (causing problems when multiple accesses to the same file)
+ introduced some more code for supporting overviews (still commented out as default, need revision)
This commit is contained in:
parent
13fa57e61f
commit
3805ee0fab
3 changed files with 140 additions and 79 deletions
|
@ -24,6 +24,10 @@
|
||||||
#include "gdal_datasource.hpp"
|
#include "gdal_datasource.hpp"
|
||||||
#include "gdal_featureset.hpp"
|
#include "gdal_featureset.hpp"
|
||||||
|
|
||||||
|
// mapnik
|
||||||
|
#include <mapnik/ptree_helpers.hpp>
|
||||||
|
#include <mapnik/geom_util.hpp>
|
||||||
|
|
||||||
|
|
||||||
using mapnik::datasource;
|
using mapnik::datasource;
|
||||||
using mapnik::parameters;
|
using mapnik::parameters;
|
||||||
|
@ -40,6 +44,7 @@ using mapnik::datasource_exception;
|
||||||
gdal_datasource::gdal_datasource(parameters const& params)
|
gdal_datasource::gdal_datasource(parameters const& params)
|
||||||
: datasource(params),
|
: datasource(params),
|
||||||
extent_(),
|
extent_(),
|
||||||
|
dataset_(0),
|
||||||
desc_(*params.get<std::string>("type"),"utf-8")
|
desc_(*params.get<std::string>("type"),"utf-8")
|
||||||
{
|
{
|
||||||
GDALAllRegister();
|
GDALAllRegister();
|
||||||
|
@ -53,7 +58,15 @@ gdal_datasource::gdal_datasource(parameters const& params)
|
||||||
else
|
else
|
||||||
dataset_name_ = *file;
|
dataset_name_ = *file;
|
||||||
|
|
||||||
dataset_ = boost::shared_ptr<GDALDataset>(reinterpret_cast<GDALDataset*>(GDALOpenShared((dataset_name_).c_str(),GA_ReadOnly)));
|
shared_dataset_ = *params_.get<mapnik::boolean>("shared",false);
|
||||||
|
|
||||||
|
#if GDAL_VERSION_NUM >= 1600
|
||||||
|
if (shared_dataset_)
|
||||||
|
dataset_ = reinterpret_cast<GDALDataset*>(GDALOpenShared((dataset_name_).c_str(),GA_ReadOnly));
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
dataset_ = reinterpret_cast<GDALDataset*>(GDALOpen((dataset_name_).c_str(),GA_ReadOnly));
|
||||||
|
|
||||||
if (! dataset_) throw datasource_exception(CPLGetLastErrorMsg());
|
if (! dataset_) throw datasource_exception(CPLGetLastErrorMsg());
|
||||||
|
|
||||||
double tr[6];
|
double tr[6];
|
||||||
|
@ -67,8 +80,7 @@ gdal_datasource::gdal_datasource(parameters const& params)
|
||||||
|
|
||||||
gdal_datasource::~gdal_datasource()
|
gdal_datasource::~gdal_datasource()
|
||||||
{
|
{
|
||||||
GDALDereferenceDataset (dataset_.get());
|
GDALClose (dataset_);
|
||||||
dataset_.reset(); // prevent delete of dataset
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int gdal_datasource::type() const
|
int gdal_datasource::type() const
|
||||||
|
|
|
@ -42,8 +42,9 @@ class gdal_datasource : public mapnik::datasource
|
||||||
private:
|
private:
|
||||||
mapnik::Envelope<double> extent_;
|
mapnik::Envelope<double> extent_;
|
||||||
std::string dataset_name_;
|
std::string dataset_name_;
|
||||||
boost::shared_ptr<GDALDataset> dataset_;
|
GDALDataset* dataset_;
|
||||||
mapnik::layer_descriptor desc_;
|
mapnik::layer_descriptor desc_;
|
||||||
|
bool shared_dataset_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -43,26 +43,93 @@ feature_ptr gdal_featureset::next()
|
||||||
{
|
{
|
||||||
first_ = false;
|
first_ = false;
|
||||||
feature_ptr feature(new Feature(1));
|
feature_ptr feature(new Feature(1));
|
||||||
|
|
||||||
GDALRasterBand * red = 0;
|
GDALRasterBand * red = 0;
|
||||||
GDALRasterBand * green = 0;
|
GDALRasterBand * green = 0;
|
||||||
GDALRasterBand * blue = 0;
|
GDALRasterBand * blue = 0;
|
||||||
GDALRasterBand * alpha = 0;
|
GDALRasterBand * alpha = 0;
|
||||||
GDALRasterBand * grey = 0;
|
GDALRasterBand * grey = 0;
|
||||||
for (int i = 0; i < dataset_.GetRasterCount() ;++i)
|
|
||||||
|
unsigned raster_xsize = dataset_.GetRasterXSize();
|
||||||
|
unsigned raster_ysize = dataset_.GetRasterYSize();
|
||||||
|
int nbands = dataset_.GetRasterCount();
|
||||||
|
|
||||||
|
double tr[6];
|
||||||
|
dataset_.GetGeoTransform(tr);
|
||||||
|
double x0 = tr[0];
|
||||||
|
double y0 = tr[3];
|
||||||
|
double x1 = tr[0] + raster_xsize * tr[1] + raster_ysize * tr[2];
|
||||||
|
double y1 = tr[3] + raster_xsize * tr[4] + raster_ysize * tr[5];
|
||||||
|
Envelope<double> raster_extent(x0,y0,x1,y1);
|
||||||
|
CoordTransform t (raster_xsize,raster_ysize,raster_extent,0,0);
|
||||||
|
Envelope<double> intersection = raster_extent.intersect(query_extent_);
|
||||||
|
Envelope<double> box = t.forward(intersection);
|
||||||
|
|
||||||
|
int start_x = int(box.minx()+0.5);
|
||||||
|
int start_y = int(box.miny()+0.5);
|
||||||
|
int width = int(box.width()+0.5);
|
||||||
|
int height = int(box.height()+0.5);
|
||||||
|
|
||||||
|
#ifdef MAPNIK_DEBUG
|
||||||
|
std::cout << boost::format("RasterWidth=%d RasterHeight=%d \n") % raster_xsize % raster_ysize;
|
||||||
|
std::cout << boost::format("StartX=%d StartY=%d Width=%d Height=%d \n") % start_x % start_y % width % height;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (width > 0 && height > 0)
|
||||||
|
{
|
||||||
|
// Some GDAL drivers operate more efficiently if they know in advance
|
||||||
|
// what set of upcoming read requests will be made. The AdviseRead()
|
||||||
|
// method allows an application to notify the driver of the region and
|
||||||
|
// bands of interest, and at what resolution the region will be read.
|
||||||
|
//
|
||||||
|
// dataset_.AdviseRead (start_x, start_y, width, height, width, height, GDT_Byte, nbands, NULL, NULL);
|
||||||
|
|
||||||
|
for (int i = 0; i < nbands; ++i)
|
||||||
{
|
{
|
||||||
GDALRasterBand * band = dataset_.GetRasterBand(i+1);
|
GDALRasterBand * band = dataset_.GetRasterBand(i+1);
|
||||||
//if (band->GetOverviewCount() > 0)
|
int band_overviews = band->GetOverviewCount();
|
||||||
//{
|
|
||||||
// band = band->GetOverview(0);
|
if (band_overviews > 0)
|
||||||
//}
|
{
|
||||||
|
#ifdef MAPNIK_DEBUG
|
||||||
|
for (int b = 0; b < band_overviews; b++)
|
||||||
|
{
|
||||||
|
GDALRasterBand * overview = band->GetOverview (b);
|
||||||
|
std::cout << boost::format("Overview=%d Width=%d Height=%d \n")
|
||||||
|
% b % overview->GetXSize() % overview->GetYSize();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
for (int b = band_overviews; --b >= 0;)
|
||||||
|
{
|
||||||
|
GDALRasterBand * overview = band->GetOverview (b);
|
||||||
|
if ((start_x + width) <= overview->GetXSize()
|
||||||
|
&& (start_y + height) <= overview->GetYSize())
|
||||||
|
{
|
||||||
|
#ifdef MAPNIK_DEBUG
|
||||||
|
std::cout << boost::format("Picked Overview=%d \n") %b;
|
||||||
|
#endif
|
||||||
|
band = overview;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// band = band->GetRasterSampleOverview (width / height);
|
||||||
|
// band = band->GetOverview (band->GetOverviewCount() - 1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef MAPNIK_DEBUG
|
#ifdef MAPNIK_DEBUG
|
||||||
int bsx,bsy;
|
int bsx,bsy;
|
||||||
|
double scale;
|
||||||
band->GetBlockSize(&bsx,&bsy);
|
band->GetBlockSize(&bsx,&bsy);
|
||||||
std::cout << boost::format("Block=%dx%d Type=%s Color=%s \n") % bsx % bsy
|
scale = band->GetScale();
|
||||||
|
std::cout << boost::format("Block=%dx%d Scale=%f Type=%s Color=%s \n") % bsx % bsy % scale
|
||||||
% GDALGetDataTypeName(band->GetRasterDataType())
|
% GDALGetDataTypeName(band->GetRasterDataType())
|
||||||
% GDALGetColorInterpretationName(band->GetColorInterpretation());
|
% GDALGetColorInterpretationName(band->GetColorInterpretation());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GDALColorInterp color_interp = band->GetColorInterpretation();
|
GDALColorInterp color_interp = band->GetColorInterpretation();
|
||||||
switch (color_interp)
|
switch (color_interp)
|
||||||
{
|
{
|
||||||
|
@ -108,54 +175,35 @@ feature_ptr gdal_featureset::next()
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned raster_xsize = dataset_.GetRasterXSize();
|
|
||||||
unsigned raster_ysize = dataset_.GetRasterYSize();
|
|
||||||
double tr[6];
|
|
||||||
dataset_.GetGeoTransform(tr);
|
|
||||||
double x0 = tr[0];
|
|
||||||
double y0 = tr[3];
|
|
||||||
double x1 = tr[0] + raster_xsize * tr[1] + raster_ysize * tr[2];
|
|
||||||
double y1 = tr[3] + raster_xsize * tr[4] + raster_ysize * tr[5];
|
|
||||||
Envelope<double> raster_extent(x0,y0,x1,y1);
|
|
||||||
CoordTransform t (raster_xsize,raster_ysize,raster_extent,0,0);
|
|
||||||
Envelope<double> intersection = raster_extent.intersect(query_extent_);
|
|
||||||
Envelope<double> box = t.forward(intersection);
|
|
||||||
|
|
||||||
int start_x = int(box.minx()+0.5);
|
|
||||||
int start_y = int(box.miny()+0.5);
|
|
||||||
int width = int(box.width()+0.5);
|
|
||||||
int height = int(box.height()+0.5);
|
|
||||||
|
|
||||||
if (width > 0 && height > 0)
|
|
||||||
{
|
|
||||||
mapnik::ImageData32 image(width,height);
|
mapnik::ImageData32 image(width,height);
|
||||||
image.set(0xffffffff);
|
image.set(0xffffffff);
|
||||||
|
|
||||||
if (red && green && blue)
|
if (red && green && blue)
|
||||||
{
|
{
|
||||||
|
//red->AdviseRead (start_x, start_y, width, height, width, height, GDT_Byte, nbands, NULL);
|
||||||
|
//green->AdviseRead(start_x, start_y, width, height, width, height, GDT_Byte, nbands, NULL);
|
||||||
|
//blue->AdviseRead (start_x, start_y, width, height, width, height, GDT_Byte, nbands, NULL);
|
||||||
red->RasterIO (GF_Read,start_x,start_y,width,height, image.getBytes() + 0, width,height, GDT_Byte,4,4*width);
|
red->RasterIO (GF_Read,start_x,start_y,width,height, image.getBytes() + 0, width,height, GDT_Byte,4,4*width);
|
||||||
green->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 1, width,height, GDT_Byte,4,4*width);
|
green->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 1, width,height, GDT_Byte,4,4*width);
|
||||||
blue->RasterIO (GF_Read,start_x,start_y,width,height, image.getBytes() + 2, width,height, GDT_Byte,4,4*width);
|
blue->RasterIO (GF_Read,start_x,start_y,width,height, image.getBytes() + 2, width,height, GDT_Byte,4,4*width);
|
||||||
if (alpha)
|
|
||||||
{
|
|
||||||
alpha->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 3, width,height, GDT_Byte,4,4*width);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (grey)
|
else if (grey)
|
||||||
{
|
{
|
||||||
|
// grey->AdviseRead (start_x, start_y, width, height, width, height, GDT_Byte, nbands, NULL);
|
||||||
grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 0, width, height,GDT_Byte,4,4*width);
|
grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 0, width, height,GDT_Byte,4,4*width);
|
||||||
grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 1, width, height,GDT_Byte,4,4*width);
|
grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 1, width, height,GDT_Byte,4,4*width);
|
||||||
grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 2, width, height,GDT_Byte,4,4*width);
|
grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 2, width, height,GDT_Byte,4,4*width);
|
||||||
|
}
|
||||||
|
|
||||||
if (alpha)
|
if (alpha)
|
||||||
{
|
{
|
||||||
|
//alpha->AdviseRead (start_x, start_y, width, height, width, height, GDT_Byte, nbands, NULL);
|
||||||
alpha->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 3, width,height, GDT_Byte,4,4*width);
|
alpha->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 3, width,height, GDT_Byte,4,4*width);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersection,image)));
|
feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersection,image)));
|
||||||
return feature;
|
return feature;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue