-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapefile.cpp
More file actions
371 lines (317 loc) · 8.75 KB
/
shapefile.cpp
File metadata and controls
371 lines (317 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*!
* *****************************************************************
* G3DTShapefile
* *****************************************************************
* \file shapefile.cpp
*
* \author M. Koren, milan.koren3@gmail.com
* Source: https:\\github.com/milan-koren/G3DTShapefile
* Licence: EUPL v. 1.2
* https://joinup.ec.europa.eu/collection/eupl
* *****************************************************************
*/
#include <QtEndian>
#include "shapefile.h"
/*!
* \brief Default constructor.
*/
ShapeFile::ShapeFile()
{
}
/*!
* \brief Virtual destructor.
*/
ShapeFile::~ShapeFile()
{
close();
}
/*!
* \brief Closes open files (shp, shx, dbf) and releases allocated memory.
*/
void ShapeFile::close()
{
this->shpFile.close();
this->shxFile.close();
if (this->shx != nullptr)
{
delete [] this->shx;
this->shx = nullptr;
}
this->dbf.close();
}
/*!
* \brief Opens shape-file in RW mode and reads shx records into memory.
* \param fileName Shape-file full path.
* \return True, if all files were sucessfully open.
*/
bool ShapeFile::open(QString fileName)
{
close();
this->shpFile.setFileName(changeFileExtension(fileName, SHP_SHPFILE_EXT));
if (this->shpFile.open(QFile::ReadWrite))
{
this->shpFile.read(reinterpret_cast<char*>(&this->shpFileHeader), sizeof(ShpFileHeader));
toLittleEndian(&this->shpFileHeader);
this->dbf.open(changeFileExtension(fileName, DBF_FILE_EXT));
return loadShx(fileName);
}
return false;
}
/*!
* \brief Checks if shape-file is open.
* \return If shape-file is open returns true;
*/
inline bool ShapeFile::isOpen()
{
return this->dbf.isOpen() && this->shpFile.isOpen() && this->shxFile.isOpen();
}
/*!
* \brief Number of all records (valid and deleted) in associated DBF.
* \return Number of records.
*/
inline qint64 ShapeFile::countAllRecords()
{
return this->dbf.countAllRecords();
}
/*!
* \brief Number of deleted records in associated DBF.
* \return Number of deleted records.
*/
inline qint64 ShapeFile::countDeletedRecords()
{
return this->dbf.countDeletedRecords();
}
/*!
* \brief Number of valid records in associated DBF.
* \return Number of valid records.
*/
inline qint64 ShapeFile::countValidRecords()
{
return this->dbf.countValidRecords();
}
/*!
* \brief Number of columns in associated DBF.
* \return Number of DBF columns.
*/
inline qint32 ShapeFile::countColumns()
{
return this->dbf.countColumns();
}
/*!
* \brief Returns the type of stored shapes. All shapes in file should be of the same type.
* \return Type of stored shapes.
*/
inline int ShapeFile::getShapeType()
{
if (this->shpFile.isOpen()) return this->shpFileHeader.shapeType;
return SHP_NONE;
}
/*!
* \brief Checks if a given record is valid.
* \param recordIndex The index of record.
* \return If record is valid returns true, false otherwise.
*/
inline bool ShapeFile::isValid(qint64 recordIndex)
{
return this->dbf.isValid(recordIndex);
}
/*!
* \brief Checks if a given record is deleted.
* \param recordIndex The index of record.
* \return If record is valid returns false, true otherwise.
*/
inline bool ShapeFile::isDeleted(qint64 recordIndex)
{
return this->dbf.isDeleted(recordIndex);
}
/*!
* \brief Creates shape object of shape-file type.
* \return Pointer to created shape, or nullptr.
*/
ShpShape *ShapeFile::createShape()
{
ShpShape *shp = nullptr;
if (isOpen())
{
switch (getShapeType())
{
case SHP_POINT:
shp = new ShpPoint();
break;
case SHP_POINTM:
shp = new ShpPointM();
break;
case SHP_POINTZ:
shp = new ShpPointZ();
break;
case SHP_MULTIPOINT:
shp = new ShpMultiPoint();
break;
case SHP_MULTIPOINTM:
shp = new ShpMultiPointM();
break;
case SHP_MULTIPOINTZ:
shp = new ShpMultiPointZ();
break;
case SHP_POLYLINE:
shp = new ShpPolyLine();
break;
case SHP_POLYLINEM:
shp = new ShpPolyLineM();
break;
case SHP_POLYLINEZ:
shp = new ShpPolyLineZ();
break;
case SHP_POLYGON:
shp = new ShpPolygon();
break;
case SHP_POLYGONM:
shp = new ShpPolygonM();
break;
case SHP_POLYGONZ:
shp = new ShpPolygonZ();
break;
case SHP_MULTIPATCH:
shp = new ShpMultiPatch();
break;
default:
shp = nullptr;
}
}
return shp;
}
/*!
* \brief Releases allocated memory and sets the pointer to nullptr.
* \param shp Reference to a shape pointer.
*/
void ShapeFile::releaseShape(ShpShape **shp)
{
if (*shp != nullptr)
{
delete *shp;
*shp = nullptr;
}
}
/*!
* \brief Reads shape from a shape-file to an existing object. The type of object have to correspond shape-file type.
* \param recordIndex Index of shape in input file.
* \param shp Reference of object to be loaded with data.
* \return True, if shape was loaded successfuly.
*/
bool ShapeFile::readShape(qint64 recordIndex, ShpShape &shp)
{
if (0 <= recordIndex && recordIndex < countAllRecords())
{
if (shp.load(this->shpFile, this->shx[recordIndex].offset)) return true;
}
shp.destroy();
return false;
}
/*!
* \brief Returns the list of column names in DBF.
* \return List of DBF column names.
*/
inline QStringList ShapeFile::getColumnNames()
{
return this->dbf.getColumnNames();
}
/*!
* \brief Returns the list of text column names in DBF.
* \return List of DBF text column names.
*/
inline QStringList ShapeFile::getTextColumnNames()
{
return this->dbf.getTextColumnNames();
}
/*!
* \brief Changes file extension.
* \param fileName Original file name, including full path and extension.
* \param newExtension New extension, including dot.
* \return File name with changed extension.
*/
QString ShapeFile::changeFileExtension(QString fileName, QString newExtension)
{
qint32 i;
i = qint32(fileName.lastIndexOf("."));
if (0 < i)
{
fileName.truncate(i);
return fileName + newExtension;
}
return fileName;
}
/*!
* \brief Swaps shape-file header to little-endiands.
* \param shpFileHeader Shape-file header.
*/
void ShapeFile::toLittleEndian(ShpFileHeader *shpFileHeader)
{
shpFileHeader->fileCode = qFromBigEndian(shpFileHeader->fileCode);
shpFileHeader->fileLength = qFromBigEndian(shpFileHeader->fileLength);
for(qint32 i=0; i<5; i++)
shpFileHeader->unused1[i]= qFromBigEndian(shpFileHeader->unused1[i]);
}
/*!
* \brief Swaps shp-record header to little-endiands.
* \param shpFileHeader Shp-record header.
*/
void ShapeFile::toLittleEndian(ShpRecordHeader *shpRecordHeader)
{
shpRecordHeader->number = qFromBigEndian(shpRecordHeader->number);
shpRecordHeader->length = qFromBigEndian(shpRecordHeader->length);
}
/*!
* \brief Swaps shx-record header to little-endiands.
* \param shpFileHeader Shx-record header.
*/
void ShapeFile::toLittleEndian(ShxRecord *shxRecord)
{
shxRecord->offset = qFromBigEndian(shxRecord->offset);
shxRecord->length = qFromBigEndian(shxRecord->length);
}
/*!
* \brief Swaps all SHX records in memory into little-endians.
*/
void ShapeFile::shxToLittleEndian()
{
if (this->shx != nullptr)
{
for(qint64 iRecord=0, nRecords = countAllRecords(); iRecord < nRecords; iRecord++)
toLittleEndian(this->shx + iRecord);
}
}
/*!
* \brief Loads SHX file into memory.
* \param fileName File name. Procedure sets correct file extension.
* \return True if SHX records were sucessufully loaded.
* \remark Input file must closed and shx deleted before procedure call.
*/
bool ShapeFile::loadShx(QString fileName)
{
qint64 nRecords;
this->shxFile.setFileName(changeFileExtension(fileName, SHP_SHXFILE_EXT));
if (this->shxFile.open(QFile::ReadWrite))
{
this->shxFile.read(reinterpret_cast<char*>(&this->shxFileHeader), sizeof(ShpFileHeader));
toLittleEndian(&this->shxFileHeader);
nRecords = countAllRecords();
this->shx = new ShxRecord[quint64(nRecords)];
this->shxFile.read(reinterpret_cast<char*>(this->shx), nRecords * qint64(sizeof(ShxRecord)));
shxToLittleEndian();
return true;
}
return false;
}
/*!
* \brief Private copy constructor.
*/
ShapeFile::ShapeFile(ShapeFile &)
{
}
/*!
* \brief Private copy assignment operator.
*/
ShapeFile &ShapeFile::operator=(ShapeFile &)
{
return *this;
}