1. Removed srid from geometry class

2. Pass resolution to bbox query
3. Use variant<int,double,string> as parameter value e.g in Python:
	ds = Raster(file="/path/to/file",lox = 12312.4,.....)
   Added extractor facility to work with mapnik::parameter (C++):
	
	mapnik::parameters params;
	params["parameter0"] = 123.456;
	params["parameter1"] = "123.456"; // initialize with string extract double later
	
	boost::optional<double> val0 = params.get<double>("parameter0");
	if (val0) 
	{
	   std::cout << *val0;
        }
	
	
	// with default value. NOTE: there is no 'parameter2' in params
	boost::optional<double> val2 = params.get<double>("parameter2",654.321);
	std::cout << * val2;
        
	// 
4. Added Gdal factory method in __init__.py

        ds =  Gdal(file="/tmp/file.tiff")
This commit is contained in:
Artem Pavlenko 2007-06-12 08:59:54 +00:00
parent e8d5172a64
commit 8010d5433f
22 changed files with 502 additions and 499 deletions

View file

@ -16,9 +16,7 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# #
#
#
#
from sys import getdlopenflags,setdlopenflags from sys import getdlopenflags,setdlopenflags
try: try:
@ -56,10 +54,10 @@ class _Envelope(Envelope,_injector):
class _Projection(Projection,_injector): class _Projection(Projection,_injector):
def forward(self,pt): def forward(self,pt):
return forward(pt,self) return forward_(pt,self)
def inverse(self,pt): def inverse(self,pt):
return inverse(pt,self) return inverse_(pt,self)
class _Datasource(Datasource,_injector): class _Datasource(Datasource,_injector):
def describe(self): def describe(self):
return Describe(self) return Describe(self)
@ -81,6 +79,10 @@ def Raster(**keywords):
keywords['type'] = 'raster' keywords['type'] = 'raster'
return CreateDatasource(keywords) return CreateDatasource(keywords)
def Gdal (**keywords):
keywords['type'] = 'gdal'
return CreateDatasource(keywords)
#register datasources #register datasources
from mapnik import DatasourceCache from mapnik import DatasourceCache
DatasourceCache.instance().register_datasources('%s' % inputpluginspath) DatasourceCache.instance().register_datasources('%s' % inputpluginspath)

View file

@ -43,11 +43,22 @@ namespace
{ {
std::string key = extract<std::string>(keys[i]); std::string key = extract<std::string>(keys[i]);
object obj = d[key]; object obj = d[key];
extract<std::string> ex(obj); extract<std::string> ex0(obj);
if (ex.check()) extract<int> ex1(obj);
extract<double> ex2(obj);
if (ex0.check())
{ {
params[key] = ex(); params[key] = ex0();
} }
else if (ex1.check())
{
params[key] = ex1();
}
else if (ex2.check())
{
params[key] = ex2();
}
} }
return mapnik::datasource_cache::create(params); return mapnik::datasource_cache::create(params);

View file

@ -70,7 +70,7 @@ struct parameters_pickle_suite : boost::python::pickle_suite
for (int i=0;i<len(keys);++i) for (int i=0;i<len(keys);++i)
{ {
std::string key=extract<std::string>(keys[i]); std::string key=extract<std::string>(keys[i]);
std::string value=extract<std::string>(d[key]); std::string value=extract<std::string>(d[key]); // FIXME: use boost::variant as a value object. Add Py_Int/Py_Float/Py_String extractors.
p[key] = value; p[key] = value;
} }
} }
@ -85,9 +85,6 @@ void export_parameters()
; ;
class_<parameters>("Parameters",init<>()) class_<parameters>("Parameters",init<>())
//.def("add",add1)
//.def("add",add2)
.def("get",&parameters::get)
.def_pickle(parameters_pickle_suite()) .def_pickle(parameters_pickle_suite())
; ;
} }

View file

@ -52,14 +52,12 @@ void export_projection ()
using mapnik::projection; using mapnik::projection;
class_<projection>("Projection", init<optional<std::string const&> >()) class_<projection>("Projection", init<optional<std::string const&> >())
.def ("forward",&projection::forward)
.def ("inverse",&projection::inverse)
.def ("params", make_function(&projection::params, .def ("params", make_function(&projection::params,
return_value_policy<copy_const_reference>())) return_value_policy<copy_const_reference>()))
.add_property ("geographic",&projection::is_geographic) .add_property ("geographic",&projection::is_geographic)
; ;
def("forward",&forward); def("forward_",&forward);
def("inverse",&inverse); def("inverse_",&inverse);
} }

View file

@ -113,7 +113,7 @@ namespace mapnik
projection proj1(lay.srs()); projection proj1(lay.srs());
proj_transform prj_trans(proj0,proj1); proj_transform prj_trans(proj0,proj1);
double x0 = ext.minx(); double x0 = ext.minx();
double y0 = ext.miny(); double y0 = ext.miny();
double z0 = 0.0; double z0 = 0.0;
@ -123,6 +123,8 @@ namespace mapnik
prj_trans.forward(x0,y0,z0); prj_trans.forward(x0,y0,z0);
prj_trans.forward(x1,y1,z1); prj_trans.forward(x1,y1,z1);
Envelope<double> bbox(x0,y0,x1,y1); Envelope<double> bbox(x0,y0,x1,y1);
double resolution = m_.getWidth()/bbox.width();
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
std::clog << bbox << "\n"; std::clog << bbox << "\n";
#endif #endif
@ -140,8 +142,8 @@ namespace mapnik
bool active_rules=false; bool active_rules=false;
feature_type_style const& style=m_.find_style(*stylesIter++); feature_type_style const& style=m_.find_style(*stylesIter++);
query q(bbox); //BBOX query query q(bbox,resolution); //BBOX query
const std::vector<rule_type>& rules=style.get_rules(); const std::vector<rule_type>& rules=style.get_rules();
std::vector<rule_type>::const_iterator ruleIter=rules.begin(); std::vector<rule_type>::const_iterator ruleIter=rules.begin();

View file

@ -47,16 +47,8 @@ namespace mapnik {
public: public:
typedef T vertex_type; typedef T vertex_type;
typedef typename vertex_type::type value_type; typedef typename vertex_type::type value_type;
private:
int srid_;
public: public:
geometry (int srid=-1) geometry () {}
: srid_(srid) {}
int srid() const
{
return srid_;
}
Envelope<double> envelope() Envelope<double> envelope()
{ {
@ -90,91 +82,89 @@ namespace mapnik {
virtual ~geometry() {} virtual ~geometry() {}
}; };
template <typename T> template <typename T>
class point : public geometry<T> class point : public geometry<T>
{ {
typedef geometry<T> geometry_base; typedef geometry<T> geometry_base;
typedef typename geometry<T>::vertex_type vertex_type; typedef typename geometry<T>::vertex_type vertex_type;
typedef typename geometry<T>::value_type value_type; typedef typename geometry<T>::value_type value_type;
private: private:
vertex_type pt_; vertex_type pt_;
public: public:
point(int srid) point() : geometry<T>() {}
: geometry<T>(srid)
{} int type() const
{
int type() const
{
return Point; return Point;
} }
void label_position(double *x, double *y) const
{ void label_position(double *x, double *y) const
{
*x = pt_.x; *x = pt_.x;
*y = pt_.y; *y = pt_.y;
} }
void move_to(value_type x,value_type y) void move_to(value_type x,value_type y)
{ {
pt_.x = x; pt_.x = x;
pt_.y = y; pt_.y = y;
} }
void line_to(value_type ,value_type ) {} void line_to(value_type ,value_type ) {}
unsigned num_points() const unsigned num_points() const
{ {
return 1; return 1;
} }
unsigned vertex(double* x, double* y) unsigned vertex(double* x, double* y)
{ {
*x = pt_.x; *x = pt_.x;
*y = pt_.y; *y = pt_.y;
return SEG_LINETO; return SEG_LINETO;
} }
void rewind(unsigned ) {} void rewind(unsigned ) {}
bool hit_test(value_type x,value_type y, double tol) const bool hit_test(value_type x,value_type y, double tol) const
{ {
return point_in_circle(pt_.x,pt_.y, x,y,tol); return point_in_circle(pt_.x,pt_.y, x,y,tol);
} }
void set_capacity(size_t) {} void set_capacity(size_t) {}
virtual ~point() {} virtual ~point() {}
}; };
template <typename T, template <typename> class Container=vertex_vector2> template <typename T, template <typename> class Container=vertex_vector2>
class polygon : public geometry<T> class polygon : public geometry<T>
{ {
typedef geometry<T> geometry_base; typedef geometry<T> geometry_base;
typedef typename geometry<T>::vertex_type vertex_type; typedef typename geometry<T>::vertex_type vertex_type;
typedef typename geometry_base::value_type value_type; typedef typename geometry_base::value_type value_type;
typedef Container<vertex_type> container_type; typedef Container<vertex_type> container_type;
private: private:
container_type cont_; container_type cont_;
mutable unsigned itr_; mutable unsigned itr_;
public: public:
polygon(int srid) polygon()
: geometry_base(srid), : geometry_base(),
itr_(0) itr_(0)
{} {}
int type() const int type() const
{ {
return Polygon; return Polygon;
} }
void label_position(double *x, double *y) const void label_position(double *x, double *y) const
{ {
unsigned size = cont_.size(); unsigned size = cont_.size();
if (size < 3) if (size < 3)
{ {
cont_.get_vertex(0,x,y); cont_.get_vertex(0,x,y);
return; return;
} }
double ai; double ai;
double atmp = 0; double atmp = 0;
double xtmp = 0; double xtmp = 0;
@ -187,82 +177,82 @@ namespace mapnik {
unsigned i,j; unsigned i,j;
for (i = size-1,j = 0; j < size; i = j, ++j) for (i = size-1,j = 0; j < size; i = j, ++j)
{ {
cont_.get_vertex(i,&x0,&y0); cont_.get_vertex(i,&x0,&y0);
cont_.get_vertex(j,&x1,&y1); cont_.get_vertex(j,&x1,&y1);
ai = x0 * y1 - x1 * y0; ai = x0 * y1 - x1 * y0;
atmp += ai; atmp += ai;
xtmp += (x1 + x0) * ai; xtmp += (x1 + x0) * ai;
ytmp += (y1 + y0) * ai; ytmp += (y1 + y0) * ai;
} }
if (atmp != 0) if (atmp != 0)
{ {
*x = xtmp/(3*atmp); *x = xtmp/(3*atmp);
*y = ytmp /(3*atmp); *y = ytmp /(3*atmp);
return; return;
} }
*x=x0; *x=x0;
*y=y0; *y=y0;
} }
void line_to(value_type x,value_type y) void line_to(value_type x,value_type y)
{ {
cont_.push_back(x,y,SEG_LINETO); cont_.push_back(x,y,SEG_LINETO);
} }
void move_to(value_type x,value_type y) void move_to(value_type x,value_type y)
{ {
cont_.push_back(x,y,SEG_MOVETO); cont_.push_back(x,y,SEG_MOVETO);
} }
unsigned num_points() const unsigned num_points() const
{ {
return cont_.size(); return cont_.size();
} }
unsigned vertex(double* x, double* y) unsigned vertex(double* x, double* y)
{ {
return cont_.get_vertex(itr_++,x,y); return cont_.get_vertex(itr_++,x,y);
} }
void rewind(unsigned ) void rewind(unsigned )
{ {
itr_=0; itr_=0;
} }
bool hit_test(value_type x,value_type y, double) const bool hit_test(value_type x,value_type y, double) const
{ {
return point_inside_path(x,y,cont_.begin(),cont_.end()); return point_inside_path(x,y,cont_.begin(),cont_.end());
} }
void set_capacity(size_t size) void set_capacity(size_t size)
{ {
cont_.set_capacity(size); cont_.set_capacity(size);
} }
virtual ~polygon() {} virtual ~polygon() {}
}; };
template <typename T, template <typename> class Container=vertex_vector2> template <typename T, template <typename> class Container=vertex_vector2>
class line_string : public geometry<T> class line_string : public geometry<T>
{ {
typedef geometry<T> geometry_base; typedef geometry<T> geometry_base;
typedef typename geometry_base::value_type value_type; typedef typename geometry_base::value_type value_type;
typedef typename geometry<T>::vertex_type vertex_type; typedef typename geometry<T>::vertex_type vertex_type;
typedef Container<vertex_type> container_type; typedef Container<vertex_type> container_type;
private: private:
container_type cont_; container_type cont_;
mutable unsigned itr_; mutable unsigned itr_;
public: public:
line_string(int srid) line_string()
: geometry_base(srid), : geometry_base(),
itr_(0) itr_(0)
{} {}
int type() const int type() const
{ {
return LineString; return LineString;
} }
void label_position(double *x, double *y) const void label_position(double *x, double *y) const
{ {
// calculate mid point on line string // calculate mid point on line string
double x0=0; double x0=0;
double y0=0; double y0=0;
@ -272,91 +262,90 @@ namespace mapnik {
unsigned size = cont_.size(); unsigned size = cont_.size();
if (size == 1) if (size == 1)
{ {
cont_.get_vertex(0,x,y); cont_.get_vertex(0,x,y);
} }
else if (size == 2) else if (size == 2)
{ {
cont_.get_vertex(0,&x0,&y0); cont_.get_vertex(0,&x0,&y0);
cont_.get_vertex(1,&x1,&y1); cont_.get_vertex(1,&x1,&y1);
*x = 0.5 * (x1 + x0); *x = 0.5 * (x1 + x0);
*y = 0.5 * (y1 + y0); *y = 0.5 * (y1 + y0);
} }
else else
{ {
double len=0.0; double len=0.0;
for (unsigned pos = 1; pos < size; ++pos) for (unsigned pos = 1; pos < size; ++pos)
{ {
cont_.get_vertex(pos-1,&x0,&y0); cont_.get_vertex(pos-1,&x0,&y0);
cont_.get_vertex(pos,&x1,&y1); cont_.get_vertex(pos,&x1,&y1);
double dx = x1 - x0; double dx = x1 - x0;
double dy = y1 - y0; double dy = y1 - y0;
len += sqrt(dx * dx + dy * dy); len += sqrt(dx * dx + dy * dy);
} }
double midlen = 0.5 * len; double midlen = 0.5 * len;
double dist = 0.0; double dist = 0.0;
for (unsigned pos = 1; pos < size;++pos) for (unsigned pos = 1; pos < size;++pos)
{ {
cont_.get_vertex(pos-1,&x0,&y0); cont_.get_vertex(pos-1,&x0,&y0);
cont_.get_vertex(pos,&x1,&y1); cont_.get_vertex(pos,&x1,&y1);
double dx = x1 - x0; double dx = x1 - x0;
double dy = y1 - y0; double dy = y1 - y0;
double seg_len = sqrt(dx * dx + dy * dy); double seg_len = sqrt(dx * dx + dy * dy);
if (( dist + seg_len) >= midlen) if (( dist + seg_len) >= midlen)
{ {
double r = (midlen - dist)/seg_len; double r = (midlen - dist)/seg_len;
*x = x0 + (x1 - x0) * r; *x = x0 + (x1 - x0) * r;
*y = y0 + (y1 - y0) * r; *y = y0 + (y1 - y0) * r;
break; break;
} }
dist += seg_len; dist += seg_len;
} }
} }
}
} void line_to(value_type x,value_type y)
void line_to(value_type x,value_type y) {
{
cont_.push_back(x,y,SEG_LINETO); cont_.push_back(x,y,SEG_LINETO);
} }
void move_to(value_type x,value_type y) void move_to(value_type x,value_type y)
{ {
cont_.push_back(x,y,SEG_MOVETO); cont_.push_back(x,y,SEG_MOVETO);
} }
unsigned num_points() const unsigned num_points() const
{ {
return cont_.size(); return cont_.size();
} }
unsigned vertex(double* x, double* y) unsigned vertex(double* x, double* y)
{ {
return cont_.get_vertex(itr_++,x,y); return cont_.get_vertex(itr_++,x,y);
} }
void rewind(unsigned ) void rewind(unsigned )
{ {
itr_=0; itr_=0;
} }
bool hit_test(value_type x,value_type y, double tol) const bool hit_test(value_type x,value_type y, double tol) const
{ {
return point_on_path(x,y,cont_.begin(),cont_.end(),tol); return point_on_path(x,y,cont_.begin(),cont_.end(),tol);
} }
void set_capacity(size_t size) void set_capacity(size_t size)
{ {
cont_.set_capacity(size); cont_.set_capacity(size);
} }
virtual ~line_string() {} virtual ~line_string() {}
}; };
typedef point<vertex2d> point_impl; typedef point<vertex2d> point_impl;
typedef line_string<vertex2d,vertex_vector2> line_string_impl; typedef line_string<vertex2d,vertex_vector2> line_string_impl;
typedef polygon<vertex2d,vertex_vector2> polygon_impl; typedef polygon<vertex2d,vertex_vector2> polygon_impl;
typedef geometry<vertex2d> geometry_type; typedef geometry<vertex2d> geometry_type;
typedef boost::shared_ptr<geometry_type> geometry_ptr; typedef boost::shared_ptr<geometry_type> geometry_ptr;
} }
#endif //GEOMETRY_HPP #endif //GEOMETRY_HPP

View file

@ -26,39 +26,84 @@
#include <string> #include <string>
#include <map> #include <map>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
#include <boost/none.hpp>
#include <boost/lexical_cast.hpp>
namespace mapnik namespace mapnik
{ {
typedef boost::variant<int,double,std::string> value_holder;
typedef std::pair<const std::string,std::string> parameter; typedef std::pair<const std::string, value_holder> parameter;
typedef std::map<const std::string,std::string> param_map; typedef std::map<const std::string, value_holder> param_map;
class parameters : public param_map template <typename T>
{ struct value_extractor_visitor : public boost::static_visitor<>
public: {
value_extractor_visitor(boost::optional<T> & var)
parameters() {} :var_(var) {}
const std::string get(std::string const& key) const
{ void operator () (T val) const
param_map::const_iterator itr=find(key); {
if (itr != end()) var_ = val;
}
template <typename T1>
void operator () (T1 val) const
{
try
{ {
return itr->second; var_ = boost::lexical_cast<T>(val);
} }
return std::string(); catch (boost::bad_lexical_cast & ) {}
} }
param_map::const_iterator iterator(std::string const& key) const boost::optional<T> & var_;
{ };
class parameters : public param_map
{
template <typename T>
struct converter
{
typedef boost::optional<T> return_type;
static return_type extract(parameters const& params,std::string const& name, boost::optional<T> const& default_value)
{
boost::optional<T> result(default_value);
parameters::const_iterator itr = params.find(name);
if (itr != params.end())
{
boost::apply_visitor(value_extractor_visitor<T>(result),itr->second);
}
return result;
}
};
public:
parameters() {}
template <typename T>
boost::optional<T> get(std::string const& key) const
{
return converter<T>::extract(*this,key, boost::none);
}
template <typename T>
boost::optional<T> get(std::string const& key, T const& default_value) const
{
return converter<T>::extract(*this,key,boost::optional<T>(default_value));
}
param_map::const_iterator iterator(std::string const& key) const
{
return find(key); return find(key);
} }
param_map::iterator iterator(std::string const& key) param_map::iterator iterator(std::string const& key)
{ {
return find(key); return find(key);
} }
};
};
} }
#endif //PARAMS_HPP #endif //PARAMS_HPP

View file

@ -32,80 +32,56 @@
#include <mapnik/envelope.hpp> #include <mapnik/envelope.hpp>
#include <mapnik/feature.hpp> #include <mapnik/feature.hpp>
namespace mapnik namespace mapnik {
{ class query
class query {
{ private:
private: Envelope<double> bbox_;
Envelope<double> bbox_; double resolution_;
filter<Feature>* filter_; std::set<std::string> names_;
std::set<std::string> names_; public:
public:
query() explicit query(const Envelope<double>& bbox, double resolution)
: bbox_(std::numeric_limits<double>::min(), : bbox_(bbox),
std::numeric_limits<double>::min(), resolution_(resolution)
std::numeric_limits<double>::max(), {}
std::numeric_limits<double>::max()),
filter_(new all_filter<Feature>)
{}
query(const Envelope<double>& bbox) query(const query& other)
: bbox_(bbox),
filter_(new all_filter<Feature>)
{}
query(const Envelope<double>& bbox, const filter<Feature>& f)
: bbox_(bbox),
filter_(f.clone())
{}
query(const query& other)
: bbox_(other.bbox_), : bbox_(other.bbox_),
filter_(other.filter_->clone()) resolution_(other.resolution_),
{} names_(other.names_)
{}
query& operator=(const query& other)
{ query& operator=(const query& other)
filter<Feature>* tmp=other.filter_->clone(); {
delete filter_; if (this == &other) return *this;
filter_=tmp;
bbox_=other.bbox_; bbox_=other.bbox_;
resolution_=other.resolution_;
names_=other.names_; names_=other.names_;
return *this; return *this;
} }
const filter<Feature>* get_filter() const double resolution() const
{ {
return filter_; return resolution_;
} }
const Envelope<double>& get_bbox() const const Envelope<double>& get_bbox() const
{ {
return bbox_; return bbox_;
} }
void set_filter(const filter<Feature>& f) void add_property_name(const std::string& name)
{ {
filter<Feature>* tmp=f.clone();
delete filter_;
filter_=tmp;
}
void add_property_name(const std::string& name)
{
names_.insert(name); names_.insert(name);
} }
const std::set<std::string>& property_names() const const std::set<std::string>& property_names() const
{ {
return names_; return names_;
} }
};
~query()
{
delete filter_;
}
};
} }

View file

@ -33,11 +33,11 @@ namespace mapnik
class MAPNIK_DECL geometry_utils class MAPNIK_DECL geometry_utils
{ {
public: public:
static geometry_ptr from_wkb(const char* wkb, unsigned size,int srid); static geometry_ptr from_wkb(const char* wkb, unsigned size);
private: private:
geometry_utils(); geometry_utils();
geometry_utils(const geometry_utils&); geometry_utils(geometry_utils const&);
geometry_utils& operator=(const geometry_utils&); geometry_utils& operator=(const geometry_utils&);
}; };
} }
#endif //WKB_HPP #endif //WKB_HPP

View file

@ -34,24 +34,26 @@ using mapnik::coord2d;
using mapnik::query; using mapnik::query;
using mapnik::featureset_ptr; using mapnik::featureset_ptr;
using mapnik::layer_descriptor; using mapnik::layer_descriptor;
using mapnik::datasource_exception;
gdal_datasource::gdal_datasource( parameters const& params) gdal_datasource::gdal_datasource( parameters const& params)
: datasource(params), : datasource(params),
extent_(), extent_(),
desc_(params.get("name"),"utf-8") desc_(*params.get<std::string>("type"),"utf-8")
{ {
GDALAllRegister(); GDALAllRegister();
dataset_ = boost::shared_ptr<GDALDataset>(reinterpret_cast<GDALDataset*>(GDALOpen(params.get("file").c_str(),GA_ReadOnly))); boost::optional<std::string> file = params.get<std::string>("file");
if (dataset_) if (!file) throw datasource_exception("missing <file> paramater");
{
double tr[6]; dataset_ = boost::shared_ptr<GDALDataset>(reinterpret_cast<GDALDataset*>(GDALOpen((*file).c_str(),GA_ReadOnly)));
dataset_->GetGeoTransform(tr); if (!dataset_) throw datasource_exception("failed to create GDALDataset");
double x0 = tr[0]; double tr[6];
double y0 = tr[3]; dataset_->GetGeoTransform(tr);
double x1 = tr[0] + dataset_->GetRasterXSize()*tr[1] + dataset_->GetRasterYSize()*tr[2]; double x0 = tr[0];
double y1 = tr[3] + dataset_->GetRasterXSize()*tr[4] + dataset_->GetRasterYSize()*tr[5]; double y0 = tr[3];
extent_.init(x0,y0,x1,y1); 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() {} gdal_datasource::~gdal_datasource() {}

View file

@ -38,29 +38,16 @@ class Connection
private: private:
PGconn *conn_; PGconn *conn_;
public: public:
Connection(std::string const& host, Connection(std::string const& connection_str)
std::string const& port,
std::string const& dbname,
std::string const& username,
std::string const& password)
{ {
conn_=PQconnectdb(connection_str.c_str());
std::string connStr;
if (host.length()) connStr += "host="+host;
if (port.length()) connStr += " port="+port;
if (dbname.length()) connStr+=" dbname="+dbname;
if (username.length()) connStr+=" user="+username;
if (password.length()) connStr+=" password="+password;
connStr+=" connect_timeout=4"; // todo: set by client (param)
conn_=PQconnectdb(connStr.c_str());
if (PQstatus(conn_) == CONNECTION_BAD) if (PQstatus(conn_) == CONNECTION_BAD)
{ {
std::clog << "connection to "<< connStr << " failed\n" std::clog << "connection ["<< connection_str<< "] failed\n"
<< PQerrorMessage(conn_)<< std::endl; << PQerrorMessage(conn_)<< std::endl;
} }
} }
bool execute(const std::string& sql) const bool execute(const std::string& sql) const
{ {
PGresult *result=PQexec(conn_,sql.c_str()); PGresult *result=PQexec(conn_,sql.c_str());

View file

@ -31,6 +31,7 @@
#include "connection.hpp" #include "connection.hpp"
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
#include <boost/optional.hpp>
using mapnik::Pool; using mapnik::Pool;
using mapnik::singleton; using mapnik::singleton;
@ -43,35 +44,45 @@ class ConnectionCreator
{ {
public: public:
ConnectionCreator(string const& host, ConnectionCreator(boost::optional<string> const& host,
string const& port, boost::optional<string> const& port,
string const& dbname, boost::optional<string> const& dbname,
string const& user, boost::optional<string> const& user,
string const& pass) boost::optional<string> const& pass)
: host_(host), : host_(host),
port_(port), port_(port),
dbname_(dbname), dbname_(dbname),
user_(user), user_(user),
pass_(pass) {} pass_(pass) {}
T* operator()() const T* operator()() const
{ {
return new T(host_,port_,dbname_,user_,pass_); return new T(connection_string());
} }
std::string id() const inline std::string id() const
{ {
return host_ + ":" return connection_string();
+ dbname_ + ":" }
+ port_ +":"
+ user_ ; inline std::string connection_string() const
} {
std::string connect_str;
if (host_) connect_str += "host=" + *host_;
if (port_) connect_str += " port=" + *port_;
if (dbname_) connect_str += " dbname=" + *dbname_;
if (user_) connect_str += " user=" + *user_;
if (pass_) connect_str += " password=" + *pass_;
connect_str += " connect_timeout=4"; // todo: set by client (param)
return connect_str;
}
private: private:
string host_; boost::optional<string> host_;
string port_; boost::optional<string> port_;
string dbname_; boost::optional<string> dbname_;
string user_; boost::optional<string> user_;
string pass_; boost::optional<string> pass_;
}; };

View file

@ -48,40 +48,22 @@ using mapnik::attribute_descriptor;
postgis_datasource::postgis_datasource(parameters const& params) postgis_datasource::postgis_datasource(parameters const& params)
: datasource (params), : datasource (params),
table_(params.get("table")), table_(*params.get<std::string>("table","")),
type_(datasource::Vector), type_(datasource::Vector),
extent_initialized_(false), extent_initialized_(false),
desc_(params.get("type"),"utf-8"), desc_(*params.get<std::string>("type"),"utf-8"),
creator_(params.get("host"), creator_(params.get<std::string>("host"),
params.get("port"), params.get<std::string>("port"),
params.get("dbname"), params.get<std::string>("dbname"),
params.get("user"), params.get<std::string>("user"),
params.get("password")) params.get<std::string>("password"))
{ {
unsigned initial_size; boost::optional<int> initial_size = params_.get<int>("inital_size",1);
unsigned max_size; boost::optional<int> max_size = params_.get<int>("max_size",10);
try
{
initial_size = boost::lexical_cast<unsigned>(params_.get("initial_size"));
}
catch (bad_lexical_cast& )
{
initial_size = 1;
}
try
{
max_size = boost::lexical_cast<unsigned>(params_.get("max_size"));
}
catch (bad_lexical_cast&)
{
max_size = 10;
}
ConnectionManager *mgr=ConnectionManager::instance(); ConnectionManager *mgr=ConnectionManager::instance();
mgr->registerPool(creator_, initial_size, max_size); mgr->registerPool(creator_, *initial_size, *max_size);
shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id()); shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id());
if (pool) if (pool)
@ -275,7 +257,9 @@ Envelope<double> postgis_datasource::envelope() const
{ {
std::ostringstream s; std::ostringstream s;
std::string table_name = table_from_sql(table_); std::string table_name = table_from_sql(table_);
if (params_.get("estimate_extent") == "true") boost::optional<std::string> estimate_extent = params_.get<std::string>("estimate_extent");
if (estimate_extent && *estimate_extent == "true")
{ {
s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)" s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)"
<< " from (select estimated_extent('" << " from (select estimated_extent('"

View file

@ -53,7 +53,7 @@ feature_ptr postgis_featureset::next()
feature_ptr feature(new Feature(count_)); feature_ptr feature(new Feature(count_));
int size=rs_->getFieldLength(0); int size=rs_->getFieldLength(0);
const char *data = rs_->getValue(0); const char *data = rs_->getValue(0);
geometry_ptr geom = geometry_utils::from_wkb(data,size,-1); geometry_ptr geom = geometry_utils::from_wkb(data,size);
totalGeomSize_+=size; totalGeomSize_+=size;
if (geom) if (geom)

View file

@ -43,32 +43,30 @@ using mapnik::layer_descriptor;
using mapnik::featureset_ptr; using mapnik::featureset_ptr;
using mapnik::query; using mapnik::query;
using mapnik::coord2d; using mapnik::coord2d;
using mapnik::datasource_exception;
raster_datasource::raster_datasource(const parameters& params) raster_datasource::raster_datasource(const parameters& params)
: datasource (params), : datasource (params),
desc_(params.get("name"),"utf-8") desc_(*params.get<std::string>("type"),"utf-8")
{ {
filename_=params.get("file");
format_=params.get("format"); boost::optional<std::string> file=params.get<std::string>("file");
if (!file) throw datasource_exception("missing <file> parameter ");
try filename_ = *file;
{ format_=*params.get<std::string>("format","tiff");
double lox=lexical_cast<double>(params.get("lox")); boost::optional<double> lox = params.get<double>("lox");
double loy=lexical_cast<double>(params.get("loy")); boost::optional<double> loy = params.get<double>("loy");
double hix=lexical_cast<double>(params.get("hix")); boost::optional<double> hix = params.get<double>("hix");
double hiy=lexical_cast<double>(params.get("hiy")); boost::optional<double> hiy = params.get<double>("hiy");
extent_.init(lox,loy,hix,hiy);
} if (lox && loy && hix && hiy)
catch (bad_lexical_cast& ex) {
{ extent_.init(*lox,*loy,*hix,*hiy);
clog << ex.what() << endl; }
} else throw datasource_exception("<lox> <loy> <hix> <hiy> are required");
} }
raster_datasource::~raster_datasource() {}
raster_datasource::~raster_datasource()
{
}
int raster_datasource::type() const int raster_datasource::type() const
{ {

View file

@ -39,17 +39,15 @@ using mapnik::filter_at_point;
using mapnik::attribute_descriptor; using mapnik::attribute_descriptor;
shape_datasource::shape_datasource(const parameters &params) shape_datasource::shape_datasource(const parameters &params)
: datasource (params) , : datasource (params),
shape_name_(params.get("file")), shape_name_(*params_.get<std::string>("file","")),
type_(datasource::Vector), type_(datasource::Vector),
file_length_(0), file_length_(0),
indexed_(false), indexed_(false),
desc_(params.get("name"),"utf-8") desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
{ {
try try
{ {
std::string encoding = params.get("encoding");
if (encoding.length() > 0) desc_.set_encoding(encoding);
shape_io shape(shape_name_); shape_io shape(shape_name_);
init(shape); init(shape);
for (int i=0;i<shape.dbf().num_fields();++i) for (int i=0;i<shape.dbf().num_fields();++i)
@ -85,17 +83,19 @@ shape_datasource::shape_datasource(const parameters &params)
} }
} }
} }
catch (datasource_exception& ex) catch (datasource_exception& ex)
{ {
std::clog<<ex.what()<<std::endl; std::clog<<ex.what()<<std::endl;
throw; throw;
} }
catch (...)
{
std::clog << " got exception ... \n";
throw;
}
} }
shape_datasource::~shape_datasource() {}
shape_datasource::~shape_datasource()
{
}
const std::string shape_datasource::name_="shape"; const std::string shape_datasource::name_="shape";

View file

@ -39,28 +39,28 @@ using mapnik::coord2d;
class shape_datasource : public datasource class shape_datasource : public datasource
{ {
public: public:
shape_datasource(const parameters &params); shape_datasource(const parameters &params);
virtual ~shape_datasource(); virtual ~shape_datasource();
int type() const; int type() const;
static std::string name(); static std::string name();
featureset_ptr features(const query& q) const; featureset_ptr features(const query& q) const;
featureset_ptr features_at_point(coord2d const& pt) const; featureset_ptr features_at_point(coord2d const& pt) const;
Envelope<double> envelope() const; Envelope<double> envelope() const;
layer_descriptor get_descriptor() const; layer_descriptor get_descriptor() const;
private: private:
shape_datasource(const shape_datasource&); shape_datasource(const shape_datasource&);
shape_datasource& operator=(const shape_datasource&); shape_datasource& operator=(const shape_datasource&);
void init(shape_io& shape); void init(shape_io& shape);
private: private:
std::string shape_name_; int type_;
int type_; std::string shape_name_;
long file_length_; long file_length_;
Envelope<double> extent_; Envelope<double> extent_;
bool indexed_; bool indexed_;
layer_descriptor desc_; layer_descriptor desc_;
static const std::string name_; static const std::string name_;
}; };
#endif //SHAPE_HPP #endif //SHAPE_HPP

View file

@ -71,7 +71,7 @@ feature_ptr shape_featureset<filterT>::next()
{ {
double x=shape_.shp().read_double(); double x=shape_.shp().read_double();
double y=shape_.shp().read_double(); double y=shape_.shp().read_double();
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl);
point->move_to(x,y); point->move_to(x,y);
feature->set_geometry(point); feature->set_geometry(point);
++count_; ++count_;
@ -81,7 +81,7 @@ feature_ptr shape_featureset<filterT>::next()
double x=shape_.shp().read_double(); double x=shape_.shp().read_double();
double y=shape_.shp().read_double(); double y=shape_.shp().read_double();
shape_.shp().read_double();//m shape_.shp().read_double();//m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl);
point->move_to(x,y); point->move_to(x,y);
feature->set_geometry(point); feature->set_geometry(point);
++count_; ++count_;
@ -92,7 +92,7 @@ feature_ptr shape_featureset<filterT>::next()
double y=shape_.shp().read_double(); double y=shape_.shp().read_double();
shape_.shp().read_double();//z shape_.shp().read_double();//z
shape_.shp().read_double();//m shape_.shp().read_double();//m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl);
point->move_to(x,y); point->move_to(x,y);
feature->set_geometry(point); feature->set_geometry(point);
++count_; ++count_;

View file

@ -85,7 +85,7 @@ feature_ptr shape_index_featureset<filterT>::next()
{ {
double x=shape_.shp().read_double(); double x=shape_.shp().read_double();
double y=shape_.shp().read_double(); double y=shape_.shp().read_double();
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl);
point->move_to(x,y); point->move_to(x,y);
feature->set_geometry(point); feature->set_geometry(point);
++count_; ++count_;
@ -95,7 +95,7 @@ feature_ptr shape_index_featureset<filterT>::next()
double x=shape_.shp().read_double(); double x=shape_.shp().read_double();
double y=shape_.shp().read_double(); double y=shape_.shp().read_double();
shape_.shp().read_double();// m shape_.shp().read_double();// m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl);
point->move_to(x,y); point->move_to(x,y);
feature->set_geometry(point); feature->set_geometry(point);
++count_; ++count_;
@ -106,7 +106,7 @@ feature_ptr shape_index_featureset<filterT>::next()
double y=shape_.shp().read_double(); double y=shape_.shp().read_double();
shape_.shp().read_double();// z shape_.shp().read_double();// z
shape_.shp().read_double();// m shape_.shp().read_double();// m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl);
point->move_to(x,y); point->move_to(x,y);
feature->set_geometry(point); feature->set_geometry(point);
++count_; ++count_;

View file

@ -96,7 +96,7 @@ geometry_ptr shape_io::read_polyline()
shp_.read_record(record); shp_.read_record(record);
int num_parts=record.read_ndr_integer(); int num_parts=record.read_ndr_integer();
int num_points=record.read_ndr_integer(); int num_points=record.read_ndr_integer();
geometry_ptr line(new line_string_impl(-1)); geometry_ptr line(new line_string_impl);
line->set_capacity(num_points + num_parts); line->set_capacity(num_points + num_parts);
if (num_parts == 1) if (num_parts == 1)
{ {
@ -151,7 +151,7 @@ geometry_ptr shape_io::read_polylinem()
shp_.read_record(record); shp_.read_record(record);
int num_parts=record.read_ndr_integer(); int num_parts=record.read_ndr_integer();
int num_points=record.read_ndr_integer(); int num_points=record.read_ndr_integer();
geometry_ptr line(new line_string_impl(-1)); geometry_ptr line(new line_string_impl);
line->set_capacity(num_points + num_parts); line->set_capacity(num_points + num_parts);
if (num_parts == 1) if (num_parts == 1)
{ {
@ -214,7 +214,7 @@ geometry_ptr shape_io::read_polylinez()
shp_.read_record(record); shp_.read_record(record);
int num_parts=record.read_ndr_integer(); int num_parts=record.read_ndr_integer();
int num_points=record.read_ndr_integer(); int num_points=record.read_ndr_integer();
geometry_ptr line(new line_string_impl(-1)); geometry_ptr line(new line_string_impl);
line->set_capacity(num_points + num_parts); line->set_capacity(num_points + num_parts);
if (num_parts == 1) if (num_parts == 1)
{ {
@ -285,7 +285,7 @@ geometry_ptr shape_io::read_polygon()
int num_parts=record.read_ndr_integer(); int num_parts=record.read_ndr_integer();
int num_points=record.read_ndr_integer(); int num_points=record.read_ndr_integer();
std::vector<int> parts(num_parts); std::vector<int> parts(num_parts);
geometry_ptr poly(new polygon_impl(-1)); geometry_ptr poly(new polygon_impl);
poly->set_capacity(num_points + num_parts); poly->set_capacity(num_points + num_parts);
for (int i=0;i<num_parts;++i) for (int i=0;i<num_parts;++i)
{ {
@ -326,7 +326,7 @@ geometry_ptr shape_io::read_polygonm()
int num_parts=record.read_ndr_integer(); int num_parts=record.read_ndr_integer();
int num_points=record.read_ndr_integer(); int num_points=record.read_ndr_integer();
std::vector<int> parts(num_parts); std::vector<int> parts(num_parts);
geometry_ptr poly(new polygon_impl(-1)); geometry_ptr poly(new polygon_impl);
poly->set_capacity(num_points + num_parts); poly->set_capacity(num_points + num_parts);
for (int i=0;i<num_parts;++i) for (int i=0;i<num_parts;++i)
{ {
@ -375,7 +375,7 @@ geometry_ptr shape_io::read_polygonz()
int num_parts=record.read_ndr_integer(); int num_parts=record.read_ndr_integer();
int num_points=record.read_ndr_integer(); int num_points=record.read_ndr_integer();
std::vector<int> parts(num_parts); std::vector<int> parts(num_parts);
geometry_ptr poly(new polygon_impl(-1)); geometry_ptr poly(new polygon_impl);
poly->set_capacity(num_points + num_parts); poly->set_capacity(num_points + num_parts);
for (int i=0;i<num_parts;++i) for (int i=0;i<num_parts;++i)
{ {

View file

@ -55,28 +55,31 @@ namespace mapnik
datasource_ptr ds; datasource_ptr ds;
try try
{ {
const std::string type=params.get("type"); boost::optional<std::string> type = params.get<std::string>("type");
map<string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(type); if (type)
if (itr!=plugins_.end()) {
{ map<string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type);
if (itr->second->handle()) if (itr!=plugins_.end())
{ {
if (itr->second->handle())
{
create_ds* create_datasource = create_ds* create_datasource =
(create_ds*) lt_dlsym(itr->second->handle(), "create"); (create_ds*) lt_dlsym(itr->second->handle(), "create");
if (!create_datasource) if (!create_datasource)
{ {
std::clog << "Cannot load symbols: " << lt_dlerror() << std::endl; std::clog << "Cannot load symbols: " << lt_dlerror() << std::endl;
} }
else else
{ {
ds=datasource_ptr(create_datasource(params),datasource_deleter()); ds=datasource_ptr(create_datasource(params), datasource_deleter());
} }
} }
else else
{ {
std::clog << "Cannot load library: " << " "<< lt_dlerror() << std::endl; std::clog << "Cannot load library: " << lt_dlerror() << std::endl;
} }
} }
}
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
std::clog<<"datasource="<<ds<<" type="<<type<<std::endl; std::clog<<"datasource="<<ds<<" type="<<type<<std::endl;
#endif #endif

View file

@ -36,7 +36,6 @@ namespace mapnik
}; };
const char* wkb_; const char* wkb_;
unsigned size_; unsigned size_;
int srid_;
unsigned pos_; unsigned pos_;
wkbByteOrder byteOrder_; wkbByteOrder byteOrder_;
bool needSwap_; bool needSwap_;
@ -52,10 +51,9 @@ namespace mapnik
wkbGeometryCollection=7 wkbGeometryCollection=7
}; };
wkb_reader(const char* wkb,unsigned size,int srid) wkb_reader(const char* wkb,unsigned size)
: wkb_(wkb), : wkb_(wkb),
size_(size), size_(size),
srid_(srid),
pos_(0), pos_(0),
byteOrder_((wkbByteOrder)wkb_[0]) byteOrder_((wkbByteOrder)wkb_[0])
{ {
@ -174,7 +172,7 @@ namespace mapnik
geometry_ptr read_point() geometry_ptr read_point()
{ {
geometry_ptr pt(new point<vertex2d>(srid_)); geometry_ptr pt(new point<vertex2d>);
double x = read_double(); double x = read_double();
double y = read_double(); double y = read_double();
pt->move_to(x,y); pt->move_to(x,y);
@ -183,7 +181,7 @@ namespace mapnik
geometry_ptr read_multipoint() geometry_ptr read_multipoint()
{ {
geometry_ptr pt(new point<vertex2d>(srid_)); geometry_ptr pt(new point<vertex2d>);
int num_points = read_integer(); int num_points = read_integer();
for (int i=0;i<num_points;++i) for (int i=0;i<num_points;++i)
{ {
@ -197,7 +195,7 @@ namespace mapnik
geometry_ptr read_linestring() geometry_ptr read_linestring()
{ {
geometry_ptr line(new line_string<vertex2d>(srid_)); geometry_ptr line(new line_string<vertex2d>);
int num_points=read_integer(); int num_points=read_integer();
CoordinateArray ar(num_points); CoordinateArray ar(num_points);
read_coords(ar); read_coords(ar);
@ -212,7 +210,7 @@ namespace mapnik
geometry_ptr read_multilinestring() geometry_ptr read_multilinestring()
{ {
geometry_ptr line(new line_string<vertex2d>(srid_)); geometry_ptr line(new line_string<vertex2d>);
int num_lines=read_integer(); int num_lines=read_integer();
for (int i=0;i<num_lines;++i) for (int i=0;i<num_lines;++i)
@ -234,7 +232,7 @@ namespace mapnik
geometry_ptr read_polygon() geometry_ptr read_polygon()
{ {
geometry_ptr poly(new polygon<vertex2d>(srid_)); geometry_ptr poly(new polygon<vertex2d>);
int num_rings=read_integer(); int num_rings=read_integer();
@ -257,7 +255,7 @@ namespace mapnik
geometry_ptr read_multipolygon() geometry_ptr read_multipolygon()
{ {
geometry_ptr poly(new polygon<vertex2d>(srid_)); geometry_ptr poly(new polygon<vertex2d>);
int num_polys=read_integer(); int num_polys=read_integer();
for (int i=0;i<num_polys;++i) for (int i=0;i<num_polys;++i)
@ -281,9 +279,9 @@ namespace mapnik
} }
}; };
geometry_ptr geometry_utils::from_wkb(const char* wkb, unsigned size,int srid) geometry_ptr geometry_utils::from_wkb(const char* wkb, unsigned size)
{ {
wkb_reader reader(wkb,size,srid); wkb_reader reader(wkb,size);
return reader.read(); return reader.read();
} }
} }