TopoJSON input plugin

skeleton implementation
This commit is contained in:
artemp 2013-09-30 14:14:58 +01:00
parent ff93a8117b
commit fddec700ad
9 changed files with 456 additions and 2 deletions

View file

@ -112,6 +112,7 @@ PLUGINS = { # plugins with external dependencies
'csv': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'}, 'csv': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'raster': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'}, 'raster': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'geojson': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'}, 'geojson': {'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'topojson':{'default':True,'path':None,'inc':None,'lib':None,'lang':'C++'},
'python': {'default':False,'path':None,'inc':None,'lib':None,'lang':'C++'}, 'python': {'default':False,'path':None,'inc':None,'lib':None,'lang':'C++'},
} }

View file

@ -28,7 +28,7 @@
#include <boost/spirit/include/phoenix.hpp> #include <boost/spirit/include/phoenix.hpp>
// //
#include <mapnik/value.hpp> #include <mapnik/value.hpp>
#include <mapnik/topology.hpp> #include <mapnik/json/topology.hpp>
// //
#include <string> #include <string>

View file

@ -20,7 +20,7 @@
* *
*****************************************************************************/ *****************************************************************************/
#include <mapnik/topojson_grammar.hpp> #include <mapnik/json/topojson_grammar.hpp>
namespace mapnik { namespace topojson { namespace mapnik { namespace topojson {

View file

@ -0,0 +1,76 @@
#
# This file is part of Mapnik (c++ mapping toolkit)
#
# Copyright (C) 2013 Artem Pavlenko
#
# Mapnik 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
#
#
Import ('env')
can_build = True
if env.get('BOOST_LIB_VERSION_FROM_HEADER'):
boost_version_from_header = int(env['BOOST_LIB_VERSION_FROM_HEADER'].split('_')[1])
if boost_version_from_header < 47:
can_build = False
if not can_build:
print 'WARNING: skipping building the optional geojson datasource plugin which requires boost >= 1.47'
else:
Import ('plugin_base')
PLUGIN_NAME = 'topojson'
plugin_env = plugin_base.Clone()
plugin_sources = Split(
"""
%(PLUGIN_NAME)s_datasource.cpp
%(PLUGIN_NAME)s_featureset.cpp
%(PLUGIN_NAME)s_grammar.cpp
""" % locals()
)
# Link Library to Dependencies
libraries = []
libraries.append(env['ICU_LIB_NAME'])
libraries.append('boost_system%s' % env['BOOST_APPEND'])
if env['THREADING'] == 'multi':
libraries.append('boost_thread%s' % env['BOOST_APPEND'])
if env['PLUGIN_LINKING'] == 'shared':
libraries.append('mapnik')
TARGET = plugin_env.SharedLibrary('../%s' % PLUGIN_NAME,
SHLIBPREFIX='',
SHLIBSUFFIX='.input',
source=plugin_sources,
LIBS=libraries,
LINKFLAGS=env['CUSTOM_LDFLAGS'])
# if the plugin links to libmapnik ensure it is built first
Depends(TARGET, env.subst('../../../src/%s' % env['MAPNIK_LIB_NAME']))
if 'uninstall' not in COMMAND_LINE_TARGETS:
env.Install(env['MAPNIK_INPUT_PLUGINS_DEST'], TARGET)
env.Alias('install', env['MAPNIK_INPUT_PLUGINS_DEST'])
plugin_obj = {
'LIBS': libraries,
'SOURCES': plugin_sources,
}
Return('plugin_obj')

View file

@ -0,0 +1,161 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 Artem Pavlenko
*
* 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
*
*****************************************************************************/
#include "topojson_datasource.hpp"
#include "topojson_featureset.hpp"
#include <fstream>
#include <algorithm>
// boost
#include <boost/variant.hpp>
#include <boost/make_shared.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/extensions/index/rtree/rtree.hpp>
// mapnik
#include <mapnik/unicode.hpp>
#include <mapnik/utils.hpp>
#include <mapnik/feature.hpp>
//#include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/debug.hpp>
#include <mapnik/proj_transform.hpp>
#include <mapnik/projection.hpp>
//#include <mapnik/util/geometry_to_ds_type.hpp>
#include <mapnik/json/topojson_grammar.hpp>
using mapnik::datasource;
using mapnik::parameters;
DATASOURCE_PLUGIN(topojson_datasource)
topojson_datasource::topojson_datasource(parameters const& params)
: datasource(params),
type_(datasource::Vector),
desc_(*params.get<std::string>("type"),
*params.get<std::string>("encoding","utf-8")),
file_(*params.get<std::string>("file","")),
extent_(),
tr_(new mapnik::transcoder(*params.get<std::string>("encoding","utf-8"))),
tree_(16,1)
{
if (file_.empty()) throw mapnik::datasource_exception("TopoJSON Plugin: missing <file> parameter");
boost::optional<std::string> base = params.get<std::string>("base");
if (base)
{
file_ = *base + "/" + file_;
}
typedef std::istreambuf_iterator<char> base_iterator_type;
#if defined (_WINDOWS)
std::ifstream is(mapnik::utf8_to_utf16(file_),std::ios_base::in | std::ios_base::binary);
#else
std::ifstream is(file_.c_str(),std::ios_base::in | std::ios_base::binary);
#endif
if (!is.is_open())
{
throw mapnik::datasource_exception("TopoJSON Plugin: could not open: '" + file_ + "'");
}
boost::spirit::multi_pass<base_iterator_type> begin =
boost::spirit::make_default_multi_pass(base_iterator_type(is));
boost::spirit::multi_pass<base_iterator_type> end =
boost::spirit::make_default_multi_pass(base_iterator_type());
//mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
mapnik::topojson::topojson_grammar<boost::spirit::multi_pass<base_iterator_type> > g;
bool result = boost::spirit::qi::phrase_parse(begin, end, g, boost::spirit::standard_wide::space, topo_);
if (!result)
{
throw mapnik::datasource_exception("topojson_datasource: Failed parse TopoJSON file '" + file_ + "'");
}
// tree_.insert(box_type(point_type(box.minx(),box.miny()),point_type(box.maxx(),box.maxy())), count++);
}
topojson_datasource::~topojson_datasource() { }
const char * topojson_datasource::name()
{
return "geojson";
}
boost::optional<mapnik::datasource::geometry_t> topojson_datasource::get_geometry_type() const
{
boost::optional<mapnik::datasource::geometry_t> result;
return result;
}
mapnik::datasource::datasource_t topojson_datasource::type() const
{
return type_;
}
mapnik::box2d<double> topojson_datasource::envelope() const
{
return extent_;
}
mapnik::layer_descriptor topojson_datasource::get_descriptor() const
{
return desc_;
}
mapnik::featureset_ptr topojson_datasource::features(mapnik::query const& q) const
{
// if the query box intersects our world extent then query for features
mapnik::box2d<double> const& b = q.get_bbox();
if (extent_.intersects(b))
{
box_type box(point_type(b.minx(),b.miny()),point_type(b.maxx(),b.maxy()));
index_array_ = tree_.find(box);
return std::make_shared<topojson_featureset>(topo_, index_array_.begin(), index_array_.end());
}
// otherwise return an empty featureset pointer
return mapnik::featureset_ptr();
}
mapnik::featureset_ptr topojson_datasource::features_at_point(mapnik::coord2d const& pt, double tol) const
{
mapnik::box2d<double> query_bbox(pt, pt);
query_bbox.pad(tol);
mapnik::query q(query_bbox);
std::vector<mapnik::attribute_descriptor> const& desc = desc_.get_descriptors();
std::vector<mapnik::attribute_descriptor>::const_iterator itr = desc.begin();
std::vector<mapnik::attribute_descriptor>::const_iterator end = desc.end();
for ( ;itr!=end;++itr)
{
q.add_property_name(itr->get_name());
}
return features(q);
}

View file

@ -0,0 +1,81 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 Artem Pavlenko
*
* 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
*
*****************************************************************************/
#ifndef TOPOJSON_DATASOURCE_HPP
#define TOPOJSON_DATASOURCE_HPP
// mapnik
#include <mapnik/datasource.hpp>
#include <mapnik/params.hpp>
#include <mapnik/query.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/coord.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/json/topology.hpp>
// boost
#include <boost/optional.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/algorithms/area.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/extensions/index/rtree/rtree.hpp>
// stl
#include <vector>
#include <string>
#include <map>
#include <deque>
#include <memory>
class topojson_datasource : public mapnik::datasource
{
public:
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::box<point_type> box_type;
typedef boost::geometry::index::rtree<box_type,std::size_t> spatial_index_type;
// constructor
topojson_datasource(mapnik::parameters const& params);
virtual ~topojson_datasource ();
mapnik::datasource::datasource_t type() const;
static const char * name();
mapnik::featureset_ptr features(mapnik::query const& q) const;
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt, double tol = 0) const;
mapnik::box2d<double> envelope() const;
mapnik::layer_descriptor get_descriptor() const;
boost::optional<mapnik::datasource::geometry_t> get_geometry_type() const;
private:
mapnik::datasource::datasource_t type_;
std::map<std::string, mapnik::parameters> statistics_;
mapnik::layer_descriptor desc_;
std::string file_;
mapnik::box2d<double> extent_;
std::shared_ptr<mapnik::transcoder> tr_;
mapnik::topojson::topology topo_;
spatial_index_type tree_;
mutable std::deque<std::size_t> index_array_;
};
#endif // FILE_DATASOURCE_HPP

View file

@ -0,0 +1,52 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 Artem Pavlenko
*
* 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
*
*****************************************************************************/
// mapnik
#include <mapnik/feature.hpp>
// stl
#include <string>
#include <vector>
#include <fstream>
#include "topojson_featureset.hpp"
topojson_featureset::topojson_featureset(mapnik::topojson::topology const& topo,
std::deque<std::size_t>::const_iterator index_itr,
std::deque<std::size_t>::const_iterator index_end)
: topo_(topo),
index_itr_(index_itr),
index_end_(index_end) {}
topojson_featureset::~topojson_featureset() {}
mapnik::feature_ptr topojson_featureset::next()
{
if (index_itr_ != index_end_)
{
std::size_t index = *index_itr_++;
if ( index < topo_.geometries.size())
{
return mapnik::feature_ptr(); // TODO
}
}
return mapnik::feature_ptr();
}

View file

@ -0,0 +1,48 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 Artem Pavlenko
*
* 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
*
*****************************************************************************/
#ifndef TOPOJSON_FEATURESET_HPP
#define TOPOJSON_FEATURESET_HPP
#include <mapnik/feature.hpp>
#include "topojson_datasource.hpp"
#include <vector>
#include <deque>
class topojson_featureset : public mapnik::Featureset
{
public:
topojson_featureset(mapnik::topojson::topology const& topo,
std::deque<std::size_t>::const_iterator index_itr,
std::deque<std::size_t>::const_iterator index_end);
virtual ~topojson_featureset();
mapnik::feature_ptr next();
private:
mapnik::box2d<double> box_;
mapnik::topojson::topology const& topo_;
std::deque<std::size_t>::const_iterator index_itr_;
std::deque<std::size_t>::const_iterator index_end_;
};
#endif // TOPOJSON_FEATURESET_HPP

View file

@ -0,0 +1,35 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 Artem Pavlenko
*
* 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
*
*****************************************************************************/
// mapnik
#include <mapnik/json/topojson_grammar_impl.hpp>
// stl
#include <string>
// boost
#include <boost/spirit/include/support_multi_pass.hpp>
namespace mapnik { namespace topojson {
//template struct topojson_grammar<std::string::const_iterator>;
template struct topojson_grammar<boost::spirit::multi_pass<std::istreambuf_iterator<char> > >;
}}