2015-10-09 13:49:58 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 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 "process_geojson_file.hpp"
|
|
|
|
#include <mapnik/geometry.hpp>
|
|
|
|
#include <mapnik/geometry_envelope.hpp>
|
|
|
|
#include <mapnik/geometry_adapters.hpp>
|
2015-10-20 21:17:32 +02:00
|
|
|
#include <mapnik/util/file_io.hpp>
|
|
|
|
#include <mapnik/util/utf_conv_win.hpp>
|
|
|
|
|
|
|
|
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
|
2015-10-09 13:49:58 +02:00
|
|
|
#pragma GCC diagnostic push
|
2015-10-28 00:38:43 +01:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
2015-10-09 13:49:58 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Wshadow"
|
2015-10-09 22:49:34 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
2015-10-09 13:49:58 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
|
|
|
#include <boost/interprocess/streams/bufferstream.hpp>
|
|
|
|
#include <boost/spirit/include/qi.hpp>
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#include <mapnik/mapped_memory_cache.hpp>
|
2015-10-20 21:17:32 +02:00
|
|
|
#endif
|
|
|
|
|
2015-10-09 13:49:58 +02:00
|
|
|
#include <mapnik/json/positions_grammar.hpp>
|
|
|
|
#include <mapnik/json/extract_bounding_box_grammar_impl.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
using base_iterator_type = char const*;
|
|
|
|
const mapnik::json::extract_bounding_box_grammar<base_iterator_type> geojson_datasource_static_bbox_grammar;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mapnik { namespace detail {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::pair<bool,box2d<double>> process_geojson_file(T & boxes, std::string const& filename)
|
|
|
|
{
|
|
|
|
mapnik::box2d<double> extent;
|
2015-10-20 21:17:32 +02:00
|
|
|
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
|
2015-10-09 13:49:58 +02:00
|
|
|
mapnik::mapped_region_ptr mapped_region;
|
|
|
|
boost::optional<mapnik::mapped_region_ptr> memory =
|
|
|
|
mapnik::mapped_memory_cache::instance().find(filename, true);
|
|
|
|
if (!memory)
|
|
|
|
{
|
2015-10-16 11:49:29 +02:00
|
|
|
std::clog << "Error : cannot memory map " << filename << std::endl;
|
2015-10-09 13:49:58 +02:00
|
|
|
return std::make_pair(false, extent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mapped_region = *memory;
|
|
|
|
}
|
|
|
|
char const* start = reinterpret_cast<char const*>(mapped_region->get_address());
|
|
|
|
char const* end = start + mapped_region->get_size();
|
2015-10-20 21:17:32 +02:00
|
|
|
#else
|
|
|
|
mapnik::util::file file(filename);
|
|
|
|
if (!file.open())
|
|
|
|
{
|
|
|
|
std::clog << "Error : cannot open " << filename << std::endl;
|
|
|
|
return std::make_pair(false, extent);
|
|
|
|
}
|
|
|
|
std::string file_buffer;
|
|
|
|
file_buffer.resize(file.size());
|
|
|
|
std::fread(&file_buffer[0], file.size(), 1, file.get());
|
|
|
|
char const* start = file_buffer.c_str();
|
|
|
|
char const* end = start + file_buffer.length();
|
|
|
|
#endif
|
|
|
|
|
2015-10-09 13:49:58 +02:00
|
|
|
boost::spirit::standard::space_type space;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!boost::spirit::qi::phrase_parse(start, end, (geojson_datasource_static_bbox_grammar)(boost::phoenix::ref(boxes)) , space))
|
|
|
|
{
|
2015-11-06 11:56:45 +01:00
|
|
|
std::clog << "mapnik-index (GeoJSON) : could not extract bounding boxes from : '" << filename << "'" << std::endl;
|
2015-10-09 13:49:58 +02:00
|
|
|
return std::make_pair(false, extent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
2015-11-03 14:31:46 +01:00
|
|
|
std::clog << "mapnik-index (GeoJSON): " << ex.what() << std::endl;
|
2015-10-09 13:49:58 +02:00
|
|
|
}
|
|
|
|
for (auto const& item : boxes)
|
|
|
|
{
|
2015-11-02 15:14:12 +01:00
|
|
|
if (item.first.valid())
|
|
|
|
{
|
|
|
|
if (!extent.valid()) extent = item.first;
|
|
|
|
else extent.expand_to_include(item.first);
|
|
|
|
}
|
2015-10-09 13:49:58 +02:00
|
|
|
}
|
|
|
|
return std::make_pair(true, extent);
|
|
|
|
}
|
|
|
|
|
|
|
|
using box_type = mapnik::box2d<double>;
|
|
|
|
using item_type = std::pair<box_type, std::pair<std::size_t, std::size_t>>;
|
|
|
|
using boxes_type = std::vector<item_type>;
|
|
|
|
template std::pair<bool,box2d<double>> process_geojson_file(boxes_type&, std::string const&);
|
|
|
|
|
|
|
|
}}
|