mapnik/plugins/input/shape/shape_io.cpp

214 lines
6 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
*
2006-03-31 12:32:02 +02:00
* This file is part of Mapnik (c++ mapping toolkit)
2005-06-14 17:06:59 +02:00
*
2014-11-20 15:25:50 +01:00
* Copyright (C) 2014 Artem Pavlenko
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +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,
2005-06-14 17:06:59 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2006-03-31 12:32:02 +02: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 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
*****************************************************************************/
2005-06-14 17:06:59 +02:00
#include "shape_io.hpp"
2011-05-10 17:03:09 +02:00
// mapnik
2012-04-08 02:20:56 +02:00
#include <mapnik/debug.hpp>
#include <mapnik/make_unique.hpp>
2011-05-10 17:03:09 +02:00
#include <mapnik/datasource.hpp>
#include <mapnik/geom_util.hpp>
2011-05-10 17:03:09 +02:00
// boost
2005-06-14 17:06:59 +02:00
using mapnik::datasource_exception;
using mapnik::geometry_type;
using mapnik::hit_test_first;
2005-06-14 17:06:59 +02:00
const std::string shape_io::SHP = ".shp";
const std::string shape_io::DBF = ".dbf";
const std::string shape_io::INDEX = ".index";
2005-06-14 17:06:59 +02:00
2012-09-03 19:27:48 +02:00
shape_io::shape_io(std::string const& shape_name, bool open_index)
: type_(shape_null),
shp_(shape_name + SHP),
dbf_(shape_name + DBF),
reclength_(0),
id_(0)
2005-06-14 17:06:59 +02:00
{
2010-09-06 21:20:59 +02:00
bool ok = (shp_.is_open() && dbf_.is_open());
if (! ok)
{
throw datasource_exception("Shape Plugin: cannot read shape file '" + shape_name + "'");
2010-09-06 21:20:59 +02:00
}
if (open_index)
2010-09-06 21:20:59 +02:00
{
try
{
index_ = std::make_unique<shape_file>(shape_name + INDEX);
}
catch (...)
{
MAPNIK_LOG_WARN(shape) << "shape_io: Could not open index=" << shape_name << INDEX;
}
2010-09-06 21:20:59 +02:00
}
2005-06-14 17:06:59 +02:00
}
shape_io::~shape_io() {}
2005-06-14 17:06:59 +02:00
void shape_io::move_to(std::streampos pos)
2005-06-14 17:06:59 +02:00
{
shp_.seek(pos);
id_ = shp_.read_xdr_integer();
reclength_ = shp_.read_xdr_integer();
2005-06-14 17:06:59 +02:00
}
shape_file& shape_io::shp()
{
return shp_;
2005-06-14 17:06:59 +02:00
}
dbf_file& shape_io::dbf()
{
return dbf_;
2005-06-14 17:06:59 +02:00
}
void shape_io::read_bbox(shape_file::record_type & record, mapnik::box2d<double> & bbox)
{
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 15:59:22 +01:00
mapnik::new_geometry::geometry shape_io::read_polyline(shape_file::record_type & record)
{
int num_parts = record.read_ndr_integer();
int num_points = record.read_ndr_integer();
if (num_parts == 1)
{
mapnik::new_geometry::line_string line;
record.skip(4);
for (int i = 0; i < num_points; ++i)
{
double x = record.read_double();
double y = record.read_double();
line.add_coord(x, y);
}
return std::move(mapnik::new_geometry::geometry(std::move(line)));
}
else
{
std::vector<int> parts(num_parts);
for (int i = 0; i < num_parts; ++i)
{
parts[i] = record.read_ndr_integer();
}
int start, end;
mapnik::new_geometry::multi_line_string multi_line;
for (int k = 0; k < num_parts; ++k)
{
start = parts[k];
if (k == num_parts - 1)
{
end = num_points;
}
else
{
end = parts[k + 1];
}
mapnik::new_geometry::line_string line;
for (int j = start; j < end; ++j)
{
double x = record.read_double();
double y = record.read_double();
line.add_coord(x, y);
}
multi_line.push_back(std::move(line));
}
return std::move(mapnik::new_geometry::geometry(std::move(multi_line)));
}
2005-06-14 17:06:59 +02:00
}
template <typename T>
bool is_clockwise(T const& points, int start, int end)
{
int num_points = end - start;
double area = 0.0;
for (int i = 0; i < num_points; ++i)
{
auto const& p0 = points[start + i];
auto const& p1 = points[start + (i + 1) % num_points];
area += std::get<0>(p0) * std::get<1>(p1) - std::get<1>(p0) * std::get<0>(p1);
}
return ( area < 0.0) ? true : false;
}
mapnik::new_geometry::geometry shape_io::read_polygon(shape_file::record_type & record)
2005-06-14 17:06:59 +02:00
{
// FIXME
return std::move(mapnik::new_geometry::geometry(mapnik::new_geometry::polygon3()));
#if 0
int num_parts = record.read_ndr_integer();
int num_points = record.read_ndr_integer();
std::vector<int> parts(num_parts);
2012-03-13 15:59:22 +01:00
using points_cont = std::vector<std::tuple<double,double> >;
points_cont points;
points.reserve(num_points);
for (int i = 0; i < num_parts; ++i)
{
parts[i] = record.read_ndr_integer();
}
for (int k = 0; k < num_points; ++k)
{
double x = record.read_double();
double y = record.read_double();
points.emplace_back(x,y);
}
//std::unique_ptr<geometry_type> poly(new geometry_type(mapnik::geometry_type::types::Polygon));
mapnik::new_geometry::polygon3 poly;
2012-07-26 12:58:48 +02:00
for (int k = 0; k < num_parts; ++k)
{
int start = parts[k];
int end;
if (k == num_parts - 1) end = num_points;
else end = parts[k + 1];
auto const& pt = points[start];
double x = std::get<0>(pt);
double y = std::get<1>(pt);
if ( k > 0 && is_clockwise(points, start, end))
{
geom.push_back(poly.release());
poly.reset(new geometry_type(mapnik::geometry_type::types::Polygon));
}
poly->move_to(x, y);
for (int j = start + 1; j < end; ++j)
{
auto const& pt = points[j];
x = std::get<0>(pt);
y = std::get<1>(pt);
poly->line_to(x, y);
}
2013-03-01 17:06:44 +01:00
poly->close_path();
}
geom.push_back(poly.release());
#endif
2005-06-14 17:06:59 +02:00
}