-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathShapeData.cpp
More file actions
380 lines (340 loc) · 11.5 KB
/
Copy pathShapeData.cpp
File metadata and controls
380 lines (340 loc) · 11.5 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
372
373
374
375
376
377
378
379
380
/**************************************************************************************
* File name: ShapeData.h
*
* Project: MapWindow Open Source (MapWinGis ActiveX control)
* Description: Declaration of CShapeData. Used for holding shape data in fast non-editabe mode.
*
**************************************************************************************
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at http://www.mozilla.org/mpl/
* See the License for the specific language governing rights and limitations
* under the License.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**************************************************************************************
* Contributor(s):
* (Open source contributors should list themselves and their modifications here). */
// Sergei Leschinski (lsu) 30 jan 2011 - created the file
#include "stdafx.h"
#include "ShapeData.h"
#include "ErrorCodes.h"
#pragma region ShapefileMemo
// Record header
// ---------------------------------------------------------
// Position Value Type Number Byte Order
// ---------------------------------------------------------
// Byte 0 Record Number Integer 1 Big
// Byte 4 Content Length Integer 1 Big
// Null Shape
// ---------------------------------------------------------
// Position Value Type Number Byte Order
// ---------------------------------------------------------
// Byte 0 Shape Type Integer 1 Little
// Points
// ---------------------------------------------------------
// Position Value Type Number Byte Order
// ---------------------------------------------------------
// Byte 0 Shape Type Integer 1 Little
// Byte 4 X Double 1 Little
// Byte 12 Y Double 1 Little
// MultiPoints
// ---------------------------------------------------------
// Position Value Type Number Byte Order
// ---------------------------------------------------------
// Byte 0 Shape Type Integer 1 Little
// Byte 4 Box Double 4 Little
// Byte 36 NumPoints Integer 1 Little
// Byte 40 Points Point NumPoints Little
// Byte X Zmin Double 1 Little
// Byte X + 8 Zmax Double 1 Little
// Byte X + 16 Zarray Double NumPoints Little
// Byte Z* Mmin Double 1 Little
// Byte Z+8* Mmax Double 1 Little
// Byte Z+16* Marray Double NumPoints Little
// X = 40 + (16 * NumPoints); Y = X + 16 + (8 * NumPoints)
// Polygons, Polylines
// ---------------------------------------------------------
// Position Value Type Number Byte Order
// ---------------------------------------------------------
// Byte 0 Shape Type Integer 1 Little
// Byte 4 Box Double 4 Little
// Byte 36 NumParts Integer 1 Little
// Byte 40 NumPoints Integer 1 Little
// Byte 44 Parts Integer NumParts Little
// Byte X Points Point NumPoints Little
// Byte Y Zmin Double 1 Little
// Byte Y + 8 Zmax Double 1 Little
// Byte Y + 16 Zarray Double NumPoints Little
// Byte Z* Mmin Double 1 Little
// Byte Z+8* Mmax Double 1 Little
// Byte Z+16* Marray Double NumPoints Little
// X = 44 + (4 * NumParts), Y= X + (16 * NumPoints), Z = Y + 16 + (8 * NumPoints)
// * optional
#pragma endregion
// *****************************************************
// get_PointXY
// *****************************************************
bool CShapeData::get_PointXY(int PointIndex, double& x, double& y)
{
if( PointIndex < 0 || PointIndex >= (int)_pointCount )
{
_lastErrorCode = tkINDEX_OUT_OF_BOUNDS;
return false;
}
else
{
x = _points[PointIndex * 2];
y = _points[PointIndex * 2 + 1];
return true;
}
}
// ********************************************************
// get_XYFast
// ********************************************************
void CShapeData::get_XYFast(int PointIndex, double& x, double& y)
{
x = _points[PointIndex * 2];
y = _points[PointIndex * 2 + 1];
}
// *****************************************************
// get_PointsXY
// *****************************************************
double* CShapeData::get_PointsXY()
{
if (_pointCount > 0)
{
return (double*)_points;
}
else
{
return NULL;
}
}
// **************************************************
// get_PartStartPoint()
// ******************************************************
int CShapeData::get_PartStartPoint(int PartIndex)
{
if( PartIndex < 0 || PartIndex >= (int)_partCount)
{
_lastErrorCode = tkINDEX_OUT_OF_BOUNDS;
return -1;
}
else
{
return _parts[PartIndex];
}
}
// ********************************************************
// get_PartEndPoint()
// ********************************************************
int CShapeData::get_PartEndPoint(int PartIndex)
{
if( PartIndex < 0 || PartIndex >= (int)_partCount)
{
_lastErrorCode = tkINDEX_OUT_OF_BOUNDS;
return -1;
}
else
{
if (PartIndex == _partCount - 1)
{
return _pointCount - 1;
}
else
{
return _parts[PartIndex + 1] - 1;
}
}
}
// ******************************************************
// get_XYBounds()
// ******************************************************
bool CShapeData::get_BoundsXY(double& xMin, double& xMax, double& yMin, double& yMax)
{
if (_bounds)
{
xMin = _bounds[0];
yMin = _bounds[1];
xMax = _bounds[2];
yMax = _bounds[3];
return true;
}
else if (_shapeType == SHP_POINT && _points)
{
// it's far more effective to check type externally to avoid duplicating values
xMin = _points[0];
xMax = _points[0];
yMin = _points[1];
yMax = _points[1];
return true;
}
else
{
// we shall not be here normally
ASSERT(FALSE);
return false;
}
}
// ******************************************************
// Clear()
// ******************************************************
void CShapeData::Clear()
{
_pointCount = 0;
_partCount = 0;
if ( _parts )
{
delete[] _parts;
_parts = NULL;
}
if ( _points )
{
delete[] _points;
_points = NULL;
}
if (_bounds)
{
delete[] _bounds;
_bounds = NULL;
}
}
#pragma region ShapeData
// ******************************************************
// put_ShapeData
// ******************************************************
// Passing the shape data from disk to the memory structures.
bool CShapeData::put_RawData(char* shapeData, int recordLength)
{
// type
_shapeType = (ShpfileType)*(int*)shapeData;
_shapeType = ShapeTypeConvert2D(_shapeType);
this->Clear();
if (_shapeType == SHP_NULLSHAPE)
{
// do nothing;
}
else if( _shapeType == SHP_POINT)
{
_pointCount = 1;
double* ddata = (double*)(shapeData + 4);
_points = new double[2];
memcpy(_points, ddata, sizeof(double) * 2);
}
else if( _shapeType == SHP_MULTIPOINT)
{
double* bounds = (double*)(shapeData + 4);
_bounds = new double[4];
memcpy(_bounds, bounds, sizeof(double) * 4); // 36
_pointCount = *(int*)(shapeData + 36);
_points = new double[_pointCount * 2];
double* points = (double*)(shapeData + 40);
memcpy(_points, points, sizeof(double) * _pointCount * 2);
}
else if( _shapeType == SHP_POLYLINE || _shapeType == SHP_POLYGON)
{
double* bounds = (double*)(shapeData + 4);
_bounds = new double[4];
memcpy(_bounds, bounds, sizeof(double) * 4); // 36
_partCount = *(int*)(shapeData + 36);
_pointCount = *(int*)(shapeData + 40);
// parts
int* parts = (int*)(shapeData + 44);
if (_partCount > 0)
{
_parts = new int[_partCount];
memcpy(_parts, parts, sizeof(int) * _partCount);
}
// points
double* points = (double*)(shapeData + 44 + sizeof(int) * _partCount);
if (_pointCount > 0)
{
_points = new double[_pointCount * 2];
memcpy(_points, points, sizeof(double) * _pointCount * 2);
}
}
return true;
}
#pragma endregion
#pragma region Utilities
// ********************************************************
// PointInRing()
// ********************************************************
bool CShapeData::PointInRing( int partIndex, double pointX, double pointY )
{
ShpfileType shptype = this->get_shapeType();
if( shptype != SHP_POLYGON &&
shptype != SHP_POLYGONZ &&
shptype != SHP_POLYGONM )
{
return false;
}
double xMin, xMax, yMin, yMax;
this->get_BoundsXY(xMin, xMax, yMin, yMax);
if (pointX < xMin || pointY < yMin || pointX > xMax || pointY > yMax)
{
return false;
}
int begin = this->get_PartStartPoint(partIndex);
int end = this->get_PartEndPoint(partIndex);
int CrossCount = 0;
for(int nPoint = begin; nPoint <= end; nPoint++)
{
double* points= this->get_PointsXY();
double x1 = points[nPoint * 2] - pointX;
double y1 = points[nPoint * 2 + 1] - pointY;
double x2 = points[(nPoint + 1) * 2] - pointX;
double y2 = points[(nPoint + 1) * 2 + 1] - pointY;
double y1y2 = y1*y2;
if(y1y2 > 0.0) // If the signs are the same
{
// Then it does not cross
continue;
}
else if(y1y2 == 0.0) // Then it has intesected a vertex
{
if(y1 == 0.0)
{
if( y2 > 0.0 )
continue;
}
else if( y1 > 0.0 )
continue;
}
if( x1 > 0.0 && x2 > 0.0 )
{
CrossCount++;
continue;
}
// Calculate Intersection
double dy = y2 - y1;
double dx = x2 - x1;
// CDM March 2008 - if dy is zero (horiz line), this will be a bad idea...
if (dy != 0)
{
if (x1 - y1*(dx/dy) > 0.0)
CrossCount++;
}
}
return (CrossCount&1) ? true : false;
}
// **************************************************************
// ShapeTypeConvert2D()
// **************************************************************
ShpfileType CShapeData::ShapeTypeConvert2D(ShpfileType shpType)
{
if (shpType == SHP_NULLSHAPE) return SHP_NULLSHAPE;
else if (shpType == SHP_POINT || shpType == SHP_POINTM || shpType == SHP_POINTZ) return SHP_POINT;
else if (shpType == SHP_MULTIPOINT || shpType == SHP_MULTIPOINTM || shpType == SHP_MULTIPOINTZ) return SHP_MULTIPOINT;
else if (shpType == SHP_POLYGON || shpType == SHP_POLYGONM || shpType == SHP_POLYGONZ) return SHP_POLYGON;
else if (shpType == SHP_POLYLINE || shpType == SHP_POLYLINEM || shpType == SHP_POLYLINEZ) return SHP_POLYLINE;
else return SHP_NULLSHAPE;
}
#pragma endregion