mapnik/plugins/input/shape/shapefile.hpp

202 lines
5 KiB
C++
Raw Normal View History

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 $
#ifndef SHAPEFILE_HPP
#define SHAPEFILE_HPP
2005-06-14 17:06:59 +02:00
#include <fstream>
#include <mapnik/envelope.hpp>
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)
: data(static_cast<char*>(::operator new(sizeof(char)*size))),
size(size),
pos(0) {}
2005-06-14 17:06:59 +02:00
char* rawdata()
{
return &data[0];
2005-06-14 17:06:59 +02:00
}
2005-06-14 17:06:59 +02:00
void skip(unsigned n)
{
pos+=n;
2005-06-14 17:06:59 +02:00
}
2005-06-14 17:06:59 +02:00
int read_ndr_integer()
{
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()
{
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()
{
double val;
2005-06-14 17:06:59 +02:00
#ifndef WORDS_BIGENDIAN
std::memcpy(&val,&data[pos],8);
2005-06-14 17:06:59 +02:00
#else
long long bits = ((long long)data[pos] & 0xff) |
((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
pos+=8;
return val;
2005-06-14 17:06:59 +02:00
}
long remains()
{
return (size-pos);
2005-06-14 17:06:59 +02:00
}
~shape_record()
{
::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
{
file_.read(rec.rawdata(),rec.size);
2005-06-14 17:06:59 +02:00
}
inline int read_xdr_integer()
{
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()
{
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()
{
double val;
2005-06-14 17:06:59 +02:00
#ifndef WORDS_BIGENDIAN
file_.read(reinterpret_cast<char*>(&val),8);
2005-06-14 17:06:59 +02:00
#else
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
return val;
2005-06-14 17:06:59 +02:00
}
inline void read_envelope(Envelope<double>& envelope)
{
#ifndef WORDS_BIGENDIAN
file_.read(reinterpret_cast<char*>(&envelope),sizeof(envelope));
2005-06-14 17:06:59 +02:00
#else
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)
{
file_.seekg(bytes,std::ios::cur);
2005-06-14 17:06:59 +02:00
}
inline void rewind()
{
seek(100);
2005-06-14 17:06:59 +02:00
}
inline void seek(std::streampos pos)
{
file_.seekg(pos,std::ios::beg);
2005-06-14 17:06:59 +02:00
}
inline std::streampos pos()
{
return file_.tellg();
2005-06-14 17:06:59 +02:00
}
inline bool is_eof()
{
return file_.eof();
2005-06-14 17:06:59 +02:00
}
private:
shape_file(const shape_file&);
shape_file& operator=(const shape_file&);
};
#endif //SHAPEFILE_HPP