Changeset 1205 for trunk/plugins/input/shape/shapefile.hpp
- Timestamp:
- 07/03/09 09:29:50 (13 months ago)
- Files:
-
- 1 modified
-
trunk/plugins/input/shape/shapefile.hpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/plugins/input/shape/shapefile.hpp
r1171 r1205 26 26 #define SHAPEFILE_HPP 27 27 28 #include <mapnik/global.hpp> 28 29 #include <mapnik/envelope.hpp> 29 30 // boost 30 31 #include <boost/utility.hpp> 31 #include <boost/ detail/endian.hpp>32 #include <boost/cstdint.hpp> 32 33 #include <boost/iostreams/stream.hpp> 34 33 35 #include <boost/iostreams/device/file.hpp> 34 36 #include <boost/iostreams/device/mapped_file.hpp> 37 //#include <boost/iostreams/device/file_descriptor.hpp> 35 38 36 39 #include <cstring> 37 40 38 41 using mapnik::Envelope; 42 using mapnik::read_int_ndr; 43 using mapnik::read_int_xdr; 44 using mapnik::read_double_ndr; 45 using mapnik::read_double_xdr; 39 46 40 47 struct shape_record 41 48 { 42 const char* data; 43 size_t size; 44 mutable size_t pos; 45 explicit shape_record(size_t size) 46 : size(size), 47 pos(0) {} 49 const char* data; 50 size_t size; 51 mutable size_t pos; 52 explicit shape_record(size_t size) 53 : //data(static_cast<char*>(::operator new(sizeof(char)*size))), 54 size(size), 55 pos(0) {} 48 56 49 void set_data(const char * data_) 50 { 51 data = data_; 52 } 57 void set_data(const char * data_) 58 { 59 data = data_; 60 } 61 62 void skip(unsigned n) 63 { 64 pos+=n; 65 } 53 66 54 void skip(unsigned n) 55 { 56 pos+=n; 57 } 67 int read_ndr_integer() 68 { 69 int val; 70 read_int_ndr(&data[pos],val); 71 pos+=4; 72 return val; 73 } 58 74 59 int read_ndr_integer() 60 { 61 int val=(data[pos] & 0xff) | 62 (data[pos+1] & 0xff) << 8 | 63 (data[pos+2] & 0xff) << 16 | 64 (data[pos+3] & 0xff) << 24; 65 pos+=4; 66 return val; 67 } 75 int read_xdr_integer() 76 { 77 int val; 78 read_int_xdr(&data[pos],val); 79 pos+=4; 80 return val; 81 } 68 82 69 int read_xdr_integer() 70 { 71 int val=(data[pos] & 0xff) << 24 | 72 (data[pos+1] & 0xff) << 16 | 73 (data[pos+2] & 0xff) << 8 | 74 (data[pos+3] & 0xff); 75 pos+=4; 76 return val; 77 } 78 79 double read_double() 80 { 81 double val; 82 #ifndef BOOST_BIG_ENDIAN 83 std::memcpy(&val,&data[pos],8); 84 #else 85 long long bits = ((long long)data[pos] & 0xff) | 86 ((long long)data[pos+1] & 0xff) << 8 | 87 ((long long)data[pos+2] & 0xff) << 16 | 88 ((long long)data[pos+3] & 0xff) << 24 | 89 ((long long)data[pos+4] & 0xff) << 32 | 90 ((long long)data[pos+5] & 0xff) << 40 | 91 ((long long)data[pos+6] & 0xff) << 48 | 92 ((long long)data[pos+7] & 0xff) << 56 ; 93 std::memcpy(&val,&bits,8); 94 #endif 95 pos+=8; 96 return val; 97 } 98 long remains() 99 { 100 return (size-pos); 101 } 102 ~shape_record() {} 83 double read_double() 84 { 85 double val; 86 read_double_ndr(&data[pos],val); 87 pos+=8; 88 return val; 89 } 90 long remains() 91 { 92 return (size-pos); 93 } 94 95 ~shape_record() 96 { 97 //::operator delete(data); 98 } 103 99 104 100 }; … … 108 104 class shape_file : boost::noncopyable 109 105 { 110 stream<mapped_file_source> file_; 111 public: 112 shape_file(); 113 shape_file(const std::string& file_name); 114 ~shape_file(); 115 bool is_open(); 116 void close(); 106 stream<mapped_file_source> file_; 107 //stream<file_source> file_; 108 public: 109 shape_file(); 110 shape_file(std::string const& file_name); 111 ~shape_file(); 112 bool is_open(); 113 void close(); 117 114 118 inline void read_record(shape_record& rec) 119 { 120 rec.set_data(file_->data() + file_.tellg()); 121 file_.seekg(rec.size,std::ios::cur); 122 } 115 inline void read_record(shape_record& rec) 116 { 117 rec.set_data(file_->data() + file_.tellg()); 118 file_.seekg(rec.size,std::ios::cur); 119 } 120 121 inline int read_xdr_integer() 122 { 123 char b[4]; 124 file_.read(b, 4); 125 int val; 126 read_int_xdr(b,val); 127 return val; 128 } 123 129 124 inline int read_xdr_integer() 125 { 126 char b[4]; 127 file_.read(b, 4); 128 return (b[3] & 0xffu) | ((b[2] & 0xffu) << 8) | 129 ((b[1] & 0xffu) << 16) | ((b[0] & 0xffu) << 24); 130 } 130 inline int read_ndr_integer() 131 { 132 char b[4]; 133 file_.read(b,4); 134 int val; 135 read_int_ndr(b,val); 136 return val; 137 } 131 138 132 inline int read_ndr_integer() 133 { 134 char b[4]; 135 file_.read(b,4); 136 return (b[0]&0xffu) | ((b[1]&0xffu) << 8) | 137 ((b[2]&0xffu) << 16) | ((b[3]&0xffu) << 24); 138 } 139 inline double read_double() 140 { 141 double val; 142 #ifndef WORDS_BIGENDIAN 143 file_.read(reinterpret_cast<char*>(&val),8); 144 #else 145 char b[8]; 146 file_.read(b,8); 147 read_double_ndr(b,val); 148 #endif 149 return val; 150 } 151 152 inline void read_envelope(Envelope<double>& envelope) 153 { 154 #ifndef WORDS_BIGENDIAN 155 file_.read(reinterpret_cast<char*>(&envelope),sizeof(envelope)); 156 #else 157 char data[4*8]; 158 file_.read(data,4*8); 159 double minx,miny,maxx,maxy; 160 read_double_ndr(data,minx); 161 read_double_ndr(data,miny); 162 read_double_ndr(data,maxx); 163 read_double_ndr(data,maxy); 164 envelope.init(minx,miny,maxx,maxy); 165 #endif 166 } 139 167 140 inline double read_double() 141 { 142 double val; 143 #ifndef WORDS_BIGENDIAN 144 file_.read(reinterpret_cast<char*>(&val),8); 145 #else 146 char b[8]; 147 file_.read(b,8); 148 long long bits = ((long long)b[0] & 0xff) | 149 ((long long)b[1] & 0xff) << 8 | 150 ((long long)b[2] & 0xff) << 16 | 151 ((long long)b[3] & 0xff) << 24 | 152 ((long long)b[4] & 0xff) << 32 | 153 ((long long)b[5] & 0xff) << 40 | 154 ((long long)b[6] & 0xff) << 48 | 155 ((long long)b[7] & 0xff) << 56 ; 156 memcpy(&val,&bits,8); 157 #endif 158 return val; 159 } 168 inline void skip(std::streampos bytes) 169 { 170 file_.seekg(bytes,std::ios::cur); 171 } 160 172 161 inline void read_envelope(Envelope<double>& envelope) 162 { 163 #ifndef WORDS_BIGENDIAN 164 file_.read(reinterpret_cast<char*>(&envelope),sizeof(envelope)); 165 #else 166 double minx=read_double(); 167 double miny=read_double(); 168 double maxx=read_double(); 169 double maxy=read_double(); 170 envelope.init(minx,miny,maxx,maxy); 171 #endif 172 } 173 inline void rewind() 174 { 175 seek(100); 176 } 173 177 174 inline void skip(std::streampos bytes)175 {176 file_.seekg(bytes,std::ios::cur);177 }178 inline void seek(std::streampos pos) 179 { 180 file_.seekg(pos,std::ios::beg); 181 } 178 182 179 inline void rewind()180 {181 seek(100);182 }183 inline std::streampos pos() 184 { 185 return file_.tellg(); 186 } 183 187 184 inline void seek(std::streampos pos) 185 { 186 file_.seekg(pos,std::ios::beg); 187 } 188 189 inline std::streampos pos() 190 { 191 return file_.tellg(); 192 } 193 194 inline bool is_eof() 195 { 196 return file_.eof(); 197 } 188 inline bool is_eof() 189 { 190 return file_.eof(); 191 } 198 192 }; 199 193
