2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2015-06-16 10:49:16 +00:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +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,
|
2005-06-14 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00:00
|
|
|
* 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
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2005-06-16 20:56:31 +00:00
|
|
|
#include "shape_io.hpp"
|
2011-05-10 15:03:09 +00:00
|
|
|
|
2011-10-22 13:27:28 +00:00
|
|
|
// mapnik
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2014-06-12 14:14:05 +00:00
|
|
|
#include <mapnik/make_unique.hpp>
|
2011-05-10 15:03:09 +00:00
|
|
|
#include <mapnik/datasource.hpp>
|
2015-03-31 11:40:20 +00:00
|
|
|
#include <mapnik/util/is_clockwise.hpp>
|
2015-05-20 21:00:30 +00:00
|
|
|
#include <mapnik/geometry_correct.hpp>
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::datasource_exception;
|
2005-06-14 15:06:59 +00:00
|
|
|
const std::string shape_io::SHP = ".shp";
|
2015-10-14 15:44:43 +00:00
|
|
|
const std::string shape_io::SHX = ".shx";
|
2005-06-14 15:06:59 +00:00
|
|
|
const std::string shape_io::DBF = ".dbf";
|
2010-09-02 20:20:42 +00:00
|
|
|
const std::string shape_io::INDEX = ".index";
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2016-02-18 16:37:46 +00:00
|
|
|
shape_io::shape_io(std::string const& shape_name, bool open_index)
|
2011-11-14 03:37:50 +00:00
|
|
|
: type_(shape_null),
|
2016-02-18 16:37:46 +00:00
|
|
|
shp_(shape_name + SHP),
|
|
|
|
shx_(shape_name + SHX),
|
|
|
|
dbf_(shape_name + DBF),
|
2011-11-14 03:37:50 +00:00
|
|
|
reclength_(0),
|
|
|
|
id_(0)
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2010-09-06 19:20:59 +00:00
|
|
|
bool ok = (shp_.is_open() && dbf_.is_open());
|
2015-10-14 15:44:43 +00:00
|
|
|
if (!ok)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2010-09-14 15:43:38 +00:00
|
|
|
throw datasource_exception("Shape Plugin: cannot read shape file '" + shape_name + "'");
|
2010-09-06 19:20:59 +00:00
|
|
|
}
|
2011-10-22 13:27:28 +00:00
|
|
|
|
2010-12-10 01:25:43 +00:00
|
|
|
if (open_index)
|
2010-09-06 19:20:59 +00:00
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
try
|
2010-11-15 03:16:23 +00:00
|
|
|
{
|
2014-05-30 14:37:44 +00:00
|
|
|
index_ = std::make_unique<shape_file>(shape_name + INDEX);
|
2010-12-10 01:25:43 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_WARN(shape) << "shape_io: Could not open index=" << shape_name << INDEX;
|
2010-11-15 03:16:23 +00:00
|
|
|
}
|
2010-09-06 19:20:59 +00:00
|
|
|
}
|
2015-10-14 15:44:43 +00:00
|
|
|
if (!index_ && !shx_.is_open())
|
|
|
|
{
|
|
|
|
throw datasource_exception("Shape Plugin: cannot read shape index file '" + shape_name + ".shx'");
|
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 13:02:31 +00:00
|
|
|
shape_io::~shape_io() {}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2012-12-11 09:37:53 +00:00
|
|
|
void shape_io::move_to(std::streampos pos)
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2011-10-22 13:27:28 +00:00
|
|
|
shp_.seek(pos);
|
|
|
|
id_ = shp_.read_xdr_integer();
|
|
|
|
reclength_ = shp_.read_xdr_integer();
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shape_file& shape_io::shp()
|
|
|
|
{
|
2011-10-22 13:27:28 +00:00
|
|
|
return shp_;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 15:44:43 +00:00
|
|
|
shape_file& shape_io::shx()
|
|
|
|
{
|
|
|
|
return shx_;
|
|
|
|
}
|
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
dbf_file& shape_io::dbf()
|
|
|
|
{
|
2011-10-22 13:27:28 +00:00
|
|
|
return dbf_;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 09:37:53 +00:00
|
|
|
void shape_io::read_bbox(shape_file::record_type & record, mapnik::box2d<double> & bbox)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2012-12-11 09:37:53 +00:00
|
|
|
double lox = record.read_double();
|
|
|
|
double loy = record.read_double();
|
|
|
|
double hix = record.read_double();
|
|
|
|
double hiy = record.read_double();
|
|
|
|
bbox.init(lox, loy, hix, hiy);
|
|
|
|
}
|
2012-03-13 14:59:22 +00:00
|
|
|
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::geometry<double> shape_io::read_polyline(shape_file::record_type & record)
|
2012-12-11 09:37:53 +00:00
|
|
|
{
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::geometry<double> geom; // default empty
|
2011-11-14 03:37:50 +00:00
|
|
|
int num_parts = record.read_ndr_integer();
|
|
|
|
int num_points = record.read_ndr_integer();
|
2015-02-18 12:21:16 +00:00
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
if (num_parts == 1)
|
|
|
|
{
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::line_string<double> line;
|
2015-02-18 12:48:05 +00:00
|
|
|
line.reserve(num_points);
|
2011-11-14 03:37:50 +00:00
|
|
|
record.skip(4);
|
2015-02-18 12:21:16 +00:00
|
|
|
for (int i = 0; i < num_points; ++i)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2015-02-18 12:21:16 +00:00
|
|
|
double x = record.read_double();
|
|
|
|
double y = record.read_double();
|
|
|
|
line.add_coord(x, y);
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
2015-04-01 13:04:05 +00:00
|
|
|
geom = std::move(line);
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-02 16:50:10 +00:00
|
|
|
std::vector<int> parts;
|
|
|
|
parts.resize(num_parts);
|
|
|
|
std::for_each(parts.begin(), parts.end(), [&](int & part) { part = record.read_ndr_integer();});
|
2011-11-14 03:37:50 +00:00
|
|
|
int start, end;
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::multi_line_string<double> multi_line;
|
2016-01-04 11:43:27 +00:00
|
|
|
multi_line.reserve(num_parts);
|
2011-11-14 03:37:50 +00:00
|
|
|
for (int k = 0; k < num_parts; ++k)
|
|
|
|
{
|
2011-10-22 13:27:28 +00:00
|
|
|
start = parts[k];
|
|
|
|
if (k == num_parts - 1)
|
|
|
|
{
|
|
|
|
end = num_points;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end = parts[k + 1];
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::line_string<double> line;
|
2015-02-18 12:48:05 +00:00
|
|
|
line.reserve(end - start);
|
2015-02-18 12:21:16 +00:00
|
|
|
for (int j = start; j < end; ++j)
|
2011-10-22 13:27:28 +00:00
|
|
|
{
|
2015-02-18 12:21:16 +00:00
|
|
|
double x = record.read_double();
|
|
|
|
double y = record.read_double();
|
|
|
|
line.add_coord(x, y);
|
2011-10-22 13:27:28 +00:00
|
|
|
}
|
2015-02-18 12:21:16 +00:00
|
|
|
multi_line.push_back(std::move(line));
|
2011-10-22 13:27:28 +00:00
|
|
|
}
|
2015-04-01 13:04:05 +00:00
|
|
|
geom = std::move(multi_line);
|
2011-10-22 13:27:28 +00:00
|
|
|
}
|
2015-04-01 13:04:05 +00:00
|
|
|
return geom;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:43:27 +00:00
|
|
|
mapnik::geometry::geometry<double> shape_io::read_polyline_parts(shape_file::record_type & record, std::vector<std::pair<int, int>> const& parts)
|
|
|
|
{
|
|
|
|
mapnik::geometry::geometry<double> geom; // default empty
|
|
|
|
int total_num_parts = record.read_ndr_integer();
|
|
|
|
int num_parts = parts.size();
|
|
|
|
mapnik::geometry::multi_line_string<double> multi_line;
|
|
|
|
multi_line.reserve(num_parts);
|
|
|
|
for (int k = 0; k < num_parts; ++k)
|
|
|
|
{
|
|
|
|
int start = parts[k].first;
|
|
|
|
int end = parts[k].second;
|
|
|
|
unsigned pos = 4 + 32 + 8 + 4 * total_num_parts + start * 16;
|
|
|
|
record.set_pos(pos);
|
|
|
|
|
|
|
|
mapnik::geometry::line_string<double> line;
|
|
|
|
line.reserve(end - start);
|
|
|
|
for (int j = start; j < end; ++j)
|
|
|
|
{
|
|
|
|
double x = record.read_double();
|
|
|
|
double y = record.read_double();
|
|
|
|
line.emplace_back(x, y);
|
|
|
|
}
|
|
|
|
multi_line.push_back(std::move(line));
|
|
|
|
}
|
|
|
|
geom = std::move(multi_line);
|
|
|
|
return geom;
|
|
|
|
}
|
|
|
|
|
2014-12-18 10:23:38 +00:00
|
|
|
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::geometry<double> shape_io::read_polygon(shape_file::record_type & record)
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::geometry<double> geom; // default empty
|
2011-10-22 13:27:28 +00:00
|
|
|
int num_parts = record.read_ndr_integer();
|
|
|
|
int num_points = record.read_ndr_integer();
|
2012-03-13 14:59:22 +00:00
|
|
|
|
2015-06-02 13:24:19 +00:00
|
|
|
std::vector<int> parts;
|
|
|
|
parts.resize(num_parts);
|
|
|
|
std::for_each(parts.begin(), parts.end(), [&](int & part) { part = record.read_ndr_integer();});
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::polygon<double> poly;
|
2015-06-02 13:24:19 +00:00
|
|
|
mapnik::geometry::multi_polygon<double> multi_poly;
|
2012-07-26 10:58:48 +00:00
|
|
|
for (int k = 0; k < num_parts; ++k)
|
2011-10-22 13:27:28 +00:00
|
|
|
{
|
|
|
|
int start = parts[k];
|
|
|
|
int end;
|
2014-12-18 10:23:38 +00:00
|
|
|
if (k == num_parts - 1) end = num_points;
|
|
|
|
else end = parts[k + 1];
|
2015-06-02 13:24:19 +00:00
|
|
|
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
2015-02-18 17:12:47 +00:00
|
|
|
ring.reserve(end - start);
|
|
|
|
for (int j = start; j < end; ++j)
|
2014-02-24 15:20:06 +00:00
|
|
|
{
|
2015-02-18 17:12:47 +00:00
|
|
|
double x = record.read_double();
|
|
|
|
double y = record.read_double();
|
|
|
|
ring.emplace_back(x, y);
|
2014-02-24 15:20:06 +00:00
|
|
|
}
|
2015-02-18 17:12:47 +00:00
|
|
|
if (k == 0)
|
2011-10-22 13:27:28 +00:00
|
|
|
{
|
2015-02-18 17:12:47 +00:00
|
|
|
poly.set_exterior_ring(std::move(ring));
|
2011-10-22 13:27:28 +00:00
|
|
|
}
|
2015-03-31 11:40:20 +00:00
|
|
|
else if (mapnik::util::is_clockwise(ring))
|
2015-02-18 17:12:47 +00:00
|
|
|
{
|
|
|
|
multi_poly.emplace_back(std::move(poly));
|
|
|
|
poly.interior_rings.clear();
|
|
|
|
poly.set_exterior_ring(std::move(ring));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
poly.add_hole(std::move(ring));
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 13:24:19 +00:00
|
|
|
|
2015-02-19 13:32:45 +00:00
|
|
|
if (multi_poly.size() > 0) // multi
|
2015-02-18 17:12:47 +00:00
|
|
|
{
|
2015-02-19 13:32:45 +00:00
|
|
|
multi_poly.emplace_back(std::move(poly));
|
2015-04-01 13:04:05 +00:00
|
|
|
geom = std::move(multi_poly);
|
2015-06-02 13:24:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-14 13:35:32 +00:00
|
|
|
geom = std::move(poly);
|
2011-10-22 13:27:28 +00:00
|
|
|
}
|
2015-05-20 21:00:30 +00:00
|
|
|
mapnik::geometry::correct(geom);
|
2015-04-01 13:04:05 +00:00
|
|
|
return geom;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2016-01-04 11:43:27 +00:00
|
|
|
|
|
|
|
mapnik::geometry::geometry<double> shape_io::read_polygon_parts(shape_file::record_type & record, std::vector<std::pair<int,int>> const& parts)
|
|
|
|
{
|
|
|
|
mapnik::geometry::geometry<double> geom; // default empty
|
|
|
|
int total_num_parts = record.read_ndr_integer();
|
|
|
|
mapnik::geometry::polygon<double> poly;
|
|
|
|
mapnik::geometry::multi_polygon<double> multi_poly;
|
|
|
|
int num_parts = parts.size();
|
|
|
|
for (int k = 0; k < num_parts; ++k)
|
|
|
|
{
|
|
|
|
int start = parts[k].first;
|
|
|
|
int end = parts[k].second;
|
|
|
|
unsigned pos = 4 + 32 + 8 + 4 * total_num_parts + start * 16;
|
|
|
|
record.set_pos(pos);
|
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
|
|
|
ring.reserve(end - start);
|
|
|
|
for (int j = start; j < end; ++j)
|
|
|
|
{
|
|
|
|
double x = record.read_double();
|
|
|
|
double y = record.read_double();
|
|
|
|
ring.emplace_back(x, y);
|
|
|
|
}
|
|
|
|
if (k == 0)
|
|
|
|
{
|
|
|
|
poly.set_exterior_ring(std::move(ring));
|
|
|
|
}
|
|
|
|
else if (mapnik::util::is_clockwise(ring))
|
|
|
|
{
|
|
|
|
multi_poly.emplace_back(std::move(poly));
|
|
|
|
poly.interior_rings.clear();
|
|
|
|
poly.set_exterior_ring(std::move(ring));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
poly.add_hole(std::move(ring));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multi_poly.size() > 0) // multi
|
|
|
|
{
|
|
|
|
multi_poly.emplace_back(std::move(poly));
|
|
|
|
geom = std::move(multi_poly);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
geom = std::move(poly);
|
|
|
|
}
|
|
|
|
mapnik::geometry::correct(geom);
|
|
|
|
return geom;
|
|
|
|
}
|