Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/move_bind_logic_to_layer' into m…
Browse files Browse the repository at this point in the history
…ove_bind_logic_to_layer
  • Loading branch information
artemp committed Mar 9, 2012
2 parents 90f453a + 4bf23dc commit a4c4c4b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 157 deletions.
51 changes: 18 additions & 33 deletions plugins/input/csv/csv_datasource.cpp
Expand Up @@ -29,23 +29,23 @@ using namespace boost::spirit;

DATASOURCE_PLUGIN(csv_datasource)

csv_datasource::csv_datasource(parameters const& params, bool bind)
csv_datasource::csv_datasource(parameters const& params)
: datasource(params),
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding", "utf-8")),
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding", "utf-8")),
extent_(),
filename_(),
inline_string_(),
file_length_(0),
row_limit_(*params_.get<int>("row_limit", 0)),
row_limit_(*params.get<int>("row_limit", 0)),
features_(),
escape_(*params_.get<std::string>("escape", "")),
separator_(*params_.get<std::string>("separator", "")),
quote_(*params_.get<std::string>("quote", "")),
escape_(*params.get<std::string>("escape", "")),
separator_(*params.get<std::string>("separator", "")),
quote_(*params.get<std::string>("quote", "")),
headers_(),
manual_headers_(boost::trim_copy(*params_.get<std::string>("headers", ""))),
strict_(*params_.get<mapnik::boolean>("strict", false)),
quiet_(*params_.get<mapnik::boolean>("quiet", false)),
filesize_max_(*params_.get<float>("filesize_max", 20.0)) // MB
manual_headers_(boost::trim_copy(*params.get<std::string>("headers", ""))),
strict_(*params.get<mapnik::boolean>("strict", false)),
quiet_(*params.get<mapnik::boolean>("quiet", false)),
filesize_max_(*params.get<float>("filesize_max", 20.0)) // MB
{
/* TODO:
general:
Expand All @@ -70,36 +70,31 @@ csv_datasource::csv_datasource(parameters const& params, bool bind)
http://boost-spirit.com/home/articles/qi-example/tracking-the-input-position-while-parsing/
*/

boost::optional<std::string> inline_string = params_.get<std::string>("inline");
boost::optional<std::string> inline_string = params.get<std::string>("inline");
if (inline_string)
{
inline_string_ = *inline_string;
}
else
{
boost::optional<std::string> file = params_.get<std::string>("file");
boost::optional<std::string> file = params.get<std::string>("file");
if (!file) throw mapnik::datasource_exception("CSV Plugin: missing <file> parameter");

boost::optional<std::string> base = params_.get<std::string>("base");
boost::optional<std::string> base = params.get<std::string>("base");
if (base)
filename_ = *base + "/" + *file;
else
filename_ = *file;
}

if (bind)
{
this->bind();
}
this->init(params);
}


csv_datasource::~csv_datasource() { }

void csv_datasource::bind() const
void csv_datasource::init(mapnik::parameters const& params)
{
if (is_bound_) return;

if (!inline_string_.empty())
{
std::istringstream in(inline_string_);
Expand All @@ -113,7 +108,6 @@ void csv_datasource::bind() const
parse_csv(in,escape_, separator_, quote_);
in.close();
}
is_bound_ = true;
}

template <typename T>
Expand Down Expand Up @@ -245,7 +239,7 @@ void csv_datasource::parse_csv(T& stream,
// grammer = boost::escaped_list_separator<char>('\\', ',', '\"');
grammer = boost::escaped_list_separator<char>(esc, sep, quo);
}
catch(const std::exception & ex)
catch(std::exception const& ex)
{
std::ostringstream s;
s << "CSV Plugin: " << ex.what();
Expand Down Expand Up @@ -371,7 +365,7 @@ void csv_datasource::parse_csv(T& stream,
break;
}
}
catch(const std::exception & ex)
catch(std::exception const& ex)
{
std::ostringstream s;
s << "CSV Plugin: error parsing headers: " << ex.what();
Expand Down Expand Up @@ -812,7 +806,7 @@ void csv_datasource::parse_csv(T& stream,
if (!quiet_) std::clog << ex.what() << "\n";
}
}
catch(const std::exception & ex)
catch(std::exception const& ex)
{
std::ostringstream s;
s << "CSV Plugin: unexpected error parsing line: " << line_number
Expand Down Expand Up @@ -846,14 +840,11 @@ datasource::datasource_t csv_datasource::type() const

mapnik::box2d<double> csv_datasource::envelope() const
{
if (!is_bound_) bind();

return extent_;
}

boost::optional<mapnik::datasource::geometry_t> csv_datasource::get_geometry_type() const
{
if (! is_bound_) bind();
boost::optional<mapnik::datasource::geometry_t> result;
int multi_type = 0;
unsigned num_features = features_.size();
Expand All @@ -876,15 +867,11 @@ boost::optional<mapnik::datasource::geometry_t> csv_datasource::get_geometry_typ

mapnik::layer_descriptor csv_datasource::get_descriptor() const
{
if (!is_bound_) bind();

return desc_;
}

mapnik::featureset_ptr csv_datasource::features(mapnik::query const& q) const
{
if (!is_bound_) bind();

const std::set<std::string>& attribute_names = q.property_names();
std::set<std::string>::const_iterator pos = attribute_names.begin();
while (pos != attribute_names.end())
Expand Down Expand Up @@ -916,7 +903,5 @@ mapnik::featureset_ptr csv_datasource::features(mapnik::query const& q) const

mapnik::featureset_ptr csv_datasource::features_at_point(mapnik::coord2d const& pt) const
{
if (!is_bound_) bind();

throw mapnik::datasource_exception("CSV Plugin: features_at_point is not supported yet");
}
4 changes: 2 additions & 2 deletions plugins/input/csv/csv_datasource.hpp
Expand Up @@ -10,7 +10,7 @@
class csv_datasource : public mapnik::datasource
{
public:
csv_datasource(mapnik::parameters const& params, bool bind=true);
csv_datasource(mapnik::parameters const& params);
virtual ~csv_datasource ();
mapnik::datasource::datasource_t type() const;
static std::string name();
Expand All @@ -19,13 +19,13 @@ class csv_datasource : public mapnik::datasource
mapnik::box2d<double> envelope() const;
boost::optional<mapnik::datasource::geometry_t> get_geometry_type() const;
mapnik::layer_descriptor get_descriptor() const;
void bind() const;
template <typename T>
void parse_csv(T& stream,
std::string const& escape,
std::string const& separator,
std::string const& quote) const;
private:
void init(mapnik::parameters const& params);
mutable mapnik::layer_descriptor desc_;
mutable mapnik::box2d<double> extent_;
mutable std::string filename_;
Expand Down
25 changes: 5 additions & 20 deletions plugins/input/kismet/kismet_datasource.cpp
Expand Up @@ -71,15 +71,15 @@ boost::mutex knd_list_mutex;
std::list<kismet_network_data> knd_list;
const unsigned int queue_size = 20;

kismet_datasource::kismet_datasource(parameters const& params, bool bind)
kismet_datasource::kismet_datasource(parameters const& params)
: datasource(params),
extent_(),
extent_initialized_(false),
type_(datasource::Vector),
srs_("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"),
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
{
boost::optional<std::string> host = params_.get<std::string>("host");
boost::optional<std::string> host = params.get<std::string>("host");
if (host)
{
host_ = *host;
Expand All @@ -89,37 +89,25 @@ kismet_datasource::kismet_datasource(parameters const& params, bool bind)
throw datasource_exception("Kismet Plugin: missing <host> parameter");
}

boost::optional<unsigned int> port = params_.get<unsigned int>("port", 2501);
boost::optional<unsigned int> port = params.get<unsigned int>("port", 2501);
if (port)
{
port_ = *port;
}

boost::optional<std::string> srs = params_.get<std::string>("srs");
boost::optional<std::string> srs = params.get<std::string>("srs");
if (srs)
{
srs_ = *srs;
}

boost::optional<std::string> ext = params_.get<std::string>("extent");
boost::optional<std::string> ext = params.get<std::string>("extent");
if (ext)
{
extent_initialized_ = extent_.from_string(*ext);
}

kismet_thread.reset(new boost::thread(boost::bind(&kismet_datasource::run, this, host_, port_)));

if (bind)
{
this->bind();
}
}

void kismet_datasource::bind() const
{
if (is_bound_) return;

is_bound_ = true;
}

kismet_datasource::~kismet_datasource()
Expand All @@ -138,7 +126,6 @@ mapnik::datasource::datasource_t kismet_datasource::type() const

box2d<double> kismet_datasource::envelope() const
{
if (! is_bound_) bind();
return extent_;
}

Expand All @@ -154,7 +141,6 @@ layer_descriptor kismet_datasource::get_descriptor() const

featureset_ptr kismet_datasource::features(query const& q) const
{
if (! is_bound_) bind();

// std::clog << "kismet_datasource::features()" << endl;

Expand All @@ -172,7 +158,6 @@ featureset_ptr kismet_datasource::features(query const& q) const

featureset_ptr kismet_datasource::features_at_point(coord2d const& pt) const
{
if (! is_bound_) bind();

// std::clog << "kismet_datasource::features_at_point()" << endl;

Expand Down
5 changes: 2 additions & 3 deletions plugins/input/kismet/kismet_datasource.hpp
Expand Up @@ -43,7 +43,7 @@
class kismet_datasource : public mapnik::datasource
{
public:
kismet_datasource(mapnik::parameters const& params, bool bind = true);
kismet_datasource(mapnik::parameters const& params);
virtual ~kismet_datasource ();
datasource::datasource_t type() const;
static std::string name();
Expand All @@ -52,11 +52,10 @@ class kismet_datasource : public mapnik::datasource
mapnik::box2d<double> envelope() const;
boost::optional<mapnik::datasource::geometry_t> get_geometry_type() const;
mapnik::layer_descriptor get_descriptor() const;
void bind() const;

private:
void init(mapnik::parameters const& params);
void run (const std::string& host, const unsigned int port);

mapnik::box2d<double> extent_;
bool extent_initialized_;
std::string host_;
Expand Down

0 comments on commit a4c4c4b

Please sign in to comment.