Added gdal raster input plug-in

This commit is contained in:
Artem Pavlenko 2007-04-25 19:15:38 +00:00
parent 5b91f9a9c5
commit f9d28e56fa
5 changed files with 361 additions and 0 deletions

View file

@ -0,0 +1,41 @@
#
# This file is part of Mapnik (c++ mapping toolkit)
#
# Copyright (C) 2007 Artem Pavlenko, Jean-Francois Doyon
#
# Mapnik 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$
Import ('env')
prefix = env['PREFIX']
install_prefix = env['DESTDIR'] + '/' + prefix
gdal_src = Split(
"""
gdal_datasource.cpp
gdal_featureset.cpp
"""
)
libraries = ['gdal' ]
if env['PLATFORM'] == 'Darwin':
libraries.append('mapnik')
gdal_inputdriver = env.SharedLibrary('gdal', source=gdal_src, SHLIBPREFIX='', SHLIBSUFFIX='.input', LIBS=libraries)
env.Install(install_prefix + '/' + env['LIBDIR_SCHEMA'] + '/mapnik/input', gdal_inputdriver)
env.Alias('install', install_prefix + '/' + env['LIBDIR_SCHEMA'] + '/mapnik/input')

View file

@ -0,0 +1,92 @@
/*****************************************************************************
*
* 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_datasource.hpp"
#include "gdal_featureset.hpp"
using mapnik::datasource;
using mapnik::parameters;
DATASOURCE_PLUGIN(gdal_datasource)
using mapnik::Envelope;
using mapnik::coord2d;
using mapnik::query;
using mapnik::featureset_ptr;
using mapnik::layer_descriptor;
gdal_datasource::gdal_datasource( parameters const& params)
: datasource(params),
extent_(),
desc_(params.get("name"),"utf-8")
{
GDALAllRegister();
dataset_ = boost::shared_ptr<GDALDataset>(reinterpret_cast<GDALDataset*>(GDALOpen(params.get("file").c_str(),GA_ReadOnly)));
if (dataset_)
{
double tr[6];
dataset_->GetGeoTransform(tr);
double x0 = tr[0];
double y0 = tr[3];
double x1 = tr[0] + dataset_->GetRasterXSize()*tr[1] + dataset_->GetRasterYSize()*tr[2];
double y1 = tr[3] + dataset_->GetRasterXSize()*tr[4] + dataset_->GetRasterYSize()*tr[5];
extent_.init(x0,y0,x1,y1);
}
}
gdal_datasource::~gdal_datasource() {}
int gdal_datasource::type() const
{
return datasource::Raster;
}
std::string gdal_datasource::name()
{
return "gdal";
}
Envelope<double> gdal_datasource::envelope() const
{
return extent_;
}
layer_descriptor gdal_datasource::get_descriptor() const
{
return desc_;
}
featureset_ptr gdal_datasource::features(query const& q) const
{
if (dataset_)
{
return featureset_ptr(new gdal_featureset(*dataset_, q));
}
return featureset_ptr();
}
featureset_ptr gdal_datasource::features_at_point(coord2d const& pt) const
{
return featureset_ptr();
}

View file

@ -0,0 +1,49 @@
/*****************************************************************************
*
* 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$
#ifndef GDAL_DATASOURCE_HPP
#define GDAL_DATASOURCE_HPP
#include <mapnik/datasource.hpp>
#include <boost/shared_ptr.hpp>
#include <gdal_priv.h>
class gdal_datasource : public mapnik::datasource
{
public:
gdal_datasource(mapnik::parameters const& params);
virtual ~gdal_datasource ();
int type() const;
static std::string name();
mapnik::featureset_ptr features( mapnik::query const& q) const;
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const;
mapnik::Envelope<double> envelope() const;
mapnik::layer_descriptor get_descriptor() const;
private:
mapnik::Envelope<double> extent_;
boost::shared_ptr<GDALDataset> dataset_;
mapnik::layer_descriptor desc_;
};
#endif // GDAL_DATASOURCE_HPP

View file

@ -0,0 +1,135 @@
/*****************************************************************************
*
* 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>
using mapnik::query;
using mapnik::Envelope;
using mapnik::Feature;
using mapnik::feature_ptr;
using mapnik::CoordTransform;
gdal_featureset::gdal_featureset(GDALDataset & dataset, query const& q)
: dataset_(dataset),
query_extent_(q.get_bbox()),
first_(true) {}
gdal_featureset::~gdal_featureset() {}
feature_ptr gdal_featureset::next()
{
if (first_)
{
first_ = false;
feature_ptr feature(new Feature(1));
GDALRasterBand * red = 0;
GDALRasterBand * green = 0;
GDALRasterBand * blue = 0;
GDALRasterBand * alpha = 0;
GDALRasterBand * grey = 0;
for (int i = 0; i < dataset_.GetRasterCount() ;++i)
{
GDALRasterBand * band = dataset_.GetRasterBand(i+1);
//int bsx,bsy;
//band->GetBlockSize(&bsx,&bsy);
//std::cout << boost::format("Block=%dx%d Type=%s Color=%s \n") % bsx % bsy
// % GDALGetDataTypeName(band->GetRasterDataType())
// % GDALGetColorInterpretationName(band->GetColorInterpretation());
GDALColorInterp color_interp = band->GetColorInterpretation();
switch (color_interp)
{
case GCI_RedBand:
red = band;
break;
case GCI_GreenBand:
green = band;
break;
case GCI_BlueBand:
blue = band;
break;
case GCI_AlphaBand:
alpha = band;
break;
case GCI_GrayIndex:
grey = band;
break;
case GCI_Undefined:
grey = band;
default:
//grey = band;
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());
int start_y = int(box.miny());
int width = int(box.width());
int height = int(box.height());
if (width > 0 && height > 0)
{
mapnik::ImageData32 image(width,height);
image.set(0xffffffff);
if (red && green && blue)
{
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);
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)
{
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() + 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);
}
}
feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersection,image)));
return feature;
}
}
return feature_ptr();
}

View file

@ -0,0 +1,44 @@
/*****************************************************************************
*
* 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$
#ifndef GDAL_FEATURESET_HPP
#define GDAL_FEATURESET_HPP
#include <mapnik/datasource.hpp>
class GDALDataset;
class gdal_featureset : public mapnik::Featureset
{
public:
gdal_featureset(GDALDataset & dataset,mapnik::query const& q);
virtual ~gdal_featureset();
mapnik::feature_ptr next();
private:
GDALDataset & dataset_;
mapnik::Envelope<double> query_extent_;
bool first_;
};
#endif // GDAL_FEATURESET_HPP