mapnik/plugins/input/osm/osm_datasource.cpp

160 lines
4.9 KiB
C++
Raw Normal View History

2008-03-01 12:49:37 +01:00
/*****************************************************************************
*
2008-03-01 12:49:37 +01:00
* This file is part of Mapnik (c++ mapping toolkit)
*
2014-11-20 15:25:50 +01:00
* Copyright (C) 2014 Artem Pavlenko
2008-03-01 12:49:37 +01: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 16:04:05 +02:00
// stl
#include <stdexcept>
#include <set>
// mapnik
2012-04-08 02:20:56 +02:00
#include <mapnik/debug.hpp>
#include <mapnik/geom_util.hpp>
#include <mapnik/query.hpp>
2012-04-08 02:20:56 +02:00
#include <mapnik/boolean.hpp>
// boost
2008-03-01 12:49:37 +01:00
#include "osm_datasource.hpp"
#include "osm_featureset.hpp"
#include "dataset_deliverer.h"
2008-03-06 22:04:47 +01:00
#include "osmtagtypes.h"
#include "osmparser.h"
2008-03-01 12:49:37 +01: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 16:04:05 +02:00
DATASOURCE_PLUGIN(osm_datasource)
osm_datasource::osm_datasource(const parameters& params)
2012-04-08 02:20:56 +02:00
: datasource (params),
extent_(),
type_(datasource::Vector),
desc_(osm_datasource::name(), *params.get<std::string>("encoding", "utf-8"))
2010-11-24 18:29:11 +01:00
{
2013-11-27 16:52:47 +01:00
osm_data_ = nullptr;
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", "");
// load the data
2011-10-22 16:04:05 +02:00
if (url != "" && bbox != "")
{
throw datasource_exception("Error loading from URL is no longer supported (removed in >= Mapnik 2.3.x");
}
2011-10-22 16:04:05 +02:00
else if (osm_filename != "")
{
// if we supplied a filename, load from file
2013-11-27 16:52:47 +01:00
if ((osm_data_ = dataset_deliverer::load_from_file(osm_filename, parser)) == nullptr)
{
std::string s("OSM Plugin: Error loading from file '");
s += osm_filename + "'";
throw datasource_exception(s);
}
2012-04-08 02:20:56 +02:00
}
else
{
throw datasource_exception("OSM Plugin: Neither 'file' nor 'url' and 'bbox' specified");
}
osm_tag_types tagtypes;
tagtypes.add_type("maxspeed", mapnik::Integer);
tagtypes.add_type("z_order", mapnik::Integer);
2011-10-22 16:04:05 +02:00
osm_data_->rewind();
// Need code to get the attributes of all the data
std::set<std::string> keys = osm_data_->get_keys();
// Add the attributes to the datasource descriptor - assume they are
// all of type String
2013-05-06 16:52:04 +02:00
for (auto const& key : keys)
{
2013-05-06 16:52:04 +02:00
desc_.add_descriptor(attribute_descriptor(key, tagtypes.get_type(key)));
}
// Get the bounds of the data and set extent_ accordingly
bounds b = osm_data_->get_bounds();
2013-05-06 16:52:04 +02:00
extent_ = box2d<double>(b.w,b.s,b.e,b.n);
2008-03-01 12:49:37 +01:00
}
osm_datasource::~osm_datasource()
{
// Do not do as is now static variable and cleaned up at exit
//delete osm_data_;
}
2008-03-01 12:49:37 +01:00
const char * osm_datasource::name()
{
2011-10-22 16:04:05 +02:00
return "osm";
}
2008-03-01 12:49:37 +01:00
mapnik::datasource::datasource_t osm_datasource::type() const
2008-03-01 12:49:37 +01:00
{
2011-10-22 16:04:05 +02:00
return type_;
2008-03-01 12:49:37 +01:00
}
layer_descriptor osm_datasource::get_descriptor() const
{
2011-10-22 16:04:05 +02:00
return desc_;
2008-03-01 12:49:37 +01:00
}
featureset_ptr osm_datasource::features(const query& q) const
{
filter_in_box filter(q.get_bbox());
// so we need to filter osm features by bbox here...
return std::make_shared<osm_featureset<filter_in_box> >(filter,
2011-10-22 16:04:05 +02:00
osm_data_,
q.property_names(),
desc_.get_encoding());
2008-03-01 12:49:37 +01:00
}
featureset_ptr osm_datasource::features_at_point(coord2d const& pt, double tol) const
2008-03-01 12:49:37 +01:00
{
2011-10-22 16:04:05 +02:00
filter_at_point filter(pt);
// collect all attribute names
std::set<std::string> names;
2014-07-08 18:02:22 +02:00
for (auto const& elem : desc_.get_descriptors())
2011-10-22 16:04:05 +02:00
{
2014-07-08 18:02:22 +02:00
names.insert(elem.get_name());
2011-10-22 16:04:05 +02:00
}
return std::make_shared<osm_featureset<filter_at_point> >(filter,
2014-07-08 18:02:22 +02:00
osm_data_,
names,
desc_.get_encoding());
2008-03-01 12:49:37 +01:00
}
2009-12-16 21:02:06 +01:00
box2d<double> osm_datasource::envelope() const
2008-03-01 12:49:37 +01:00
{
return extent_;
2008-03-01 12:49:37 +01:00
}
boost::optional<mapnik::datasource_geometry_t> osm_datasource::get_geometry_type() const
{
return boost::optional<mapnik::datasource_geometry_t>(mapnik::datasource_geometry_t::Collection);
}