mapnik/src/wkb.cpp

485 lines
14 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
2012-02-02 02:53:35 +01: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
*
2021-01-05 15:39:07 +01:00
* Copyright (C) 2021 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
2012-04-08 02:20:56 +02:00
// mapnik
#include <mapnik/debug.hpp>
#include <mapnik/global.hpp>
#include <mapnik/wkb.hpp>
2019-05-07 12:19:37 +02:00
#include <mapnik/geometry.hpp>
#include <mapnik/util/noncopyable.hpp>
2017-03-27 17:14:51 +02:00
#include <mapnik/geometry/correct.hpp>
#include <memory>
namespace mapnik {
2012-07-25 18:31:57 +02:00
struct wkb_reader : util::noncopyable
2010-06-02 13:03:30 +02:00
{
private:
2010-06-02 13:03:30 +02:00
const char* wkb_;
2013-09-19 05:31:59 +02:00
std::size_t size_;
std::size_t pos_;
2010-06-02 13:03:30 +02:00
wkbByteOrder byteOrder_;
bool needSwap_;
wkbFormat format_;
public:
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
enum wkbGeometryType {
wkbPoint = 1,
wkbLineString = 2,
wkbPolygon = 3,
wkbMultiPoint = 4,
wkbMultiLineString = 5,
wkbMultiPolygon = 6,
wkbGeometryCollection = 7,
// Z
wkbPointZ = 1001,
wkbLineStringZ = 1002,
wkbPolygonZ = 1003,
wkbMultiPointZ = 1004,
wkbMultiLineStringZ = 1005,
wkbMultiPolygonZ = 1006,
wkbGeometryCollectionZ = 1007,
// M
wkbPointM = 2001,
wkbLineStringM = 2002,
wkbPolygonM = 2003,
wkbMultiPointM = 2004,
wkbMultiLineStringM = 2005,
wkbMultiPolygonM = 2006,
wkbGeometryCollectionM = 2007,
// ZM
wkbPointZM = 3001,
wkbLineStringZM = 3002,
wkbPolygonZM = 3003,
wkbMultiPointZM = 3004,
wkbMultiLineStringZM = 3005,
wkbMultiPolygonZM = 3006,
wkbGeometryCollectionZM = 3007
2015-02-19 16:04:14 +01:00
};
2013-09-19 05:35:14 +02:00
wkb_reader(const char* wkb, std::size_t size, wkbFormat format)
: wkb_(wkb)
, size_(size)
, pos_(0)
, format_(format)
2010-06-02 13:03:30 +02:00
{
// try to determine WKB format automatically
if (format_ == wkbAuto)
{
if (size_ >= 44 && static_cast<unsigned char>(wkb_[0]) == static_cast<unsigned char>(0x00) &&
static_cast<unsigned char>(wkb_[38]) == static_cast<unsigned char>(0x7C) &&
static_cast<unsigned char>(wkb_[size_ - 1]) == static_cast<unsigned char>(0xFE))
2011-10-23 02:25:09 +02:00
{
format_ = wkbSpatiaLite;
}
else
{
format_ = wkbGeneric;
}
}
2010-06-02 13:03:30 +02:00
switch (format_)
{
case wkbSpatiaLite:
byteOrder_ = static_cast<wkbByteOrder>(wkb_[1]);
pos_ = 39;
break;
case wkbGeneric:
default:
byteOrder_ = static_cast<wkbByteOrder>(wkb_[0]);
pos_ = 1;
break;
2010-06-02 13:03:30 +02:00
}
needSwap_ = byteOrder_ ? wkbXDR : wkbNDR;
2010-06-02 13:03:30 +02:00
}
2012-02-02 02:53:35 +01:00
2015-04-09 22:22:51 +02:00
mapnik::geometry::geometry<double> read()
2010-06-02 13:03:30 +02:00
{
2017-03-27 17:14:51 +02:00
mapnik::geometry::geometry<double> geom = mapnik::geometry::geometry_empty();
int type = read_integer();
2010-06-02 13:03:30 +02:00
switch (type)
{
case wkbPoint: {
auto pt = read_point();
if (!std::isnan(pt.x) && !std::isnan(pt.y))
geom = std::move(pt);
break;
}
case wkbLineString:
geom = read_linestring();
break;
case wkbPolygon:
geom = read_polygon();
break;
case wkbMultiPoint:
geom = read_multipoint();
break;
case wkbMultiLineString:
geom = read_multilinestring();
break;
case wkbMultiPolygon:
geom = read_multipolygon();
break;
case wkbGeometryCollection:
geom = read_collection();
break;
case wkbPointZ:
case wkbPointM: {
auto pt = read_point<true>();
if (!std::isnan(pt.x) && !std::isnan(pt.y))
geom = std::move(pt);
break;
}
case wkbPointZM: {
auto pt = read_point<true, true>();
if (!std::isnan(pt.x) && !std::isnan(pt.y))
geom = std::move(pt);
break;
}
case wkbLineStringZ:
case wkbLineStringM:
geom = read_linestring<true>();
break;
case wkbLineStringZM:
geom = read_linestring<true, true>();
break;
case wkbPolygonZ:
case wkbPolygonM:
geom = read_polygon<true>();
break;
case wkbPolygonZM:
geom = read_polygon<true, true>();
break;
case wkbMultiPointZ:
case wkbMultiPointM:
geom = read_multipoint<true>();
break;
case wkbMultiPointZM:
geom = read_multipoint<true, true>();
break;
case wkbMultiLineStringZ:
case wkbMultiLineStringM:
geom = read_multilinestring<true>();
break;
case wkbMultiLineStringZM:
geom = read_multilinestring<true, true>();
break;
case wkbMultiPolygonZ:
case wkbMultiPolygonM:
geom = read_multipolygon<true>();
break;
case wkbMultiPolygonZM:
geom = read_multipolygon<true, true>();
break;
case wkbGeometryCollectionZ:
case wkbGeometryCollectionM:
case wkbGeometryCollectionZM:
geom = read_collection();
break;
default:
break;
}
return geom;
2010-06-02 13:03:30 +02:00
}
2012-02-02 02:53:35 +01:00
private:
2012-02-02 02:53:35 +01:00
int read_integer()
2010-06-02 13:03:30 +02:00
{
2014-05-06 19:06:47 +02:00
std::int32_t n;
2010-06-02 13:03:30 +02:00
if (needSwap_)
{
read_int32_xdr(wkb_ + pos_, n);
2012-02-02 02:53:35 +01:00
}
else
2010-06-02 13:03:30 +02:00
{
read_int32_ndr(wkb_ + pos_, n);
2010-06-02 13:03:30 +02:00
}
pos_ += 4;
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
return n;
}
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
double read_double()
{
double d;
if (needSwap_)
{
read_double_xdr(wkb_ + pos_, d);
}
2012-02-02 02:53:35 +01:00
else
{
2010-06-02 13:03:30 +02:00
read_double_ndr(wkb_ + pos_, d);
}
pos_ += 8;
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
return d;
}
2012-02-02 02:53:35 +01:00
template<typename Ring, bool Z = false, bool M = false>
void read_coords(Ring& ring, std::size_t num_points)
2010-06-02 13:03:30 +02:00
{
double x, y;
if (!needSwap_)
2010-06-02 13:03:30 +02:00
{
for (std::size_t i = 0; i < num_points; ++i)
{
read_double_ndr(wkb_ + pos_, x);
read_double_ndr(wkb_ + pos_ + 8, y);
ring.emplace_back(x, y);
pos_ += 16; // skip XY
if (Z)
pos_ += 8;
if (M)
pos_ += 8;
}
}
2012-02-02 02:53:35 +01:00
else
{
for (std::size_t i = 0; i < num_points; ++i)
{
read_double_xdr(wkb_ + pos_, x);
read_double_xdr(wkb_ + pos_ + 8, y);
ring.emplace_back(x, y);
pos_ += 16; // skip XY
if (Z)
pos_ += 8;
if (M)
pos_ += 8;
}
2010-06-02 13:03:30 +02:00
}
}
template<bool Z = false, bool M = false>
2015-04-09 22:22:51 +02:00
mapnik::geometry::point<double> read_point()
2010-06-02 13:03:30 +02:00
{
double x = read_double();
double y = read_double();
if (Z)
pos_ += 8;
if (M)
pos_ += 8;
2015-04-09 22:22:51 +02:00
return mapnik::geometry::point<double>(x, y);
}
template<bool Z = false, bool M = false>
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_point<double> read_multipoint()
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_point<double> multi_point;
int num_points = read_integer();
2015-02-19 16:04:14 +01:00
multi_point.reserve(num_points);
for (int i = 0; i < num_points; ++i)
{
pos_ += 5;
multi_point.emplace_back(read_point<Z, M>());
}
2015-02-19 16:04:14 +01:00
return multi_point;
2010-06-02 13:03:30 +02:00
}
2012-02-02 02:53:35 +01:00
template<bool M = false, bool Z = false>
2015-04-09 22:22:51 +02:00
mapnik::geometry::line_string<double> read_linestring()
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::line_string<double> line;
int num_points = read_integer();
if (num_points > 0)
{
2015-02-19 16:04:14 +01:00
line.reserve(num_points);
2015-04-09 22:22:51 +02:00
read_coords<mapnik::geometry::line_string<double>, M, Z>(line, num_points);
}
2015-02-19 16:04:14 +01:00
return line;
}
template<bool M = false, bool Z = false>
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_line_string<double> read_multilinestring()
{
int num_lines = read_integer();
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_line_string<double> multi_line;
2015-02-19 16:04:14 +01:00
multi_line.reserve(num_lines);
for (int i = 0; i < num_lines; ++i)
{
pos_ += 5;
multi_line.push_back(read_linestring<M, Z>());
}
2015-02-19 16:04:14 +01:00
return multi_line;
2010-06-02 13:03:30 +02:00
}
2012-02-02 02:53:35 +01:00
template<bool M = false, bool Z = false>
2015-04-09 22:22:51 +02:00
mapnik::geometry::polygon<double> read_polygon()
{
int num_rings = read_integer();
2015-04-09 22:22:51 +02:00
mapnik::geometry::polygon<double> poly;
poly.reserve(num_rings);
2015-02-19 16:04:14 +01:00
for (int i = 0; i < num_rings; ++i)
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::linear_ring<double> ring;
2015-02-19 16:04:14 +01:00
int num_points = read_integer();
if (num_points > 0)
{
2015-02-19 16:04:14 +01:00
ring.reserve(num_points);
2015-04-09 22:22:51 +02:00
read_coords<mapnik::geometry::linear_ring<double>, M, Z>(ring, num_points);
}
poly.push_back(std::move(ring));
}
2015-02-19 16:04:14 +01:00
return poly;
}
template<bool M = false, bool Z = false>
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_polygon<double> read_multipolygon()
{
int num_polys = read_integer();
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_polygon<double> multi_poly;
multi_poly.reserve(num_polys);
for (int i = 0; i < num_polys; ++i)
{
pos_ += 5;
multi_poly.push_back(read_polygon<M, Z>());
2010-11-18 22:52:20 +01:00
}
2015-02-19 16:04:14 +01:00
return multi_poly;
2010-11-18 22:52:20 +01:00
}
2012-02-02 02:53:35 +01:00
2015-04-09 22:22:51 +02:00
mapnik::geometry::geometry_collection<double> read_collection()
{
int num_geometries = read_integer();
2015-04-09 22:22:51 +02:00
mapnik::geometry::geometry_collection<double> collection;
collection.reserve(num_geometries);
for (int i = 0; i < num_geometries; ++i)
{
pos_ += 1; // skip byte order
collection.push_back(read());
}
return collection;
}
std::string wkb_geometry_type_string(int type)
{
std::stringstream s;
switch (type)
{
case wkbPoint:
s << "Point";
break;
case wkbPointZ:
s << "PointZ";
break;
case wkbPointM:
s << "PointM";
break;
case wkbPointZM:
s << "PointZM";
break;
case wkbMultiPoint:
s << "MultiPoint";
break;
case wkbMultiPointZ:
s << "MultiPointZ";
break;
case wkbMultiPointM:
s << "MultiPointM";
break;
case wkbMultiPointZM:
s << "MultiPointZM";
break;
case wkbLineString:
s << "LineString";
break;
case wkbLineStringZ:
s << "LineStringZ";
break;
case wkbLineStringM:
s << "LineStringM";
break;
case wkbLineStringZM:
s << "LineStringZM";
break;
case wkbMultiLineString:
s << "MultiLineString";
break;
case wkbMultiLineStringZ:
s << "MultiLineStringZ";
break;
case wkbMultiLineStringM:
s << "MultiLineStringM";
break;
case wkbMultiLineStringZM:
s << "MultiLineStringZM";
break;
case wkbPolygon:
s << "Polygon";
break;
case wkbPolygonZ:
s << "PolygonZ";
break;
case wkbPolygonM:
s << "PolygonM";
break;
case wkbPolygonZM:
s << "PolygonZM";
break;
case wkbMultiPolygon:
s << "MultiPolygon";
break;
case wkbMultiPolygonZ:
s << "MultiPolygonZ";
break;
case wkbMultiPolygonM:
s << "MultiPolygonM";
break;
case wkbMultiPolygonZM:
s << "MultiPolygonZM";
break;
case wkbGeometryCollection:
s << "GeometryCollection";
break;
case wkbGeometryCollectionZ:
s << "GeometryCollectionZ";
break;
case wkbGeometryCollectionM:
s << "GeometryCollectionM";
break;
case wkbGeometryCollectionZM:
s << "GeometryCollectionZM";
break;
default:
s << "wkbUnknown(" << type << ")";
break;
}
return s.str();
}
2010-11-18 22:52:20 +01:00
};
mapnik::geometry::geometry<double> geometry_utils::from_wkb(const char* wkb, std::size_t size, wkbFormat format)
2010-06-02 13:03:30 +02:00
{
wkb_reader reader(wkb, size, format);
2015-04-09 22:22:51 +02:00
mapnik::geometry::geometry<double> geom(reader.read());
// note: this will only be applied to polygons
mapnik::geometry::correct(geom);
2015-03-31 21:30:05 +02:00
return geom;
2012-02-02 02:53:35 +01:00
}
2015-02-19 16:04:14 +01:00
} // namespace mapnik