This repository was archived by the owner on Feb 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSOP_Shapefile.C
More file actions
619 lines (479 loc) · 15.8 KB
/
Copy pathSOP_Shapefile.C
File metadata and controls
619 lines (479 loc) · 15.8 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include "SOP_Shapefile.h"
#include <UT/UT_DSOVersion.h>
#include <UT/UT_Interrupt.h>
#include <OP/OP_Operator.h>
#include <OP/OP_OperatorTable.h>
#include <PRM/PRM_Include.h>
#include <PRM/PRM_SpareData.h>
#include <FS/FS_Info.h>
#include <GU/GU_Detail.h>
#include <GU/GU_PrimPoly.h>
#include <shapefil.h>
#define SOP_SHAPEFILE_NAME "shapefile"
#define SOP_SHAPEFILE_DESCRIPTION "ShapeFile"
#define SOP_SHAPEFILE_PARAM_FILE "shape_file"
#define SOP_SHAPEFILE_PARAM_CREATE_SHAPE_GROUPS "create_shape_groups"
#define SOP_SHAPEFILE_PARAM_CREATE_SHAPE_ATTRIBUTES "create_shape_attributes"
#define SOP_SHAPEFILE_PARAM_SWAP_YZ_AXIS "swap_yz_axis"
#define SOP_SHAPEFILE_ATTRIB_POINT_SHAPE_NUM "point_shape_num"
#define SOP_SHAPEFILE_ATTRIB_POINT_SHAPE_PART_NUM "point_shape_part_num"
#define SOP_SHAPEFILE_ATTRIB_PRIMITIVE_SHAPE_NUM "primitive_shape_num"
#define SOP_SHAPEFILE_ATTRIB_PRIMITIVE_SHAPE_PART_NUM "primitive_shape_part_num"
#define SOP_SHAPEFILE_GROUP_SHAPE_POINT "shape_point"
#define SOP_SHAPEFILE_GROUP_SHAPE_POLYGON "shape_polygon"
#define SOP_SHAPEFILE_GROUP_SHAPE_POLYLINE "shape_polyline"
static PRM_Name s_name_file(SOP_SHAPEFILE_PARAM_FILE, "Shape File");
static PRM_Name s_name_create_shape_groups(SOP_SHAPEFILE_PARAM_CREATE_SHAPE_GROUPS, "Create Shape Groups");
static PRM_Name s_name_create_shape_attributes(SOP_SHAPEFILE_PARAM_CREATE_SHAPE_ATTRIBUTES, "Create Shape Attributes");
static PRM_Name s_name_swap_yz_axis(SOP_SHAPEFILE_PARAM_SWAP_YZ_AXIS, "Swap YZ Axis");
static PRM_SpareData s_shape_file_picker(PRM_SpareArgs() << PRM_SpareToken(PRM_SpareData::getFileChooserModeToken(),
PRM_SpareData::getFileChooserModeValRead()) << PRM_SpareToken(PRM_SpareData::getFileChooserPatternToken(),
SOP_Shapefile::fileExtensionFilterString()));
static PRM_Default s_default_create_shape_groups(false);
static PRM_Default s_default_create_shape_attributes(false);
static PRM_Default s_default_swap_yz_axis(true);
PRM_Template
SOP_Shapefile::myTemplateList[] =
{
PRM_Template(PRM_TOGGLE, 1, &s_name_create_shape_groups, &s_default_create_shape_groups),
PRM_Template(PRM_TOGGLE, 1, &s_name_create_shape_attributes, &s_default_create_shape_attributes),
PRM_Template(PRM_TOGGLE, 1, &s_name_swap_yz_axis, &s_default_swap_yz_axis),
PRM_Template(PRM_FILE, 1, &s_name_file, 0, 0, 0, 0, &s_shape_file_picker),
PRM_Template()
};
OP_Node*
SOP_Shapefile::myConstructor(OP_Network* network, const char* name, OP_Operator* op)
{
return new SOP_Shapefile(network, name, op);
}
const char*
SOP_Shapefile::fileExtensionFilterString()
{
return "*.shp";
}
SOP_Shapefile::SOP_Shapefile(OP_Network* network, const char* name, OP_Operator* op) :
SOP_Node(network, name, op)
{
}
SOP_Shapefile::~SOP_Shapefile()
{
}
OP_ERROR
SOP_Shapefile::cookMySop(OP_Context& context)
{
if(error() >= UT_ERROR_ABORT)
{
return error();
}
gdp->clearAndDestroy();
// Get current execution time for parameter evaluation.
fpreal t = context.getTime();
// Retrieve shapefile.
UT_String shape_file_path = "";
if(!getParamShapefile(shape_file_path, t))
{
return processError(context, "Invalid shapefile specified.");
}
// Create shapefile handle.
SHPHandle shp_handle = SHPOpen(shape_file_path.c_str(), "rb");
if(!shp_handle)
{
return processError(context, "Unable to open shapefile for reading.");
}
// Retrieve the number of entities and their types.
int shp_num_entities = 0;
int shp_entities_type = SHPT_NULL;
SHPGetInfo(shp_handle, &shp_num_entities, &shp_entities_type, nullptr, nullptr);
if(!shp_num_entities)
{
SHPClose(shp_handle);
return processError(context, "No shapes were found.");
}
// Process all shapes in a file.
for(int ids = 0; ids < shp_entities_type; ++ids)
{
// Retrieve shape object at this index.
SHPObject* shp_object = SHPReadObject(shp_handle, ids);
if(!shp_object)
{
processWarning("Invalid object.");
continue;
}
// If shape type is invalid, skip.
if(shp_object->nSHPType == SHPT_NULL)
{
processWarning("Skipping a null shape.");
continue;
}
// Make sure we have parts in this shape.
if(!shp_object->nParts)
{
processWarning("Skipping a shape with zero parts.");
continue;
}
switch(shp_object->nSHPType)
{
case SHPT_POINT:
case SHPT_POINTZ:
{
if(!addShapePoint(shp_object, t))
{
processWarning("Skipping an invalid point shape.");
}
break;
}
case SHPT_ARC:
case SHPT_ARCZ:
{
if(!addShapePolyline(shp_object, t))
{
processWarning("Skipping an invalid polyline shape.");
}
break;
}
case SHPT_POLYGON:
case SHPT_POLYGONZ:
{
if(!addShapePolygon(shp_object, t))
{
processWarning("Skipping an invalid polygon shape.");
}
break;
}
case SHPT_MULTIPOINT:
case SHPT_MULTIPOINTZ:
{
processWarning("Skipping unsupported mulitpoint shape.");
break;
}
case SHPT_POINTM:
case SHPT_ARCM:
case SHPT_POLYGONM:
case SHPT_MULTIPOINTM:
case SHPT_MULTIPATCH:
default:
{
processWarning("Skipping unsupported shape.");
break;
}
}
}
// Close the handle.
SHPClose(shp_handle);
return error(context);
}
OP_ERROR
SOP_Shapefile::processError(OP_Context& context, const char* reason)
{
if(reason)
{
UT_WorkBuffer buf;
buf.sprintf("Shapefile: %s", reason);
addError(SOP_MESSAGE, buf.buffer());
}
return error(context);
}
void
SOP_Shapefile::processWarning(const char* reason)
{
if(reason)
{
UT_WorkBuffer buf;
buf.sprintf("Shapefile: %s", reason);
addWarning(SOP_MESSAGE, buf.buffer());
}
}
bool
SOP_Shapefile::getParamShapefile(UT_String& shape_file, fpreal t) const
{
shape_file = "";
evalString(shape_file, SOP_SHAPEFILE_PARAM_FILE, 0, t);
if(!shape_file || !shape_file.length())
{
return false;
}
FS_Info file_info(shape_file);
if(!file_info.fileExists())
{
return false;
}
return true;
}
bool
SOP_Shapefile::getParamCreateShapeGroups(fpreal t) const
{
return evalInt(SOP_SHAPEFILE_PARAM_CREATE_SHAPE_GROUPS, 0, t) != 0;
}
bool
SOP_Shapefile::getParamCreateShapeAttributes(fpreal t) const
{
return evalInt(SOP_SHAPEFILE_PARAM_CREATE_SHAPE_ATTRIBUTES, 0, t) != 0;
}
bool
SOP_Shapefile::getParamSwapYZAxis(fpreal t) const
{
return evalInt(SOP_SHAPEFILE_PARAM_SWAP_YZ_AXIS, 0, t) != 0;
}
bool
SOP_Shapefile::getShapeVertexIndices(SHPObject* shp_object, int part_idx, int& vertex_first, int& vertex_last) const
{
vertex_first = 0;
vertex_last = 0;
if(!shp_object || part_idx < 0 || shp_object->nParts <= part_idx)
{
return false;
}
vertex_first = shp_object->panPartStart[part_idx];
vertex_last = vertex_first;
if(part_idx == shp_object->nParts - 1)
{
vertex_last = shp_object->nVertices - 1;
}
else
{
vertex_last = shp_object->panPartStart[part_idx + 1] - 1;
}
return true;
}
bool
SOP_Shapefile::getShapePointPositions(SHPObject* shp_object, int shp_idx, bool use_z, fpreal t,
UT_Array<UT_Vector3>& point_positions) const
{
point_positions.clear();
if(!shp_object)
{
return false;
}
int vertex_first = 0;
int vertex_last = 0;
bool swap_yz_axis = getParamSwapYZAxis(t);
if(!getShapeVertexIndices(shp_object, shp_idx, vertex_first, vertex_last))
{
return false;
}
for(int idx = vertex_first; idx <= vertex_last; ++idx)
{
float px = shp_object->padfX[idx];
float py = shp_object->padfY[idx];
float pz = 0.0f;
if(use_z)
{
pz = shp_object->padfZ[idx];
}
if(swap_yz_axis)
{
point_positions.append(UT_Vector3(px, pz, py));
}
else
{
point_positions.append(UT_Vector3(px, py, pz));
}
}
return point_positions.size() > 0;
}
bool
SOP_Shapefile::addShapePoint(SHPObject* shp_object, fpreal t)
{
if(!shp_object)
{
return false;
}
if(shp_object->nSHPType != SHPT_POINT && shp_object->nSHPType != SHPT_POINTZ)
{
return false;
}
GA_PointGroup* group_point = nullptr;
if(getParamCreateShapeGroups(t))
{
group_point = findGroupPoint(SOP_SHAPEFILE_GROUP_SHAPE_POINT);
}
for(int idp = 0; idp < shp_object->nParts; ++idp)
{
UT_Array<UT_Vector3> point_positions;
if(!getShapePointPositions(shp_object, idp, (shp_object->nSHPType == SHPT_POINTZ), t, point_positions))
{
return false;
}
for(int idx = 0; idx < point_positions.size(); ++idx)
{
const UT_Vector3& point_position = point_positions(idx);
GA_Offset point_offset = gdp->appendPoint();
gdp->setPos3(point_offset, point_position);
if(group_point)
{
group_point->addOffset(point_offset);
}
if(getParamCreateShapeAttributes(t))
{
setPointAttributeShapeNumber(point_offset, shp_object->nShapeId);
setPointAttributeShapePartNumber(point_offset, idp);
}
}
}
return true;
}
bool
SOP_Shapefile::addShapePolygon(SHPObject* shp_object, fpreal t)
{
if(!shp_object)
{
return false;
}
if(shp_object->nSHPType != SHPT_POLYGON && shp_object->nSHPType != SHPT_POLYGONZ)
{
return false;
}
GA_PrimitiveGroup* group_primitive = nullptr;
if(getParamCreateShapeGroups(t))
{
group_primitive = findGroupPrimitive(SOP_SHAPEFILE_GROUP_SHAPE_POLYGON);
}
for(int idp = 0; idp < shp_object->nParts; ++idp)
{
UT_Array<UT_Vector3> point_positions;
if(!getShapePointPositions(shp_object, idp, (shp_object->nSHPType == SHPT_POLYGONZ), t, point_positions))
{
return false;
}
GU_PrimPoly* prim_poly = GU_PrimPoly::build(gdp, 0, GU_POLY_CLOSED);
for(int idx = 0; idx < point_positions.size(); ++idx)
{
const UT_Vector3& point_position = point_positions(idx);
GA_Offset point_offset = gdp->appendPoint();
gdp->setPos3(point_offset, point_position);
prim_poly->appendVertex(point_offset);
}
prim_poly->close();
if(group_primitive)
{
GA_Offset prim_offset = prim_poly->getMapOffset();
group_primitive->addOffset(prim_offset);
}
if(getParamCreateShapeAttributes(t))
{
GA_Offset prim_offset = prim_poly->getMapOffset();
setPrimitiveAttributeShapeNumber(prim_offset, shp_object->nShapeId);
setPrimitiveAttributeShapePartNumber(prim_offset, idp);
}
}
return true;
}
bool
SOP_Shapefile::addShapePolyline(SHPObject* shp_object, fpreal t)
{
if(!shp_object)
{
return false;
}
if(shp_object->nSHPType != SHPT_ARC && shp_object->nSHPType != SHPT_ARCZ)
{
return false;
}
GA_PrimitiveGroup* group_primitive = nullptr;
if(getParamCreateShapeGroups(t))
{
group_primitive = findGroupPrimitive(SOP_SHAPEFILE_GROUP_SHAPE_POLYLINE);
}
for(int idp = 0; idp < shp_object->nParts; ++idp)
{
UT_Array<UT_Vector3> point_positions;
if(!getShapePointPositions(shp_object, idp, (shp_object->nSHPType == SHPT_ARCZ), t, point_positions))
{
return false;
}
GU_PrimPoly* prim_poly = GU_PrimPoly::build(gdp, 0, GU_POLY_OPEN);
for(int idx = 0; idx < point_positions.size(); ++idx)
{
const UT_Vector3& point_position = point_positions(idx);
GA_Offset point_offset = gdp->appendPoint();
gdp->setPos3(point_offset, point_position);
prim_poly->appendVertex(point_offset);
}
if(group_primitive)
{
GA_Offset prim_offset = prim_poly->getMapOffset();
group_primitive->addOffset(prim_offset);
}
if(getParamCreateShapeAttributes(t))
{
GA_Offset prim_offset = prim_poly->getMapOffset();
setPrimitiveAttributeShapeNumber(prim_offset, shp_object->nShapeId);
setPrimitiveAttributeShapePartNumber(prim_offset, idp);
}
}
return true;
}
GA_PointGroup*
SOP_Shapefile::findGroupPoint(const UT_String& group_name) const
{
if(!group_name.isstring())
{
return nullptr;
}
GA_PointGroup* group = gdp->findPointGroup(group_name);
if(!group)
{
group = gdp->newPointGroup(group_name);
}
return group;
}
GA_PrimitiveGroup*
SOP_Shapefile::findGroupPrimitive(const UT_String& group_name) const
{
if(!group_name.isstring())
{
return nullptr;
}
GA_PrimitiveGroup* group = gdp->findPrimitiveGroup(group_name);
if(!group)
{
group = gdp->newPrimitiveGroup(group_name);
}
return group;
}
void
SOP_Shapefile::setPointAttributeShapeNumber(GA_Offset point_offset, int32 shape_number)
{
setAttribute(point_offset, GA_ATTRIB_POINT, SOP_SHAPEFILE_ATTRIB_POINT_SHAPE_NUM, shape_number);
}
void
SOP_Shapefile::setPointAttributeShapePartNumber(GA_Offset point_offset, int32 shape_part_number)
{
setAttribute(point_offset, GA_ATTRIB_POINT, SOP_SHAPEFILE_ATTRIB_POINT_SHAPE_PART_NUM, shape_part_number);
}
void
SOP_Shapefile::setPrimitiveAttributeShapeNumber(GA_Offset prim_offset, int32 shape_number)
{
setAttribute(prim_offset, GA_ATTRIB_PRIMITIVE, SOP_SHAPEFILE_ATTRIB_PRIMITIVE_SHAPE_NUM, shape_number);
}
void
SOP_Shapefile::setPrimitiveAttributeShapePartNumber(GA_Offset prim_offset, int32 shape_part_number)
{
setAttribute(prim_offset, GA_ATTRIB_PRIMITIVE, SOP_SHAPEFILE_ATTRIB_PRIMITIVE_SHAPE_PART_NUM, shape_part_number);
}
void
SOP_Shapefile::setAttribute(GA_Offset offset, GA_AttributeOwner attrib_owner, const UT_String& attribute_name, int attribute_value)
{
if(GA_INVALID_OFFSET == offset || !attribute_name.isValidVariableName())
{
return;
}
GA_Attribute* attribute = gdp->findIntTuple(attrib_owner, attribute_name, 1);
GA_RWHandleI handle_attribute(attribute);
if(!handle_attribute.isValid())
{
attribute = gdp->addIntTuple(attrib_owner, attribute_name, 1);
handle_attribute.bind(attribute);
if(!handle_attribute.isValid())
{
return;
}
}
handle_attribute.set(offset, attribute_value);
}
void
newSopOperator(OP_OperatorTable* table)
{
table->addOperator(new OP_Operator(SOP_SHAPEFILE_NAME, SOP_SHAPEFILE_DESCRIPTION,
SOP_Shapefile::myConstructor, SOP_Shapefile::myTemplateList, 0, 0, 0, OP_FLAG_GENERATOR));
}