forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgraticule.c
1214 lines (1003 loc) · 41.8 KB
/
mapgraticule.c
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
/**********************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Graticule Renderer
* Author: John Novak, Novacell Technologies (jnovak@novacell.com)
*
**********************************************************************
* Copyright (c) 2003, John Novak, Novacell Technologies
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* 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
****************************************************************************/
#include "mapserver.h"
#include <assert.h>
#include "mapproject.h"
/**********************************************************************************************************************
*
*/
typedef enum {
posBottom = 1,
posTop,
posLeft,
posRight
} msGraticulePosition;
typedef enum {
lpDefault = 0,
lpDDMMSS = 1,
lpDDMM,
lpDD
} msLabelProcessingType;
void DefineAxis( int iTickCountTarget, double *Min, double *Max, double *Inc );
static int _AdjustLabelPosition( layerObj *pLayer, shapeObj *pShape, msGraticulePosition ePosition );
static void _FormatLabel( layerObj *pLayer, shapeObj *pShape, double dDataToFormat );
int msGraticuleLayerInitItemInfo(layerObj *layer);
#define MAPGRATICULE_ARC_SUBDIVISION_DEFAULT (256)
#define MAPGRATICULE_ARC_MINIMUM (16)
#define MAPGRATICULE_FORMAT_STRING_DEFAULT "%5.2g"
#define MAPGRATICULE_FORMAT_STRING_DDMMSS "%3d %02d %02d"
#define MAPGRATICULE_FORMAT_STRING_DDMM "%3d %02d"
#define MAPGRATICULE_FORMAT_STRING_DD "%3d"
/**********************************************************************************************************************
*
*/
int msGraticuleLayerOpen(layerObj *layer)
{
graticuleObj *pInfo = (graticuleObj *) layer->layerinfo;
if( pInfo == NULL )
return MS_FAILURE;
pInfo->dincrementlatitude = 15.0;
pInfo->dincrementlongitude = 15.0;
pInfo->dwhichlatitude = -90.0;
pInfo->dwhichlongitude = -180.0;
pInfo->bvertical = 1;
if( layer->numclasses == 0 )
msDebug( "GRID layer has no classes, nothing will be rendered.\n" );
if( layer->numclasses > 0 && layer->class[0]->numlabels > 0 )
pInfo->blabelaxes = 1;
else
pInfo->blabelaxes = 0;
if( pInfo->labelformat == NULL ) {
pInfo->labelformat = (char *) msSmallMalloc( strlen( MAPGRATICULE_FORMAT_STRING_DEFAULT ) + 1 );
pInfo->ilabeltype = (int) lpDefault;
strcpy( pInfo->labelformat, MAPGRATICULE_FORMAT_STRING_DEFAULT );
} else if( strcmp( pInfo->labelformat, "DDMMSS" ) == 0 ) {
msFree(pInfo->labelformat);
pInfo->labelformat = (char *) msSmallMalloc( strlen( MAPGRATICULE_FORMAT_STRING_DDMMSS ) + 1 );
pInfo->ilabeltype = (int) lpDDMMSS;
strcpy( pInfo->labelformat, MAPGRATICULE_FORMAT_STRING_DDMMSS );
} else if( strcmp( pInfo->labelformat, "DDMM" ) == 0 ) {
msFree(pInfo->labelformat);
pInfo->labelformat = (char *) msSmallMalloc( strlen( MAPGRATICULE_FORMAT_STRING_DDMM ) + 1 );
pInfo->ilabeltype = (int) lpDDMM;
strcpy( pInfo->labelformat, MAPGRATICULE_FORMAT_STRING_DDMM );
} else if( strcmp( pInfo->labelformat, "DD" ) == 0 ) {
msFree(pInfo->labelformat);
pInfo->labelformat = (char *) msSmallMalloc( strlen( MAPGRATICULE_FORMAT_STRING_DD ) + 1 );
pInfo->ilabeltype = (int) lpDD;
strcpy( pInfo->labelformat, MAPGRATICULE_FORMAT_STRING_DD );
}
return MS_SUCCESS;
}
/**********************************************************************************************************************
* Return MS_TRUE if layer is open, MS_FALSE otherwise.
*/
int msGraticuleLayerIsOpen(layerObj *layer)
{
if(layer->layerinfo)
return MS_TRUE;
return MS_FALSE;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerClose(layerObj *layer)
{
graticuleObj *pInfo = (graticuleObj *) layer->layerinfo;
if( pInfo->labelformat ) {
free( pInfo->labelformat );
pInfo->labelformat = NULL;
}
if( pInfo->pboundingpoints ) {
free( pInfo->pboundingpoints );
pInfo->pboundingpoints = NULL;
}
if( pInfo->pboundinglines ) {
free( pInfo->pboundinglines );
pInfo->pboundinglines = NULL;
}
free(layer->layerinfo);
layer->layerinfo=NULL;
return MS_SUCCESS;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
{
graticuleObj *pInfo = (graticuleObj *) layer->layerinfo;
int iAxisTickCount = 0;
rectObj rectMapCoordinates;
if(msCheckParentPointer(layer->map,"map") == MS_FAILURE)
return MS_FAILURE;
pInfo->dstartlatitude = rect.miny;
pInfo->dstartlongitude = rect.minx;
pInfo->dendlatitude = rect.maxy;
pInfo->dendlongitude = rect.maxx;
pInfo->bvertical = 1;
pInfo->extent = rect;
if( pInfo->minincrement > 0.0 ) {
pInfo->dincrementlongitude = pInfo->minincrement;
pInfo->dincrementlatitude = pInfo->minincrement;
} else if( pInfo->maxincrement > 0.0 ) {
pInfo->dincrementlongitude = pInfo->maxincrement;
pInfo->dincrementlatitude = pInfo->maxincrement;
} else {
pInfo->dincrementlongitude = 0;
pInfo->dincrementlatitude = 0;
}
if( pInfo->maxarcs > 0 )
iAxisTickCount = (int) pInfo->maxarcs;
else if( pInfo->minarcs > 0 )
iAxisTickCount = (int) pInfo->minarcs;
DefineAxis( iAxisTickCount, &pInfo->dstartlongitude, &pInfo->dendlongitude, &pInfo->dincrementlongitude);
DefineAxis( iAxisTickCount, &pInfo->dstartlatitude, &pInfo->dendlatitude, &pInfo->dincrementlatitude);
pInfo->dwhichlatitude = pInfo->dstartlatitude;
pInfo->dwhichlongitude = pInfo->dstartlongitude;
if( pInfo->minincrement > 0.0 && pInfo->maxincrement > 0.0 && pInfo->minincrement == pInfo->maxincrement ) {
pInfo->dincrementlongitude = pInfo->minincrement;
pInfo->dincrementlatitude = pInfo->minincrement;
} else if( pInfo->minincrement > 0.0 ) {
pInfo->dincrementlongitude = pInfo->minincrement;
pInfo->dincrementlatitude = pInfo->minincrement;
} else if( pInfo->maxincrement > 0.0 ) {
pInfo->dincrementlongitude = pInfo->maxincrement;
pInfo->dincrementlatitude = pInfo->maxincrement;
}
/*
* If using PROJ, project rect back into map system, and generate rect corner points in native system.
* These lines will be used when generating labels to get correct placement at arc/rect edge intersections.
*/
rectMapCoordinates = layer->map->extent;
pInfo->pboundinglines = (lineObj *) msSmallMalloc( sizeof( lineObj ) * 4 );
pInfo->pboundingpoints = (pointObj *) msSmallMalloc( sizeof( pointObj ) * 8 );
{
/*
* top
*/
pInfo->pboundinglines[0].numpoints = 2;
pInfo->pboundinglines[0].point = &pInfo->pboundingpoints[0];
pInfo->pboundinglines[0].point[0].x = rectMapCoordinates.minx;
pInfo->pboundinglines[0].point[0].y = rectMapCoordinates.maxy;
pInfo->pboundinglines[0].point[1].x = rectMapCoordinates.maxx;
pInfo->pboundinglines[0].point[1].y = rectMapCoordinates.maxy;
#ifdef USE_PROJ
if(layer->project && msProjectionsDiffer(&(layer->projection), &(layer->map->projection)))
msProjectLine(&layer->map->projection, &layer->projection, &pInfo->pboundinglines[0]);
else
layer->project = MS_FALSE;
#endif
/*
* bottom
*/
pInfo->pboundinglines[1].numpoints = 2;
pInfo->pboundinglines[1].point = &pInfo->pboundingpoints[2];
pInfo->pboundinglines[1].point[0].x = rectMapCoordinates.minx;
pInfo->pboundinglines[1].point[0].y = rectMapCoordinates.miny;
pInfo->pboundinglines[1].point[1].x = rectMapCoordinates.maxx;
pInfo->pboundinglines[1].point[1].y = rectMapCoordinates.miny;
#ifdef USE_PROJ
if(layer->project && msProjectionsDiffer(&(layer->projection), &(layer->map->projection)))
msProjectLine(&layer->map->projection, &layer->projection, &pInfo->pboundinglines[1]);
else
layer->project = MS_FALSE;
#endif
/*
* left
*/
pInfo->pboundinglines[2].numpoints = 2;
pInfo->pboundinglines[2].point = &pInfo->pboundingpoints[4];
pInfo->pboundinglines[2].point[0].x = rectMapCoordinates.minx;
pInfo->pboundinglines[2].point[0].y = rectMapCoordinates.miny;
pInfo->pboundinglines[2].point[1].x = rectMapCoordinates.minx;
pInfo->pboundinglines[2].point[1].y = rectMapCoordinates.maxy;
#ifdef USE_PROJ
if(layer->project && msProjectionsDiffer(&(layer->projection), &(layer->map->projection)))
msProjectLine(&layer->map->projection, &layer->projection, &pInfo->pboundinglines[2]);
else
layer->project = MS_FALSE;
#endif
/*
* right
*/
pInfo->pboundinglines[3].numpoints = 2;
pInfo->pboundinglines[3].point = &pInfo->pboundingpoints[6];
pInfo->pboundinglines[3].point[0].x = rectMapCoordinates.maxx;
pInfo->pboundinglines[3].point[0].y = rectMapCoordinates.miny;
pInfo->pboundinglines[3].point[1].x = rectMapCoordinates.maxx;
pInfo->pboundinglines[3].point[1].y = rectMapCoordinates.maxy;
#ifdef USE_PROJ
if(layer->project && msProjectionsDiffer(&(layer->projection), &(layer->map->projection)))
msProjectLine(&layer->map->projection, &layer->projection, &pInfo->pboundinglines[3]);
else
layer->project = MS_FALSE;
#endif
}
return MS_SUCCESS;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerNextShape(layerObj *layer, shapeObj *shape)
{
graticuleObj *pInfo = (graticuleObj *) layer->layerinfo;
if( pInfo->minsubdivides <= 0.0 || pInfo->maxsubdivides <= 0.0 )
pInfo->minsubdivides = pInfo->maxsubdivides = MAPGRATICULE_ARC_SUBDIVISION_DEFAULT;
shape->numlines = 1;
shape->type = MS_SHAPE_LINE;
shape->line = (lineObj *) msSmallMalloc(sizeof( lineObj ));
shape->line->numpoints = (int) pInfo->maxsubdivides;
/*
* Subdivide and draw current arc, rendering the arc labels first
*/
if( pInfo->bvertical ) {
int iPointIndex;
double dArcDelta = (pInfo->dendlatitude - pInfo->dstartlatitude) / (double) shape->line->numpoints;
double dArcPosition = pInfo->dstartlatitude + dArcDelta;
double dStartY, dDeltaX;
switch( pInfo->ilabelstate ) {
case 0:
if(!pInfo->blabelaxes) { /* Bottom */
pInfo->ilabelstate++;
shape->numlines = 0;
return MS_SUCCESS;
}
dDeltaX = (pInfo->dwhichlongitude - pInfo->pboundinglines[1].point[0].x) / (pInfo->pboundinglines[1].point[1].x - pInfo->pboundinglines[1].point[0].x);
if (dDeltaX < 0)
dDeltaX=dDeltaX*-1;
dStartY = (pInfo->pboundinglines[1].point[1].y - pInfo->pboundinglines[1].point[0].y) * dDeltaX + pInfo->pboundinglines[1].point[0].y;
shape->line->numpoints = (int) 2;
shape->line->point = (pointObj *) msSmallMalloc( sizeof( pointObj ) * 2 );
shape->line->point[0].x = pInfo->dwhichlongitude;
shape->line->point[0].y = dStartY;
shape->line->point[1].x = pInfo->dwhichlongitude;
shape->line->point[1].y = dStartY + dArcDelta;
_FormatLabel( layer, shape, shape->line->point[0].x );
if(_AdjustLabelPosition( layer, shape, posBottom ) != MS_SUCCESS)
return MS_FAILURE;
pInfo->ilabelstate++;
return MS_SUCCESS;
case 1:
if(!pInfo->blabelaxes) { /* Top */
pInfo->ilabelstate++;
shape->numlines = 0;
return MS_SUCCESS;
}
dDeltaX = (pInfo->dwhichlongitude - pInfo->pboundinglines[0].point[0].x) / (pInfo->pboundinglines[0].point[1].x - pInfo->pboundinglines[0].point[0].x );
if (dDeltaX < 0)
dDeltaX=dDeltaX*-1;
dStartY = (pInfo->pboundinglines[0].point[1].y - pInfo->pboundinglines[0].point[0].y) * dDeltaX + pInfo->pboundinglines[0].point[1].y;
shape->line->numpoints = (int) 2;
shape->line->point = (pointObj *) msSmallMalloc( sizeof( pointObj ) * 2 );
shape->line->point[0].x = pInfo->dwhichlongitude;
shape->line->point[0].y = dStartY - dArcDelta;
shape->line->point[1].x = pInfo->dwhichlongitude;
shape->line->point[1].y = dStartY;
_FormatLabel( layer, shape, shape->line->point[0].x );
if(_AdjustLabelPosition( layer, shape, posTop ) != MS_SUCCESS)
return MS_FAILURE;
pInfo->ilabelstate++;
return MS_SUCCESS;
case 2:
shape->line->numpoints = (int) shape->line->numpoints + 1;
shape->line->point = (pointObj *) msSmallMalloc( sizeof( pointObj ) * shape->line->numpoints );
shape->line->point[0].x = pInfo->dwhichlongitude;
shape->line->point[0].y = pInfo->dstartlatitude;
for( iPointIndex = 1; iPointIndex < shape->line->numpoints; iPointIndex++ ) {
shape->line->point[iPointIndex].x = pInfo->dwhichlongitude;
shape->line->point[iPointIndex].y = dArcPosition;
dArcPosition += dArcDelta;
}
pInfo->ilabelstate = 0;
pInfo->dwhichlongitude += pInfo->dincrementlongitude;
break;
default:
pInfo->ilabelstate = 0;
break;
}
} else { /*horizontal*/
int iPointIndex;
double dArcDelta = (pInfo->dendlongitude - pInfo->dstartlongitude) / (double) shape->line->numpoints;
double dArcPosition = pInfo->dstartlongitude + dArcDelta;
double dStartX, dDeltaY;
switch( pInfo->ilabelstate ) {
case 0:
if(!pInfo->blabelaxes) { /* Left side */
pInfo->ilabelstate++;
shape->numlines = 0;
return MS_SUCCESS;
}
dDeltaY = (pInfo->dwhichlatitude - pInfo->pboundinglines[2].point[0].y) / (pInfo->pboundinglines[2].point[1].y - pInfo->pboundinglines[2].point[0].y );
if (dDeltaY < 0)
dDeltaY= dDeltaY*-1;
dStartX = (pInfo->pboundinglines[2].point[1].x - pInfo->pboundinglines[2].point[0].x) * dDeltaY + pInfo->pboundinglines[2].point[0].x;
shape->line->numpoints = (int) 2;
shape->line->point = (pointObj *) msSmallMalloc( sizeof( pointObj ) * 2 );
shape->line->point[0].x = dStartX;
shape->line->point[0].y = pInfo->dwhichlatitude;
shape->line->point[1].x = dStartX + dArcDelta;
shape->line->point[1].y = pInfo->dwhichlatitude;
_FormatLabel( layer, shape, shape->line->point[0].y );
if(_AdjustLabelPosition( layer, shape, posLeft ) != MS_SUCCESS)
return MS_FAILURE;
pInfo->ilabelstate++;
return MS_SUCCESS;
case 1:
if(!pInfo->blabelaxes) { /* Right side */
pInfo->ilabelstate++;
shape->numlines = 0;
return MS_SUCCESS;
}
dDeltaY = (pInfo->dwhichlatitude - pInfo->pboundinglines[3].point[0].y) / (pInfo->pboundinglines[3].point[1].y - pInfo->pboundinglines[3].point[0].y );
if (dDeltaY < 0)
dDeltaY= dDeltaY*-1;
dStartX = (pInfo->pboundinglines[3].point[1].x - pInfo->pboundinglines[3].point[0].x) * dDeltaY + pInfo->pboundinglines[3].point[0].x;
shape->line->numpoints = (int) 2;
shape->line->point = (pointObj *) msSmallMalloc( sizeof( pointObj ) * 2 );
shape->line->point[0].x = dStartX - dArcDelta;
shape->line->point[0].y = pInfo->dwhichlatitude;
shape->line->point[1].x = dStartX;
shape->line->point[1].y = pInfo->dwhichlatitude;
_FormatLabel( layer, shape, shape->line->point[0].y );
if(_AdjustLabelPosition( layer, shape, posRight ) != MS_SUCCESS)
return MS_FAILURE;
pInfo->ilabelstate++;
return MS_SUCCESS;
case 2:
shape->line->numpoints = (int) shape->line->numpoints + 1;
shape->line->point = (pointObj *) msSmallMalloc( sizeof( pointObj ) * shape->line->numpoints );
shape->line->point[0].x = pInfo->dstartlongitude;
shape->line->point[0].y = pInfo->dwhichlatitude;
for(iPointIndex = 1; iPointIndex < shape->line->numpoints; iPointIndex++) {
shape->line->point[iPointIndex].x = dArcPosition;
shape->line->point[iPointIndex].y = pInfo->dwhichlatitude;
dArcPosition += dArcDelta;
}
pInfo->ilabelstate = 0;
pInfo->dwhichlatitude += pInfo->dincrementlatitude;
break;
default:
pInfo->ilabelstate = 0;
break;
}
}
/*
* Increment and move to next arc
*/
if( pInfo->bvertical && pInfo->dwhichlongitude > pInfo->dendlongitude ) {
pInfo->dwhichlatitude = pInfo->dstartlatitude;
pInfo->bvertical = 0;
}
if (pInfo->dwhichlatitude > pInfo->dendlatitude) {
/* free the lineObj and pointObj that have been erroneously allocated beforehand */
msFreeShape(shape);
return MS_DONE;
}
return MS_SUCCESS;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerGetItems(layerObj *layer)
{
char **ppItemName = (char **) msSmallMalloc( sizeof( char * ) );
*ppItemName = (char *) msSmallMalloc( 64 ); /* why is this necessary? */
strcpy( *ppItemName, "Graticule" );
layer->numitems = 1;
layer->items = ppItemName;
return MS_SUCCESS;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerInitItemInfo(layerObj *layer)
{
return MS_SUCCESS;
}
/**********************************************************************************************************************
*
*/
void msGraticuleLayerFreeItemInfo(layerObj *layer)
{
return;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerGetShape(layerObj *layer, shapeObj *shape, resultObj *record)
{
return MS_FAILURE;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerGetExtent(layerObj *layer, rectObj *extent)
{
graticuleObj *pInfo = (graticuleObj *) layer->layerinfo;
if(pInfo) {
*extent = pInfo->extent;
return MS_SUCCESS;
}
return MS_FAILURE;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerGetAutoStyle(mapObj *map, layerObj *layer, classObj *c, shapeObj* shape)
{
return MS_SUCCESS;
}
/************************************************************************/
/* msGraticuleLayerFreeIntersectionPoints */
/* */
/* Free intersection object. */
/************************************************************************/
void msGraticuleLayerFreeIntersectionPoints( graticuleIntersectionObj *psValue)
{
int i=0;
if (psValue) {
for (i=0; i<psValue->nTop; i++)
msFree(psValue->papszTopLabels[i]);
msFree(psValue->papszTopLabels);
msFree(psValue->pasTop);
for (i=0; i<psValue->nBottom; i++)
msFree(psValue->papszBottomLabels[i]);
msFree(psValue->papszBottomLabels);
msFree(psValue->pasBottom);
for (i=0; i<psValue->nLeft; i++)
msFree(psValue->papszLeftLabels[i]);
msFree(psValue->papszLeftLabels);
msFree(psValue->pasLeft);
for (i=0; i<psValue->nRight; i++)
msFree(psValue->papszRightLabels[i]);
msFree(psValue->papszRightLabels);
msFree(psValue->pasRight);
msFree(psValue);
}
}
/************************************************************************/
/* msGraticuleLayerInitIntersectionPoints */
/* */
/* init intersection object. */
/************************************************************************/
static void msGraticuleLayerInitIntersectionPoints( graticuleIntersectionObj *psValue)
{
if (psValue) {
psValue->nTop = 0;
psValue->pasTop = NULL;
psValue->papszTopLabels = NULL;
psValue->nBottom = 0;
psValue->pasBottom = NULL;
psValue->papszBottomLabels = NULL;
psValue->nLeft = 0;
psValue->pasLeft = NULL;
psValue->papszLeftLabels = NULL;
psValue->nRight = 0;
psValue->pasRight = NULL;
psValue->papszRightLabels = NULL;
}
}
/************************************************************************/
/* msGraticuleLayerGetIntersectionPoints */
/* */
/* Utility function thar returns all intersection positions and */
/* labels (4 sides of the map) for a grid layer. */
/************************************************************************/
graticuleIntersectionObj *msGraticuleLayerGetIntersectionPoints(mapObj *map,
layerObj *layer)
{
shapeObj shapegrid, tmpshape;
rectObj searchrect;
int status;
pointObj oFirstPoint;
pointObj oLastPoint;
lineObj oLineObj;
rectObj cliprect;
graticuleObj *pInfo = NULL;
double dfTmp;
graticuleIntersectionObj *psValues = NULL;
int i=0;
if (layer->connectiontype != MS_GRATICULE)
return NULL;
pInfo = (graticuleObj *) layer->layerinfo;
/*set cellsize if bnot already set*/
if (map->cellsize == 0)
map->cellsize = msAdjustExtent(&(map->extent),map->width,map->height);
psValues = (graticuleIntersectionObj *)msSmallMalloc(sizeof(graticuleIntersectionObj));
msGraticuleLayerInitIntersectionPoints(psValues);
if(layer->transform == MS_TRUE)
searchrect = map->extent;
else {
searchrect.minx = searchrect.miny = 0;
searchrect.maxx = map->width-1;
searchrect.maxy = map->height-1;
}
#ifdef USE_PROJ
if((map->projection.numargs > 0) && (layer->projection.numargs > 0))
msProjectRect(&map->projection, &layer->projection, &searchrect); /* project the searchrect to source coords */
#endif
msLayerOpen(layer);
status = msLayerWhichShapes(layer, searchrect, MS_FALSE);
if(status == MS_DONE) { /* no overlap */
msLayerClose(layer);
return NULL;
} else if(status != MS_SUCCESS) {
msLayerClose(layer);
return NULL;
}
/* step through the target shapes */
msInitShape(&shapegrid);
cliprect.minx = map->extent.minx- map->cellsize;
cliprect.miny = map->extent.miny- map->cellsize;
cliprect.maxx = map->extent.maxx + map->cellsize;
cliprect.maxy = map->extent.maxy + map->cellsize;
/* clip using the layer projection */
/* msProjectRect(&map->projection , &layer->projection, &cliprect); */
while((status = msLayerNextShape(layer, &shapegrid)) == MS_SUCCESS) {
/*don't really need a class here*/
/*
shapegrid.classindex = msShapeGetClass(layer, &shapegrid, map->scaledenom, NULL, 0);
if((shapegrid.classindex == -1) || (layer->class[shapegrid.classindex]->status == MS_OFF)) {
msFreeShape(&shapegrid);
continue;
}
*/
msInitShape(&tmpshape);
msCopyShape(&shapegrid, &tmpshape);
/* status = msDrawShape(map, layer, &tmpshape, image, -1); */
if(layer->project && msProjectionsDiffer(&(layer->projection), &(map->projection)))
msProjectShape(&layer->projection, &map->projection, &shapegrid);
msClipPolylineRect(&shapegrid, cliprect);
msTransformShapeToPixelRound(&shapegrid, map->extent, map->cellsize);
if(shapegrid.numlines <= 0 || shapegrid.line[0].numpoints < 2) { /* once clipped the shape didn't need to be drawn */
msFreeShape(&shapegrid);
msFreeShape(&tmpshape);
continue;
}
if(shapegrid.numlines >= 1 && shapegrid.line[0].numpoints >=2) { /* && shapegrid.text) */
int iTmpLine = 0;
int nNumPoints = 0;
/*grid code seems to retunr lines that can double cross the extenst??*/
/*creating a more than one clipped shape. Take the shape that has the most
points, which should be the most likley to be correct*/
if (shapegrid.numlines > 1) {
for (i=0; i<shapegrid.numlines; i++) {
if (shapegrid.line[i].numpoints > nNumPoints) {
nNumPoints = shapegrid.line[i].numpoints;
iTmpLine = i;
}
}
}
/* get the first and last point*/
oFirstPoint.x = shapegrid.line[iTmpLine].point[0].x;
oFirstPoint.y = shapegrid.line[iTmpLine].point[0].y;
oLineObj = shapegrid.line[iTmpLine];
oLastPoint.x = oLineObj.point[oLineObj.numpoints-1].x;
oLastPoint.y = oLineObj.point[oLineObj.numpoints-1].y;
if ( pInfo->bvertical) { /*vertical*/
/*SHAPES ARE DRAWN FROM BOTTOM TO TOP.*/
/*Normally lines are drawn FROM BOTTOM TO TOP but not always for some reason, so
make sure that firstpoint < lastpoint in y, We are in pixel coordinates so y increases as we
we go down*/
if (oFirstPoint.y < oLastPoint.y) {
dfTmp = oFirstPoint.x;
oFirstPoint.x = oLastPoint.x;
oLastPoint.x = dfTmp;
dfTmp = oFirstPoint.y;
oFirstPoint.y = oLastPoint.y;
oLastPoint.y = dfTmp;
}
/*first point should cross the BOTTOM base where y== map->height*/
if (abs ((int)oFirstPoint.y - map->height) <=1) {
char *pszLabel=NULL;
oFirstPoint.y = map->height;
/*validate point is in map width/height*/
if (oFirstPoint.x < 0 || oFirstPoint.x > map->width)
continue;
/*validate point is in map width/height*/
if (oLastPoint.x < 0 || oLastPoint.x > map->width)
continue;
if (shapegrid.text)
pszLabel = msStrdup(shapegrid.text);
else {
_FormatLabel(layer, &tmpshape, tmpshape.line[0].point[tmpshape.line[0].numpoints-1].x );
if (tmpshape.text)
pszLabel = msStrdup(tmpshape.text);
else
pszLabel = msStrdup("unknown");
}
/*validate that the value is not already in the array*/
if ( psValues->nBottom > 0) {
/* if (psValues->pasBottom[psValues->nBottom-1].x == oFirstPoint.x)
continue; */
for (i=0; i<psValues->nBottom; i++) {
if (psValues->pasBottom[i].x == oFirstPoint.x)
break;
}
if (i < psValues->nBottom)
continue;
}
if (psValues->pasBottom == NULL) {
psValues->pasBottom = (pointObj *)msSmallMalloc(sizeof(pointObj));
psValues->papszBottomLabels = (char **)msSmallMalloc(sizeof(char *));
psValues->nBottom = 1;
} else {
psValues->nBottom++;
psValues->pasBottom = (pointObj *)msSmallRealloc(psValues->pasBottom, sizeof(pointObj)*psValues->nBottom);
psValues->papszBottomLabels = (char **)msSmallRealloc(psValues->papszBottomLabels, sizeof(char *)*psValues->nBottom);
}
psValues->pasBottom[psValues->nBottom-1].x = oFirstPoint.x;
psValues->pasBottom[psValues->nBottom-1].y = oFirstPoint.y;
psValues->papszBottomLabels[psValues->nBottom-1] = msStrdup(pszLabel);
msFree(pszLabel);
}
/*first point should cross the TOP base where y==0*/
if (abs((int)oLastPoint.y) <=1) {
char *pszLabel=NULL;
oLastPoint.y = 0;
/*validate point is in map width/height*/
if (oLastPoint.x < 0 || oLastPoint.x > map->width)
continue;
if (shapegrid.text)
pszLabel = msStrdup(shapegrid.text);
else {
_FormatLabel(layer, &tmpshape, tmpshape.line[0].point[tmpshape.line[0].numpoints-1].x );
if (tmpshape.text)
pszLabel = msStrdup(tmpshape.text);
else
pszLabel = msStrdup("unknown");
}
/*validate if same value is not already there*/
if ( psValues->nTop > 0) {
/* if (psValues->pasTop[psValues->nTop-1].x == oLastPoint.x)
continue; */
for (i=0; i<psValues->nTop; i++) {
if (psValues->pasTop[i].x == oLastPoint.x ||
strcasecmp(pszLabel, psValues->papszTopLabels[i]) == 0)
break;
}
if (i < psValues->nTop)
continue;
}
if (psValues->pasTop == NULL) {
psValues->pasTop = (pointObj *)msSmallMalloc(sizeof(pointObj));
psValues->papszTopLabels = (char **)msSmallMalloc(sizeof(char *));
psValues->nTop = 1;
} else {
psValues->nTop++;
psValues->pasTop = (pointObj *)msSmallRealloc(psValues->pasTop, sizeof(pointObj)*psValues->nTop);
psValues->papszTopLabels = (char **)msSmallRealloc(psValues->papszTopLabels, sizeof(char *)*psValues->nTop);
}
psValues->pasTop[psValues->nTop-1].x = oLastPoint.x;
psValues->pasTop[psValues->nTop-1].y = oLastPoint.y;
psValues->papszTopLabels[psValues->nTop-1] = msStrdup(pszLabel);
msFree(pszLabel);
}
} else { /*horzontal*/
/*Normally lines are drawn from left to right but not always for some reason, so
make sure that firstpoint < lastpoint in x*/
if (oFirstPoint.x > oLastPoint.x) {
dfTmp = oFirstPoint.x;
oFirstPoint.x = oLastPoint.x;
oLastPoint.x = dfTmp;
dfTmp = oFirstPoint.y;
oFirstPoint.y = oLastPoint.y;
oLastPoint.y = dfTmp;
}
/*first point should cross the LEFT base where x=0 axis*/
if (abs((int)oFirstPoint.x) <=1) {
char *pszLabel=NULL;
oFirstPoint.x = 0;
/*validate point is in map width/height*/
if (oFirstPoint.y < 0 || oFirstPoint.y > map->height)
continue;
if (shapegrid.text)
pszLabel = msStrdup(shapegrid.text);
else {
_FormatLabel(layer, &tmpshape, tmpshape.line[0].point[tmpshape.line[0].numpoints-1].y );
if (tmpshape.text)
pszLabel = msStrdup(tmpshape.text);
else
pszLabel = msStrdup("unknown");
}
/*validate that the previous value is not the same*/
if ( psValues->nLeft > 0) {
/* if (psValues->pasLeft[psValues->nLeft-1].y == oFirstPoint.y)
continue; */
for (i=0; i<psValues->nLeft; i++) {
if (psValues->pasLeft[i].y == oFirstPoint.y)
break;
}
if (i < psValues->nLeft)
continue;
}
if (psValues->pasLeft == NULL) {
psValues->pasLeft = (pointObj *)msSmallMalloc(sizeof(pointObj));
psValues->papszLeftLabels = (char **)msSmallMalloc(sizeof(char *));
psValues->nLeft = 1;
} else {
psValues->nLeft++;
psValues->pasLeft = (pointObj *)msSmallRealloc(psValues->pasLeft, sizeof(pointObj)*psValues->nLeft);
psValues->papszLeftLabels = (char **)msSmallRealloc(psValues->papszLeftLabels, sizeof(char *)*psValues->nLeft);
}
psValues->pasLeft[psValues->nLeft-1].x = oFirstPoint.x;
psValues->pasLeft[psValues->nLeft-1].y = oFirstPoint.y;
psValues->papszLeftLabels[psValues->nLeft-1] = msStrdup(pszLabel);
msFree(pszLabel);
}
/*first point should cross the RIGHT base where x=map=>width axis*/
if (abs((int)oLastPoint.x - map->width) <=1) {
char *pszLabel=NULL;
oLastPoint.x = map->width;
/*validate point is in map width/height*/
if (oLastPoint.y < 0 || oLastPoint.y > map->height)
continue;
if (shapegrid.text)
pszLabel = msStrdup(shapegrid.text);
else {
_FormatLabel(layer, &tmpshape, tmpshape.line[0].point[tmpshape.line[0].numpoints-1].y );
if (tmpshape.text)
pszLabel = msStrdup(tmpshape.text);
else
pszLabel = msStrdup("unknown");
}
/*validate that the previous value is not the same*/
if ( psValues->nRight > 0) {
/* if (psValues->pasRight[psValues->nRight-1].y == oLastPoint.y)
continue; */
for (i=0; i<psValues->nRight; i++) {
if (psValues->pasRight[i].y == oLastPoint.y)
break;
}
if (i < psValues->nRight)
continue;
}
if (psValues->pasRight == NULL) {
psValues->pasRight = (pointObj *)msSmallMalloc(sizeof(pointObj));
psValues->papszRightLabels = (char **)msSmallMalloc(sizeof(char *));
psValues->nRight = 1;
} else {
psValues->nRight++;
psValues->pasRight = (pointObj *)msSmallRealloc(psValues->pasRight, sizeof(pointObj)*psValues->nRight);
psValues->papszRightLabels = (char **)msSmallRealloc(psValues->papszRightLabels, sizeof(char *)*psValues->nRight);
}
psValues->pasRight[psValues->nRight-1].x = oLastPoint.x;
psValues->pasRight[psValues->nRight-1].y = oLastPoint.y;
psValues->papszRightLabels[psValues->nRight-1] = msStrdup(pszLabel);
msFree(pszLabel);
}
}
msFreeShape(&shapegrid);
msFreeShape(&tmpshape);
}
msInitShape(&shapegrid);
}
msLayerClose(layer);
return psValues;
}
/**********************************************************************************************************************
*
*/
int msGraticuleLayerInitializeVirtualTable(layerObj *layer)
{
assert(layer != NULL);
assert(layer->vtable != NULL);
layer->vtable->LayerInitItemInfo = msGraticuleLayerInitItemInfo; /* should use defaults for item info functions */
layer->vtable->LayerFreeItemInfo = msGraticuleLayerFreeItemInfo;
layer->vtable->LayerOpen = msGraticuleLayerOpen;
layer->vtable->LayerIsOpen = msGraticuleLayerIsOpen;
layer->vtable->LayerWhichShapes = msGraticuleLayerWhichShapes;
layer->vtable->LayerNextShape = msGraticuleLayerNextShape;
/* layer->vtable->LayerResultsGetShape, use default */
layer->vtable->LayerGetShape = msGraticuleLayerGetShape;
layer->vtable->LayerClose = msGraticuleLayerClose;
layer->vtable->LayerGetItems = msGraticuleLayerGetItems;
layer->vtable->LayerGetExtent = msGraticuleLayerGetExtent;
layer->vtable->LayerGetAutoStyle = msGraticuleLayerGetAutoStyle;
/* layer->vtable->LayerCloseConnection, use default */;
layer->vtable->LayerSetTimeFilter = msLayerMakePlainTimeFilter;
/* layer->vtable->LayerApplyFilterToLayer, use default */
/* layer->vtable->LayerCreateItems, use default */
/* layer->vtable->LayerGetNumFeatures, use default */