-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathShapefileDrawing.cpp
More file actions
2375 lines (2071 loc) · 67.7 KB
/
ShapefileDrawing.cpp
File metadata and controls
2375 lines (2071 loc) · 67.7 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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************************
* File name: ShapefileDrawing.cpp
*
* Project: MapWindow Open Source (MapWinGis ActiveX control)
* Description: draws shapefile either from disk or memory, taking into count
* styles (aka categories of drawing options), selection, visibility expression for shapes,
* collision avoidance for points
*
**************************************************************************************
* 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) 25 june 2010 - created the file
#include "StdAfx.h"
#include "ShapefileDrawing.h"
#include "LinePattern.h"
#include "ShapefileReader.h"
#include "Shape.h"
#include "GeometryHelper.h"
#include "macros.h"
#include "PointSymbols.h"
#include "ShapefileCategories.h"
#include "TableHelper.h"
#include "ImageHelper.h"
// MEMO: there are several formats to hold shape data while drawing
// there are 2 switches: regular/edit mode; and fast/slow mode
// regular-disk: PolygonData (pointers to the positions of the memory from fread)
// regular-fast: IShapeData (CShapeData class)
// edit-COM: IShapeData (CShapeWrapperCOM class)
// edit-fast: IShapeData (CShapeWrapper class)
using namespace Gdiplus;
// ReSharper disable once CppInconsistentNaming
enum tkDrawingShape
{
// ReSharper disable once CppInconsistentNaming
pshPixel = 0,
// ReSharper disable once CppInconsistentNaming
pshEllipse = 1,
// ReSharper disable once CppInconsistentNaming
pshPolygon = 2,
// ReSharper disable once CppInconsistentNaming
pshPicture = 3,
// ReSharper disable once CppInconsistentNaming
pshCharacter = 4,
};
#pragma region MainDrawing
//*******************************************************************
// Draw()
//*******************************************************************
bool CShapefileDrawer::Draw(const CRect& rcBounds, IShapefile* sf)
{
if (!sf) return false;
_shapefile = dynamic_cast<CShapefile*>(sf);
FILE* file = static_cast<CShapefile*>(sf)->get_File();
#ifdef USE_TIMER
CTimer tmr;
tmr.Init("c:\\mw_output.txt");
tmr.Start();
tmr.PrintTime("Before bounds");
#endif
// -------------------------------------------------------
// check bounds
// -------------------------------------------------------
double zMin, zMax;
IExtents* box = nullptr;
sf->get_Extents(&box);
box->GetBounds(&_xMin, &_yMin, &zMin, &_xMax, &_yMax, &zMax);
box->Release();
box = nullptr;
if (_xMin > _extents->right || _xMax<_extents->left || _yMin>_extents->top || _yMax < _extents->bottom)
return false;
#ifdef USE_TIMER
tmr.PrintTime("After bounds");
#endif
// --------------------------------------------------------
// reading shapefile properties
// --------------------------------------------------------
long numShapes;
VARIANT_BOOL useQTree;
VARIANT_BOOL useSpatialIndex;
VARIANT_BOOL hasSpatialIndex;
tkSelectionAppearance selectionAppearance;
_shapefile->get_SelectionAppearance(&selectionAppearance);
_shapefile->get_FastMode(&_fastMode);
_shapefile->get_ShapefileType(&_shptype);
_shapefile->get_NumShapes(&numShapes);
_shapefile->get_EditingShapes(&_isEditing);
_shapefile->get_UseQTree(&useQTree);
_shapefile->get_UseSpatialIndex(&useSpatialIndex);
_shapefile->get_HasSpatialIndex(&hasSpatialIndex);
useSpatialIndex = (useSpatialIndex && hasSpatialIndex);
// get 2D type for not checking it afterwards
_shptype = ShapeUtility::Convert2D(_shptype);
// clearing the paths
_vertexPathes.clear();
#ifdef USE_TIMER
tmr.PrintTime("Before reading drawing options");
#endif
// --------------------------------------------------------
// acquiring drawing options
// --------------------------------------------------------
// default options
IShapeDrawingOptions* iDefOpt = nullptr;
_shapefile->get_DefaultDrawingOptions(&iDefOpt);
CDrawingOptionsEx* defaultOptions = ((CShapeDrawingOptions*)iDefOpt)->get_UnderlyingOptions();
iDefOpt->Release(); iDefOpt = nullptr;
// selection options
CDrawingOptionsEx* selectionOptions = nullptr;
if (selectionAppearance == saDrawingOptions)
{
IShapeDrawingOptions* iSelOpt = nullptr;
_shapefile->get_SelectionDrawingOptions(&iSelOpt);
selectionOptions = ((CShapeDrawingOptions*)iSelOpt)->get_UnderlyingOptions();
iSelOpt->Release(); iSelOpt = nullptr;
}
else
{
// a default options will be used with drawing transparent selection on top of it
OLE_COLOR color;
unsigned char transp;
selectionOptions = defaultOptions;
_shapefile->get_SelectionColor(&color);
_shapefile->get_SelectionTransparency(&transp);
m_selectionColor = color;
m_selectionTransparency = transp;
}
// categories
IShapefileCategories* icategories = nullptr;
_shapefile->get_Categories(&icategories);
CShapefileCategories* categories = (CShapefileCategories*)icategories;
icategories->Release();
#ifdef USE_TIMER
tmr.PrintTime("Before reading drawing options");
#endif
// --------------------------------------------------------
// Settings DC/graphics options
// --------------------------------------------------------
int* qtreeResult = nullptr; // results of quad tree selection
vector<long>* selectResult = nullptr; // results of spatial index selection
int offset; // position (number) of a shape in the shapefile
// --------------------------------------------------------
// Reading from disk
// --------------------------------------------------------
if (!_isEditing)
{
if (file == nullptr)
{
CallbackHelper::AssertionFailed("Shapefile rendering: file doesn't exist.");
return false;
}
CComBSTR fname;
sf->get_Filename(&fname);
CCriticalSection* readLock = ((CShapefile*)sf)->get_ReadLock();
// reading index
USES_CONVERSION;
_sfReader = new CShapefileReader();
if (!_sfReader->ReadShapefileIndex(OLE2W(fname), file, readLock))
{
delete _sfReader;
_sfReader = nullptr;
return false;
}
// ---------------------------------------------------------
// extracting shapes from spatial index
// ---------------------------------------------------------
if (useSpatialIndex)
{
selectResult = SelectShapesFromSpatialIndex(OLE2A(fname), _extents); // TODO: use Unicode
if (!selectResult)
{
useSpatialIndex = VARIANT_FALSE;
}
else
{
numShapes = selectResult->size();
sort(selectResult->begin(), selectResult->end());
}
}
}
#ifdef USE_TIMER
tmr.PrintTime("After reading shape index");
#endif
long numCategories;
categories->get_Count(&numCategories);
std::vector<vector<int>> categoryIndices;
std::vector<vector<int>> categorySelIndices; // used for selectionAppearance == saSelectionColor only
categoryIndices.resize(numCategories + 2); // +1 = default options; +2 = selection options
categorySelIndices.resize(numCategories + 1); // +1 = default options;
// --------------------------------------------------------------
// Analyzing visibility expression
// --------------------------------------------------------------
std::vector<long> arr;
CStringW err;
bool useAll = true;
CComBSTR expr;
_shapefile->get_VisibilityExpression(&expr);
if (SysStringLen(expr) > 0)
{
CComPtr<ITable> tbl = nullptr;
_shapefile->get_Table(&tbl);
USES_CONVERSION;
if (TableHelper::Cast(tbl)->QueryCore(OLE2CW(expr), arr, err))
{
useAll = false;
}
}
// --------------------------------------------------------------
// Extracting shapes using quad tree
// --------------------------------------------------------------
if (useQTree & _isEditing)
{
int shapesCount;
IExtents* bBox = nullptr;
ComHelper::CreateExtents(&bBox);
bBox->SetBounds(_extents->left, _extents->bottom, 0, _extents->right, _extents->top, 0);
dynamic_cast<CShapefile*>(sf)->QuickQueryInEditMode(bBox, &qtreeResult, &shapesCount);
if (qtreeResult == nullptr)
{
bBox->Release(); bBox = nullptr;
goto cleaning;
}
numShapes = (long)shapesCount;
}
// --------------------------------------------------------------------
// nullify the screen size of all shapes
// the size is used to choose whether to draw labels and charts or not
// --------------------------------------------------------------------
_shapeData = _shapefile->get_ShapeVector();
if (_shptype == SHP_POLYGON || _shptype == SHP_POLYLINE)
{
int size = _shapeData->size();
for (int i = 0; i < size; i++)
{
(*_shapeData)[i]->size = 0;
(*_shapeData)[i]->isVisible(false);
}
}
else if (_shptype == SHP_POINT)
{
// Since there may be (tens of) thousands of shapes,
// I don't want to check for rotation field specifications
// and field indices on each iteration; so instead, I am
// checking once for field specification and index, and
// then iterating shapes to set rotation-specific values.
// NOTE: the following is superceded by the rotationExpression
// is a Rotation field specified ?
//if (defaultOptions->rotationField.GetLength() > 0)
//{
// long idx;
// CComBSTR bstrName(defaultOptions->rotationField);
// _shapefile->get_FieldIndexByName(bstrName, &idx);
// // iterate shapes, set rotation based on field value
// // NOTE that this uses the existing 'rotation' field,
// // and thus takes precedence over options-based rotation
// for (long i = 0; i < (long)_shapeData->size(); i++)
// {
// VARIANT rotation;
// _shapefile->get_CellValue(idx, i, &rotation);
// (*_shapeData)[i]->rotation = rotation.dblVal;
// }
//}
}
// --------------------------------------------------------------
// Building lists of shape indices for each category
// --------------------------------------------------------------
unsigned int k = 0; // position in arr of visible shapes
for (int i = 0; i < (int)numShapes; i++)
{
if (useQTree && _isEditing)
{
offset = qtreeResult[i];
}
else
{
if (!_isEditing && useSpatialIndex)
{
offset = (*selectResult)[i] - 1;
}
else
{
offset = i;
}
}
// searching the index
if (!useAll)
{
bool stop = false;
//empty list?
if (arr.size() <= 0) {
stop = true;
}
else
{
//determine if the shape is visible
while (arr[k] < offset)
{
k++;
if (k >= arr.size())
{
stop = true;
break;
}
}
}
// there can't be any visible shapes
if (stop)
{
break;
}
// missing this shape, because it complies with the expression
if (arr[k] > offset)
{
continue;
}
}
if (offset >= (int)_shapeData->size())
{
// TODO: is is possible to check that the index has the same number of shapes as shapefile?
if (!_isEditing && useSpatialIndex) {
CallbackHelper::ErrorMsg("Invalid spatial index. Index of shape is outside bounds.");
}
else {
CallbackHelper::ErrorMsg("Shapefile drawing: index of shape is outside bounds.");
}
break;
}
// whether it was hidden explicitly by user
if ((*_shapeData)[offset]->hidden()) {
continue;
}
// marking shape as visible; it may still fall out of extents but it is inefficient to test it here
(*_shapeData)[offset]->isVisible(true);
bool selected = (*_shapeData)[offset]->selected();
if (selected)
{
if (selectionAppearance == saDrawingOptions)
{
categoryIndices[numCategories + 1].push_back(offset); // selection options
}
else
{
long catIndex = (*_shapeData)[offset]->category;
if (catIndex < 0 || catIndex >= numCategories)
{
categorySelIndices[numCategories].push_back(offset); // default options
}
else
{
categorySelIndices[catIndex].push_back(offset); // category
}
}
}
else
{
long catIndex = (*_shapeData)[offset]->category;
if (catIndex < 0 || catIndex >= numCategories)
{
categoryIndices[numCategories].push_back(offset); // default options
}
else
{
categoryIndices[catIndex].push_back(offset); // category
// TODO: check whether the drawing options are different from default
}
}
}
// -----------------------------------------------------
// Drawing
// -----------------------------------------------------
CDrawingOptionsEx* options = nullptr;
#ifdef USE_TIMER
tmr.PrintTime("Before reading data");
#endif
// in some cases selected objects should be drawn first
bool selectionFirst = false;
if (_shptype == SHP_POINT || _shptype == SHP_MULTIPOINT)
{
tkCollisionMode mode;
sf->get_CollisionMode(&mode);
if (mode != AllowCollisions)
{
selectionFirst = true;
}
}
// drawing selection at the bottom
if (selectionFirst)
{
if (selectionAppearance == saSelectionColor)
{
for (int i = categorySelIndices.size() - 1; i >= 0; i--)
{
if (i == numCategories)
{
options = defaultOptions;
}
else if (i < numCategories)
{
options = ((CShapefileCategories*)categories)->get_UnderlyingOptions(i);
}
std::vector<int>* indices = &categorySelIndices[i];
this->DrawCategory(options, indices, true);
}
}
else // selection drawing options
{
options = selectionOptions;
std::vector<int>* indices = &categoryIndices[numCategories + 1];
this->DrawCategory(options, indices, false); // false: options set in the selection itself so no need to draw it on top
}
}
// drawing unselected shapes
for (int i = categoryIndices.size() - 2; i >= 0; i--)
{
if (i == numCategories)
{
options = defaultOptions;
}
else if (i < numCategories)
{
options = ((CShapefileCategories*)categories)->get_UnderlyingOptions(i);
}
else
{
CallbackHelper::AssertionFailed("Drawing options for category aren't found.");
}
std::vector<int>* indices = &categoryIndices[i];
this->DrawCategory(options, indices, false);
}
// drawing selection at the top
if (!selectionFirst)
{
if (selectionAppearance == saSelectionColor)
{
for (int i = categorySelIndices.size() - 1; i >= 0; i--)
{
if (i == numCategories)
{
options = defaultOptions;
}
else if (i < numCategories)
{
options = ((CShapefileCategories*)categories)->get_UnderlyingOptions(i);
}
std::vector<int>* indices = &categorySelIndices[i];
this->DrawCategory(options, indices, true);
}
}
else
{
options = selectionOptions;
std::vector<int>* indices = &categoryIndices[numCategories + 1];
this->DrawCategory(options, indices, false); // false: options set in the selection itself so no need to draw it on top
}
}
// drawing the vertices
for (unsigned int i = 0; i < _vertexPathes.size(); i++)
{
DrawVertices(_vertexPathes[i].path, _vertexPathes[i].options);
}
// clearing paths
for (unsigned int i = 0; i < _vertexPathes.size(); i++)
{
delete _vertexPathes[i].path;
}
#ifdef USE_TIMER
tmr.PrintTime("After drawing");
tmr.Stop();
#endif
// ------------------------------------------
// final cleaning
// ------------------------------------------
cleaning:
if (!_isEditing)
{
delete _sfReader;
_sfReader = nullptr;
}
if (useQTree)
{
// make sure it was allocated
if (qtreeResult)
{
delete[] qtreeResult;
}
}
else
{
if (useSpatialIndex && selectResult)
{
selectResult->clear();
delete selectResult;
selectResult = nullptr;
}
}
return true;
}
// ********************************************************
// DrawCategory()
// ********************************************************
void CShapefileDrawer::DrawCategory(CDrawingOptionsEx* options, std::vector<int>* indices, bool drawSelection)
{
if (indices->size() == 0)
return;
if ((!options->visible) || (!options->fillVisible && !options->linesVisible && !options->verticesVisible))
{
return;
}
if (!options->IsVisible(this->_scale, this->_currentZoom))
return;
if (_shptype == SHP_POINT || _shptype == SHP_MULTIPOINT)
options->_shpType = tkSimpleShapeType::shpPoint;
else if (_shptype == SHP_POLYLINE)
options->_shpType = tkSimpleShapeType::shpPolyline;
else if (_shptype == SHP_POLYGON)
options->_shpType = tkSimpleShapeType::shpPolygon;
else
options->_shpType = tkSimpleShapeType::shpNone;
options->scale = this->_scale;
// ----------------------------------------------------
// auto selecting the fastest drawing mode
// for the current set of options
// ----------------------------------------------------
if (options->lineWidth == 1.0f && options->lineTransparency == 255 && options->linesVisible)
{
options->drawingMode = vdmGDIMixed;
}
else
{
options->drawingMode = vdmGDIPlus;
}
if (options->pointSymbolType == ptSymbolFontCharacter || options->pointSymbolType == ptSymbolPicture)
{
options->drawingMode = vdmGDIPlus;
}
// circles look more neat in GDI+
if (options->pointSymbolType == ptSymbolStandard && options->pointShapeType == ptShapeCircle)
{
options->drawingMode = vdmGDIPlus;
}
if (_forceGdiplus)
{
options->drawingMode = vdmGDIPlus;
}
if (_dc)
{
_dc->SetBkColor(options->fillBgColor);
if (options->fillBgTransparent)
{
_dc->SetBkMode(TRANSPARENT);
}
else
{
_dc->SetBkMode(OPAQUE);
}
}
// ------------------------------------------------
// perform drawing
// ------------------------------------------------
if (_shptype == SHP_POINT || _shptype == SHP_MULTIPOINT)
{
this->DrawPointCategory(options, indices, drawSelection);
}
else if (_shptype == SHP_POLYLINE || _shptype == SHP_POLYGON)
{
if (_shptype == SHP_POLYLINE && options->useLinePattern && options->CanUseLinePattern())
{
this->DrawLinePatternCategory(options, indices, drawSelection);
}
else
{
if (options->drawingMode == vdmGDIMixed && _shptype == SHP_POLYLINE)
{
this->DrawLineCategoryGDI(options, indices, drawSelection); // only lines are drawn here
}
else
{
this->DrawPolyCategory(options, indices, drawSelection);
}
}
}
}
#pragma endregion
#pragma region DrawPointCategory
// *************************************************************
// GetVisibilityMask()
// *************************************************************
void CShapefileDrawer::GetVisibilityMask(std::vector<int>& indices, vector<bool>& visibilityMask)
{
long numShapes;
_shapefile->get_NumShapes(&numShapes);
visibilityMask.clear();
visibilityMask.resize(numShapes, false);
for (size_t i = 0; i < indices.size(); i++)
{
visibilityMask[indices[i]] = true;
}
}
// *************************************************************
// DrawPointsCategory()
// *************************************************************
void CShapefileDrawer::DrawPointCategory(CDrawingOptionsEx* options, std::vector<int>* indices, bool drawSelection)
{
IShapeWrapper* shp = nullptr;
tkDrawingShape pntShape;
GraphicsPath* path = nullptr;
GraphicsPath* path2 = nullptr; // for frame around character
float* data = nullptr;
Bitmap* bmPixel = nullptr;
int numPoints = 0;
int size = int(options->pointSize / 2.0);
OLE_COLOR pixelColor;
bool missingIcon = false;
if (options->pointSymbolType == ptSymbolPicture)
{
if (ImageHelper::IsEmpty(options->picture))
{
CallbackHelper::ErrorMsg("ShapeDrawingOptions.Picture is empty when icon for point is expected.");
missingIcon = true;
}
}
// creating a symbol to draw
if (options->pointSymbolType == ptSymbolStandard || missingIcon)
{
// receiving coordinates to define shape of symbol
if (options->pointSize <= 1.0)
{
pntShape = pshPixel;
pixelColor = options->linesVisible ? options->lineColor : options->fillColor;
}
else if (options->pointShapeType == ptShapeCircle)
{
pntShape = pshEllipse;
}
else
{
options->drawingMode = vdmGDIPlus; // GDI drawing will lead to rounding coordinates to integers, and distortions as a result
pntShape = pshPolygon;
// regular point rotation is set here
data = get_SimplePointShape(options->pointShapeType, options->pointSize, options->rotation, options->pointNumSides, options->pointShapeRatio, &numPoints);
if (!data)
return;
}
// GDI+ is used at least partially
if (options->drawingMode == vdmGDIPlus || options->drawingMode == vdmGDIMixed)
{
if (pntShape == pshEllipse)
{
options->InitGdiPlusBrush(&RectF(Gdiplus::REAL(-size), Gdiplus::REAL(-size), Gdiplus::REAL(options->pointSize), Gdiplus::REAL(options->pointSize)));
pntShape = pshEllipse;
path = new GraphicsPath();
path->StartFigure();
path->AddEllipse((Gdiplus::REAL)(-options->pointSize / 2.0), (Gdiplus::REAL)(-options->pointSize / 2.0), options->pointSize, options->pointSize);
path->CloseFigure();
}
else if (pntShape == pshPolygon)
{
options->InitGdiPlusBrush(&RectF(Gdiplus::REAL(-size), Gdiplus::REAL(-size), Gdiplus::REAL(options->pointSize), Gdiplus::REAL(options->pointSize)));
path = new GraphicsPath();
path->StartFigure();
path->AddLines(reinterpret_cast<Gdiplus::PointF*>(data), numPoints);
path->CloseFigure();
}
else if (pntShape == pshPixel && options->drawingMode == vdmGDIPlus)
{
bmPixel = new Bitmap(1, 1, _graphics);
long alpha = ((long)options->fillTransparency) << 24;
bmPixel->SetPixel(0, 0, Color(alpha | BGR_TO_RGB(pixelColor)));
}
}
// drawing with GDI
if (options->drawingMode == vdmGDI || options->drawingMode == vdmGDIMixed)
{
m_hdc = _graphics->GetHDC();
_dc = CDC::FromHandle(m_hdc);
if (pntShape != pshPixel)
options->InitGdiBrushAndPen(_dc);
}
}
else if (options->pointSymbolType == ptSymbolPicture)
{
options->InitGdiPlusPicture();
if (!options->bitmapPlus)
return;
pntShape = pshPicture;
}
else if (options->pointSymbolType == ptSymbolFontCharacter)
{
options->InitGdiPlusBrush(&RectF((Gdiplus::REAL)-size, (Gdiplus::REAL)-size, (Gdiplus::REAL)options->pointSize, (Gdiplus::REAL)options->pointSize));
m_hdc = _graphics->GetHDC();
_dc = CDC::FromHandle(m_hdc);
path = options->get_FontCharacterPath(_dc, false);
_graphics->ReleaseHDC(m_hdc);
_dc = nullptr;
Gdiplus::Matrix mtx;
mtx.Reset();
pntShape = pshPolygon;
}
// frame for a symbol
if (path && options->drawFrame)
{
path2 = options->GetFrameForPath(*path);
}
VARIANT_BOOL fastMode;
_shapefile->get_FastMode(&fastMode);
double x = 0, y = 0;
int shapeIndex;
tkCollisionMode collisionMode;
_shapefile->get_CollisionMode(&collisionMode);
// sorting
vector<long>* sorting;
((CShapefile*)_shapefile)->GetSorting(&sorting);
bool hasSorting = sorting != nullptr && sorting->size() > 0;
vector<bool> visibilityMask;
if (hasSorting) {
GetVisibilityMask(*indices, visibilityMask);
}
size_t numShapes = hasSorting ? visibilityMask.size() : indices->size();
for (int j = 0; j < (int)numShapes; j++)
{
if (hasSorting) {
shapeIndex = (*sorting)[j];
if (!visibilityMask[shapeIndex]) {
continue;
}
}
else {
shapeIndex = (*indices)[j];
}
// ------------------------------------------------------
// Reading point data
// ------------------------------------------------------
std::vector<PointWithId> points;
if (!_isEditing)
{
int recordLength;
char* data = _sfReader->ReadShapeData(shapeIndex, recordLength);
if (data)
{
if (_shptype == SHP_POINT)
{
x = *(double*)(data + 4); // 4 bytes on shape type
y = *(double*)(data + 12);
delete[] data;
points.push_back(PointWithId(x, y, shapeIndex));
}
else
{
PolygonData* pdata = _sfReader->ReadMultiPointData(data);
for (int i = 0; i < pdata->pointCount; i++)
{
x = pdata->points[i * 2];
y = pdata->points[i * 2 + 1];
points.push_back(PointWithId(x, y, shapeIndex));
}
delete pdata;
delete[] data;
}
}
else
continue;
}
else
{
shp = _shapefile->get_ShapeWrapper(shapeIndex);
if (!shp) continue;
for (int i = 0; i < shp->get_PointCount(); i++)
{
shp->get_PointXY(i, x, y);
points.push_back(PointWithId(x, y, shapeIndex));
}
}
for (size_t i = 0; i < points.size(); i++)
{
x = points[i].x;
y = points[i].y;
if (x > _extents->right || x < _extents->left || y > _extents->top || y < _extents->bottom) continue;
// ------------------------------------------------------
// Collision avoidance
// ------------------------------------------------------
int xInt = static_cast<int>((x - _extents->left) * _dx);
int yInt = static_cast<int>((_extents->top - y) * _dy);
// preventing point collision
if (!collisionMode == AllowCollisions)
{
CCollisionList* list = collisionMode == LocalList ? &_localCollisionList : _collisionList;
CRect* rect = nullptr;
if (options->pointSymbolType == ptSymbolPicture && options->picture != nullptr)
{
long width, height;
options->picture->get_Width(&width);
options->picture->get_Height(&height);
int wd = static_cast<int>((double)width * options->scaleX / 2.0);
int ht = static_cast<int>((double)height * options->scaleY / 2.0);
if (!options->alignIconByBottom)
{
rect = new CRect(xInt - wd, yInt - ht, xInt + wd, yInt + ht);
}
else
{
rect = new CRect(xInt - wd, yInt - ht * 2, xInt + wd, yInt);
}
}
else
{
rect = new CRect(xInt - int(options->pointSize / 2.0),
yInt - int(options->pointSize / 2.0),
xInt + int(options->pointSize / 2.0),
yInt + int(options->pointSize / 2.0));
}
if (list->HaveCollision(*rect) && _avoidCollisions)
{
delete rect;
continue;
}
else
{
(*_shapeData)[shapeIndex]->wasRendered(true);
list->AddRectangle(rect, 0, 0);
delete rect;
}
}
else
(*_shapeData)[shapeIndex]->wasRendered(true);
_shapeCount++;
// ------------------------------------------------------
// Drawing
// ------------------------------------------------------
if (pntShape == pshPixel)
{
if (drawSelection)
{
_dc->SetPixelV(xInt, yInt, m_selectionColor);
}
else
{
if (options->drawingMode == vdmGDIPlus)
{
_graphics->DrawImage(bmPixel, xInt, yInt);
}
else
{
_dc->SetPixelV(xInt, yInt, pixelColor);
}
}
(*_shapeData)[shapeIndex]->size = 1;
}
else if (pntShape == pshPicture)
{
Gdiplus::Matrix mtxInit;
_graphics->GetTransform(&mtxInit);
long width, height;
options->picture->get_Width(&width);
options->picture->get_Height(&height);
int wd = static_cast<int>((double)width * options->scaleX / 2.0);
int ht = static_cast<int>((double)height * options->scaleY / 2.0);
_graphics->TranslateTransform((float)(xInt), (float)(yInt));
// see if individual shape has a specified rotation
// (set either through shape rotation property, or from rotationExpression)
float angle = (float)((*_shapeData)[points[i].id])->rotation;
// if not set explicitly, try to grab it from category
if (angle == 0)
angle = (float)options->rotation;
// if any angle is specified, apply it
if (angle != 0)
_graphics->RotateTransform(angle);
// if reflecting
if (options->pointReflectionType != prtNone)
{
Gdiplus::Matrix flipMatrix;
// set up appropriate transformation
if (options->pointReflectionType == prtLeftToRight)
flipMatrix.SetElements(-1, 0, 0, 1, 0, 0);
else if (options->pointReflectionType == prtTopToBottom)