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: shapefile.hh 33 2005-04-04 13:01:03Z pavlenko $
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#ifndef SHAPEFILE_HPP
|
|
|
|
#define SHAPEFILE_HPP
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/envelope.hpp>
|
2005-06-17 14:40:51 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
using namespace mapnik;
|
|
|
|
|
|
|
|
struct shape_record
|
|
|
|
{
|
|
|
|
char* data;
|
|
|
|
size_t size;
|
|
|
|
size_t pos;
|
|
|
|
explicit shape_record(size_t size)
|
2006-10-04 13:22:18 +02:00
|
|
|
: data(static_cast<char*>(::operator new(sizeof(char)*size))),
|
|
|
|
size(size),
|
|
|
|
pos(0) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
char* rawdata()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
return &data[0];
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
void skip(unsigned n)
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
pos+=n;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
int read_ndr_integer()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int val=(data[pos] & 0xff) |
|
|
|
|
(data[pos+1] & 0xff) << 8 |
|
|
|
|
(data[pos+2] & 0xff) << 16 |
|
|
|
|
(data[pos+3] & 0xff) << 24;
|
|
|
|
pos+=4;
|
|
|
|
return val;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int read_xdr_integer()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
int val=(data[pos] & 0xff) << 24 |
|
|
|
|
(data[pos+1] & 0xff) << 16 |
|
|
|
|
(data[pos+2] & 0xff) << 8 |
|
|
|
|
(data[pos+3] & 0xff);
|
|
|
|
pos+=4;
|
|
|
|
return val;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double read_double()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
double val;
|
2005-06-14 17:06:59 +02:00
|
|
|
#ifndef WORDS_BIGENDIAN
|
2006-10-04 13:22:18 +02:00
|
|
|
std::memcpy(&val,&data[pos],8);
|
2005-06-14 17:06:59 +02:00
|
|
|
#else
|
|
|
|
long long bits = ((long long)data[pos] & 0xff) |
|
2006-10-04 13:22:18 +02:00
|
|
|
((long long)data[pos+1] & 0xff) << 8 |
|
|
|
|
((long long)data[pos+2] & 0xff) << 16 |
|
|
|
|
((long long)data[pos+3] & 0xff) << 24 |
|
|
|
|
((long long)data[pos+4] & 0xff) << 32 |
|
|
|
|
((long long)data[pos+5] & 0xff) << 40 |
|
|
|
|
((long long)data[pos+6] & 0xff) << 48 |
|
|
|
|
((long long)data[pos+7] & 0xff) << 56 ;
|
|
|
|
std::memcpy(&val,&bits,8);
|
2005-06-14 17:06:59 +02:00
|
|
|
#endif
|
2006-10-04 13:22:18 +02:00
|
|
|
pos+=8;
|
|
|
|
return val;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
long remains()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
return (size-pos);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
~shape_record()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
::operator delete(data);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class shape_file
|
|
|
|
{
|
|
|
|
std::ifstream file_;
|
|
|
|
//static const int buffer_size = 16;
|
|
|
|
//char buff_[buffer_size];
|
|
|
|
public:
|
|
|
|
shape_file();
|
|
|
|
shape_file(const std::string& file_name);
|
|
|
|
~shape_file();
|
|
|
|
bool open(const std::string& file_name);
|
|
|
|
bool is_open();
|
|
|
|
void close();
|
|
|
|
|
2005-11-30 00:31:22 +01:00
|
|
|
inline void read_record(shape_record& rec)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
file_.read(rec.rawdata(),rec.size);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int read_xdr_integer()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
char b[4];
|
|
|
|
file_.read(b, 4);
|
|
|
|
return b[3] & 0xffu | (b[2] & 0xffu) << 8 |
|
|
|
|
(b[1] & 0xffu) << 16 | (b[0] & 0xffu) << 24;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int read_ndr_integer()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
char b[4];
|
|
|
|
file_.read(b,4);
|
|
|
|
return b[0]&0xffu | (b[1]&0xffu) << 8 |
|
|
|
|
(b[2]&0xffu) << 16 | (b[3]&0xffu) << 24;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline double read_double()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
double val;
|
2005-06-14 17:06:59 +02:00
|
|
|
#ifndef WORDS_BIGENDIAN
|
2006-10-04 13:22:18 +02:00
|
|
|
file_.read(reinterpret_cast<char*>(&val),8);
|
2005-06-14 17:06:59 +02:00
|
|
|
#else
|
2006-10-04 13:22:18 +02:00
|
|
|
char b[8];
|
|
|
|
file_.read(b,8);
|
|
|
|
long long bits = ((long long)b[0] & 0xff) |
|
|
|
|
((long long)b[1] & 0xff) << 8 |
|
|
|
|
((long long)b[2] & 0xff) << 16 |
|
|
|
|
((long long)b[3] & 0xff) << 24 |
|
|
|
|
((long long)b[4] & 0xff) << 32 |
|
|
|
|
((long long)b[5] & 0xff) << 40 |
|
|
|
|
((long long)b[6] & 0xff) << 48 |
|
|
|
|
((long long)b[7] & 0xff) << 56 ;
|
|
|
|
memcpy(&val,&bits,8);
|
2005-06-14 17:06:59 +02:00
|
|
|
#endif
|
2006-10-04 13:22:18 +02:00
|
|
|
return val;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void read_envelope(Envelope<double>& envelope)
|
|
|
|
{
|
|
|
|
#ifndef WORDS_BIGENDIAN
|
2006-10-04 13:22:18 +02:00
|
|
|
file_.read(reinterpret_cast<char*>(&envelope),sizeof(envelope));
|
2005-06-14 17:06:59 +02:00
|
|
|
#else
|
2006-10-04 13:22:18 +02:00
|
|
|
double minx=read_double();
|
|
|
|
double miny=read_double();
|
|
|
|
double maxx=read_double();
|
|
|
|
double maxy=read_double();
|
|
|
|
envelope.init(minx,miny,maxx,maxy);
|
2005-06-14 17:06:59 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void skip(std::streampos bytes)
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
file_.seekg(bytes,std::ios::cur);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void rewind()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
seek(100);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void seek(std::streampos pos)
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
file_.seekg(pos,std::ios::beg);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::streampos pos()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
return file_.tellg();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_eof()
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
return file_.eof();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
shape_file(const shape_file&);
|
|
|
|
shape_file& operator=(const shape_file&);
|
|
|
|
};
|
2006-10-04 13:22:18 +02:00
|
|
|
|
|
|
|
#endif //SHAPEFILE_HPP
|