return ref_ptr<Feature> from datasource next() method

should play better with python
This commit is contained in:
Artem Pavlenko 2005-09-08 13:09:04 +00:00
parent 03467ac96e
commit 820b6bb23a
9 changed files with 98 additions and 89 deletions

View file

@ -25,10 +25,10 @@
#include <sstream> #include <sstream>
#include "connection_manager.hpp" #include "connection_manager.hpp"
DATASOURCE_PLUGIN(PostgisDatasource); DATASOURCE_PLUGIN(postgis_datasource);
const std::string PostgisDatasource::GEOMETRY_COLUMNS="geometry_columns"; const std::string postgis_datasource::GEOMETRY_COLUMNS="geometry_columns";
const std::string PostgisDatasource::SPATIAL_REF_SYS="spatial_ref_system"; const std::string postgis_datasource::SPATIAL_REF_SYS="spatial_ref_system";
using std::cerr; using std::cerr;
using std::cout; using std::cout;
@ -37,7 +37,7 @@ using std::endl;
using boost::lexical_cast; using boost::lexical_cast;
using boost::bad_lexical_cast; using boost::bad_lexical_cast;
PostgisDatasource::PostgisDatasource(const Parameters& params) postgis_datasource::postgis_datasource(const Parameters& params)
: table_(params.get("table")), : table_(params.get("table")),
type_(datasource::Vector), type_(datasource::Vector),
desc_(params.get("name")), desc_(params.get("name")),
@ -142,24 +142,24 @@ PostgisDatasource::PostgisDatasource(const Parameters& params)
} }
} }
std::string PostgisDatasource::name_="postgis"; std::string postgis_datasource::name_="postgis";
std::string PostgisDatasource::name() std::string postgis_datasource::name()
{ {
return name_; return name_;
} }
int PostgisDatasource::type() const int postgis_datasource::type() const
{ {
return type_; return type_;
} }
layer_descriptor const& PostgisDatasource::get_descriptor() const layer_descriptor const& postgis_datasource::get_descriptor() const
{ {
return desc_; return desc_;
} }
std::string PostgisDatasource::table_from_sql(const std::string& sql) std::string postgis_datasource::table_from_sql(const std::string& sql)
{ {
std::string table_name(sql); std::string table_name(sql);
transform(table_name.begin(),table_name.end(),table_name.begin(),tolower); transform(table_name.begin(),table_name.end(),table_name.begin(),tolower);
@ -174,7 +174,7 @@ std::string PostgisDatasource::table_from_sql(const std::string& sql)
return table_name; return table_name;
} }
featureset_ptr PostgisDatasource::features(const query& q) const featureset_ptr postgis_datasource::features(const query& q) const
{ {
Featureset *fs=0; Featureset *fs=0;
Envelope<double> const& box=q.get_bbox(); Envelope<double> const& box=q.get_bbox();
@ -202,15 +202,15 @@ featureset_ptr PostgisDatasource::features(const query& q) const
s << box.maxx() << " " << box.maxy() << ")'::box3d,"<<srid_<<")"; s << box.maxx() << " " << box.maxy() << ")'::box3d,"<<srid_<<")";
cout << s.str() << endl; cout << s.str() << endl;
ref_ptr<ResultSet> rs=conn->executeQuery(s.str(),1); ref_ptr<ResultSet> rs=conn->executeQuery(s.str(),1);
fs=new PostgisFeatureset(rs,props.size()); fs=new postgis_featureset(rs,props.size());
} }
} }
return featureset_ptr(fs); return featureset_ptr(fs);
} }
const Envelope<double>& PostgisDatasource::envelope() const const Envelope<double>& postgis_datasource::envelope() const
{ {
return extent_; return extent_;
} }
PostgisDatasource::~PostgisDatasource() {} postgis_datasource::~postgis_datasource() {}

View file

@ -33,7 +33,7 @@
using namespace mapnik; using namespace mapnik;
class PostgisDatasource : public datasource class postgis_datasource : public datasource
{ {
static const std::string GEOMETRY_COLUMNS; static const std::string GEOMETRY_COLUMNS;
static const std::string SPATIAL_REF_SYS; static const std::string SPATIAL_REF_SYS;
@ -54,15 +54,15 @@ public:
featureset_ptr features(const query& q) const; featureset_ptr features(const query& q) const;
mapnik::Envelope<double> const& envelope() const; mapnik::Envelope<double> const& envelope() const;
layer_descriptor const& get_descriptor() const; layer_descriptor const& get_descriptor() const;
PostgisDatasource(const Parameters &params); postgis_datasource(const Parameters &params);
~PostgisDatasource(); ~postgis_datasource();
private: private:
static std::string table_from_sql(const std::string& sql); static std::string table_from_sql(const std::string& sql);
PostgisDatasource(const PostgisDatasource&); postgis_datasource(const postgis_datasource&);
PostgisDatasource& operator=(const PostgisDatasource&); postgis_datasource& operator=(const postgis_datasource&);
}; };
class PostgisFeatureset : public Featureset class postgis_featureset : public Featureset
{ {
private: private:
ref_ptr<ResultSet> rs_; ref_ptr<ResultSet> rs_;
@ -70,13 +70,13 @@ private:
mutable int totalGeomSize_; mutable int totalGeomSize_;
mutable int count_; mutable int count_;
public: public:
PostgisFeatureset(const ref_ptr<ResultSet>& rs,unsigned num_attrs); postgis_featureset(const ref_ptr<ResultSet>& rs,unsigned num_attrs);
void dispose(); void dispose();
Feature* next(); feature_ptr next();
~PostgisFeatureset(); ~postgis_featureset();
private: private:
PostgisFeatureset(const PostgisFeatureset&); postgis_featureset(const postgis_featureset&);
const PostgisFeatureset& operator=(const PostgisFeatureset&); const postgis_featureset& operator=(const postgis_featureset&);
}; };
#endif //POSTGIS_HPP #endif //POSTGIS_HPP

View file

@ -27,21 +27,20 @@ using boost::lexical_cast;
using boost::bad_lexical_cast; using boost::bad_lexical_cast;
using std::string; using std::string;
PostgisFeatureset::PostgisFeatureset(const ref_ptr<ResultSet>& rs, postgis_featureset::postgis_featureset(const ref_ptr<ResultSet>& rs,
unsigned num_attrs=0) unsigned num_attrs=0)
: rs_(rs), : rs_(rs),
num_attrs_(num_attrs), num_attrs_(num_attrs),
totalGeomSize_(0), totalGeomSize_(0),
count_(0) {} count_(0) {}
Feature* PostgisFeatureset::next() feature_ptr postgis_featureset::next()
{ {
Feature *feature=0;
if (rs_->next()) if (rs_->next())
{ {
const char* buf = rs_->getValue(0); const char* buf = rs_->getValue(0);
int id = int4net(buf); int id = int4net(buf);
feature_ptr feature(new Feature(id));
int size=rs_->getFieldLength(1); int size=rs_->getFieldLength(1);
const char *data=rs_->getValue(1); const char *data=rs_->getValue(1);
geometry_ptr geom=geometry_utils::from_wkb(data,size,-1); geometry_ptr geom=geometry_utils::from_wkb(data,size,-1);
@ -49,7 +48,7 @@ Feature* PostgisFeatureset::next()
if (geom) if (geom)
{ {
feature=new Feature(id,geom); feature->set_geometry(geom);
feature->reserve_props(num_attrs_); feature->reserve_props(num_attrs_);
unsigned start=2; unsigned start=2;
for (unsigned pos=0;pos<num_attrs_;++pos) for (unsigned pos=0;pos<num_attrs_;++pos)
@ -91,18 +90,19 @@ Feature* PostgisFeatureset::next()
} }
++count_; ++count_;
} }
return feature;
} }
else else
{ {
rs_->close(); rs_->close();
std::cout << "totalGeomSize="<<totalGeomSize_<<" bytes"<<std::endl; std::cout << "totalGeomSize="<<totalGeomSize_<<" bytes"<<std::endl;
std::cout << "count="<<count_<<std::endl; std::cout << "count="<<count_<<std::endl;
return feature_ptr(0);
} }
return feature;
} }
PostgisFeatureset::~PostgisFeatureset() postgis_featureset::~postgis_featureset()
{ {
rs_->close(); rs_->close();
} }

View file

@ -50,23 +50,22 @@ shape_featureset<filterT>::shape_featureset(const filterT& filter,
template <typename filterT> template <typename filterT>
Feature* shape_featureset<filterT>::next() feature_ptr shape_featureset<filterT>::next()
{ {
Feature* feature=0;
std::streampos pos=shape_.shp().pos(); std::streampos pos=shape_.shp().pos();
if (pos < std::streampos(file_length_ * 2)) if (pos < std::streampos(file_length_ * 2))
{ {
shape_.move_to(pos); shape_.move_to(pos);
int type=shape_.type(); int type=shape_.type();
int id=shape_.id_; feature_ptr feature(new Feature(shape_.id_));
if (type == shape_io::shape_point) if (type == shape_io::shape_point)
{ {
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(-1));
point->move_to(x,y); point->move_to(x,y);
feature=new Feature(id,point); feature->set_geometry(point);
++count_; ++count_;
} }
else if (type == shape_io::shape_pointm) else if (type == shape_io::shape_pointm)
@ -76,7 +75,7 @@ Feature* shape_featureset<filterT>::next()
shape_.shp().read_double();//m shape_.shp().read_double();//m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl(-1));
point->move_to(x,y); point->move_to(x,y);
feature=new Feature(id,point); feature->set_geometry(point);
++count_; ++count_;
} }
else if (type == shape_io::shape_pointz) else if (type == shape_io::shape_pointz)
@ -87,7 +86,7 @@ Feature* shape_featureset<filterT>::next()
shape_.shp().read_double();//m shape_.shp().read_double();//m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl(-1));
point->move_to(x,y); point->move_to(x,y);
feature=new Feature(id,point); feature->set_geometry(point);
++count_; ++count_;
} }
else else
@ -97,7 +96,7 @@ Feature* shape_featureset<filterT>::next()
unsigned reclen=shape_.reclength_; unsigned reclen=shape_.reclength_;
shape_.move_to(long(shape_.shp().pos()) + 2 * reclen - 36); shape_.move_to(long(shape_.shp().pos()) + 2 * reclen - 36);
if ((long)shape_.shp().pos() >= file_length_ * 2) if ((long)shape_.shp().pos() >= file_length_ * 2)
return 0; return feature_ptr(0);
} }
switch (type) switch (type)
@ -106,47 +105,47 @@ Feature* shape_featureset<filterT>::next()
case shape_io::shape_polyline: case shape_io::shape_polyline:
{ {
geometry_ptr line = shape_.read_polyline(); geometry_ptr line = shape_.read_polyline();
feature=new Feature(shape_.id_,line); feature->set_geometry(line);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polylinem: case shape_io::shape_polylinem:
{ {
geometry_ptr line = shape_.read_polylinem(); geometry_ptr line = shape_.read_polylinem();
feature=new Feature(shape_.id_,line); feature->set_geometry(line);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polylinez: case shape_io::shape_polylinez:
{ {
geometry_ptr line = shape_.read_polylinez(); geometry_ptr line = shape_.read_polylinez();
feature=new Feature(shape_.id_,line); feature->set_geometry(line);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polygon: case shape_io::shape_polygon:
{ {
geometry_ptr poly = shape_.read_polygon(); geometry_ptr poly = shape_.read_polygon();
feature=new Feature(shape_.id_,poly); feature->set_geometry(poly);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polygonm: case shape_io::shape_polygonm:
{ {
geometry_ptr poly = shape_.read_polygonm(); geometry_ptr poly = shape_.read_polygonm();
feature=new Feature(shape_.id_,poly); feature->set_geometry(poly);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polygonz: case shape_io::shape_polygonz:
{ {
geometry_ptr poly = shape_.read_polygon(); geometry_ptr poly = shape_.read_polygonz();
feature=new Feature(shape_.id_,poly); feature->set_geometry(poly);
++count_; ++count_;
break; break;
} }
default: default:
return 0; return feature_ptr(0);
} }
if (attr_ids_.size()) if (attr_ids_.size())
@ -157,7 +156,7 @@ Feature* shape_featureset<filterT>::next()
{ {
try try
{ {
shape_.dbf().add_attribute(*pos,feature);//TODO optimize!!! shape_.dbf().add_attribute(*pos,feature.get());//TODO optimize!!!
} }
catch (...) catch (...)
{ {
@ -167,11 +166,14 @@ Feature* shape_featureset<filterT>::next()
} }
} }
} }
}
if (!feature)
std::cout<<" total shapes read="<<count_<<"\n";
return feature; return feature;
} }
else
{
std::cout<<" total shapes read="<<count_<<"\n";
return feature_ptr(0);
}
}
template <typename filterT> template <typename filterT>

View file

@ -40,7 +40,7 @@ public:
shape_featureset(const filterT& filter, const std::string& shape_file, shape_featureset(const filterT& filter, const std::string& shape_file,
const std::set<std::string>& attribute_names,long file_length); const std::set<std::string>& attribute_names,long file_length);
virtual ~shape_featureset(); virtual ~shape_featureset();
Feature* next(); feature_ptr next();
private: private:
shape_featureset(const shape_featureset&); shape_featureset(const shape_featureset&);
const shape_featureset& operator=(const shape_featureset&); const shape_featureset& operator=(const shape_featureset&);

View file

@ -58,21 +58,23 @@ shape_index_featureset<filterT>::shape_index_featureset(const filterT& filter,
} }
template <typename filterT> template <typename filterT>
Feature* shape_index_featureset<filterT>::next() feature_ptr shape_index_featureset<filterT>::next()
{ {
Feature *f=0;
if (itr_!=ids_.end()) if (itr_!=ids_.end())
{ {
int pos=*itr_++; int pos=*itr_++;
shape_.move_to(pos); shape_.move_to(pos);
int type=shape_.type(); int type=shape_.type();
feature_ptr feature(new Feature(shape_.id_));
if (type==shape_io::shape_point) if (type==shape_io::shape_point)
{ {
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(-1));
point->move_to(x,y); point->move_to(x,y);
f=new Feature(shape_.id_,point); feature->set_geometry(point);
++count_; ++count_;
} }
else if (type == shape_io::shape_pointm) else if (type == shape_io::shape_pointm)
@ -82,7 +84,7 @@ Feature* shape_index_featureset<filterT>::next()
shape_.shp().read_double();// m shape_.shp().read_double();// m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl(-1));
point->move_to(x,y); point->move_to(x,y);
f=new Feature(shape_.id_,point); feature->set_geometry(point);
++count_; ++count_;
} }
else if (type == shape_io::shape_pointz) else if (type == shape_io::shape_pointz)
@ -93,7 +95,7 @@ Feature* shape_index_featureset<filterT>::next()
shape_.shp().read_double();// m shape_.shp().read_double();// m
geometry_ptr point(new point_impl(-1)); geometry_ptr point(new point_impl(-1));
point->move_to(x,y); point->move_to(x,y);
f=new Feature(shape_.id_,point); feature->set_geometry(point);
++count_; ++count_;
} }
else else
@ -110,21 +112,21 @@ Feature* shape_index_featureset<filterT>::next()
case shape_io::shape_polyline: case shape_io::shape_polyline:
{ {
geometry_ptr line = shape_.read_polyline(); geometry_ptr line = shape_.read_polyline();
f=new Feature(shape_.id_,line); feature->set_geometry(line);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polylinem: case shape_io::shape_polylinem:
{ {
geometry_ptr line = shape_.read_polylinem(); geometry_ptr line = shape_.read_polylinem();
f=new Feature(shape_.id_,line); feature->set_geometry(line);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polylinez: case shape_io::shape_polylinez:
{ {
geometry_ptr line = shape_.read_polylinez(); geometry_ptr line = shape_.read_polylinez();
f=new Feature(shape_.id_,line); feature->set_geometry(line);
++count_; ++count_;
break; break;
} }
@ -132,35 +134,35 @@ Feature* shape_index_featureset<filterT>::next()
{ {
geometry_ptr poly = shape_.read_polygon(); geometry_ptr poly = shape_.read_polygon();
f=new Feature(shape_.id_,poly); feature->set_geometry(poly);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polygonm: case shape_io::shape_polygonm:
{ {
geometry_ptr poly = shape_.read_polygonm(); geometry_ptr poly = shape_.read_polygonm();
f=new Feature(shape_.id_,poly); feature->set_geometry(poly);
++count_; ++count_;
break; break;
} }
case shape_io::shape_polygonz: case shape_io::shape_polygonz:
{ {
geometry_ptr poly = shape_.read_polygonz(); geometry_ptr poly = shape_.read_polygonz();
f=new Feature(shape_.id_,poly); feature->set_geometry(poly);
++count_; ++count_;
break; break;
} }
} }
if (attr_ids_.size()) if (attr_ids_.size())
{ {
f->reserve_props(attr_ids_.size()); feature->reserve_props(attr_ids_.size());
shape_.dbf().move_to(shape_.id_); shape_.dbf().move_to(shape_.id_);
std::vector<int>::const_iterator pos=attr_ids_.begin(); std::vector<int>::const_iterator pos=attr_ids_.begin();
while (pos!=attr_ids_.end()) while (pos!=attr_ids_.end())
{ {
try try
{ {
shape_.dbf().add_attribute(*pos,f); shape_.dbf().add_attribute(*pos,feature.get());
} }
catch (...) catch (...)
{ {
@ -170,9 +172,13 @@ Feature* shape_index_featureset<filterT>::next()
} }
} }
} }
return feature;
}
else
{
std::cout<<count_<<" features\n";
return feature_ptr(0);
} }
if (!f) std::cout<<count_<<" features\n";
return f;
} }

View file

@ -40,7 +40,7 @@ public:
shape_index_featureset(const filterT& filter,const std::string& shape_file, shape_index_featureset(const filterT& filter,const std::string& shape_file,
const std::set<std::string>& attribute_names); const std::set<std::string>& attribute_names);
virtual ~shape_index_featureset(); virtual ~shape_index_featureset();
Feature* next(); feature_ptr next();
private: private:
//no copying //no copying
shape_index_featureset(const shape_index_featureset&); shape_index_featureset(const shape_index_featureset&);

View file

@ -35,7 +35,7 @@ namespace mapnik
typedef ref_ptr<Feature> feature_ptr; typedef ref_ptr<Feature> feature_ptr;
struct Featureset struct Featureset
{ {
virtual Feature* next()=0; virtual feature_ptr next()=0;
virtual ~Featureset() {}; virtual ~Featureset() {};
}; };

View file

@ -68,7 +68,7 @@ namespace mapnik
std::vector<rule_type*> else_rules; std::vector<rule_type*> else_rules;
bool active_rules=false; bool active_rules=false;
const std::vector<rule_type>& rules=style.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();
while (ruleIter!=rules.end()) while (ruleIter!=rules.end())
@ -104,7 +104,7 @@ namespace mapnik
featureset_ptr fs=ds->features(q); featureset_ptr fs=ds->features(q);
if (fs) if (fs)
{ {
Feature* feature=0; feature_ptr feature;
while ((feature = fs->next())) while ((feature = fs->next()))
{ {
bool do_else=true; bool do_else=true;
@ -148,30 +148,31 @@ namespace mapnik
} }
} }
} }
delete feature; //delete feature;
} }
} }
if (l.isSelectable() && l.selection().size()) //TODO !!!
{ //if (l.isSelectable() && l.selection().size()) //TODO !!!
//{
//volatile style_cache* styles=style_cache::instance(); //volatile style_cache* styles=style_cache::instance();
const Style& style=style_cache::instance()->find(l.selection_style()); // const Style& style=style_cache::instance()->find(l.selection_style());
std::vector<ref_ptr<Feature> >& selection=l.selection(); // std::vector<ref_ptr<Feature> >& selection=l.selection();
Style::Iterator pos = style.begin(); // Style::Iterator pos = style.begin();
if (pos!=style.end()) { // if (pos!=style.end()) {
std::vector<ref_ptr<Feature> >::iterator itr=selection.begin(); // std::vector<ref_ptr<Feature> >::iterator itr=selection.begin();
//
while (itr!=selection.end()) // while (itr!=selection.end())
{ // {
geometry_ptr& geom=(*itr)->get_geometry(); // geometry_ptr& geom=(*itr)->get_geometry();
geom->transform(t); // geom->transform(t);
(*pos)->render(*geom,image); // (*pos)->render(*geom,image);
++itr; // ++itr;
} // }
} // }
l.clear_selection(); // l.clear_selection();
} //}
} }
} }
} }