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 "connection_manager.hpp"
DATASOURCE_PLUGIN(PostgisDatasource);
DATASOURCE_PLUGIN(postgis_datasource);
const std::string PostgisDatasource::GEOMETRY_COLUMNS="geometry_columns";
const std::string PostgisDatasource::SPATIAL_REF_SYS="spatial_ref_system";
const std::string postgis_datasource::GEOMETRY_COLUMNS="geometry_columns";
const std::string postgis_datasource::SPATIAL_REF_SYS="spatial_ref_system";
using std::cerr;
using std::cout;
@ -37,7 +37,7 @@ using std::endl;
using boost::lexical_cast;
using boost::bad_lexical_cast;
PostgisDatasource::PostgisDatasource(const Parameters& params)
postgis_datasource::postgis_datasource(const Parameters& params)
: table_(params.get("table")),
type_(datasource::Vector),
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_;
}
int PostgisDatasource::type() const
int postgis_datasource::type() const
{
return type_;
}
layer_descriptor const& PostgisDatasource::get_descriptor() const
layer_descriptor const& postgis_datasource::get_descriptor() const
{
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);
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;
}
featureset_ptr PostgisDatasource::features(const query& q) const
featureset_ptr postgis_datasource::features(const query& q) const
{
Featureset *fs=0;
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_<<")";
cout << s.str() << endl;
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);
}
const Envelope<double>& PostgisDatasource::envelope() const
const Envelope<double>& postgis_datasource::envelope() const
{
return extent_;
}
PostgisDatasource::~PostgisDatasource() {}
postgis_datasource::~postgis_datasource() {}

View file

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

View file

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

View file

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

View file

@ -40,7 +40,7 @@ public:
shape_featureset(const filterT& filter, const std::string& shape_file,
const std::set<std::string>& attribute_names,long file_length);
virtual ~shape_featureset();
Feature* next();
feature_ptr next();
private:
shape_featureset(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>
Feature* shape_index_featureset<filterT>::next()
{
Feature *f=0;
feature_ptr shape_index_featureset<filterT>::next()
{
if (itr_!=ids_.end())
{
int pos=*itr_++;
shape_.move_to(pos);
int type=shape_.type();
feature_ptr feature(new Feature(shape_.id_));
if (type==shape_io::shape_point)
{
double x=shape_.shp().read_double();
double y=shape_.shp().read_double();
geometry_ptr point(new point_impl(-1));
point->move_to(x,y);
f=new Feature(shape_.id_,point);
feature->set_geometry(point);
++count_;
}
else if (type == shape_io::shape_pointm)
@ -82,7 +84,7 @@ Feature* shape_index_featureset<filterT>::next()
shape_.shp().read_double();// m
geometry_ptr point(new point_impl(-1));
point->move_to(x,y);
f=new Feature(shape_.id_,point);
feature->set_geometry(point);
++count_;
}
else if (type == shape_io::shape_pointz)
@ -93,7 +95,7 @@ Feature* shape_index_featureset<filterT>::next()
shape_.shp().read_double();// m
geometry_ptr point(new point_impl(-1));
point->move_to(x,y);
f=new Feature(shape_.id_,point);
feature->set_geometry(point);
++count_;
}
else
@ -110,21 +112,21 @@ Feature* shape_index_featureset<filterT>::next()
case shape_io::shape_polyline:
{
geometry_ptr line = shape_.read_polyline();
f=new Feature(shape_.id_,line);
feature->set_geometry(line);
++count_;
break;
}
case shape_io::shape_polylinem:
{
geometry_ptr line = shape_.read_polylinem();
f=new Feature(shape_.id_,line);
feature->set_geometry(line);
++count_;
break;
}
case shape_io::shape_polylinez:
{
geometry_ptr line = shape_.read_polylinez();
f=new Feature(shape_.id_,line);
feature->set_geometry(line);
++count_;
break;
}
@ -132,35 +134,35 @@ Feature* shape_index_featureset<filterT>::next()
{
geometry_ptr poly = shape_.read_polygon();
f=new Feature(shape_.id_,poly);
feature->set_geometry(poly);
++count_;
break;
}
case shape_io::shape_polygonm:
{
geometry_ptr poly = shape_.read_polygonm();
f=new Feature(shape_.id_,poly);
feature->set_geometry(poly);
++count_;
break;
}
case shape_io::shape_polygonz:
{
geometry_ptr poly = shape_.read_polygonz();
f=new Feature(shape_.id_,poly);
feature->set_geometry(poly);
++count_;
break;
}
}
if (attr_ids_.size())
{
f->reserve_props(attr_ids_.size());
feature->reserve_props(attr_ids_.size());
shape_.dbf().move_to(shape_.id_);
std::vector<int>::const_iterator pos=attr_ids_.begin();
while (pos!=attr_ids_.end())
{
try
{
shape_.dbf().add_attribute(*pos,f);
shape_.dbf().add_attribute(*pos,feature.get());
}
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,
const std::set<std::string>& attribute_names);
virtual ~shape_index_featureset();
Feature* next();
feature_ptr next();
private:
//no copying
shape_index_featureset(const shape_index_featureset&);

View file

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

View file

@ -68,7 +68,7 @@ namespace mapnik
std::vector<rule_type*> else_rules;
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();
while (ruleIter!=rules.end())
@ -104,7 +104,7 @@ namespace mapnik
featureset_ptr fs=ds->features(q);
if (fs)
{
Feature* feature=0;
feature_ptr feature;
while ((feature = fs->next()))
{
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();
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();
if (pos!=style.end()) {
std::vector<ref_ptr<Feature> >::iterator itr=selection.begin();
while (itr!=selection.end())
{
geometry_ptr& geom=(*itr)->get_geometry();
geom->transform(t);
(*pos)->render(*geom,image);
++itr;
}
}
l.clear_selection();
}
// Style::Iterator pos = style.begin();
// if (pos!=style.end()) {
// std::vector<ref_ptr<Feature> >::iterator itr=selection.begin();
//
// while (itr!=selection.end())
// {
// geometry_ptr& geom=(*itr)->get_geometry();
// geom->transform(t);
// (*pos)->render(*geom,image);
// ++itr;
// }
// }
// l.clear_selection();
//}
}
}
}