2007-03-08 18:19:42 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2007-03-08 18:19:42 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 10:49:16 +00:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2007-03-08 18:19:42 +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-04-08 00:20:56 +00:00
|
|
|
#include "shape_datasource.hpp"
|
|
|
|
#include "shape_featureset.hpp"
|
|
|
|
#include "shape_index_featureset.hpp"
|
2009-03-30 23:51:58 +00:00
|
|
|
|
|
|
|
// boost
|
2009-06-08 08:22:02 +00:00
|
|
|
#include <boost/version.hpp>
|
2014-10-21 23:37:27 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
2009-03-30 23:51:58 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2014-10-21 23:37:27 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2009-03-30 23:51:58 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/debug.hpp>
|
2014-06-12 14:14:05 +00:00
|
|
|
#include <mapnik/make_unique.hpp>
|
2013-06-03 02:28:24 +00:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/global.hpp>
|
2015-06-02 10:10:41 +00:00
|
|
|
#include <mapnik/util/utf_conv_win.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/boolean.hpp>
|
|
|
|
#include <mapnik/util/conversions.hpp>
|
|
|
|
#include <mapnik/geom_util.hpp>
|
|
|
|
#include <mapnik/timer.hpp>
|
2013-01-08 22:17:31 +00:00
|
|
|
#include <mapnik/value_types.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
|
2011-05-16 23:41:34 +00:00
|
|
|
// stl
|
|
|
|
#include <fstream>
|
2014-10-01 01:35:07 +00:00
|
|
|
#include <sstream>
|
2011-05-16 23:41:34 +00:00
|
|
|
#include <stdexcept>
|
2009-06-08 08:22:02 +00:00
|
|
|
|
2007-03-08 18:19:42 +00:00
|
|
|
DATASOURCE_PLUGIN(shape_datasource)
|
|
|
|
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::String;
|
|
|
|
using mapnik::Double;
|
|
|
|
using mapnik::Integer;
|
2012-12-03 05:37:39 +00:00
|
|
|
using mapnik::Boolean;
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::datasource_exception;
|
|
|
|
using mapnik::filter_in_box;
|
|
|
|
using mapnik::filter_at_point;
|
|
|
|
using mapnik::attribute_descriptor;
|
|
|
|
|
2014-05-30 09:15:29 +00:00
|
|
|
shape_datasource::shape_datasource(parameters const& params)
|
2010-09-02 20:20:42 +00:00
|
|
|
: datasource (params),
|
|
|
|
type_(datasource::Vector),
|
|
|
|
file_length_(0),
|
|
|
|
indexed_(false),
|
2013-01-04 17:23:06 +00:00
|
|
|
row_limit_(*params.get<mapnik::value_integer>("row_limit",0)),
|
2014-06-26 09:51:24 +00:00
|
|
|
desc_(shape_datasource::name(), *params.get<std::string>("encoding","utf-8"))
|
2007-03-08 18:19:42 +00:00
|
|
|
{
|
2012-12-17 18:03:07 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "shape_datasource::init");
|
|
|
|
#endif
|
2010-09-02 20:20:42 +00:00
|
|
|
boost::optional<std::string> file = params.get<std::string>("file");
|
2010-11-18 23:46:01 +00:00
|
|
|
if (!file) throw datasource_exception("Shape Plugin: missing <file> parameter");
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
boost::optional<std::string> base = params.get<std::string>("base");
|
|
|
|
if (base)
|
|
|
|
shape_name_ = *base + "/" + *file;
|
|
|
|
else
|
|
|
|
shape_name_ = *file;
|
2009-03-30 23:51:58 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
boost::algorithm::ireplace_last(shape_name_,".shp","");
|
2013-06-03 02:28:24 +00:00
|
|
|
if (!mapnik::util::exists(shape_name_ + ".shp"))
|
2010-09-02 20:20:42 +00:00
|
|
|
{
|
2010-11-18 23:46:01 +00:00
|
|
|
throw datasource_exception("Shape Plugin: shapefile '" + shape_name_ + ".shp' does not exist");
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
2013-06-03 02:28:24 +00:00
|
|
|
if (mapnik::util::is_directory(shape_name_ + ".shp"))
|
2010-09-02 20:20:42 +00:00
|
|
|
{
|
2010-11-18 23:46:01 +00:00
|
|
|
throw datasource_exception("Shape Plugin: shapefile '" + shape_name_ + ".shp' appears to be a directory not a file");
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
2013-06-03 02:28:24 +00:00
|
|
|
if (!mapnik::util::exists(shape_name_ + ".dbf"))
|
2011-02-28 17:44:56 +00:00
|
|
|
{
|
|
|
|
throw datasource_exception("Shape Plugin: shapefile '" + shape_name_ + ".dbf' does not exist");
|
|
|
|
}
|
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
try
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
2012-12-17 18:03:07 +00:00
|
|
|
mapnik::progress_timer __stats2__(std::clog, "shape_datasource::init(get_column_description)");
|
2012-04-08 00:20:56 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-30 14:37:44 +00:00
|
|
|
std::unique_ptr<shape_io> shape_ref = std::make_unique<shape_io>(shape_name_);
|
2011-02-09 23:41:41 +00:00
|
|
|
init(*shape_ref);
|
|
|
|
for (int i=0;i<shape_ref->dbf().num_fields();++i)
|
2010-09-02 20:20:42 +00:00
|
|
|
{
|
2011-02-09 23:41:41 +00:00
|
|
|
field_descriptor const& fd=shape_ref->dbf().descriptor(i);
|
2010-09-02 20:20:42 +00:00
|
|
|
std::string fld_name=fd.name_;
|
|
|
|
switch (fd.type_)
|
|
|
|
{
|
2011-07-12 00:59:43 +00:00
|
|
|
case 'C': // character
|
2013-08-07 22:58:01 +00:00
|
|
|
case 'D': // date
|
2010-09-02 20:20:42 +00:00
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name, String));
|
|
|
|
break;
|
2012-12-03 05:37:39 +00:00
|
|
|
case 'L': // logical
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name, Boolean));
|
|
|
|
break;
|
2013-08-07 22:58:01 +00:00
|
|
|
case 'N': // numeric
|
2011-07-12 00:59:43 +00:00
|
|
|
case 'O': // double
|
|
|
|
case 'F': // float
|
2007-03-08 18:19:42 +00:00
|
|
|
{
|
2010-09-02 20:20:42 +00:00
|
|
|
if (fd.dec_>0)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2010-09-02 20:20:42 +00:00
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,Double,false,8));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,Integer,false,4));
|
|
|
|
}
|
|
|
|
break;
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
|
|
|
default:
|
2011-07-12 00:59:43 +00:00
|
|
|
// I - long
|
|
|
|
// G - ole
|
|
|
|
// + - autoincrement
|
2013-08-07 22:58:01 +00:00
|
|
|
// @ - timestamp
|
|
|
|
// B - binary
|
|
|
|
// l - long
|
|
|
|
// M - memo
|
|
|
|
MAPNIK_LOG_ERROR(shape) << "shape_datasource: Unknown type=" << fd.type_;
|
2010-09-02 20:20:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-03 05:37:39 +00:00
|
|
|
catch (datasource_exception const& ex)
|
2011-02-28 17:44:56 +00:00
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_ERROR(shape) << "Shape Plugin: error processing field attributes, " << ex.what();
|
2011-02-28 17:44:56 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
2010-09-02 20:20:42 +00:00
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_ERROR(shape) << "Shape Plugin: error processing field attributes, " << ex.what();
|
2010-09-02 20:20:42 +00:00
|
|
|
throw;
|
|
|
|
}
|
2011-02-09 23:41:41 +00:00
|
|
|
catch (...) // exception: pipe_select_interrupter: Too many open files
|
2010-09-02 20:20:42 +00:00
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_ERROR(shape) << "Shape Plugin: error processing field attributes";
|
2010-09-02 20:20:42 +00:00
|
|
|
throw;
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 20:59:15 +00:00
|
|
|
void shape_datasource::init(shape_io& shape)
|
2007-03-08 18:19:42 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "shape_datasource::init");
|
|
|
|
#endif
|
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
//first read header from *.shp
|
|
|
|
int file_code=shape.shp().read_xdr_integer();
|
|
|
|
if (file_code!=9994)
|
|
|
|
{
|
2014-10-01 01:35:07 +00:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "Shape Plugin: wrong file code " << file_code;
|
|
|
|
throw datasource_exception(s.str());
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
shape.shp().skip(5*4);
|
|
|
|
file_length_=shape.shp().read_xdr_integer();
|
|
|
|
int version=shape.shp().read_ndr_integer();
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
if (version!=1000)
|
|
|
|
{
|
2014-10-01 01:35:07 +00:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "Shape Plugin: nvalid version number " << version;
|
|
|
|
throw datasource_exception(s.str());
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-01-17 06:09:46 +00:00
|
|
|
shape_type_ = static_cast<shape_io::shapeType>(shape.shp().read_ndr_integer());
|
|
|
|
if (shape_type_ == shape_io::shape_multipatch)
|
2011-05-19 21:48:45 +00:00
|
|
|
throw datasource_exception("Shape Plugin: shapefile multipatch type is not supported");
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
shape.shp().read_envelope(extent_);
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_LOG
|
2012-04-09 01:00:51 +00:00
|
|
|
const double zmin = shape.shp().read_double();
|
|
|
|
const double zmax = shape.shp().read_double();
|
|
|
|
const double mmin = shape.shp().read_double();
|
|
|
|
const double mmax = shape.shp().read_double();
|
2010-01-29 23:56:25 +00:00
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(shape) << "shape_datasource: Z min/max=" << zmin << "," << zmax;
|
|
|
|
MAPNIK_LOG_DEBUG(shape) << "shape_datasource: M min/max=" << mmin << "," << mmax;
|
2010-01-20 15:26:06 +00:00
|
|
|
#endif
|
2010-01-29 23:56:25 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
// check if we have an index file around
|
|
|
|
indexed_ = shape.has_index();
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(shape) << "shape_datasource: Extent=" << extent_;
|
|
|
|
MAPNIK_LOG_DEBUG(shape) << "shape_datasource: File length=" << file_length_;
|
|
|
|
MAPNIK_LOG_DEBUG(shape) << "shape_datasource: Shape type=" << shape_type_;
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 20:59:15 +00:00
|
|
|
shape_datasource::~shape_datasource() {}
|
|
|
|
|
2012-07-21 01:34:41 +00:00
|
|
|
const char * shape_datasource::name()
|
2010-11-14 14:49:42 +00:00
|
|
|
{
|
|
|
|
return "shape";
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:09:46 +00:00
|
|
|
datasource::datasource_t shape_datasource::type() const
|
2007-03-08 18:19:42 +00:00
|
|
|
{
|
2010-09-02 20:20:42 +00:00
|
|
|
return type_;
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
layer_descriptor shape_datasource::get_descriptor() const
|
|
|
|
{
|
2010-09-02 20:20:42 +00:00
|
|
|
return desc_;
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 14:37:44 +00:00
|
|
|
featureset_ptr shape_datasource::features(query const& q) const
|
2007-03-08 18:19:42 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "shape_datasource::features");
|
|
|
|
#endif
|
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
filter_in_box filter(q.get_bbox());
|
|
|
|
if (indexed_)
|
|
|
|
{
|
2014-05-30 14:37:44 +00:00
|
|
|
std::unique_ptr<shape_io> shape_ptr = std::make_unique<shape_io>(shape_name_);
|
2010-09-02 20:20:42 +00:00
|
|
|
return featureset_ptr
|
|
|
|
(new shape_index_featureset<filter_in_box>(filter,
|
2014-05-30 14:37:44 +00:00
|
|
|
std::move(shape_ptr),
|
2010-09-02 20:20:42 +00:00
|
|
|
q.property_names(),
|
2011-08-05 21:20:21 +00:00
|
|
|
desc_.get_encoding(),
|
2011-08-26 07:02:36 +00:00
|
|
|
shape_name_,
|
|
|
|
row_limit_));
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-20 13:00:11 +00:00
|
|
|
return std::make_shared<shape_featureset<filter_in_box> >(filter,
|
2014-05-30 14:37:44 +00:00
|
|
|
shape_name_,
|
|
|
|
q.property_names(),
|
|
|
|
desc_.get_encoding(),
|
|
|
|
file_length_,
|
|
|
|
row_limit_);
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 13:12:10 +00:00
|
|
|
featureset_ptr shape_datasource::features_at_point(coord2d const& pt, double tol) const
|
2007-03-08 18:19:42 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "shape_datasource::features_at_point");
|
|
|
|
#endif
|
|
|
|
|
2012-12-11 22:46:52 +00:00
|
|
|
filter_at_point filter(pt,tol);
|
2010-09-02 20:20:42 +00:00
|
|
|
// collect all attribute names
|
|
|
|
std::vector<attribute_descriptor> const& desc_vector = desc_.get_descriptors();
|
|
|
|
std::vector<attribute_descriptor>::const_iterator itr = desc_vector.begin();
|
|
|
|
std::vector<attribute_descriptor>::const_iterator end = desc_vector.end();
|
|
|
|
std::set<std::string> names;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
while (itr != end)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2010-09-02 20:20:42 +00:00
|
|
|
names.insert(itr->get_name());
|
|
|
|
++itr;
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-09-02 20:20:42 +00:00
|
|
|
if (indexed_)
|
|
|
|
{
|
2014-05-30 14:37:44 +00:00
|
|
|
std::unique_ptr<shape_io> shape_ptr = std::make_unique<shape_io>(shape_name_);
|
2010-09-02 20:20:42 +00:00
|
|
|
return featureset_ptr
|
|
|
|
(new shape_index_featureset<filter_at_point>(filter,
|
2014-05-30 14:37:44 +00:00
|
|
|
std::move(shape_ptr),
|
2010-09-02 20:20:42 +00:00
|
|
|
names,
|
2011-08-05 21:20:21 +00:00
|
|
|
desc_.get_encoding(),
|
2011-08-26 07:02:36 +00:00
|
|
|
shape_name_,
|
|
|
|
row_limit_));
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-20 13:00:11 +00:00
|
|
|
return std::make_shared<shape_featureset<filter_at_point> >(filter,
|
2014-05-30 14:37:44 +00:00
|
|
|
shape_name_,
|
|
|
|
names,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
file_length_,
|
|
|
|
row_limit_);
|
2010-09-02 20:20:42 +00:00
|
|
|
}
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
box2d<double> shape_datasource::envelope() const
|
2007-03-08 18:19:42 +00:00
|
|
|
{
|
2010-09-02 20:20:42 +00:00
|
|
|
return extent_;
|
2007-03-08 18:19:42 +00:00
|
|
|
}
|
2012-01-17 06:09:46 +00:00
|
|
|
|
2015-03-24 11:13:31 +00:00
|
|
|
boost::optional<mapnik::datasource_geometry_t> shape_datasource::get_geometry_type() const
|
2012-01-17 06:09:46 +00:00
|
|
|
{
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "shape_datasource::get_geometry_type");
|
|
|
|
#endif
|
|
|
|
|
2015-03-24 11:13:31 +00:00
|
|
|
boost::optional<mapnik::datasource_geometry_t> result;
|
2012-01-17 06:09:46 +00:00
|
|
|
switch (shape_type_)
|
|
|
|
{
|
2012-02-02 01:37:35 +00:00
|
|
|
case shape_io::shape_point:
|
|
|
|
case shape_io::shape_pointm:
|
|
|
|
case shape_io::shape_pointz:
|
|
|
|
case shape_io::shape_multipoint:
|
|
|
|
case shape_io::shape_multipointm:
|
|
|
|
case shape_io::shape_multipointz:
|
|
|
|
{
|
2015-03-24 11:13:31 +00:00
|
|
|
result.reset(mapnik::datasource_geometry_t::Point);
|
2012-02-02 01:37:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case shape_io::shape_polyline:
|
|
|
|
case shape_io::shape_polylinem:
|
|
|
|
case shape_io::shape_polylinez:
|
|
|
|
{
|
2015-03-24 11:13:31 +00:00
|
|
|
result.reset(mapnik::datasource_geometry_t::LineString);
|
2012-02-02 01:37:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case shape_io::shape_polygon:
|
|
|
|
case shape_io::shape_polygonm:
|
|
|
|
case shape_io::shape_polygonz:
|
|
|
|
{
|
2015-03-24 11:13:31 +00:00
|
|
|
result.reset(mapnik::datasource_geometry_t::Polygon);
|
2012-02-02 01:37:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2012-01-17 06:09:46 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|