From a84b397f485e2a90cd1b9522c45df058d7da235e Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sun, 30 May 2010 03:17:59 +0000 Subject: [PATCH] markers_converter.hpp was moved to markers_placement.hpp --- include/mapnik/markers_converter.hpp | 0 plugins/input/gdal/gdal_datasource.cpp | 60 +++++++---------- plugins/input/gdal/gdal_datasource.hpp | 16 +++-- plugins/input/gdal/gdal_featureset.cpp | 67 ++++++++----------- plugins/input/gdal/gdal_featureset.hpp | 5 +- plugins/input/sqlite/sqlite_datasource.cpp | 77 +++++++++++++++++----- plugins/input/sqlite/sqlite_datasource.hpp | 2 + plugins/input/sqlite/sqlite_featureset.cpp | 4 +- 8 files changed, 128 insertions(+), 103 deletions(-) delete mode 100644 include/mapnik/markers_converter.hpp diff --git a/include/mapnik/markers_converter.hpp b/include/mapnik/markers_converter.hpp deleted file mode 100644 index e69de29bb..000000000 diff --git a/plugins/input/gdal/gdal_datasource.cpp b/plugins/input/gdal/gdal_datasource.cpp index e3ba97900..c9fd49dec 100644 --- a/plugins/input/gdal/gdal_datasource.cpp +++ b/plugins/input/gdal/gdal_datasource.cpp @@ -41,36 +41,18 @@ using mapnik::layer_descriptor; using mapnik::datasource_exception; - -/* - * Opens a GDALDataset and returns a pointer to it. - * Caller is responsible for calling GDALClose on it - */ -inline GDALDataset *gdal_datasource::open_dataset() const -{ - GDALDataset *dataset; -#if GDAL_VERSION_NUM >= 1600 - if (shared_dataset_) - dataset = reinterpret_cast(GDALOpenShared((dataset_name_).c_str(),GA_ReadOnly)); - else -#endif - dataset = reinterpret_cast(GDALOpen((dataset_name_).c_str(),GA_ReadOnly)); - - if (! dataset) throw datasource_exception(CPLGetLastErrorMsg()); - return dataset; -} - - - gdal_datasource::gdal_datasource(parameters const& params) : datasource(params), - desc_(*params.get("type"),"utf-8") + desc_(*params.get("type"),"utf-8"), + shared_dataset_(*params_.get("shared",false)), + band_(*params_.get("band", -1)) { #ifdef MAPNIK_DEBUG std::clog << "\nGDAL Plugin: Initializing...\n"; #endif + // todo GDALAllRegister(); boost::optional file = params.get("file"); @@ -82,25 +64,25 @@ gdal_datasource::gdal_datasource(parameters const& params) else dataset_name_ = *file; - shared_dataset_ = *params_.get("shared",false); - band_ = *params_.get("band", -1); +#if GDAL_VERSION_NUM >= 1600 + if (shared_dataset_) + dataset_ = reinterpret_cast(GDALOpenShared((dataset_name_).c_str(),GA_ReadOnly)); +#endif + else + dataset_ = reinterpret_cast(GDALOpen((dataset_name_).c_str(),GA_ReadOnly)); - GDALDataset *dataset = open_dataset(); - - // TODO: Make more class attributes from geotransform... - width_ = dataset->GetRasterXSize(); - height_ = dataset->GetRasterYSize(); + width_ = dataset_->GetRasterXSize(); + height_ = dataset_->GetRasterYSize(); double tr[6]; - dataset->GetGeoTransform(tr); - double dx = tr[1]; - double dy = tr[5]; + dataset_->GetGeoTransform(tr); + dx_ = tr[1]; + dy_ = tr[5]; double x0 = tr[0]; double y0 = tr[3]; - double x1 = tr[0] + width_ * dx + height_ *tr[2]; - double y1 = tr[3] + width_ *tr[4] + height_ * dy; + double x1 = tr[0] + width_ * dx_ + height_ *tr[2]; + double y1 = tr[3] + width_ *tr[4] + height_ * dy_; extent_.init(x0,y0,x1,y1); - GDALClose(dataset); #ifdef MAPNIK_DEBUG std::clog << "GDAL Plugin: Raster Size=" << width_ << "," << height_ << "\n"; @@ -109,7 +91,9 @@ gdal_datasource::gdal_datasource(parameters const& params) } -gdal_datasource::~gdal_datasource() {} +gdal_datasource::~gdal_datasource() { + GDALClose(dataset_); +} int gdal_datasource::type() const { @@ -134,11 +118,11 @@ layer_descriptor gdal_datasource::get_descriptor() const featureset_ptr gdal_datasource::features(query const& q) const { gdal_query gq = q; - return featureset_ptr(new gdal_featureset(*open_dataset(), band_, gq)); + return featureset_ptr(new gdal_featureset(*dataset_, band_, gq, extent_, dx_, dy_)); } featureset_ptr gdal_datasource::features_at_point(coord2d const& pt) const { gdal_query gq = pt; - return featureset_ptr(new gdal_featureset(*open_dataset(), band_, gq)); + return featureset_ptr(new gdal_featureset(*dataset_, band_, gq, extent_, dx_, dy_)); } diff --git a/plugins/input/gdal/gdal_datasource.hpp b/plugins/input/gdal/gdal_datasource.hpp index c0f6cf13a..4bdb97c0f 100644 --- a/plugins/input/gdal/gdal_datasource.hpp +++ b/plugins/input/gdal/gdal_datasource.hpp @@ -32,22 +32,24 @@ class gdal_datasource : public mapnik::datasource { public: gdal_datasource(mapnik::parameters const& params); - virtual ~gdal_datasource (); + virtual ~gdal_datasource(); int type() const; static std::string name(); - mapnik::featureset_ptr features( mapnik::query const& q) const; + mapnik::featureset_ptr features(mapnik::query const& q) const; mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const; mapnik::box2d envelope() const; mapnik::layer_descriptor get_descriptor() const; private: - mapnik::box2d extent_; - std::string dataset_name_; - int band_; mapnik::layer_descriptor desc_; + bool shared_dataset_; + int band_; + mapnik::box2d extent_; + double dx_; + double dy_; + std::string dataset_name_; unsigned width_; unsigned height_; - bool shared_dataset_; - inline GDALDataset *open_dataset() const; + GDALDataset *dataset_; }; diff --git a/plugins/input/gdal/gdal_featureset.cpp b/plugins/input/gdal/gdal_featureset.cpp index f964634f1..0279ddcf4 100644 --- a/plugins/input/gdal/gdal_featureset.cpp +++ b/plugins/input/gdal/gdal_featureset.cpp @@ -35,30 +35,25 @@ using mapnik::feature_ptr; using mapnik::CoordTransform; using mapnik::point_impl; using mapnik::geometry2d; +using mapnik::datasource_exception; -gdal_featureset::gdal_featureset(GDALDataset & dataset, int band, gdal_query q) +gdal_featureset::gdal_featureset(GDALDataset & dataset, int band, gdal_query q, const mapnik::box2d & extent, double dx, double dy) : dataset_(dataset), band_(band), gquery_(q), + extent_(extent), + dx_(dx), + dy_(dy), first_(true) {} -gdal_featureset::~gdal_featureset() -{ -#ifdef MAPNIK_DEBUG - std::clog << "GDAL Plugin: closing dataset = " << &dataset_ << "\n"; -#endif - GDALClose(&dataset_); -} +gdal_featureset::~gdal_featureset() {} feature_ptr gdal_featureset::next() { if (first_) { first_ = false; -#ifdef MAPNIK_DEBUG - std::clog << "GDAL Plugin: featureset, dataset = " << &dataset_ << "\n"; -#endif query *q = boost::get(&gquery_); if(q) { @@ -88,29 +83,14 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q) unsigned raster_width = dataset_.GetRasterXSize(); unsigned raster_height = dataset_.GetRasterYSize(); - - // TODO - pull from class attributes... - double tr[6]; - dataset_.GetGeoTransform(tr); - - double dx = tr[1]; - double dy = tr[5]; - - double x0 = tr[0] + (raster_height) * tr[2]; // minx - double y0 = tr[3] + (raster_height) * tr[5]; // miny - - double x1 = tr[0] + (raster_width) * tr[1]; // maxx - double y1 = tr[3] + (raster_width) * tr[4]; // maxy - - box2d raster_extent(x0,y0,x1,y1); - - CoordTransform t(raster_width,raster_height,raster_extent,0,0); - box2d intersect = raster_extent.intersect(q.get_bbox()); + + CoordTransform t(raster_width,raster_height,extent_,0,0); + box2d intersect = extent_.intersect(q.get_bbox()); box2d box = t.forward(intersect); //size of resized output pixel in source image domain - double margin_x = 1.0/(fabs(dx)*boost::get<0>(q.resolution())); - double margin_y = 1.0/(fabs(dy)*boost::get<1>(q.resolution())); + double margin_x = 1.0/(fabs(dx_)*boost::get<0>(q.resolution())); + double margin_y = 1.0/(fabs(dy_)*boost::get<1>(q.resolution())); if (margin_x < 1) margin_x = 1.0; if (margin_y < 1) @@ -141,7 +121,7 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q) intersect = t.backward(feature_raster_extent); #ifdef MAPNIK_DEBUG - std::clog << "GDAL Plugin: Raster extent=" << raster_extent << "\n"; + std::clog << "GDAL Plugin: Raster extent=" << extent_ << "\n"; std::clog << "GDAL Plugin: View extent=" << intersect << "\n"; std::clog << "GDAL Plugin: Query resolution=" << boost::get<0>(q.resolution()) << "," << boost::get<1>(q.resolution()) << "\n"; std::clog << boost::format("GDAL Plugin: StartX=%d StartY=%d Width=%d Height=%d \n") % x_off % y_off % width % height; @@ -173,15 +153,26 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q) if (band_>0) // we are querying a single band { - float *imageData = (float*)image.getBytes(); - GDALRasterBand * band = dataset_.GetRasterBand(band_); - band->RasterIO(GF_Read, x_off, y_off, width, height, - imageData, image.width(), image.height(), - GDT_Float32, 0, 0); + if (band_ > nbands) + { + throw datasource_exception((boost::format("GDAL Plugin: invalid band, dataset only has %d bands\n") % nbands).str()); + } + else + { + float *imageData = (float*)image.getBytes(); + GDALRasterBand * band = dataset_.GetRasterBand(band_); + band->RasterIO(GF_Read, x_off, y_off, width, height, + imageData, image.width(), image.height(), + GDT_Float32, 0, 0); + + feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersect,image))); + } - feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersect,image))); + + } + else // working with all bands { for (int i = 0; i < nbands; ++i) diff --git a/plugins/input/gdal/gdal_featureset.hpp b/plugins/input/gdal/gdal_featureset.hpp index ce0bfba48..ca784c131 100644 --- a/plugins/input/gdal/gdal_featureset.hpp +++ b/plugins/input/gdal/gdal_featureset.hpp @@ -36,7 +36,7 @@ class gdal_featureset : public mapnik::Featureset { public: - gdal_featureset(GDALDataset & dataset, int band, gdal_query q); + gdal_featureset(GDALDataset & dataset, int band, gdal_query q, const mapnik::box2d & extent, double dx, double dy); virtual ~gdal_featureset(); mapnik::feature_ptr next(); private: @@ -46,6 +46,9 @@ class gdal_featureset : public mapnik::Featureset GDALDataset & dataset_; int band_; gdal_query gquery_; + mapnik::box2d extent_; + double dx_; + double dy_; bool first_; }; diff --git a/plugins/input/sqlite/sqlite_datasource.cpp b/plugins/input/sqlite/sqlite_datasource.cpp index 08ee402ec..20b487bd8 100644 --- a/plugins/input/sqlite/sqlite_datasource.cpp +++ b/plugins/input/sqlite/sqlite_datasource.cpp @@ -32,6 +32,9 @@ #include #include #include +#include + +#define MAPNIK_DEBUG using std::clog; using std::endl; @@ -81,17 +84,23 @@ sqlite_datasource::sqlite_datasource(parameters const& params) extent_(), extent_initialized_(false), type_(datasource::Vector), - table_(*params.get("table","")), - metadata_(*params.get("metadata","")), - geometry_field_(*params.get("geometry_field","the_geom")), - key_field_(*params.get("key_field","OGC_FID")), + table_(*params_.get("table","")), + geometry_table_(*params_.get("geometry_table","")), + geometry_field_(*params_.get("geometry_field","the_geom")), + metadata_(*params_.get("metadata","")), + key_field_(*params_.get("key_field","OGC_FID")), row_offset_(*params_.get("row_offset",0)), row_limit_(*params_.get("row_limit",0)), - desc_(*params.get("type"), *params.get("encoding","utf-8")), - format_(mapnik::wkbGeneric) + desc_(*params_.get("type"), + *params.get("encoding","utf-8")), + format_(mapnik::wkbGeneric), + show_queries_(*params_.get("show_queries",false)) + { boost::optional file = params.get("file"); - if (!file) throw datasource_exception("missing parameter"); + if (!file) throw datasource_exception("SQLite: missing parameter"); + + if (table_.empty()) throw mapnik::datasource_exception("SQLite: missing parameter"); boost::optional wkb = params.get("wkb_format"); if (wkb) @@ -109,10 +118,12 @@ sqlite_datasource::sqlite_datasource(parameters const& params) else dataset_name_ = *file; - if (!boost::filesystem::exists(dataset_name_)) throw datasource_exception(dataset_name_ + " does not exist"); + if (!boost::filesystem::exists(dataset_name_)) throw datasource_exception("SQLite: " + dataset_name_ + " does not exist"); + // todo support for in-memory handle dataset_ = new sqlite_connection (dataset_name_); + // Todo - refactor into utility function boost::optional ext = params_.get("extent"); if (ext) { @@ -147,14 +158,25 @@ sqlite_datasource::sqlite_datasource(parameters const& params) extent_initialized_ = true; } } + + if(geometry_table_.empty()) + { + geometry_table_ = table_from_sql(table_); + } - std::string table_name = table_from_sql(table_); + //std::string table_name = table_from_sql(table_); if (metadata_ != "" && ! extent_initialized_) { std::ostringstream s; s << "select xmin, ymin, xmax, ymax from " << metadata_; - s << " where lower(f_table_name) = lower('" << table_name << "')"; + s << " where lower(f_table_name) = lower('" << geometry_table_ << "')"; + + if (show_queries_) + { + clog << boost::format("SQLite: sending query: %s\n") % s.str(); + } + boost::scoped_ptr rs (dataset_->execute_query (s.str())); if (rs->is_valid () && rs->step_next()) { @@ -172,7 +194,13 @@ sqlite_datasource::sqlite_datasource(parameters const& params) { std::ostringstream s; s << "select count (*) from sqlite_master"; - s << " where lower(name) = lower('idx_" << table_name << "_" << geometry_field_ << "')"; + s << " where lower(name) = lower('idx_" << geometry_table_ << "_" << geometry_field_ << "')"; + + if (show_queries_) + { + clog << boost::format("SQLite: sending query: %s\n") % s.str(); + } + boost::scoped_ptr rs (dataset_->execute_query (s.str())); if (rs->is_valid () && rs->step_next()) { @@ -192,9 +220,17 @@ sqlite_datasource::sqlite_datasource(parameters const& params) as all column_type are SQLITE_NULL */ - std::string::size_type idx = table_.find(table_name); + std::string::size_type idx = table_.find(geometry_table_); std::ostringstream s; - s << "select * from (" << table_.substr(0,idx + table_name.length()) << ") limit 1"; + std::string table_query = table_.substr(0,idx + geometry_table_.length()); + clog << boost::format("table query: %s\n") % table_query; + + s << "select * from (" << table_query << ") limit 1"; + + if (show_queries_) + { + clog << boost::format("SQLite: sending query: %s\n") % s.str(); + } boost::scoped_ptr rs (dataset_->execute_query (s.str())); if (rs->is_valid () && rs->step_next()) @@ -223,7 +259,7 @@ sqlite_datasource::sqlite_datasource(parameters const& params) default: #ifdef MAPNIK_DEBUG - clog << "unknown type_oid="<=" << e.minx() << " and xmin<=" << e.maxx() ; spatial_sql << " and ymax>=" << e.miny() << " and ymin<=" << e.maxy() << ")"; if (boost::algorithm::ifind_first(query,"where")) { boost::algorithm::ireplace_first(query, "where", spatial_sql.str() + " and"); } - else if (boost::algorithm::find_first(query,table_name)) + else if (boost::algorithm::find_first(query,geometry_table_)) { - boost::algorithm::ireplace_first(query, table_name , table_name + " " + spatial_sql.str()); + boost::algorithm::ireplace_first(query, geometry_table_ , geometry_table_ + " " + spatial_sql.str()); } } @@ -313,6 +349,11 @@ featureset_ptr sqlite_datasource::features(query const& q) const std::cerr << s.str() << "\n"; #endif + if (show_queries_) + { + clog << boost::format("SQLite: sending query: %s\n") % s.str(); + } + boost::shared_ptr rs (dataset_->execute_query (s.str())); return featureset_ptr (new sqlite_featureset(rs, desc_.get_encoding(), format_, multiple_geometries_)); diff --git a/plugins/input/sqlite/sqlite_datasource.hpp b/plugins/input/sqlite/sqlite_datasource.hpp index 390dc17c9..a4db76c63 100644 --- a/plugins/input/sqlite/sqlite_datasource.hpp +++ b/plugins/input/sqlite/sqlite_datasource.hpp @@ -48,6 +48,8 @@ class sqlite_datasource : public mapnik::datasource mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const; mapnik::box2d envelope() const; mapnik::layer_descriptor get_descriptor() const; + mutable std::string geometry_table_; + bool show_queries_; private: static const std::string name_; mapnik::box2d extent_; diff --git a/plugins/input/sqlite/sqlite_featureset.cpp b/plugins/input/sqlite/sqlite_featureset.cpp index eb0c10c7c..678715b64 100644 --- a/plugins/input/sqlite/sqlite_featureset.cpp +++ b/plugins/input/sqlite/sqlite_featureset.cpp @@ -30,9 +30,11 @@ #include #include -// ogr +// sqlite #include "sqlite_featureset.hpp" +//#define MAPNIK_DEBUG + using std::clog; using std::endl;