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
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 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
|
|
|
|
|
|
|
//$Id: wkb.cpp 19 2005-03-22 13:53:27Z pavlenko $
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/wkb.hpp>
|
2007-10-08 19:42:41 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/geom_util.hpp>
|
2007-09-16 13:23:51 +02:00
|
|
|
#include <mapnik/feature.hpp>
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-06-08 09:08:30 +02:00
|
|
|
#include <boost/detail/endian.hpp>
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
struct wkb_reader
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
enum wkbByteOrder {
|
2006-10-04 13:22:18 +02:00
|
|
|
wkbXDR=0,
|
|
|
|
wkbNDR=1
|
2007-09-16 13:23:51 +02:00
|
|
|
};
|
|
|
|
const char* wkb_;
|
|
|
|
unsigned size_;
|
|
|
|
unsigned pos_;
|
|
|
|
wkbByteOrder byteOrder_;
|
|
|
|
bool needSwap_;
|
2009-02-10 20:09:16 +01:00
|
|
|
wkbFormat format_;
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
public:
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
enum wkbGeometryType {
|
2006-10-04 13:22:18 +02:00
|
|
|
wkbPoint=1,
|
|
|
|
wkbLineString=2,
|
|
|
|
wkbPolygon=3,
|
|
|
|
wkbMultiPoint=4,
|
|
|
|
wkbMultiLineString=5,
|
|
|
|
wkbMultiPolygon=6,
|
|
|
|
wkbGeometryCollection=7
|
2007-09-16 13:23:51 +02:00
|
|
|
};
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-02-10 20:09:16 +01:00
|
|
|
wkb_reader(const char* wkb,unsigned size,wkbFormat format)
|
2006-10-04 13:22:18 +02:00
|
|
|
: wkb_(wkb),
|
|
|
|
size_(size),
|
|
|
|
pos_(0),
|
2009-02-10 20:09:16 +01:00
|
|
|
format_(format)
|
2007-09-16 13:23:51 +02:00
|
|
|
{
|
2009-02-10 20:09:16 +01:00
|
|
|
switch (format_)
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2009-02-23 16:00:25 +01:00
|
|
|
case wkbSpatiaLite:
|
2009-02-10 20:09:16 +01:00
|
|
|
byteOrder_ = (wkbByteOrder) wkb_[1];
|
|
|
|
pos_ = 39;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wkbGeneric:
|
|
|
|
default:
|
|
|
|
byteOrder_ = (wkbByteOrder) wkb_[0];
|
|
|
|
pos_ = 1;
|
|
|
|
break;
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
2009-06-08 09:08:30 +02:00
|
|
|
#ifndef BOOST_BIG_ENDIAN
|
2006-10-04 13:22:18 +02:00
|
|
|
needSwap_=byteOrder_?wkbXDR:wkbNDR;
|
2005-06-14 17:06:59 +02:00
|
|
|
#else
|
2006-10-04 13:22:18 +02:00
|
|
|
needSwap_=byteOrder_?wkbNDR:wkbXDR;
|
2005-06-14 17:06:59 +02:00
|
|
|
#endif
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
~wkb_reader() {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-11-02 13:50:15 +01:00
|
|
|
void read_multi(Feature & feature)
|
2007-09-16 13:23:51 +02:00
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int type=read_integer();
|
|
|
|
switch (type)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
case wkbPoint:
|
|
|
|
read_point(feature);
|
|
|
|
break;
|
|
|
|
case wkbLineString:
|
|
|
|
read_linestring(feature);
|
|
|
|
break;
|
|
|
|
case wkbPolygon:
|
|
|
|
read_polygon(feature);
|
|
|
|
break;
|
|
|
|
case wkbMultiPoint:
|
|
|
|
read_multipoint(feature);
|
|
|
|
break;
|
|
|
|
case wkbMultiLineString:
|
|
|
|
read_multilinestring(feature);
|
|
|
|
break;
|
|
|
|
case wkbMultiPolygon:
|
|
|
|
read_multipolygon(feature);
|
|
|
|
break;
|
|
|
|
case wkbGeometryCollection:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2007-11-02 13:50:15 +01:00
|
|
|
|
|
|
|
void read(Feature & feature)
|
|
|
|
{
|
|
|
|
int type=read_integer();
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case wkbPoint:
|
|
|
|
read_point(feature);
|
|
|
|
break;
|
|
|
|
case wkbLineString:
|
|
|
|
read_linestring(feature);
|
|
|
|
break;
|
|
|
|
case wkbPolygon:
|
|
|
|
read_polygon(feature);
|
|
|
|
break;
|
|
|
|
case wkbMultiPoint:
|
|
|
|
read_multipoint_2(feature);
|
|
|
|
break;
|
|
|
|
case wkbMultiLineString:
|
|
|
|
read_multilinestring_2(feature);
|
|
|
|
break;
|
|
|
|
case wkbMultiPolygon:
|
|
|
|
read_multipolygon_2(feature);
|
|
|
|
break;
|
|
|
|
case wkbGeometryCollection:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
wkb_reader(const wkb_reader&);
|
|
|
|
wkb_reader& operator=(const wkb_reader&);
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
int read_integer()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int n;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
if (!needSwap_)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
memcpy(&n,wkb_+pos_,4);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
const char* b=wkb_+pos_;
|
2008-07-29 20:06:15 +02:00
|
|
|
n = (b[3]&0xff) | ((b[2]&0xff)<<8) | ((b[1]&0xff)<<16) | ((b[0]&0xff)<<24);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
|
|
|
pos_+=4;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
return n;
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
double read_double()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
double d;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
if (!needSwap_)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
memcpy(&d,wkb_+pos_,8);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
// we rely on the fact that "long long" is in C standard,
|
|
|
|
// but not in C++ yet
|
|
|
|
// this is not quite portable
|
|
|
|
const char* b= wkb_+pos_;
|
2008-07-29 20:06:15 +02:00
|
|
|
long long n = ((long long)b[7]&0xff) |
|
|
|
|
(((long long)b[6]&0xff)<<8) |
|
|
|
|
(((long long)b[5]&0xff)<<16) |
|
|
|
|
(((long long)b[4]&0xff)<<24) |
|
|
|
|
(((long long)b[3]&0xff)<<32) |
|
|
|
|
(((long long)b[2]&0xff)<<40) |
|
|
|
|
(((long long)b[1]&0xff)<<48) |
|
|
|
|
(((long long)b[0]&0xff)<<56);
|
2007-09-16 13:23:51 +02:00
|
|
|
memcpy(&d,&n,8);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
|
|
|
pos_+=8;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
return d;
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
void read_coords(CoordinateArray& ar)
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int size=sizeof(coord<double,2>)*ar.size();
|
|
|
|
if (!needSwap_)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
std::memcpy(&ar[0],wkb_+pos_,size);
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
for (unsigned i=0;i<ar.size();++i)
|
|
|
|
{
|
|
|
|
ar[i].x=read_double();
|
|
|
|
ar[i].y=read_double();
|
|
|
|
}
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
|
|
|
pos_+=size;
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
void read_point(Feature & feature)
|
|
|
|
{
|
|
|
|
geometry2d * pt = new point<vertex2d>;
|
2006-10-04 13:22:18 +02:00
|
|
|
double x = read_double();
|
|
|
|
double y = read_double();
|
|
|
|
pt->move_to(x,y);
|
2007-09-16 13:23:51 +02:00
|
|
|
feature.add_geometry(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_multipoint(Feature & feature)
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int num_points = read_integer();
|
|
|
|
for (int i=0;i<num_points;++i)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
pos_+=5;
|
|
|
|
read_point(feature);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
|
|
|
|
2007-11-02 13:50:15 +01:00
|
|
|
void read_multipoint_2(Feature & feature)
|
|
|
|
{
|
|
|
|
geometry2d * pt = new point<vertex2d>;
|
|
|
|
int num_points = read_integer();
|
|
|
|
for (int i=0;i<num_points;++i)
|
|
|
|
{
|
|
|
|
pos_+=5;
|
|
|
|
double x = read_double();
|
|
|
|
double y = read_double();
|
|
|
|
pt->move_to(x,y);
|
|
|
|
}
|
|
|
|
feature.add_geometry(pt);
|
|
|
|
}
|
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
void read_linestring(Feature & feature)
|
|
|
|
{
|
|
|
|
geometry2d * line = new line_string<vertex2d>;
|
2006-10-04 13:22:18 +02:00
|
|
|
int num_points=read_integer();
|
|
|
|
CoordinateArray ar(num_points);
|
|
|
|
read_coords(ar);
|
|
|
|
line->set_capacity(num_points);
|
|
|
|
line->move_to(ar[0].x,ar[0].y);
|
|
|
|
for (int i=1;i<num_points;++i)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
line->line_to(ar[i].x,ar[i].y);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
feature.add_geometry(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_multilinestring(Feature & feature)
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int num_lines=read_integer();
|
|
|
|
for (int i=0;i<num_lines;++i)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
pos_+=5;
|
|
|
|
read_linestring(feature);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-11-02 13:50:15 +01:00
|
|
|
void read_multilinestring_2(Feature & feature)
|
|
|
|
{
|
|
|
|
geometry2d * line = new line_string<vertex2d>;
|
|
|
|
int num_lines=read_integer();
|
|
|
|
unsigned capacity = 0;
|
|
|
|
for (int i=0;i<num_lines;++i)
|
|
|
|
{
|
|
|
|
pos_+=5;
|
|
|
|
int num_points=read_integer();
|
|
|
|
capacity+=num_points;
|
|
|
|
CoordinateArray ar(num_points);
|
|
|
|
read_coords(ar);
|
|
|
|
line->set_capacity(capacity);
|
|
|
|
line->move_to(ar[0].x,ar[0].y);
|
|
|
|
for (int i=1;i<num_points;++i)
|
|
|
|
{
|
|
|
|
line->line_to(ar[i].x,ar[i].y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
feature.add_geometry(line);
|
|
|
|
}
|
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
void read_polygon(Feature & feature)
|
|
|
|
{
|
|
|
|
geometry2d * poly = new polygon<vertex2d>;
|
2006-10-04 13:22:18 +02:00
|
|
|
int num_rings=read_integer();
|
2007-11-02 13:50:15 +01:00
|
|
|
unsigned capacity = 0;
|
2006-10-04 13:22:18 +02:00
|
|
|
for (int i=0;i<num_rings;++i)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
int num_points=read_integer();
|
2007-11-02 13:50:15 +01:00
|
|
|
capacity+=num_points;
|
2007-09-16 13:23:51 +02:00
|
|
|
CoordinateArray ar(num_points);
|
|
|
|
read_coords(ar);
|
2007-11-02 13:50:15 +01:00
|
|
|
poly->set_capacity(capacity);
|
2007-09-16 13:23:51 +02:00
|
|
|
poly->move_to(ar[0].x,ar[0].y);
|
|
|
|
for (int j=1;j<num_points;++j)
|
|
|
|
{
|
|
|
|
poly->line_to(ar[j].x,ar[j].y);
|
|
|
|
}
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
feature.add_geometry(poly);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
void read_multipolygon(Feature & feature)
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int num_polys=read_integer();
|
|
|
|
for (int i=0;i<num_polys;++i)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
pos_+=5;
|
|
|
|
read_polygon(feature);
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2007-11-02 13:50:15 +01:00
|
|
|
|
|
|
|
void read_multipolygon_2(Feature & feature)
|
|
|
|
{
|
|
|
|
geometry2d * poly = new polygon<vertex2d>;
|
|
|
|
int num_polys=read_integer();
|
|
|
|
unsigned capacity = 0;
|
|
|
|
for (int i=0;i<num_polys;++i)
|
|
|
|
{
|
|
|
|
pos_+=5;
|
|
|
|
int num_rings=read_integer();
|
|
|
|
for (int i=0;i<num_rings;++i)
|
|
|
|
{
|
|
|
|
int num_points=read_integer();
|
|
|
|
capacity += num_points;
|
|
|
|
CoordinateArray ar(num_points);
|
|
|
|
read_coords(ar);
|
|
|
|
poly->set_capacity(capacity);
|
|
|
|
poly->move_to(ar[0].x,ar[0].y);
|
|
|
|
|
|
|
|
for (int j=1;j<num_points;++j)
|
|
|
|
{
|
|
|
|
poly->line_to(ar[j].x,ar[j].y);
|
|
|
|
}
|
|
|
|
poly->line_to(ar[0].x,ar[0].y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
feature.add_geometry(poly);
|
|
|
|
}
|
2007-09-16 13:23:51 +02:00
|
|
|
};
|
|
|
|
|
2009-02-10 20:09:16 +01:00
|
|
|
void geometry_utils::from_wkb (Feature & feature,
|
|
|
|
const char* wkb,
|
|
|
|
unsigned size,
|
|
|
|
bool multiple_geometries,
|
|
|
|
wkbFormat format)
|
2007-09-16 13:23:51 +02:00
|
|
|
{
|
2009-02-10 20:09:16 +01:00
|
|
|
wkb_reader reader(wkb,size,format);
|
2007-11-02 13:50:15 +01:00
|
|
|
if (multiple_geometries)
|
|
|
|
return reader.read_multi(feature);
|
|
|
|
else
|
|
|
|
return reader.read(feature);
|
2007-09-16 13:23:51 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|