2012-06-13 14:30:58 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2012-06-13 14:30:58 +02: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "geojson_datasource.hpp"
|
|
|
|
#include "geojson_featureset.hpp"
|
2015-10-16 14:15:20 +02:00
|
|
|
#include "geojson_index_featureset.hpp"
|
2015-10-19 11:54:52 +02:00
|
|
|
#include "geojson_memory_index_featureset.hpp"
|
2012-06-13 14:30:58 +02:00
|
|
|
#include <fstream>
|
2012-07-26 01:11:51 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2012-06-13 14:30:58 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2015-05-15 21:52:07 +02:00
|
|
|
|
2012-06-13 14:30:58 +02:00
|
|
|
// mapnik
|
2015-01-26 10:19:18 +01:00
|
|
|
#include <mapnik/boolean.hpp>
|
2013-01-04 18:23:06 +01:00
|
|
|
#include <mapnik/unicode.hpp>
|
2015-06-02 12:10:41 +02:00
|
|
|
#include <mapnik/util/utf_conv_win.hpp>
|
2013-01-04 18:23:06 +01:00
|
|
|
#include <mapnik/feature.hpp>
|
2015-01-26 11:08:17 +01:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2013-01-04 18:23:06 +01:00
|
|
|
#include <mapnik/feature_kv_iterator.hpp>
|
2016-08-02 16:34:59 +02:00
|
|
|
#include <mapnik/value/types.hpp>
|
2017-01-26 09:51:37 +01:00
|
|
|
#include <mapnik/geometry/box2d.hpp>
|
2012-07-25 04:21:55 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2012-06-13 14:30:58 +02:00
|
|
|
#include <mapnik/proj_transform.hpp>
|
|
|
|
#include <mapnik/projection.hpp>
|
2014-08-08 13:13:49 +02:00
|
|
|
#include <mapnik/util/variant.hpp>
|
2014-10-15 20:36:57 +02:00
|
|
|
#include <mapnik/util/file_io.hpp>
|
2015-03-19 12:09:07 +01:00
|
|
|
#include <mapnik/util/geometry_to_ds_type.hpp>
|
2016-08-02 15:43:02 +02:00
|
|
|
#include <mapnik/geometry/boost_adapters.hpp>
|
2015-10-16 14:15:20 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
|
|
|
#include <mapnik/util/spatial_index.hpp>
|
|
|
|
#include <mapnik/geom_util.hpp>
|
2016-11-21 16:35:27 +01:00
|
|
|
#include <mapnik/json/parse_feature.hpp>
|
2016-11-28 10:16:25 +01:00
|
|
|
#include <mapnik/json/extract_bounding_boxes_x3.hpp>
|
2012-06-13 14:30:58 +02:00
|
|
|
|
2015-10-16 22:34:53 +02:00
|
|
|
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2015-02-11 03:50:27 +01:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2015-02-03 13:33:55 +01:00
|
|
|
#include <mapnik/mapped_memory_cache.hpp>
|
2015-02-11 03:50:27 +01:00
|
|
|
#endif
|
2015-02-03 13:33:55 +01:00
|
|
|
|
2012-06-13 14:30:58 +02:00
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
2022-01-29 12:52:23 +01:00
|
|
|
DATASOURCE_PLUGIN_IMPL(geojson_datasource_plugin, geojson_datasource);
|
|
|
|
DATASOURCE_PLUGIN_EXPORT(geojson_datasource_plugin);
|
|
|
|
DATASOURCE_PLUGIN_EMPTY_INIT(geojson_datasource_plugin);
|
2012-06-13 14:30:58 +02:00
|
|
|
|
2015-01-07 11:35:21 +01:00
|
|
|
struct attr_value_converter
|
2012-07-26 01:11:51 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::eAttributeType operator()(mapnik::value_integer) const { return mapnik::Integer; }
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::eAttributeType operator()(double) const { return mapnik::Double; }
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::eAttributeType operator()(float) const { return mapnik::Double; }
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::eAttributeType operator()(bool) const { return mapnik::Boolean; }
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::eAttributeType operator()(std::string const&) const { return mapnik::String; }
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::eAttributeType operator()(mapnik::value_unicode_string const&) const { return mapnik::String; }
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::eAttributeType operator()(mapnik::value_null const&) const { return mapnik::String; }
|
2012-07-26 01:11:51 +02:00
|
|
|
};
|
|
|
|
|
2012-12-17 19:03:07 +01:00
|
|
|
geojson_datasource::geojson_datasource(parameters const& params)
|
2022-01-26 10:43:31 +01:00
|
|
|
: datasource(params)
|
|
|
|
, type_(datasource::Vector)
|
|
|
|
, desc_(geojson_datasource::name(), *params.get<std::string>("encoding", "utf-8"))
|
|
|
|
, filename_()
|
|
|
|
, from_inline_string_(false)
|
|
|
|
, extent_()
|
|
|
|
, features_()
|
|
|
|
, tree_(nullptr)
|
|
|
|
, num_features_to_query_(
|
|
|
|
std::max(mapnik::value_integer(1), *params.get<mapnik::value_integer>("num_features_to_query", 5)))
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
2014-07-29 01:16:10 +02:00
|
|
|
boost::optional<std::string> inline_string = params.get<std::string>("inline");
|
2016-09-03 00:51:58 +02:00
|
|
|
if (!inline_string)
|
2014-07-29 01:16:10 +02:00
|
|
|
{
|
|
|
|
boost::optional<std::string> file = params.get<std::string>("file");
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!file)
|
|
|
|
throw mapnik::datasource_exception("GeoJSON Plugin: missing <file> parameter");
|
2013-06-02 21:12:03 +02:00
|
|
|
|
2014-07-29 01:16:10 +02:00
|
|
|
boost::optional<std::string> base = params.get<std::string>("base");
|
|
|
|
if (base)
|
|
|
|
filename_ = *base + "/" + *file;
|
|
|
|
else
|
|
|
|
filename_ = *file;
|
2015-10-16 14:15:20 +02:00
|
|
|
has_disk_index_ = mapnik::util::exists(filename_ + ".index");
|
2014-07-29 01:16:10 +02:00
|
|
|
}
|
2015-10-16 14:15:20 +02:00
|
|
|
|
2016-09-03 00:51:58 +02:00
|
|
|
if (inline_string)
|
2014-07-29 01:16:10 +02:00
|
|
|
{
|
2016-09-03 00:51:58 +02:00
|
|
|
from_inline_string_ = true;
|
|
|
|
char const* start = inline_string->c_str();
|
|
|
|
char const* end = start + inline_string->size();
|
2015-02-03 13:33:55 +01:00
|
|
|
parse_geojson(start, end);
|
2014-07-29 01:16:10 +02:00
|
|
|
}
|
2015-10-16 14:15:20 +02:00
|
|
|
else if (has_disk_index_)
|
|
|
|
{
|
|
|
|
initialise_disk_index(filename_);
|
|
|
|
}
|
2014-07-29 01:16:10 +02:00
|
|
|
else
|
|
|
|
{
|
2015-02-03 13:33:55 +01:00
|
|
|
cache_features_ = *params.get<mapnik::boolean_type>("cache_features", true);
|
2015-10-16 22:34:53 +02:00
|
|
|
#if !defined(MAPNIK_MEMORY_MAPPED_FILE)
|
2014-10-15 20:36:57 +02:00
|
|
|
mapnik::util::file file(filename_);
|
2016-03-18 10:02:19 +01:00
|
|
|
if (!file)
|
2014-07-29 01:16:10 +02:00
|
|
|
{
|
2014-11-22 04:57:24 +01:00
|
|
|
throw mapnik::datasource_exception("GeoJSON Plugin: could not open: '" + filename_ + "'");
|
2014-07-29 01:16:10 +02:00
|
|
|
}
|
2015-01-26 10:19:18 +01:00
|
|
|
|
2014-08-27 01:22:12 +02:00
|
|
|
std::string file_buffer;
|
2014-10-15 20:36:57 +02:00
|
|
|
file_buffer.resize(file.size());
|
2017-01-13 14:57:15 +01:00
|
|
|
auto count = std::fread(&file_buffer[0], file.size(), 1, file.get());
|
2015-02-03 13:33:55 +01:00
|
|
|
char const* start = file_buffer.c_str();
|
2017-01-13 14:57:15 +01:00
|
|
|
char const* end = (count == 1) ? start + file_buffer.length() : start;
|
2015-02-03 13:33:55 +01:00
|
|
|
#else
|
|
|
|
boost::optional<mapnik::mapped_region_ptr> mapped_region =
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::mapped_memory_cache::instance().find(filename_, false);
|
2015-02-03 13:33:55 +01:00
|
|
|
if (!mapped_region)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
throw std::runtime_error("could not get file mapping for " + filename_);
|
2015-02-03 13:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char const* start = reinterpret_cast<char const*>((*mapped_region)->get_address());
|
|
|
|
char const* end = start + (*mapped_region)->get_size();
|
2016-09-03 00:51:58 +02:00
|
|
|
#endif
|
2015-01-26 10:19:18 +01:00
|
|
|
if (cache_features_)
|
|
|
|
{
|
2015-02-03 13:33:55 +01:00
|
|
|
parse_geojson(start, end);
|
2015-01-26 10:19:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-18 12:16:44 +02:00
|
|
|
initialise_index(start, end);
|
2015-01-26 10:19:18 +01:00
|
|
|
}
|
2013-06-02 21:12:03 +02:00
|
|
|
}
|
2014-07-29 01:16:10 +02:00
|
|
|
}
|
2013-06-02 21:12:03 +02:00
|
|
|
|
2014-07-30 12:28:23 +02:00
|
|
|
namespace {
|
2016-11-28 10:28:12 +01:00
|
|
|
|
2016-03-24 18:12:16 +01:00
|
|
|
using box_type = box2d<double>;
|
2017-08-15 12:14:11 +02:00
|
|
|
using boxes_type = std::vector<std::pair<box_type, std::pair<std::uint64_t, std::uint64_t>>>;
|
2015-02-03 13:33:55 +01:00
|
|
|
using base_iterator_type = char const*;
|
2015-03-07 21:00:38 +01:00
|
|
|
const mapnik::transcoder geojson_datasource_static_tr("utf8");
|
2016-11-28 10:16:25 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace
|
2014-07-30 12:28:23 +02:00
|
|
|
|
2016-02-25 15:27:21 +01:00
|
|
|
void geojson_datasource::initialise_descriptor(mapnik::feature_ptr const& feature)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
for (auto const& kv : *feature)
|
2016-02-25 15:27:21 +01:00
|
|
|
{
|
|
|
|
auto const& name = std::get<0>(kv);
|
|
|
|
if (!desc_.has_name(name))
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
desc_.add_descriptor(
|
|
|
|
mapnik::attribute_descriptor(name, mapnik::util::apply_visitor(attr_value_converter(), std::get<1>(kv))));
|
2016-02-25 15:27:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 14:15:20 +02:00
|
|
|
void geojson_datasource::initialise_disk_index(std::string const& filename)
|
|
|
|
{
|
|
|
|
// read extent
|
2017-08-18 12:16:44 +02:00
|
|
|
using value_type = mapnik::util::index_record;
|
2015-10-16 14:15:20 +02:00
|
|
|
std::ifstream index(filename_ + ".index", std::ios::binary);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!index)
|
|
|
|
throw mapnik::datasource_exception("GeoJSON Plugin: could not open: '" + filename_ + ".index'");
|
|
|
|
auto ext_f =
|
|
|
|
mapnik::util::spatial_index<value_type, mapnik::bounding_box_filter<float>, std::ifstream, mapnik::box2d<float>>::
|
|
|
|
bounding_box(index);
|
|
|
|
extent_ = {ext_f.minx(), ext_f.miny(), ext_f.maxx(), ext_f.maxy()};
|
2017-08-16 11:35:04 +02:00
|
|
|
mapnik::bounding_box_filter<float> filter(ext_f);
|
2015-10-16 14:15:20 +02:00
|
|
|
std::vector<value_type> positions;
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::util::spatial_index<value_type, mapnik::bounding_box_filter<float>, std::ifstream, mapnik::box2d<float>>::
|
|
|
|
query_first_n(filter, index, positions, num_features_to_query_);
|
2015-10-16 14:15:20 +02:00
|
|
|
|
|
|
|
mapnik::util::file file(filename_);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!file)
|
|
|
|
throw mapnik::datasource_exception("GeoJSON Plugin: could not open: '" + filename_ + "'");
|
2016-08-16 11:55:56 +02:00
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
2015-10-16 14:15:20 +02:00
|
|
|
for (auto const& pos : positions)
|
|
|
|
{
|
2017-08-16 11:35:04 +02:00
|
|
|
std::fseek(file.get(), pos.off, SEEK_SET);
|
2015-10-16 14:15:20 +02:00
|
|
|
std::vector<char> record;
|
2017-08-16 11:35:04 +02:00
|
|
|
record.resize(pos.size);
|
|
|
|
auto count = std::fread(record.data(), pos.size, 1, file.get());
|
2015-10-16 14:15:20 +02:00
|
|
|
auto const* start = record.data();
|
2022-01-26 10:43:31 +01:00
|
|
|
auto const* end = (count == 1) ? start + record.size() : start;
|
2016-08-15 13:21:02 +02:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1));
|
2016-11-21 16:35:27 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
mapnik::json::parse_feature(start, end, *feature, geojson_datasource_static_tr);
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2015-10-16 14:15:20 +02:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse geojson feature");
|
|
|
|
}
|
2016-02-25 15:27:21 +01:00
|
|
|
initialise_descriptor(feature);
|
2015-10-16 14:15:20 +02:00
|
|
|
}
|
2016-08-15 13:21:02 +02:00
|
|
|
desc_.order_by_name();
|
2015-10-16 14:15:20 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
namespace mapnik {
|
|
|
|
namespace json {
|
2016-11-28 18:41:38 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename Iterator>
|
|
|
|
void parse_geometry_or_feature(Iterator& itr, Iterator const& end, feature_impl& feature, transcoder const& tr)
|
2016-11-28 18:41:38 +01:00
|
|
|
{
|
|
|
|
Iterator start = itr;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
parse_feature(start, end, feature, tr);
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2016-11-28 18:41:38 +01:00
|
|
|
{
|
|
|
|
start = itr;
|
|
|
|
parse_geometry(start, end, feature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace json
|
|
|
|
} // namespace mapnik
|
2016-11-28 18:41:38 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename Iterator>
|
2015-01-26 10:19:18 +01:00
|
|
|
void geojson_datasource::initialise_index(Iterator start, Iterator end)
|
|
|
|
{
|
2016-03-24 18:12:16 +01:00
|
|
|
boxes_type boxes;
|
2016-11-28 18:41:38 +01:00
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
2015-02-03 13:33:55 +01:00
|
|
|
Iterator itr = start;
|
2016-11-28 10:16:25 +01:00
|
|
|
try
|
2015-01-26 10:19:18 +01:00
|
|
|
{
|
2016-11-28 10:16:25 +01:00
|
|
|
mapnik::json::extract_bounding_boxes(itr, end, boxes);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (itr != end || boxes.empty())
|
|
|
|
throw std::exception();
|
2016-11-28 10:16:25 +01:00
|
|
|
// bulk insert initialise r-tree
|
|
|
|
tree_ = std::make_unique<spatial_index_type>(boxes);
|
|
|
|
// calculate total extent
|
|
|
|
std::size_t feature_count = 0;
|
2016-11-28 18:41:38 +01:00
|
|
|
|
2016-11-28 10:16:25 +01:00
|
|
|
for (auto const& item : boxes)
|
|
|
|
{
|
|
|
|
auto const& box = std::get<0>(item);
|
|
|
|
auto const& geometry_index = std::get<1>(item);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!extent_.valid())
|
|
|
|
extent_ = box;
|
|
|
|
else
|
|
|
|
extent_.expand_to_include(box);
|
2016-11-28 10:16:25 +01:00
|
|
|
if (feature_count++ < num_features_to_query_)
|
|
|
|
{
|
|
|
|
// parse first N features to extract attributes schema.
|
|
|
|
// NOTE: this doesn't yield correct answer for geoJSON in general, just an indication
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp feature
|
2016-11-28 10:16:25 +01:00
|
|
|
Iterator itr2 = start + geometry_index.first;
|
|
|
|
Iterator end2 = itr2 + geometry_index.second;
|
2016-11-28 10:28:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
mapnik::json::parse_feature(itr2, end2, *feature, geojson_datasource_static_tr);
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2016-11-28 10:16:25 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse geojson feature");
|
|
|
|
}
|
|
|
|
initialise_descriptor(feature);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2016-11-28 10:16:25 +01:00
|
|
|
{
|
2015-10-15 18:35:46 +02:00
|
|
|
cache_features_ = true; // force caching single feature
|
2022-01-26 10:43:31 +01:00
|
|
|
itr = start; // reset iteraror
|
2015-10-15 18:35:46 +02:00
|
|
|
std::size_t start_id = 1;
|
2016-11-28 18:41:38 +01:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, start_id)); // single feature
|
|
|
|
try
|
|
|
|
{
|
|
|
|
mapnik::json::parse_geometry_or_feature(itr, end, *feature, geojson_datasource_static_tr);
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2015-10-15 18:35:46 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
if (from_inline_string_)
|
|
|
|
throw mapnik::datasource_exception(
|
|
|
|
"geojson_datasource: Failed to parse GeoJSON file from in-memory string");
|
|
|
|
else
|
|
|
|
throw mapnik::datasource_exception("geojson_datasource: Failed to parse GeoJSON file '" + filename_ +
|
|
|
|
"'");
|
2015-10-15 18:35:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 18:41:38 +01:00
|
|
|
features_.push_back(std::move(feature));
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
using values_container = std::vector<std::pair<box_type, std::pair<std::uint64_t, std::uint64_t>>>;
|
2015-10-15 18:35:46 +02:00
|
|
|
values_container values;
|
|
|
|
values.reserve(features_.size());
|
|
|
|
|
|
|
|
std::size_t geometry_index = 0;
|
|
|
|
for (mapnik::feature_ptr const& f : features_)
|
|
|
|
{
|
|
|
|
mapnik::box2d<double> box = f->envelope();
|
|
|
|
if (box.valid())
|
|
|
|
{
|
|
|
|
if (geometry_index == 0)
|
|
|
|
{
|
|
|
|
extent_ = box;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extent_.expand_to_include(box);
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
values.emplace_back(box, std::make_pair(geometry_index, 0));
|
2016-02-25 15:27:21 +01:00
|
|
|
}
|
|
|
|
if (geometry_index++ < num_features_to_query_)
|
|
|
|
{
|
|
|
|
initialise_descriptor(f);
|
2015-10-15 18:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// packing algorithm
|
2017-08-18 12:16:44 +02:00
|
|
|
tree_ = std::make_unique<spatial_index_type>(values);
|
2015-01-26 10:19:18 +01:00
|
|
|
}
|
2016-08-15 13:21:02 +02:00
|
|
|
desc_.order_by_name();
|
2015-01-26 10:19:18 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename Iterator>
|
2015-02-03 13:33:55 +01:00
|
|
|
void geojson_datasource::parse_geojson(Iterator start, Iterator end)
|
2014-07-29 01:16:10 +02:00
|
|
|
{
|
2014-07-30 12:01:54 +02:00
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
2014-11-24 12:24:21 +01:00
|
|
|
std::size_t start_id = 1;
|
2015-10-15 16:12:16 +02:00
|
|
|
Iterator itr = start;
|
2015-11-18 15:43:40 +01:00
|
|
|
try
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
2016-11-28 18:41:38 +01:00
|
|
|
boxes_type boxes;
|
|
|
|
mapnik::json::extract_bounding_boxes(itr, end, boxes);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (itr != end || boxes.empty())
|
|
|
|
throw std::exception(); // ensure we've consumed all input and we extracted at least one bbox;
|
2016-11-28 18:41:38 +01:00
|
|
|
for (auto const& item : boxes)
|
2015-11-18 15:43:40 +01:00
|
|
|
{
|
2016-11-28 18:41:38 +01:00
|
|
|
auto const& geometry_index = std::get<1>(item);
|
|
|
|
Iterator itr2 = start + geometry_index.first;
|
|
|
|
Iterator end2 = itr2 + geometry_index.second;
|
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, start_id++));
|
|
|
|
mapnik::json::parse_feature(itr2, end2, *feature, geojson_datasource_static_tr);
|
|
|
|
features_.push_back(std::move(feature));
|
2015-11-18 15:43:40 +01:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2015-10-15 16:12:16 +02:00
|
|
|
{
|
|
|
|
itr = start;
|
|
|
|
// try parsing as single Feature or single Geometry JSON
|
2016-11-28 18:41:38 +01:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, start_id)); // single feature
|
|
|
|
try
|
2015-10-15 16:12:16 +02:00
|
|
|
{
|
2016-11-28 18:41:38 +01:00
|
|
|
mapnik::json::parse_geometry_or_feature(itr, end, *feature, geojson_datasource_static_tr);
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2016-11-28 18:41:38 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
if (from_inline_string_)
|
|
|
|
throw mapnik::datasource_exception(
|
|
|
|
"geojson_datasource: Failed to parse GeoJSON file from in-memory string");
|
|
|
|
else
|
|
|
|
throw mapnik::datasource_exception("geojson_datasource: Failed to parse GeoJSON file '" + filename_ +
|
|
|
|
"'");
|
2016-11-28 18:41:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
features_.push_back(std::move(feature));
|
2015-10-15 16:12:16 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
using values_container = std::vector<std::pair<box_type, std::pair<std::uint64_t, std::uint64_t>>>;
|
2014-10-16 03:51:53 +02:00
|
|
|
values_container values;
|
|
|
|
values.reserve(features_.size());
|
|
|
|
|
2014-08-20 17:08:41 +02:00
|
|
|
std::size_t geometry_index = 0;
|
2013-09-30 12:36:04 +02:00
|
|
|
for (mapnik::feature_ptr const& f : features_)
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
2013-09-30 22:23:38 +02:00
|
|
|
mapnik::box2d<double> box = f->envelope();
|
2014-10-16 03:51:53 +02:00
|
|
|
if (box.valid())
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
2014-10-16 03:51:53 +02:00
|
|
|
if (geometry_index == 0)
|
2012-07-26 01:11:51 +02:00
|
|
|
{
|
2014-10-16 03:51:53 +02:00
|
|
|
extent_ = box;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extent_.expand_to_include(box);
|
2012-07-26 01:11:51 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
values.emplace_back(box, std::make_pair(geometry_index, 0));
|
2012-12-03 14:12:09 +01:00
|
|
|
}
|
2016-02-25 15:27:21 +01:00
|
|
|
if (geometry_index < num_features_to_query_)
|
|
|
|
{
|
|
|
|
initialise_descriptor(f);
|
|
|
|
}
|
2014-08-20 17:08:41 +02:00
|
|
|
++geometry_index;
|
2012-06-13 14:30:58 +02:00
|
|
|
}
|
2014-10-16 03:51:53 +02:00
|
|
|
// packing algorithm
|
2017-08-18 12:16:44 +02:00
|
|
|
tree_ = std::make_unique<spatial_index_type>(values);
|
2012-06-13 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2016-02-25 15:27:21 +01:00
|
|
|
geojson_datasource::~geojson_datasource() {}
|
2012-06-13 14:30:58 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
const char* geojson_datasource::name()
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
2012-07-21 03:34:41 +02:00
|
|
|
return "geojson";
|
2012-06-13 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 14:12:09 +01:00
|
|
|
mapnik::datasource::datasource_t geojson_datasource::type() const
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapnik::box2d<double> geojson_datasource::envelope() const
|
|
|
|
{
|
|
|
|
return extent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapnik::layer_descriptor geojson_datasource::get_descriptor() const
|
|
|
|
{
|
|
|
|
return desc_;
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:13:31 +01:00
|
|
|
boost::optional<mapnik::datasource_geometry_t> geojson_datasource::get_geometry_type() const
|
2015-03-19 12:09:07 +01:00
|
|
|
{
|
2015-03-24 12:13:31 +01:00
|
|
|
boost::optional<mapnik::datasource_geometry_t> result;
|
2015-03-19 12:09:07 +01:00
|
|
|
int multi_type = 0;
|
2015-10-16 14:15:20 +02:00
|
|
|
if (has_disk_index_)
|
|
|
|
{
|
2017-08-18 12:16:44 +02:00
|
|
|
using value_type = mapnik::util::index_record;
|
2015-10-16 18:31:36 +02:00
|
|
|
std::ifstream index(filename_ + ".index", std::ios::binary);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!index)
|
|
|
|
throw mapnik::datasource_exception("GeoJSON Plugin: could not open: '" + filename_ + ".index'");
|
|
|
|
mapnik::bounding_box_filter<float> filter(
|
|
|
|
mapnik::box2d<float>(extent_.minx(), extent_.miny(), extent_.maxx(), extent_.maxy()));
|
2015-10-16 18:31:36 +02:00
|
|
|
std::vector<value_type> positions;
|
|
|
|
mapnik::util::spatial_index<value_type,
|
2017-08-16 11:35:04 +02:00
|
|
|
mapnik::bounding_box_filter<float>,
|
|
|
|
std::ifstream,
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::box2d<float>>::query_first_n(filter,
|
|
|
|
index,
|
|
|
|
positions,
|
|
|
|
num_features_to_query_);
|
2015-10-16 14:15:20 +02:00
|
|
|
|
2015-10-16 18:31:36 +02:00
|
|
|
mapnik::util::file file(filename_);
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!file)
|
|
|
|
throw mapnik::datasource_exception("GeoJSON Plugin: could not open: '" + filename_ + "'");
|
2016-08-16 11:55:56 +02:00
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
2015-10-16 18:31:36 +02:00
|
|
|
for (auto const& pos : positions)
|
|
|
|
{
|
2017-08-16 11:35:04 +02:00
|
|
|
std::fseek(file.get(), pos.off, SEEK_SET);
|
2015-10-16 18:31:36 +02:00
|
|
|
std::vector<char> record;
|
2017-08-16 11:35:04 +02:00
|
|
|
record.resize(pos.size);
|
|
|
|
auto count = std::fread(record.data(), pos.size, 1, file.get());
|
2015-10-16 18:31:36 +02:00
|
|
|
auto const* start = record.data();
|
2022-01-26 10:43:31 +01:00
|
|
|
auto const* end = (count == 1) ? start + record.size() : start;
|
2015-10-21 12:12:55 +02:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp feature
|
2016-11-28 10:28:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
mapnik::json::parse_feature(start, end, *feature, geojson_datasource_static_tr);
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2015-10-16 18:31:36 +02:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse geojson feature");
|
|
|
|
}
|
|
|
|
result = mapnik::util::to_ds_type(feature->get_geometry());
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
int type = static_cast<int>(*result);
|
|
|
|
if (multi_type > 0 && multi_type != type)
|
|
|
|
{
|
|
|
|
result.reset(mapnik::datasource_geometry_t::Collection);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
multi_type = type;
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 14:15:20 +02:00
|
|
|
}
|
|
|
|
else if (cache_features_)
|
2015-03-19 12:09:07 +01:00
|
|
|
{
|
2016-09-23 11:52:56 +02:00
|
|
|
std::size_t num_features = features_.size();
|
|
|
|
for (std::size_t i = 0; i < num_features && i < num_features_to_query_; ++i)
|
2015-03-19 12:09:07 +01:00
|
|
|
{
|
2015-03-24 12:13:31 +01:00
|
|
|
result = mapnik::util::to_ds_type(features_[i]->get_geometry());
|
2015-03-19 12:09:07 +01:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
int type = static_cast<int>(*result);
|
|
|
|
if (multi_type > 0 && multi_type != type)
|
|
|
|
{
|
2015-03-24 12:13:31 +01:00
|
|
|
result.reset(mapnik::datasource_geometry_t::Collection);
|
2015-03-19 12:09:07 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
multi_type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mapnik::util::file file(filename_);
|
2016-03-18 10:02:19 +01:00
|
|
|
if (!file)
|
2015-03-19 12:09:07 +01:00
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("GeoJSON Plugin: could not open: '" + filename_ + "'");
|
|
|
|
}
|
|
|
|
auto itr = tree_->qbegin(boost::geometry::index::intersects(extent_));
|
|
|
|
auto end = tree_->qend();
|
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
2022-01-26 10:43:31 +01:00
|
|
|
for (std::size_t count = 0; itr != end && count < num_features_to_query_; ++itr, ++count)
|
2015-03-19 12:09:07 +01:00
|
|
|
{
|
|
|
|
geojson_datasource::item_type const& item = *itr;
|
2017-08-15 12:14:11 +02:00
|
|
|
std::uint64_t file_offset = item.second.first;
|
|
|
|
std::uint64_t size = item.second.second;
|
2015-03-19 12:09:07 +01:00
|
|
|
|
|
|
|
std::fseek(file.get(), file_offset, SEEK_SET);
|
|
|
|
std::vector<char> json;
|
|
|
|
json.resize(size);
|
2017-01-13 14:57:15 +01:00
|
|
|
auto count_objects = std::fread(json.data(), size, 1, file.get());
|
2015-03-19 12:09:07 +01:00
|
|
|
using chr_iterator_type = char const*;
|
2015-06-12 23:51:18 +02:00
|
|
|
chr_iterator_type start2 = json.data();
|
2017-01-13 14:57:15 +01:00
|
|
|
chr_iterator_type end2 = (count_objects == 1) ? start2 + json.size() : start2;
|
2015-03-19 12:09:07 +01:00
|
|
|
|
2015-10-21 12:12:55 +02:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp feature
|
2016-11-28 10:28:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
mapnik::json::parse_feature(start2, end2, *feature, geojson_datasource_static_tr);
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (...)
|
2015-03-19 12:09:07 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse geojson feature");
|
|
|
|
}
|
2016-11-28 10:28:12 +01:00
|
|
|
|
2015-03-24 12:13:31 +01:00
|
|
|
result = mapnik::util::to_ds_type(feature->get_geometry());
|
2015-03-19 12:09:07 +01:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
int type = static_cast<int>(*result);
|
|
|
|
if (multi_type > 0 && multi_type != type)
|
|
|
|
{
|
2015-03-24 12:13:31 +01:00
|
|
|
result.reset(mapnik::datasource_geometry_t::Collection);
|
2015-03-19 12:09:07 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
multi_type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-13 14:30:58 +02:00
|
|
|
mapnik::featureset_ptr geojson_datasource::features(mapnik::query const& q) const
|
|
|
|
{
|
|
|
|
// if the query box intersects our world extent then query for features
|
2015-01-26 09:42:06 +01:00
|
|
|
mapnik::box2d<double> const& box = q.get_bbox();
|
|
|
|
if (extent_.intersects(box))
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
2014-08-19 23:48:21 +02:00
|
|
|
geojson_featureset::array_type index_array;
|
2014-10-16 03:51:53 +02:00
|
|
|
if (tree_)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
tree_->query(boost::geometry::index::intersects(box), std::back_inserter(index_array));
|
2015-11-23 11:19:49 +01:00
|
|
|
// sort index array to preserve original feature ordering in GeoJSON
|
2022-01-26 10:43:31 +01:00
|
|
|
std::sort(index_array.begin(), index_array.end(), [](item_type const& item0, item_type const& item1) {
|
|
|
|
return item0.second.first < item1.second.first;
|
|
|
|
});
|
2015-01-26 10:19:18 +01:00
|
|
|
if (cache_features_)
|
|
|
|
{
|
|
|
|
return std::make_shared<geojson_featureset>(features_, std::move(index_array));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-19 11:54:52 +02:00
|
|
|
return std::make_shared<geojson_memory_index_featureset>(filename_, std::move(index_array));
|
2015-01-26 10:19:18 +01:00
|
|
|
}
|
2014-10-16 03:51:53 +02:00
|
|
|
}
|
2015-10-16 14:15:20 +02:00
|
|
|
else if (has_disk_index_)
|
|
|
|
{
|
2017-08-16 11:35:04 +02:00
|
|
|
auto const& bbox = q.get_bbox();
|
2022-01-26 10:43:31 +01:00
|
|
|
mapnik::bounding_box_filter<float> const filter(
|
|
|
|
mapnik::box2d<float>(bbox.minx(), bbox.miny(), bbox.maxx(), bbox.maxy()));
|
2015-10-16 14:15:20 +02:00
|
|
|
return std::make_shared<geojson_index_featureset>(filename_, filter);
|
|
|
|
}
|
2012-12-03 14:12:09 +01:00
|
|
|
}
|
2016-07-13 16:16:33 +02:00
|
|
|
// otherwise return an empty featureset
|
2016-08-15 11:41:30 +02:00
|
|
|
return mapnik::make_invalid_featureset();
|
2012-06-13 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2012-09-28 15:12:10 +02:00
|
|
|
mapnik::featureset_ptr geojson_datasource::features_at_point(mapnik::coord2d const& pt, double tol) const
|
2012-06-13 14:30:58 +02:00
|
|
|
{
|
2013-05-22 17:38:25 +02:00
|
|
|
mapnik::box2d<double> query_bbox(pt, pt);
|
|
|
|
query_bbox.pad(tol);
|
|
|
|
mapnik::query q(query_bbox);
|
2015-10-09 12:26:39 +02:00
|
|
|
for (auto const& attr_info : desc_.get_descriptors())
|
2013-05-22 17:38:25 +02:00
|
|
|
{
|
2015-10-09 12:26:39 +02:00
|
|
|
q.add_property_name(attr_info.get_name());
|
2013-05-22 17:38:25 +02:00
|
|
|
}
|
|
|
|
return features(q);
|
2012-06-13 14:30:58 +02:00
|
|
|
}
|