markers_converter.hpp was moved to markers_placement.hpp
This commit is contained in:
parent
89f6b32b76
commit
a84b397f48
8 changed files with 128 additions and 103 deletions
|
@ -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<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());
|
||||
return dataset;
|
||||
}
|
||||
|
||||
|
||||
|
||||
gdal_datasource::gdal_datasource(parameters const& params)
|
||||
: datasource(params),
|
||||
desc_(*params.get<std::string>("type"),"utf-8")
|
||||
desc_(*params.get<std::string>("type"),"utf-8"),
|
||||
shared_dataset_(*params_.get<mapnik::boolean>("shared",false)),
|
||||
band_(*params_.get<int>("band", -1))
|
||||
{
|
||||
|
||||
#ifdef MAPNIK_DEBUG
|
||||
std::clog << "\nGDAL Plugin: Initializing...\n";
|
||||
#endif
|
||||
|
||||
// todo
|
||||
GDALAllRegister();
|
||||
|
||||
boost::optional<std::string> file = params.get<std::string>("file");
|
||||
|
@ -82,25 +64,25 @@ gdal_datasource::gdal_datasource(parameters const& params)
|
|||
else
|
||||
dataset_name_ = *file;
|
||||
|
||||
shared_dataset_ = *params_.get<mapnik::boolean>("shared",false);
|
||||
band_ = *params_.get<int>("band", -1);
|
||||
#if GDAL_VERSION_NUM >= 1600
|
||||
if (shared_dataset_)
|
||||
dataset_ = reinterpret_cast<GDALDataset*>(GDALOpenShared((dataset_name_).c_str(),GA_ReadOnly));
|
||||
#endif
|
||||
else
|
||||
dataset_ = reinterpret_cast<GDALDataset*>(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_));
|
||||
}
|
||||
|
|
|
@ -40,14 +40,16 @@ class gdal_datasource : public mapnik::datasource
|
|||
mapnik::box2d<double> envelope() const;
|
||||
mapnik::layer_descriptor get_descriptor() const;
|
||||
private:
|
||||
mapnik::box2d<double> extent_;
|
||||
std::string dataset_name_;
|
||||
int band_;
|
||||
mapnik::layer_descriptor desc_;
|
||||
bool shared_dataset_;
|
||||
int band_;
|
||||
mapnik::box2d<double> extent_;
|
||||
double dx_;
|
||||
double dy_;
|
||||
std::string dataset_name_;
|
||||
unsigned width_;
|
||||
unsigned height_;
|
||||
bool shared_dataset_;
|
||||
inline GDALDataset *open_dataset() const;
|
||||
GDALDataset *dataset_;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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<double> & 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<query>(&gquery_);
|
||||
if(q) {
|
||||
|
@ -89,28 +84,13 @@ 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<double> raster_extent(x0,y0,x1,y1);
|
||||
|
||||
CoordTransform t(raster_width,raster_height,raster_extent,0,0);
|
||||
box2d<double> intersect = raster_extent.intersect(q.get_bbox());
|
||||
CoordTransform t(raster_width,raster_height,extent_,0,0);
|
||||
box2d<double> intersect = extent_.intersect(q.get_bbox());
|
||||
box2d<double> 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;
|
||||
|
@ -172,6 +152,12 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
|
|||
typedef std::vector<int,int> pallete;
|
||||
|
||||
if (band_>0) // we are querying a single band
|
||||
{
|
||||
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_);
|
||||
|
@ -182,6 +168,11 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
|
|||
feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersect,image)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
else // working with all bands
|
||||
{
|
||||
for (int i = 0; i < nbands; ++i)
|
||||
|
|
|
@ -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<double> & 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<double> extent_;
|
||||
double dx_;
|
||||
double dy_;
|
||||
bool first_;
|
||||
};
|
||||
|
||||
|
|
|
@ -32,6 +32,9 @@
|
|||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/format.hpp>
|
||||
|
||||
#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<std::string>("table","")),
|
||||
metadata_(*params.get<std::string>("metadata","")),
|
||||
geometry_field_(*params.get<std::string>("geometry_field","the_geom")),
|
||||
key_field_(*params.get<std::string>("key_field","OGC_FID")),
|
||||
table_(*params_.get<std::string>("table","")),
|
||||
geometry_table_(*params_.get<std::string>("geometry_table","")),
|
||||
geometry_field_(*params_.get<std::string>("geometry_field","the_geom")),
|
||||
metadata_(*params_.get<std::string>("metadata","")),
|
||||
key_field_(*params_.get<std::string>("key_field","OGC_FID")),
|
||||
row_offset_(*params_.get<int>("row_offset",0)),
|
||||
row_limit_(*params_.get<int>("row_limit",0)),
|
||||
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8")),
|
||||
format_(mapnik::wkbGeneric)
|
||||
desc_(*params_.get<std::string>("type"),
|
||||
*params.get<std::string>("encoding","utf-8")),
|
||||
format_(mapnik::wkbGeneric),
|
||||
show_queries_(*params_.get<mapnik::boolean>("show_queries",false))
|
||||
|
||||
{
|
||||
boost::optional<std::string> file = params.get<std::string>("file");
|
||||
if (!file) throw datasource_exception("missing <file> parameter");
|
||||
if (!file) throw datasource_exception("SQLite: missing <file> parameter");
|
||||
|
||||
if (table_.empty()) throw mapnik::datasource_exception("SQLite: missing <table> parameter");
|
||||
|
||||
boost::optional<std::string> wkb = params.get<std::string>("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<std::string> ext = params_.get<std::string>("extent");
|
||||
if (ext)
|
||||
{
|
||||
|
@ -148,13 +159,24 @@ sqlite_datasource::sqlite_datasource(parameters const& params)
|
|||
}
|
||||
}
|
||||
|
||||
std::string table_name = table_from_sql(table_);
|
||||
if(geometry_table_.empty())
|
||||
{
|
||||
geometry_table_ = 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<sqlite_resultset> 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<sqlite_resultset> 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<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
||||
if (rs->is_valid () && rs->step_next())
|
||||
|
@ -283,19 +319,19 @@ featureset_ptr sqlite_datasource::features(query const& q) const
|
|||
|
||||
if (use_spatial_index_)
|
||||
{
|
||||
std::string table_name = table_from_sql(query);
|
||||
//std::string table_name = table_from_sql(query);
|
||||
std::ostringstream spatial_sql;
|
||||
spatial_sql << std::setprecision(16);
|
||||
spatial_sql << " where rowid in (select pkid from idx_" << table_name << "_" << geometry_field_;
|
||||
spatial_sql << " where rowid in (select pkid from idx_" << geometry_table_ << "_" << geometry_field_;
|
||||
spatial_sql << " where xmax>=" << 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<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
||||
|
||||
return featureset_ptr (new sqlite_featureset(rs, desc_.get_encoding(), format_, multiple_geometries_));
|
||||
|
|
|
@ -48,6 +48,8 @@ class sqlite_datasource : public mapnik::datasource
|
|||
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const;
|
||||
mapnik::box2d<double> envelope() const;
|
||||
mapnik::layer_descriptor get_descriptor() const;
|
||||
mutable std::string geometry_table_;
|
||||
bool show_queries_;
|
||||
private:
|
||||
static const std::string name_;
|
||||
mapnik::box2d<double> extent_;
|
||||
|
|
|
@ -30,9 +30,11 @@
|
|||
#include <mapnik/wkb.hpp>
|
||||
#include <mapnik/unicode.hpp>
|
||||
|
||||
// ogr
|
||||
// sqlite
|
||||
#include "sqlite_featureset.hpp"
|
||||
|
||||
//#define MAPNIK_DEBUG
|
||||
|
||||
using std::clog;
|
||||
using std::endl;
|
||||
|
||||
|
|
Loading…
Reference in a new issue