2008-03-01 11:49:37 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2008-03-01 11:49:37 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-22 14:04:05 +00:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2008-03-01 11:49:37 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
// stl
|
|
|
|
#include <iostream>
|
2011-12-05 22:30:55 +00:00
|
|
|
#include <sstream>
|
2011-10-22 14:04:05 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <set>
|
|
|
|
|
2011-05-16 23:41:34 +00:00
|
|
|
// mapnik
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2011-05-16 23:41:34 +00:00
|
|
|
#include <mapnik/geom_util.hpp>
|
|
|
|
#include <mapnik/query.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/boolean.hpp>
|
2011-05-16 23:41:34 +00:00
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
|
2008-03-01 11:49:37 +00:00
|
|
|
#include "osm_datasource.hpp"
|
|
|
|
#include "osm_featureset.hpp"
|
2009-02-15 13:51:07 +00:00
|
|
|
#include "dataset_deliverer.h"
|
2008-03-06 21:04:47 +00:00
|
|
|
#include "osmtagtypes.h"
|
2009-02-15 13:51:07 +00:00
|
|
|
#include "osmparser.h"
|
2008-03-01 11:49:37 +00:00
|
|
|
|
|
|
|
using mapnik::String;
|
|
|
|
using mapnik::Double;
|
|
|
|
using mapnik::Integer;
|
|
|
|
using mapnik::datasource_exception;
|
|
|
|
using mapnik::filter_in_box;
|
|
|
|
using mapnik::filter_at_point;
|
|
|
|
using mapnik::attribute_descriptor;
|
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
DATASOURCE_PLUGIN(osm_datasource)
|
|
|
|
|
|
|
|
osm_datasource::osm_datasource(const parameters& params, bool bind)
|
2012-04-08 00:20:56 +00:00
|
|
|
: datasource (params),
|
|
|
|
extent_(),
|
|
|
|
type_(datasource::Vector),
|
|
|
|
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding", "utf-8"))
|
2010-11-24 17:29:11 +00:00
|
|
|
{
|
2010-10-24 06:34:18 +00:00
|
|
|
if (bind)
|
|
|
|
{
|
|
|
|
this->bind();
|
2010-11-24 17:29:11 +00:00
|
|
|
}
|
2010-10-24 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void osm_datasource::bind() const
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2010-10-24 06:34:18 +00:00
|
|
|
if (is_bound_) return;
|
2009-02-15 13:51:07 +00:00
|
|
|
|
2010-10-24 06:34:18 +00:00
|
|
|
osm_data_ = NULL;
|
2011-10-22 14:04:05 +00:00
|
|
|
std::string osm_filename = *params_.get<std::string>("file", "");
|
|
|
|
std::string parser = *params_.get<std::string>("parser", "libxml2");
|
|
|
|
std::string url = *params_.get<std::string>("url", "");
|
|
|
|
std::string bbox = *params_.get<std::string>("bbox", "");
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2009-02-15 13:51:07 +00:00
|
|
|
|
|
|
|
// load the data
|
2011-10-22 14:04:05 +00:00
|
|
|
if (url != "" && bbox != "")
|
2009-02-15 13:51:07 +00:00
|
|
|
{
|
2012-03-20 22:02:19 +00:00
|
|
|
// if we supplied a url and a bounding box, load from the url
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_datasource: loading_from_url url=" << url << ",bbox=" << bbox;
|
2012-04-08 00:20:56 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
if ((osm_data_ = dataset_deliverer::load_from_url(url, bbox, parser)) == NULL)
|
2009-02-15 13:51:07 +00:00
|
|
|
{
|
|
|
|
throw datasource_exception("Error loading from URL");
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
else if (osm_filename != "")
|
2009-02-15 13:51:07 +00:00
|
|
|
{
|
2012-03-20 22:02:19 +00:00
|
|
|
// if we supplied a filename, load from file
|
|
|
|
if ((osm_data_ = dataset_deliverer::load_from_file(osm_filename, parser)) == NULL)
|
2009-02-15 13:51:07 +00:00
|
|
|
{
|
2011-12-05 22:30:55 +00:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "OSM Plugin: Error loading from file '" << osm_filename << "'";
|
|
|
|
throw datasource_exception(s.str());
|
2010-11-13 21:43:38 +00:00
|
|
|
}
|
2012-04-08 00:20:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-20 22:02:19 +00:00
|
|
|
throw datasource_exception("OSM Plugin: Neither 'file' nor 'url' and 'bbox' specified");
|
2009-02-15 13:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-20 22:02:19 +00:00
|
|
|
osm_tag_types tagtypes;
|
|
|
|
tagtypes.add_type("maxspeed", mapnik::Integer);
|
|
|
|
tagtypes.add_type("z_order", mapnik::Integer);
|
2011-10-22 14:04:05 +00:00
|
|
|
|
2012-03-20 22:02:19 +00:00
|
|
|
osm_data_->rewind();
|
2009-02-15 13:51:07 +00:00
|
|
|
|
2012-03-20 22:02:19 +00:00
|
|
|
// Need code to get the attributes of all the data
|
|
|
|
std::set<std::string> keys = osm_data_->get_keys();
|
2009-02-15 13:51:07 +00:00
|
|
|
|
2012-03-20 22:02:19 +00:00
|
|
|
// Add the attributes to the datasource descriptor - assume they are
|
|
|
|
// all of type String
|
|
|
|
for (std::set<std::string>::iterator i = keys.begin(); i != keys.end(); i++)
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(*i, tagtypes.get_type(*i)));
|
2009-02-15 13:51:07 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-03-20 22:02:19 +00:00
|
|
|
// Get the bounds of the data and set extent_ accordingly
|
|
|
|
bounds b = osm_data_->get_bounds();
|
|
|
|
extent_ = box2d<double>(b.w, b.s, b.e, b.n);
|
2010-10-24 06:34:18 +00:00
|
|
|
is_bound_ = true;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
osm_datasource::~osm_datasource()
|
|
|
|
{
|
2009-04-01 00:43:08 +00:00
|
|
|
// Do not do as is now static variable and cleaned up at exit
|
2011-11-14 03:37:50 +00:00
|
|
|
//delete osm_data_;
|
2009-02-15 13:51:07 +00:00
|
|
|
}
|
2008-03-01 11:49:37 +00:00
|
|
|
|
2010-11-14 14:55:21 +00:00
|
|
|
std::string osm_datasource::name()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
return "osm";
|
2010-11-14 14:55:21 +00:00
|
|
|
}
|
2008-03-01 11:49:37 +00:00
|
|
|
|
2012-01-17 06:09:46 +00:00
|
|
|
mapnik::datasource::datasource_t osm_datasource::type() const
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
return type_;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
layer_descriptor osm_datasource::get_descriptor() const
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
return desc_;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr osm_datasource::features(const query& q) const
|
|
|
|
{
|
2012-03-20 22:02:19 +00:00
|
|
|
if (!is_bound_) bind();
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-10-24 06:34:18 +00:00
|
|
|
filter_in_box filter(q.get_bbox());
|
2009-02-15 13:51:07 +00:00
|
|
|
// so we need to filter osm features by bbox here...
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-05-16 23:41:34 +00:00
|
|
|
return boost::make_shared<osm_featureset<filter_in_box> >(filter,
|
2011-10-22 14:04:05 +00:00
|
|
|
osm_data_,
|
|
|
|
q.property_names(),
|
|
|
|
desc_.get_encoding());
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr osm_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
2012-03-20 22:02:19 +00:00
|
|
|
if (!is_bound_) bind();
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
filter_at_point filter(pt);
|
|
|
|
// collect all attribute names
|
|
|
|
std::vector<attribute_descriptor> const& desc_vector = desc_.get_descriptors();
|
|
|
|
std::vector<attribute_descriptor>::const_iterator itr = desc_vector.begin();
|
|
|
|
std::vector<attribute_descriptor>::const_iterator end = desc_vector.end();
|
|
|
|
std::set<std::string> names;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
while (itr != end)
|
|
|
|
{
|
|
|
|
names.insert(itr->get_name());
|
|
|
|
++itr;
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-05-16 23:41:34 +00:00
|
|
|
return boost::make_shared<osm_featureset<filter_at_point> >(filter,
|
2011-10-22 14:04:05 +00:00
|
|
|
osm_data_,
|
|
|
|
names,
|
|
|
|
desc_.get_encoding());
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
box2d<double> osm_datasource::envelope() const
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2012-03-20 22:02:19 +00:00
|
|
|
if (!is_bound_) bind();
|
2011-11-14 03:37:50 +00:00
|
|
|
return extent_;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
2012-01-17 06:09:46 +00:00
|
|
|
|
|
|
|
boost::optional<mapnik::datasource::geometry_t> osm_datasource::get_geometry_type() const
|
|
|
|
{
|
|
|
|
if (! is_bound_) bind();
|
|
|
|
return boost::optional<mapnik::datasource::geometry_t>(mapnik::datasource::Collection);
|
2012-03-20 22:02:19 +00:00
|
|
|
}
|