2012-04-08 00:20:56 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2017-05-05 11:02:01 +00:00
|
|
|
* Copyright (C) 2017 Artem Pavlenko
|
2012-04-08 00:20:56 +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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-08-17 20:46:32 +00:00
|
|
|
#include "csv_utils.hpp"
|
2016-02-16 03:06:11 +00:00
|
|
|
#include "csv_getline.hpp"
|
2015-08-21 11:52:42 +00:00
|
|
|
#include "csv_datasource.hpp"
|
|
|
|
#include "csv_featureset.hpp"
|
|
|
|
#include "csv_inline_featureset.hpp"
|
2015-09-25 15:47:03 +00:00
|
|
|
#include "csv_index_featureset.hpp"
|
2011-10-12 01:11:59 +00:00
|
|
|
// boost
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
// mapnik
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2015-06-02 10:10:41 +00:00
|
|
|
#include <mapnik/util/utf_conv_win.hpp>
|
2013-01-04 03:27:53 +00:00
|
|
|
#include <mapnik/unicode.hpp>
|
2011-10-12 01:11:59 +00:00
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/feature_factory.hpp>
|
|
|
|
#include <mapnik/memory_featureset.hpp>
|
2012-03-07 18:16:41 +00:00
|
|
|
#include <mapnik/boolean.hpp>
|
2012-12-07 22:06:13 +00:00
|
|
|
#include <mapnik/util/trim.hpp>
|
2016-02-25 01:42:51 +00:00
|
|
|
#include <mapnik/geometry.hpp>
|
2016-08-02 13:43:02 +00:00
|
|
|
#include <mapnik/geometry/boost_adapters.hpp>
|
2015-03-19 11:09:07 +00:00
|
|
|
#include <mapnik/util/geometry_to_ds_type.hpp>
|
2016-08-02 14:34:59 +00:00
|
|
|
#include <mapnik/value/types.hpp>
|
2015-09-25 15:47:03 +00:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2016-02-25 01:42:51 +00:00
|
|
|
#include <mapnik/make_unique.hpp>
|
2015-09-25 15:47:03 +00:00
|
|
|
#include <mapnik/util/spatial_index.hpp>
|
|
|
|
#include <mapnik/geom_util.hpp>
|
2015-10-16 20:34:53 +00:00
|
|
|
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
|
2015-10-01 18:18:11 +00:00
|
|
|
#pragma GCC diagnostic push
|
2015-11-08 01:53:09 +00:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2015-09-09 09:53:17 +00:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
|
|
|
#include <boost/interprocess/streams/bufferstream.hpp>
|
2015-10-01 18:18:11 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2015-09-09 09:53:17 +00:00
|
|
|
#include <mapnik/mapped_memory_cache.hpp>
|
|
|
|
#endif
|
|
|
|
|
2011-10-12 01:11:59 +00:00
|
|
|
// stl
|
|
|
|
#include <sstream>
|
2011-11-10 00:45:18 +00:00
|
|
|
#include <fstream>
|
2011-11-14 03:33:57 +00:00
|
|
|
#include <vector>
|
2011-10-12 01:11:59 +00:00
|
|
|
#include <string>
|
2013-01-04 03:27:53 +00:00
|
|
|
#include <algorithm>
|
2011-10-12 01:11:59 +00:00
|
|
|
|
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
|
|
|
DATASOURCE_PLUGIN(csv_datasource)
|
|
|
|
|
2012-12-17 18:03:07 +00:00
|
|
|
csv_datasource::csv_datasource(parameters const& params)
|
2015-08-24 10:23:59 +00:00
|
|
|
: datasource(params),
|
2014-06-26 09:51:24 +00:00
|
|
|
desc_(csv_datasource::name(), *params.get<std::string>("encoding", "utf-8")),
|
|
|
|
ctx_(std::make_shared<mapnik::context_type>()),
|
2016-02-26 19:28:42 +00:00
|
|
|
tree_(nullptr)
|
2011-10-12 01:11:59 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
row_limit_ = *params.get<mapnik::value_integer>("row_limit", 0);
|
|
|
|
manual_headers_ = mapnik::util::trim_copy(*params.get<std::string>("headers", ""));
|
|
|
|
strict_ = *params.get<mapnik::boolean_type>("strict", false);
|
|
|
|
|
2015-10-02 11:20:54 +00:00
|
|
|
auto quote_param = params.get<std::string>("quote");
|
|
|
|
if (quote_param)
|
|
|
|
{
|
|
|
|
auto val = mapnik::util::trim_copy(*quote_param);
|
2015-10-05 08:34:02 +00:00
|
|
|
if (!val.empty()) quote_ = val.front(); // we pick pick first non-space char
|
|
|
|
}
|
|
|
|
|
|
|
|
auto separator_param = params.get<std::string>("separator");
|
|
|
|
if (separator_param)
|
|
|
|
{
|
|
|
|
auto val = mapnik::util::trim_copy(*separator_param);
|
|
|
|
if (!val.empty()) separator_ = val.front();
|
2015-10-02 11:20:54 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 18:57:03 +00:00
|
|
|
boost::optional<std::string> ext = params.get<std::string>("extent");
|
|
|
|
if (ext && !ext->empty())
|
|
|
|
{
|
|
|
|
extent_initialized_ = extent_.from_string(*ext);
|
|
|
|
}
|
|
|
|
|
2012-12-17 18:03:07 +00:00
|
|
|
boost::optional<std::string> inline_string = params.get<std::string>("inline");
|
2011-10-12 01:11:59 +00:00
|
|
|
if (inline_string)
|
|
|
|
{
|
|
|
|
inline_string_ = *inline_string;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-17 18:03:07 +00:00
|
|
|
boost::optional<std::string> file = params.get<std::string>("file");
|
2011-10-12 01:11:59 +00:00
|
|
|
if (!file) throw mapnik::datasource_exception("CSV Plugin: missing <file> parameter");
|
2012-12-17 18:03:07 +00:00
|
|
|
boost::optional<std::string> base = params.get<std::string>("base");
|
2011-10-12 01:11:59 +00:00
|
|
|
if (base)
|
|
|
|
filename_ = *base + "/" + *file;
|
|
|
|
else
|
|
|
|
filename_ = *file;
|
2015-09-25 15:47:03 +00:00
|
|
|
|
|
|
|
has_disk_index_ = mapnik::util::exists(filename_ + ".index");
|
2011-10-12 01:11:59 +00:00
|
|
|
}
|
|
|
|
if (!inline_string_.empty())
|
|
|
|
{
|
|
|
|
std::istringstream in(inline_string_);
|
2015-10-05 08:34:02 +00:00
|
|
|
parse_csv(in);
|
2011-10-12 01:11:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-16 20:34:53 +00:00
|
|
|
#if defined (MAPNIK_MEMORY_MAPPED_FILE)
|
2015-09-09 09:53:17 +00:00
|
|
|
using file_source_type = boost::interprocess::ibufferstream;
|
|
|
|
file_source_type in;
|
|
|
|
mapnik::mapped_region_ptr mapped_region;
|
|
|
|
boost::optional<mapnik::mapped_region_ptr> memory =
|
|
|
|
mapnik::mapped_memory_cache::instance().find(filename_, true);
|
|
|
|
if (memory)
|
|
|
|
{
|
|
|
|
mapped_region = *memory;
|
|
|
|
in.buffer(static_cast<char*>(mapped_region->get_address()),mapped_region->get_size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("could not create file mapping for " + filename_);
|
|
|
|
}
|
|
|
|
#elif defined (_WINDOWS)
|
2013-05-21 19:51:31 +00:00
|
|
|
std::ifstream in(mapnik::utf8_to_utf16(filename_),std::ios_base::in | std::ios_base::binary);
|
2015-09-09 09:53:17 +00:00
|
|
|
if (!in.is_open())
|
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("CSV Plugin: could not open: '" + filename_ + "'");
|
|
|
|
}
|
2013-05-21 19:51:31 +00:00
|
|
|
#else
|
2011-10-15 03:28:23 +00:00
|
|
|
std::ifstream in(filename_.c_str(),std::ios_base::in | std::ios_base::binary);
|
2011-10-12 01:11:59 +00:00
|
|
|
if (!in.is_open())
|
2013-05-21 19:55:08 +00:00
|
|
|
{
|
2011-10-12 01:11:59 +00:00
|
|
|
throw mapnik::datasource_exception("CSV Plugin: could not open: '" + filename_ + "'");
|
2013-05-21 19:55:08 +00:00
|
|
|
}
|
2015-09-09 09:53:17 +00:00
|
|
|
#endif
|
2015-10-05 08:34:02 +00:00
|
|
|
parse_csv(in);
|
2015-09-25 15:47:03 +00:00
|
|
|
|
|
|
|
if (has_disk_index_ && !extent_initialized_)
|
|
|
|
{
|
|
|
|
// read bounding box from *.index
|
2017-08-18 10:16:44 +00:00
|
|
|
using value_type = mapnik::util::index_record;
|
2015-09-25 15:47:03 +00:00
|
|
|
std::ifstream index(filename_ + ".index", std::ios::binary);
|
|
|
|
if (!index) throw mapnik::datasource_exception("CSV Plugin: could not open: '" + filename_ + ".index'");
|
2017-08-18 10:16:44 +00:00
|
|
|
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() };
|
|
|
|
|
2015-09-25 15:47:03 +00:00
|
|
|
}
|
2015-09-09 09:53:17 +00:00
|
|
|
//in.close(); no need to call close, rely on dtor
|
2011-10-12 01:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 11:52:42 +00:00
|
|
|
csv_datasource::~csv_datasource() {}
|
2015-06-09 09:17:55 +00:00
|
|
|
|
2016-02-26 19:28:42 +00:00
|
|
|
void csv_datasource::parse_csv(std::istream & csv_file)
|
2011-10-12 01:11:59 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
std::vector<item_type> boxes;
|
2016-03-11 22:23:16 +00:00
|
|
|
csv_utils::csv_file_parser::parse_csv_and_boxes(csv_file, boxes);
|
2013-10-30 18:57:03 +00:00
|
|
|
|
2015-06-01 12:58:37 +00:00
|
|
|
std::for_each(headers_.begin(), headers_.end(),
|
|
|
|
[ & ](std::string const& header){ ctx_->push(header); });
|
2012-01-17 18:34:08 +00:00
|
|
|
|
2016-02-26 19:28:42 +00:00
|
|
|
if (!has_disk_index_)
|
2011-10-15 03:28:23 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
// bulk insert initialise r-tree
|
|
|
|
tree_ = std::make_unique<spatial_index_type>(boxes);
|
2012-08-31 19:07:35 +00:00
|
|
|
}
|
2016-02-26 19:28:42 +00:00
|
|
|
}
|
2015-09-25 15:47:03 +00:00
|
|
|
|
2016-02-26 19:28:42 +00:00
|
|
|
void csv_datasource::add_feature(mapnik::value_integer index,
|
|
|
|
mapnik::csv_line const & values)
|
|
|
|
{
|
|
|
|
if (index != 1) return;
|
2015-10-05 14:22:09 +00:00
|
|
|
|
2016-02-26 19:28:42 +00:00
|
|
|
for (std::size_t i = 0; i < values.size(); ++i)
|
|
|
|
{
|
|
|
|
std::string const& header = headers_.at(i);
|
|
|
|
std::string value = mapnik::util::trim_copy(values[i]);
|
|
|
|
int value_length = value.length();
|
|
|
|
if (locator_.index == i && (locator_.type == csv_utils::geometry_column_locator::WKT
|
|
|
|
|| locator_.type == csv_utils::geometry_column_locator::GEOJSON)) continue;
|
|
|
|
|
|
|
|
// First we detect likely strings,
|
|
|
|
// then try parsing likely numbers,
|
|
|
|
// then try converting to bool,
|
|
|
|
// finally falling back to string type.
|
|
|
|
|
|
|
|
// An empty string or a string of "null" will be parsed
|
|
|
|
// as a string rather than a true null value.
|
|
|
|
// Likely strings are either empty values, very long values
|
|
|
|
// or values with leading zeros like 001 (which are not safe
|
|
|
|
// to assume are numbers)
|
|
|
|
|
|
|
|
bool matched = false;
|
|
|
|
bool has_dot = value.find(".") != std::string::npos;
|
|
|
|
if (value.empty() || (value_length > 20) || (value_length > 1 && !has_dot && value[0] == '0'))
|
2011-11-04 11:18:40 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
matched = true;
|
|
|
|
desc_.add_descriptor(mapnik::attribute_descriptor(header, mapnik::String));
|
2011-10-17 18:03:50 +00:00
|
|
|
}
|
2016-02-26 19:28:42 +00:00
|
|
|
else if (csv_utils::is_likely_number(value))
|
2011-10-15 03:28:23 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
bool has_e = value.find("e") != std::string::npos;
|
|
|
|
if (has_dot || has_e)
|
2012-08-20 21:06:07 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
double float_val = 0.0;
|
|
|
|
if (mapnik::util::string2double(value,float_val))
|
2016-02-01 20:23:26 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
matched = true;
|
|
|
|
desc_.add_descriptor(mapnik::attribute_descriptor(header,mapnik::Double));
|
2016-02-01 20:23:26 +00:00
|
|
|
}
|
2012-08-20 21:06:07 +00:00
|
|
|
}
|
2016-02-26 19:28:42 +00:00
|
|
|
else
|
2011-10-12 01:11:59 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
mapnik::value_integer int_val = 0;
|
|
|
|
if (mapnik::util::string2int(value,int_val))
|
2015-08-24 10:23:59 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
matched = true;
|
|
|
|
desc_.add_descriptor(mapnik::attribute_descriptor(header,mapnik::Integer));
|
2011-10-15 03:28:23 +00:00
|
|
|
}
|
2011-10-12 01:11:59 +00:00
|
|
|
}
|
2011-10-31 23:09:29 +00:00
|
|
|
}
|
2016-02-26 19:28:42 +00:00
|
|
|
if (!matched)
|
2011-10-15 03:28:23 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
// NOTE: we don't use mapnik::util::string2bool
|
|
|
|
// here because we don't want to treat 'on' and 'off'
|
|
|
|
// as booleans, only 'true' and 'false'
|
|
|
|
if (csv_utils::ignore_case_equal(value, "true") || csv_utils::ignore_case_equal(value, "false"))
|
2011-10-15 03:28:23 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
desc_.add_descriptor(mapnik::attribute_descriptor(header, mapnik::Boolean));
|
2011-10-15 03:28:23 +00:00
|
|
|
}
|
2016-02-26 19:28:42 +00:00
|
|
|
else // fallback to normal string
|
2011-10-15 03:28:23 +00:00
|
|
|
{
|
2016-02-26 19:28:42 +00:00
|
|
|
desc_.add_descriptor(mapnik::attribute_descriptor(header, mapnik::String));
|
2011-10-15 03:28:23 +00:00
|
|
|
}
|
2011-10-12 01:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-21 01:34:41 +00:00
|
|
|
const char * csv_datasource::name()
|
2011-10-12 01:11:59 +00:00
|
|
|
{
|
|
|
|
return "csv";
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:09:46 +00:00
|
|
|
datasource::datasource_t csv_datasource::type() const
|
2011-10-12 01:11:59 +00:00
|
|
|
{
|
|
|
|
return datasource::Vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapnik::box2d<double> csv_datasource::envelope() const
|
|
|
|
{
|
|
|
|
return extent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapnik::layer_descriptor csv_datasource::get_descriptor() const
|
|
|
|
{
|
|
|
|
return desc_;
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:28:42 +00:00
|
|
|
boost::optional<mapnik::datasource_geometry_t>
|
|
|
|
csv_datasource::get_geometry_type_impl(std::istream & stream) const
|
2015-03-19 11:09:07 +00:00
|
|
|
{
|
2015-03-24 11:13:31 +00:00
|
|
|
boost::optional<mapnik::datasource_geometry_t> result;
|
2015-10-06 10:32:21 +00:00
|
|
|
if (tree_)
|
2015-03-19 11:09:07 +00:00
|
|
|
{
|
2015-10-06 10:32:21 +00:00
|
|
|
int multi_type = 0;
|
|
|
|
auto itr = tree_->qbegin(boost::geometry::index::intersects(extent_));
|
|
|
|
auto end = tree_->qend();
|
|
|
|
for (std::size_t count = 0; itr !=end && count < 5; ++itr, ++count)
|
2015-03-19 11:09:07 +00:00
|
|
|
{
|
2015-10-06 10:32:21 +00:00
|
|
|
csv_datasource::item_type const& item = *itr;
|
2017-08-15 10:14:11 +00:00
|
|
|
std::uint64_t file_offset = item.second.first;
|
|
|
|
std::uint64_t size = item.second.second;
|
2015-10-06 10:32:21 +00:00
|
|
|
stream.seekg(file_offset);
|
|
|
|
std::vector<char> record;
|
|
|
|
record.resize(size);
|
|
|
|
stream.read(record.data(), size);
|
|
|
|
std::string str(record.begin(), record.end());
|
|
|
|
try
|
2015-03-19 11:09:07 +00:00
|
|
|
{
|
2015-10-06 10:32:21 +00:00
|
|
|
auto values = csv_utils::parse_line(str, separator_, quote_);
|
2016-02-26 19:28:42 +00:00
|
|
|
auto geom = csv_utils::extract_geometry(values, locator_);
|
2015-10-06 10:32:21 +00:00
|
|
|
result = mapnik::util::to_ds_type(geom);
|
|
|
|
if (result)
|
2015-08-21 11:52:42 +00:00
|
|
|
{
|
2015-10-06 10:32:21 +00:00
|
|
|
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-08-21 11:52:42 +00:00
|
|
|
}
|
2015-10-06 10:32:21 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
|
|
|
if (strict_) throw ex;
|
|
|
|
else MAPNIK_LOG_ERROR(csv) << ex.what();
|
2015-08-21 11:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-06 10:32:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// try reading *.index
|
2017-08-18 10:16:44 +00:00
|
|
|
using value_type = mapnik::util::index_record;
|
2015-10-06 10:32:21 +00:00
|
|
|
std::ifstream index(filename_ + ".index", std::ios::binary);
|
|
|
|
if (!index) throw mapnik::datasource_exception("CSV Plugin: could not open: '" + filename_ + ".index'");
|
2017-08-18 10:16:44 +00:00
|
|
|
mapnik::bounding_box_filter<float> filter{mapnik::box2d<float>(extent_.minx(), extent_.miny(), extent_.maxx(), extent_.maxy())};
|
2015-10-06 10:32:21 +00:00
|
|
|
std::vector<value_type> positions;
|
|
|
|
mapnik::util::spatial_index<value_type,
|
2017-08-18 10:16:44 +00:00
|
|
|
mapnik::bounding_box_filter<float>,
|
|
|
|
std::ifstream,
|
|
|
|
mapnik::box2d<float>>::query_first_n(filter, index, positions, 5);
|
2015-10-06 10:32:21 +00:00
|
|
|
int multi_type = 0;
|
|
|
|
for (auto const& val : positions)
|
2015-08-21 11:52:42 +00:00
|
|
|
{
|
2017-08-18 10:16:44 +00:00
|
|
|
stream.seekg(val.off);
|
2015-10-06 10:32:21 +00:00
|
|
|
std::vector<char> record;
|
2017-08-18 10:16:44 +00:00
|
|
|
record.resize(val.size);
|
|
|
|
stream.read(record.data(), val.size);
|
2015-10-06 10:32:21 +00:00
|
|
|
std::string str(record.begin(), record.end());
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto values = csv_utils::parse_line(str, separator_, quote_);
|
2016-02-26 19:28:42 +00:00
|
|
|
auto geom = csv_utils::extract_geometry(values, locator_);
|
2015-10-06 10:32:21 +00:00
|
|
|
result = mapnik::util::to_ds_type(geom);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
|
|
|
if (strict_) throw ex;
|
|
|
|
else MAPNIK_LOG_ERROR(csv) << ex.what();
|
|
|
|
}
|
2015-03-19 11:09:07 +00:00
|
|
|
}
|
2015-10-06 10:32:21 +00:00
|
|
|
|
2015-03-19 11:09:07 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-27 11:05:02 +00:00
|
|
|
boost::optional<mapnik::datasource_geometry_t> csv_datasource::get_geometry_type() const
|
|
|
|
{
|
|
|
|
if (inline_string_.empty())
|
|
|
|
{
|
|
|
|
#if defined (_WINDOWS)
|
|
|
|
std::ifstream in(mapnik::utf8_to_utf16(filename_),std::ios_base::in | std::ios_base::binary);
|
|
|
|
#else
|
|
|
|
std::ifstream in(filename_.c_str(),std::ios_base::in | std::ios_base::binary);
|
|
|
|
#endif
|
|
|
|
if (!in.is_open())
|
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("CSV Plugin: could not open: '" + filename_ + "'");
|
|
|
|
}
|
|
|
|
return get_geometry_type_impl(in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::stringstream in(inline_string_);
|
|
|
|
return get_geometry_type_impl(in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-12 01:11:59 +00:00
|
|
|
mapnik::featureset_ptr csv_datasource::features(mapnik::query const& q) const
|
|
|
|
{
|
2015-08-21 11:52:42 +00:00
|
|
|
for (auto const& name : q.property_names())
|
2011-12-05 20:03:38 +00:00
|
|
|
{
|
|
|
|
bool found_name = false;
|
2015-08-21 11:52:42 +00:00
|
|
|
for (auto const& header : headers_)
|
2011-12-05 20:03:38 +00:00
|
|
|
{
|
2015-08-21 11:52:42 +00:00
|
|
|
if (header == name)
|
2011-12-05 20:03:38 +00:00
|
|
|
{
|
|
|
|
found_name = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 11:52:42 +00:00
|
|
|
if (!found_name)
|
2011-12-05 20:03:38 +00:00
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2015-08-21 11:52:42 +00:00
|
|
|
s << "CSV Plugin: no attribute '" << name << "'. Valid attributes are: "
|
2011-12-05 20:03:38 +00:00
|
|
|
<< boost::algorithm::join(headers_, ",") << ".";
|
|
|
|
throw mapnik::datasource_exception(s.str());
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 11:52:42 +00:00
|
|
|
|
|
|
|
mapnik::box2d<double> const& box = q.get_bbox();
|
|
|
|
if (extent_.intersects(box))
|
|
|
|
{
|
|
|
|
if (tree_)
|
|
|
|
{
|
2015-09-09 10:14:15 +00:00
|
|
|
csv_featureset::array_type index_array;
|
2015-08-21 11:52:42 +00:00
|
|
|
tree_->query(boost::geometry::index::intersects(box),std::back_inserter(index_array));
|
|
|
|
std::sort(index_array.begin(),index_array.end(),
|
|
|
|
[] (item_type const& item0, item_type const& item1)
|
|
|
|
{
|
|
|
|
return item0.second.first < item1.second.first;
|
|
|
|
});
|
|
|
|
if (inline_string_.empty())
|
2015-08-24 13:41:04 +00:00
|
|
|
{
|
2015-10-02 11:20:54 +00:00
|
|
|
return std::make_shared<csv_featureset>(filename_, locator_, separator_, quote_, headers_, ctx_, std::move(index_array));
|
2015-08-24 13:41:04 +00:00
|
|
|
}
|
2015-08-21 11:52:42 +00:00
|
|
|
else
|
2015-08-24 13:41:04 +00:00
|
|
|
{
|
2015-10-02 11:20:54 +00:00
|
|
|
return std::make_shared<csv_inline_featureset>(inline_string_, locator_, separator_, quote_, headers_, ctx_, std::move(index_array));
|
2015-08-24 13:41:04 +00:00
|
|
|
}
|
2015-08-21 11:52:42 +00:00
|
|
|
}
|
2015-09-25 15:47:03 +00:00
|
|
|
else if (has_disk_index_)
|
|
|
|
{
|
2017-08-18 10:16:44 +00:00
|
|
|
auto const& bbox = q.get_bbox();
|
|
|
|
mapnik::bounding_box_filter<float> const filter(mapnik::box2d<float>(bbox.minx(), bbox.miny(), bbox.maxx(), bbox.maxy()));
|
2015-10-02 11:20:54 +00:00
|
|
|
return std::make_shared<csv_index_featureset>(filename_, filter, locator_, separator_, quote_, headers_, ctx_);
|
2015-09-25 15:47:03 +00:00
|
|
|
}
|
2015-08-21 11:52:42 +00:00
|
|
|
}
|
2016-08-15 09:41:30 +00:00
|
|
|
return mapnik::make_invalid_featureset();
|
2011-10-12 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 13:12:10 +00:00
|
|
|
mapnik::featureset_ptr csv_datasource::features_at_point(mapnik::coord2d const& pt, double tol) const
|
2011-10-12 01:11:59 +00:00
|
|
|
{
|
2015-08-24 13:41:04 +00:00
|
|
|
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();
|
|
|
|
for (auto const& item : desc)
|
|
|
|
{
|
|
|
|
q.add_property_name(item.get_name());
|
|
|
|
}
|
|
|
|
return features(q);
|
2011-10-12 01:11:59 +00:00
|
|
|
}
|