forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapdraw.c
3675 lines (3235 loc) · 144 KB
/
mapdraw.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: High level msDrawMap() implementation and related functions.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* 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
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include <assert.h>
#include <math.h>
#include "mapserver.h"
#include "maptime.h"
#include "mapcopy.h"
#include "mapfile.h"
#include "mapows.h"
/* msPrepareImage()
*
* Returns a new imageObj ready for rendering the current map.
*
* If allow_nonsquare is set to MS_TRUE then the caller should call
* msMapRestoreRealExtent() once they are done with the image.
* This should be set to MS_TRUE only when called from msDrawMap(), see bug 945.
*/
imageObj *msPrepareImage(mapObj *map, int allow_nonsquare)
{
int i, status;
imageObj *image=NULL;
double geo_cellsize;
if(map->width == -1 || map->height == -1) {
msSetError(MS_MISCERR, "Image dimensions not specified.", "msPrepareImage()");
return(NULL);
}
msFreeLabelCache(&(map->labelcache));
msInitLabelCache(&(map->labelcache)); /* this clears any previously allocated cache */
/* clear any previously created mask layer images */
for(i=0; i<map->numlayers; i++) {
if(GET_LAYER(map, i)->maskimage) {
msFreeImage(GET_LAYER(map, i)->maskimage);
GET_LAYER(map, i)->maskimage = NULL;
}
}
status = msValidateContexts(map); /* make sure there are no recursive REQUIRES or LABELREQUIRES expressions */
if(status != MS_SUCCESS) return NULL;
if(!map->outputformat) {
msSetError(MS_IMGERR, "Map outputformat not set!", "msPrepareImage()");
return(NULL);
} else if (MS_RENDERER_PLUGIN(map->outputformat)) {
rendererVTableObj *renderer = map->outputformat->vtable;
colorObj *bg = &map->imagecolor;
map->imagecolor.alpha=255;
if(map->transparent == MS_TRUE) {
/* don't set the image color */
bg = NULL;
}
image = renderer->createImage(map->width, map->height, map->outputformat,bg);
if (image == NULL)
return(NULL);
image->format = map->outputformat;
image->format->refcount++;
image->width = map->width;
image->height = map->height;
image->resolution = map->resolution;
image->resolutionfactor = map->resolution/map->defresolution;
if (map->web.imagepath)
image->imagepath = msStrdup(map->web.imagepath);
if (map->web.imageurl)
image->imageurl = msStrdup(map->web.imageurl);
} else if( MS_RENDERER_IMAGEMAP(map->outputformat) ) {
image = msImageCreateIM(map->width, map->height, map->outputformat,
map->web.imagepath, map->web.imageurl, map->resolution, map->defresolution);
} else if( MS_RENDERER_RAWDATA(map->outputformat) ) {
image = msImageCreate(map->width, map->height, map->outputformat,
map->web.imagepath, map->web.imageurl, map->resolution, map->defresolution, &map->imagecolor);
} else {
image = NULL;
}
if(!image) {
msSetError(MS_IMGERR, "Unable to initialize image.", "msPrepareImage()");
return(NULL);
}
image->map = map;
/*
* If we want to support nonsquare pixels, note that now, otherwise
* adjust the extent size to have square pixels.
*
* If allow_nonsquare is set to MS_TRUE then the caller should call
* msMapRestoreRealExtent() once they are done with the image.
* This should be set to MS_TRUE only when called from msDrawMap(), see bug 945.
*/
if( allow_nonsquare && msTestConfigOption( map, "MS_NONSQUARE", MS_FALSE ) ) {
double cellsize_x = (map->extent.maxx - map->extent.minx)/map->width;
double cellsize_y = (map->extent.maxy - map->extent.miny)/map->height;
if( cellsize_y != 0.0
&& (fabs(cellsize_x/cellsize_y) > 1.00001
|| fabs(cellsize_x/cellsize_y) < 0.99999) ) {
map->gt.need_geotransform = MS_TRUE;
if (map->debug)
msDebug( "msDrawMap(): kicking into non-square pixel preserving mode.\n" );
}
map->cellsize = (cellsize_x*0.5 + cellsize_y*0.5);
} else
map->cellsize = msAdjustExtent(&(map->extent),map->width,map->height);
status = msCalculateScale(map->extent,map->units,map->width,map->height, map->resolution, &map->scaledenom);
if(status != MS_SUCCESS) {
msFreeImage(image);
return(NULL);
}
/* update geotransform based on adjusted extent. */
msMapComputeGeotransform( map );
/* Do we need to fake out stuff for rotated support? */
if( map->gt.need_geotransform )
msMapSetFakedExtent( map );
/* We will need a cellsize that represents a real georeferenced */
/* coordinate cellsize here, so compute it from saved extents. */
geo_cellsize = map->cellsize;
if( map->gt.need_geotransform == MS_TRUE ) {
double cellsize_x = (map->saved_extent.maxx - map->saved_extent.minx)
/ map->width;
double cellsize_y = (map->saved_extent.maxy - map->saved_extent.miny)
/ map->height;
geo_cellsize = sqrt(cellsize_x*cellsize_x + cellsize_y*cellsize_y)
/ sqrt(2.0);
}
/* compute layer/class/style/label scale factors now */
for(int lid=0; lid<map->numlayers; lid++) {
layerObj * layer = GET_LAYER(map, lid);
if(layer->sizeunits != MS_PIXELS)
layer->scalefactor = (msInchesPerUnit(layer->sizeunits,0)/msInchesPerUnit(map->units,0)) / geo_cellsize;
else if(layer->symbolscaledenom > 0 && map->scaledenom > 0)
layer->scalefactor = layer->symbolscaledenom/map->scaledenom*map->resolution/map->defresolution;
else
layer->scalefactor = map->resolution/map->defresolution;
for (int cid=0 ; cid<layer->numclasses ; cid++)
{
classObj * class = GET_CLASS(map, lid, cid);
if (class->sizeunits == MS_INHERIT)
class->scalefactor = layer->scalefactor;
else if (class->sizeunits != MS_PIXELS)
class->scalefactor = (msInchesPerUnit(class->sizeunits,0)/msInchesPerUnit(map->units,0)) / geo_cellsize;
else if (layer->symbolscaledenom > 0 && map->scaledenom > 0)
class->scalefactor = layer->symbolscaledenom/map->scaledenom*map->resolution/map->defresolution;
else
class->scalefactor = map->resolution/map->defresolution;
for (int sid=0 ; sid<class->numstyles ; sid++)
{
styleObj * style = class->styles[sid];
if (style->sizeunits == MS_INHERIT)
style->scalefactor = class->scalefactor;
else if (style->sizeunits != MS_PIXELS)
style->scalefactor = (msInchesPerUnit(style->sizeunits,0)/msInchesPerUnit(map->units,0)) / geo_cellsize;
else if (layer->symbolscaledenom > 0 && map->scaledenom > 0)
style->scalefactor = layer->symbolscaledenom/map->scaledenom*map->resolution/map->defresolution;
else
style->scalefactor = map->resolution/map->defresolution;
}
for (int sid=0 ; sid<class->numlabels ; sid++)
{
labelObj * label = class->labels[sid];
if (label->sizeunits == MS_INHERIT)
label->scalefactor = class->scalefactor;
else if (label->sizeunits != MS_PIXELS)
label->scalefactor = (msInchesPerUnit(label->sizeunits,0)/msInchesPerUnit(map->units,0)) / geo_cellsize;
else if (layer->symbolscaledenom > 0 && map->scaledenom > 0)
label->scalefactor = layer->symbolscaledenom/map->scaledenom*map->resolution/map->defresolution;
else
label->scalefactor = map->resolution/map->defresolution;
for (int lsid=0 ; lsid<label->numstyles ; lsid++)
{
styleObj * lstyle = label->styles[lsid];
if (lstyle->sizeunits == MS_INHERIT)
lstyle->scalefactor = label->scalefactor;
else if (lstyle->sizeunits != MS_PIXELS)
lstyle->scalefactor = (msInchesPerUnit(lstyle->sizeunits,0)/msInchesPerUnit(map->units,0)) / geo_cellsize;
else if (layer->symbolscaledenom > 0 && map->scaledenom > 0)
lstyle->scalefactor = layer->symbolscaledenom/map->scaledenom*map->resolution/map->defresolution;
else
lstyle->scalefactor = map->resolution/map->defresolution;
}
}
}
}
image->refpt.x = MS_MAP2IMAGE_X_IC_DBL(0, map->extent.minx, 1.0/map->cellsize);
image->refpt.y = MS_MAP2IMAGE_Y_IC_DBL(0, map->extent.maxy, 1.0/map->cellsize);
return image;
}
static int msCompositeRasterBuffer(mapObj *map, imageObj *img, rasterBufferObj *rb, LayerCompositer *comp) {
int ret = MS_SUCCESS;
if(MS_IMAGE_RENDERER(img)->compositeRasterBuffer) {
while(comp && ret == MS_SUCCESS) {
rasterBufferObj *rb_ptr = rb;
CompositingFilter *filter = comp->filter;
if(filter && comp->next) {
/* if we have another compositor to apply, then we need to copy the rasterBufferObj. Otherwise
* we can work on it directly */
rb_ptr = (rasterBufferObj*)msSmallCalloc(sizeof(rasterBufferObj),1);
msCopyRasterBuffer(rb_ptr,rb);
}
while(filter && ret == MS_SUCCESS) {
ret = msApplyCompositingFilter(map,rb_ptr,filter);
filter = filter->next;
}
if(ret == MS_SUCCESS)
ret = MS_IMAGE_RENDERER(img)->compositeRasterBuffer(img,rb_ptr,comp->comp_op, comp->opacity);
if(rb_ptr != rb) {
msFreeRasterBuffer(rb_ptr);
msFree(rb_ptr);
}
comp = comp->next;
}
} else {
ret = MS_FAILURE;
}
return ret;
}
/*
* Generic function to render the map file.
* The type of the image created is based on the imagetype parameter in the map file.
*
* mapObj *map - map object loaded in MapScript or via a mapfile to use
* int querymap - is this map the result of a query operation, MS_TRUE|MS_FALSE
*/
imageObj *msDrawMap(mapObj *map, int querymap)
{
int i;
layerObj *lp=NULL;
int status = MS_FAILURE;
imageObj *image = NULL;
struct mstimeval mapstarttime = {0}, mapendtime = {0};
struct mstimeval starttime = {0}, endtime = {0};
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
enum MS_CONNECTION_TYPE lastconnectiontype;
httpRequestObj *pasOWSReqInfo=NULL;
int numOWSLayers=0;
int numOWSRequests=0;
wmsParamsObj sLastWMSParams;
#endif
if(map->debug >= MS_DEBUGLEVEL_TUNING) msGettimeofday(&mapstarttime, NULL);
if(querymap) { /* use queryMapObj image dimensions */
if(map->querymap.width != -1) map->width = map->querymap.width;
if(map->querymap.height != -1) map->height = map->querymap.height;
}
msApplyMapConfigOptions(map);
image = msPrepareImage(map, MS_TRUE);
if(!image) {
msSetError(MS_IMGERR, "Unable to initialize image.", "msDrawMap()");
return(NULL);
}
if( map->debug >= MS_DEBUGLEVEL_DEBUG )
msDebug( "msDrawMap(): rendering using outputformat named %s (%s).\n",
map->outputformat->name,
map->outputformat->driver );
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
/* Time the OWS query phase */
if(map->debug >= MS_DEBUGLEVEL_TUNING ) msGettimeofday(&starttime, NULL);
/* How many OWS (WMS/WFS) layers do we have to draw?
* Note: numOWSLayers is the number of actual layers and numOWSRequests is
* the number of HTTP requests which could be lower if multiple layers
* are merged into the same request.
*/
numOWSLayers=0;
for(i=0; i<map->numlayers; i++) {
if(map->layerorder[i] == -1 )
continue;
lp = GET_LAYER(map,map->layerorder[i]);
if( lp->connectiontype != MS_WMS &&
lp->connectiontype != MS_WFS ) {
continue;
}
numOWSLayers++;
}
if (numOWSLayers > 0) {
/* Alloc and init pasOWSReqInfo...
*/
pasOWSReqInfo = (httpRequestObj *)malloc(numOWSLayers*sizeof(httpRequestObj));
if (pasOWSReqInfo == NULL) {
msSetError(MS_MEMERR, "Allocation of httpRequestObj failed.", "msDrawMap()");
return NULL;
}
msHTTPInitRequestObj(pasOWSReqInfo, numOWSLayers);
msInitWmsParamsObj(&sLastWMSParams);
/* Pre-download all WMS/WFS layers in parallel before starting to draw map */
lastconnectiontype = MS_SHAPEFILE;
for(i=0; i<map->numlayers; i++) {
if(map->layerorder[i] == -1 )
continue;
lp = GET_LAYER(map,map->layerorder[i]);
if( lp->connectiontype != MS_WMS &&
lp->connectiontype != MS_WFS ) {
continue;
}
if( !msLayerIsVisible(map, lp) )
continue;
#ifdef USE_WMS_LYR
if(lp->connectiontype == MS_WMS) {
if(msPrepareWMSLayerRequest(map->layerorder[i], map, lp, 1, lastconnectiontype, &sLastWMSParams, 0, 0, 0, NULL, pasOWSReqInfo, &numOWSRequests) == MS_FAILURE) {
msFreeWmsParamsObj(&sLastWMSParams);
msFreeImage(image);
msFree(pasOWSReqInfo);
return NULL;
}
}
#endif
#ifdef USE_WFS_LYR
if(lp->connectiontype == MS_WFS) {
if(msPrepareWFSLayerRequest(map->layerorder[i], map, lp, pasOWSReqInfo, &numOWSRequests) == MS_FAILURE) {
msFreeWmsParamsObj(&sLastWMSParams);
msFreeImage(image);
msFree(pasOWSReqInfo);
return NULL;
}
}
#endif
lastconnectiontype = lp->connectiontype;
}
msFreeWmsParamsObj(&sLastWMSParams);
} /* if numOWSLayers > 0 */
if(numOWSRequests && msOWSExecuteRequests(pasOWSReqInfo, numOWSRequests, map, MS_TRUE) == MS_FAILURE) {
msFreeImage(image);
msFree(pasOWSReqInfo);
return NULL;
}
if(map->debug >= MS_DEBUGLEVEL_TUNING) {
msGettimeofday(&endtime, NULL);
msDebug("msDrawMap(): WMS/WFS set-up and query, %.3fs\n",
(endtime.tv_sec+endtime.tv_usec/1.0e6)-
(starttime.tv_sec+starttime.tv_usec/1.0e6) );
}
#endif /* USE_WMS_LYR || USE_WFS_LYR */
/* OK, now we can start drawing */
for(i=0; i<map->numlayers; i++) {
if(map->layerorder[i] != -1) {
const char *force_draw_label_cache = NULL;
lp = (GET_LAYER(map, map->layerorder[i]));
if(lp->postlabelcache) /* wait to draw */
continue;
if(map->debug >= MS_DEBUGLEVEL_TUNING || lp->debug >= MS_DEBUGLEVEL_TUNING ) msGettimeofday(&starttime, NULL);
if(!msLayerIsVisible(map, lp)) continue;
if(lp->connectiontype == MS_WMS) {
#ifdef USE_WMS_LYR
if(MS_RENDERER_PLUGIN(image->format) || MS_RENDERER_RAWDATA(image->format))
status = msDrawWMSLayerLow(map->layerorder[i], pasOWSReqInfo, numOWSRequests, map, lp, image);
else {
msSetError(MS_WMSCONNERR, "Output format '%s' doesn't support WMS layers.", "msDrawMap()", image->format->name);
status = MS_FAILURE;
}
if(status == MS_FAILURE) {
msSetError(MS_WMSCONNERR,
"Failed to draw WMS layer named '%s'. This most likely happened because "
"the remote WMS server returned an invalid image, and XML exception "
"or another unexpected result in response to the GetMap request. Also check "
"and make sure that the layer's connection URL is valid.",
"msDrawMap()", lp->name);
msFreeImage(image);
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
return(NULL);
}
#else /* ndef USE_WMS_LYR */
msSetError(MS_WMSCONNERR, "MapServer not built with WMS Client support, unable to render layer '%s'.", "msDrawMap()", lp->name);
msFreeImage(image);
return(NULL);
#endif
} else { /* Default case: anything but WMS layers */
if(querymap)
status = msDrawQueryLayer(map, lp, image);
else
status = msDrawLayer(map, lp, image);
if(status == MS_FAILURE) {
msSetError(MS_IMGERR, "Failed to draw layer named '%s'.", "msDrawMap()", lp->name);
msFreeImage(image);
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif /* USE_WMS_LYR || USE_WFS_LYR */
return(NULL);
}
}
if(map->debug >= MS_DEBUGLEVEL_TUNING || lp->debug >= MS_DEBUGLEVEL_TUNING) {
msGettimeofday(&endtime, NULL);
msDebug("msDrawMap(): Layer %d (%s), %.3fs\n",
map->layerorder[i], lp->name?lp->name:"(null)",
(endtime.tv_sec+endtime.tv_usec/1.0e6)-
(starttime.tv_sec+starttime.tv_usec/1.0e6) );
}
/* Flush layer cache in-between layers if requested by PROCESSING directive*/
force_draw_label_cache = msLayerGetProcessingKey(lp, "FORCE_DRAW_LABEL_CACHE");
if (force_draw_label_cache &&
strncasecmp(force_draw_label_cache,"FLUSH",5)==0) {
if(map->debug >= MS_DEBUGLEVEL_V)
msDebug("msDrawMap(): PROCESSING FORCE_DRAW_LABEL_CACHE=FLUSH found.\n");
if(msDrawLabelCache(map, image) != MS_SUCCESS) {
msFreeImage(image);
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif /* USE_WMS_LYR || USE_WFS_LYR */
return(NULL);
}
msFreeLabelCache(&(map->labelcache));
msInitLabelCache(&(map->labelcache));
} /* PROCESSING FORCE_DRAW_LABEL_CACHE */
}
}
if(map->scalebar.status == MS_EMBED && !map->scalebar.postlabelcache) {
/* We need to temporarily restore the original extent for drawing */
/* the scalebar as it uses the extent to recompute cellsize. */
if(map->gt.need_geotransform)
msMapRestoreRealExtent(map);
if(MS_SUCCESS != msEmbedScalebar(map, image)) {
msFreeImage( image );
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
/* Cleanup WMS/WFS Request stuff */
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif
return NULL;
}
if(map->gt.need_geotransform)
msMapSetFakedExtent(map);
}
if(map->legend.status == MS_EMBED && !map->legend.postlabelcache) {
if( msEmbedLegend(map, image) != MS_SUCCESS ) {
msFreeImage( image );
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
/* Cleanup WMS/WFS Request stuff */
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif
return NULL;
}
}
if(msDrawLabelCache(map, image) != MS_SUCCESS) {
msFreeImage(image);
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif /* USE_WMS_LYR || USE_WFS_LYR */
return(NULL);
}
for(i=0; i<map->numlayers; i++) { /* for each layer, check for postlabelcache layers */
lp = (GET_LAYER(map, map->layerorder[i]));
if(!lp->postlabelcache) continue;
if(!msLayerIsVisible(map, lp)) continue;
if(map->debug >= MS_DEBUGLEVEL_TUNING || lp->debug >= MS_DEBUGLEVEL_TUNING) msGettimeofday(&starttime, NULL);
if(lp->connectiontype == MS_WMS) {
#ifdef USE_WMS_LYR
if(MS_RENDERER_PLUGIN(image->format) || MS_RENDERER_RAWDATA(image->format))
status = msDrawWMSLayerLow(map->layerorder[i], pasOWSReqInfo, numOWSRequests, map, lp, image);
#else
status = MS_FAILURE;
#endif
} else {
if(querymap)
status = msDrawQueryLayer(map, lp, image);
else
status = msDrawLayer(map, lp, image);
}
if(status == MS_FAILURE) {
msFreeImage(image);
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif /* USE_WMS_LYR || USE_WFS_LYR */
return(NULL);
}
if(map->debug >= MS_DEBUGLEVEL_TUNING || lp->debug >= MS_DEBUGLEVEL_TUNING) {
msGettimeofday(&endtime, NULL);
msDebug("msDrawMap(): Layer %d (%s), %.3fs\n",
map->layerorder[i], lp->name?lp->name:"(null)",
(endtime.tv_sec+endtime.tv_usec/1.0e6)-
(starttime.tv_sec+starttime.tv_usec/1.0e6) );
}
}
/* Do we need to fake out stuff for rotated support? */
/* This really needs to be done on every preceeding exit point too... */
if(map->gt.need_geotransform)
msMapRestoreRealExtent(map);
if(map->legend.status == MS_EMBED && map->legend.postlabelcache)
if(MS_UNLIKELY(MS_FAILURE == msEmbedLegend(map, image))) {
msFreeImage( image );
return NULL;
}
if(map->scalebar.status == MS_EMBED && map->scalebar.postlabelcache) {
/* We need to temporarily restore the original extent for drawing */
/* the scalebar as it uses the extent to recompute cellsize. */
if(map->gt.need_geotransform)
msMapRestoreRealExtent(map);
if(MS_SUCCESS != msEmbedScalebar(map, image)) {
msFreeImage( image );
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
/* Cleanup WMS/WFS Request stuff */
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif
return NULL;
}
if(map->gt.need_geotransform)
msMapSetFakedExtent(map);
}
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
/* Cleanup WMS/WFS Request stuff */
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif
if(map->debug >= MS_DEBUGLEVEL_TUNING) {
msGettimeofday(&mapendtime, NULL);
msDebug("msDrawMap() total time: %.3fs\n",
(mapendtime.tv_sec+mapendtime.tv_usec/1.0e6)-
(mapstarttime.tv_sec+mapstarttime.tv_usec/1.0e6) );
}
return(image);
}
/*
* Test whether a layer should be drawn or not in the current map view and
* at the current scale.
* Returns TRUE if layer is visible, FALSE if not.
*/
int msLayerIsVisible(mapObj *map, layerObj *layer)
{
int i;
if(!layer->data && !layer->tileindex && !layer->connection && !layer->features && !layer->grid)
return(MS_FALSE); /* no data associated with this layer, not an error since layer may be used as a template from MapScript */
if(layer->type == MS_LAYER_QUERY || layer->type == MS_LAYER_TILEINDEX) return(MS_FALSE);
if((layer->status != MS_ON) && (layer->status != MS_DEFAULT)) return(MS_FALSE);
/* Do comparisons of layer scale vs map scale now, since msExtentsOverlap() */
/* can be slow */
if(map->scaledenom > 0) {
/* layer scale boundaries should be checked first */
if((layer->maxscaledenom > 0) && (map->scaledenom > layer->maxscaledenom)) {
if( layer->debug >= MS_DEBUGLEVEL_V ) {
msDebug("msLayerIsVisible(): Skipping layer (%s) because LAYER.MAXSCALE is too small for this MAP scale\n", layer->name);
}
return(MS_FALSE);
}
if(/*(layer->minscaledenom > 0) &&*/ (map->scaledenom <= layer->minscaledenom)) {
if( layer->debug >= MS_DEBUGLEVEL_V ) {
msDebug("msLayerIsVisible(): Skipping layer (%s) because LAYER.MINSCALE is too large for this MAP scale\n", layer->name);
}
return(MS_FALSE);
}
}
/* Only return MS_FALSE if it is definitely false. Sometimes it will return MS_UNKNOWN, which we
** consider true, for this use case (it might be visible, try and draw it, see what happens). */
if ( msExtentsOverlap(map, layer) == MS_FALSE ) {
if( layer->debug >= MS_DEBUGLEVEL_V ) {
msDebug("msLayerIsVisible(): Skipping layer (%s) because LAYER.EXTENT does not intersect MAP.EXTENT\n", layer->name);
}
return(MS_FALSE);
}
if(msEvalContext(map, layer, layer->requires) == MS_FALSE) return(MS_FALSE);
if(map->scaledenom > 0) {
/* now check class scale boundaries (all layers *must* pass these tests) */
if(layer->numclasses > 0) {
for(i=0; i<layer->numclasses; i++) {
if((layer->class[i]->maxscaledenom > 0) && (map->scaledenom > layer->class[i]->maxscaledenom))
continue; /* can skip this one, next class */
if((layer->class[i]->minscaledenom > 0) && (map->scaledenom <= layer->class[i]->minscaledenom))
continue; /* can skip this one, next class */
break; /* can't skip this class (or layer for that matter) */
}
if(i == layer->numclasses) {
if( layer->debug >= MS_DEBUGLEVEL_V ) {
msDebug("msLayerIsVisible(): Skipping layer (%s) because no CLASS in the layer is in-scale for this MAP scale\n", layer->name);
}
return(MS_FALSE);
}
}
}
if (layer->maxscaledenom <= 0 && layer->minscaledenom <= 0) {
if((layer->maxgeowidth > 0) && ((map->extent.maxx - map->extent.minx) > layer->maxgeowidth)) {
if( layer->debug >= MS_DEBUGLEVEL_V ) {
msDebug("msLayerIsVisible(): Skipping layer (%s) because LAYER width is much smaller than map width\n", layer->name);
}
return(MS_FALSE);
}
if((layer->mingeowidth > 0) && ((map->extent.maxx - map->extent.minx) < layer->mingeowidth)) {
if( layer->debug >= MS_DEBUGLEVEL_V ) {
msDebug("msLayerIsVisible(): Skipping layer (%s) because LAYER width is much larger than map width\n", layer->name);
}
return(MS_FALSE);
}
}
return MS_TRUE; /* All tests passed. Layer is visible. */
}
#define LAYER_NEEDS_COMPOSITING(layer) (((layer)->compositer != NULL) && ((layer)->compositer->next || (layer)->compositor->opacity < 100 || (layer)->compositor->compop != MS_COMPOP_SRC_OVER || (layer)->compositer->filter ))
/*
* Generic function to render a layer object.
*/
int msDrawLayer(mapObj *map, layerObj *layer, imageObj *image)
{
imageObj *image_draw = image;
outputFormatObj *altFormat=NULL;
int retcode=MS_SUCCESS;
const char *alternativeFomatString = NULL;
layerObj *maskLayer = NULL;
if(!msLayerIsVisible(map, layer))
return MS_SUCCESS;
if(layer->compositer && !layer->compositer->next && layer->compositer->opacity == 0) return MS_SUCCESS; /* layer is completely transparent, skip it */
/* conditions may have changed since this layer last drawn, so retest
layer->project (Bug #673) */
layer->project = msProjectionsDiffer(&(layer->projection),&(map->projection));
/* make sure labelcache setting is set correctly if postlabelcache is set. This is done by the parser but
may have been altered by a mapscript. see #5142 */
if(layer->postlabelcache) {
layer->labelcache = MS_FALSE;
}
if(layer->mask) {
int maskLayerIdx;
/* render the mask layer in its own imageObj */
if(!MS_IMAGE_RENDERER(image)->supports_pixel_buffer) {
msSetError(MS_MISCERR, "Layer (%s) references references a mask layer, but the selected renderer does not support them", "msDrawLayer()",
layer->name);
return (MS_FAILURE);
}
maskLayerIdx = msGetLayerIndex(map,layer->mask);
if(maskLayerIdx == -1) {
msSetError(MS_MISCERR, "Layer (%s) references unknown mask layer (%s)", "msDrawLayer()",
layer->name,layer->mask);
return (MS_FAILURE);
}
maskLayer = GET_LAYER(map, maskLayerIdx);
if(!maskLayer->maskimage) {
int i;
int origstatus, origlabelcache;
altFormat = msSelectOutputFormat(map, "png24");
msInitializeRendererVTable(altFormat);
/* TODO: check the png24 format hasn't been tampered with, i.e. it's agg */
maskLayer->maskimage= msImageCreate(image->width, image->height,altFormat,
image->imagepath, image->imageurl, map->resolution, map->defresolution, NULL);
if (!maskLayer->maskimage) {
msSetError(MS_MISCERR, "Unable to initialize mask image.", "msDrawLayer()");
return (MS_FAILURE);
}
/*
* force the masked layer to status on, and turn off the labelcache so that
* eventual labels are added to the temporary image instead of being added
* to the labelcache
*/
origstatus = maskLayer->status;
origlabelcache = maskLayer->labelcache;
maskLayer->status = MS_ON;
maskLayer->labelcache = MS_OFF;
/* draw the mask layer in the temporary image */
retcode = msDrawLayer(map, maskLayer, maskLayer->maskimage);
maskLayer->status = origstatus;
maskLayer->labelcache = origlabelcache;
if(retcode != MS_SUCCESS) {
return MS_FAILURE;
}
/*
* hack to work around bug #3834: if we have use an alternate renderer, the symbolset may contain
* symbols that reference it. We want to remove those references before the altFormat is destroyed
* to avoid a segfault and/or a leak, and so the the main renderer doesn't pick the cache up thinking
* it's for him.
*/
for(i=0; i<map->symbolset.numsymbols; i++) {
if (map->symbolset.symbol[i]!=NULL) {
symbolObj *s = map->symbolset.symbol[i];
if(s->renderer == MS_IMAGE_RENDERER(maskLayer->maskimage)) {
MS_IMAGE_RENDERER(maskLayer->maskimage)->freeSymbol(s);
s->renderer = NULL;
}
}
}
/* set the imagetype from the original outputformat back (it was removed by msSelectOutputFormat() */
msFree(map->imagetype);
map->imagetype = msStrdup(image->format->name);
}
}
altFormat = NULL;
/* inform the rendering device that layer draw is starting. */
msImageStartLayer(map, layer, image);
/*check if an alternative renderer should be used for this layer*/
alternativeFomatString = msLayerGetProcessingKey( layer, "RENDERER");
if (MS_RENDERER_PLUGIN(image_draw->format) && alternativeFomatString!=NULL &&
(altFormat= msSelectOutputFormat(map, alternativeFomatString))) {
rendererVTableObj *renderer=NULL;
msInitializeRendererVTable(altFormat);
image_draw = msImageCreate(image->width, image->height,
altFormat, image->imagepath, image->imageurl, map->resolution, map->defresolution, &map->imagecolor);
renderer = MS_IMAGE_RENDERER(image_draw);
renderer->startLayer(image_draw,map,layer);
} else if (MS_RENDERER_PLUGIN(image_draw->format)) {
rendererVTableObj *renderer = MS_IMAGE_RENDERER(image_draw);
if ((layer->mask && layer->connectiontype!=MS_WMS && layer->type != MS_LAYER_RASTER) || layer->compositer) {
/* masking occurs at the pixel/layer level for raster images, so we don't need to create a temporary image
in these cases
*/
if (layer->mask || renderer->compositeRasterBuffer) {
image_draw = msImageCreate(image->width, image->height,
image->format, image->imagepath, image->imageurl, map->resolution, map->defresolution, NULL);
if (!image_draw) {
msSetError(MS_MISCERR, "Unable to initialize temporary transparent image.",
"msDrawLayer()");
return (MS_FAILURE);
}
image_draw->map = map;
renderer->startLayer(image_draw,map,layer);
}
}
}
/*
** redirect procesing of some layer types.
*/
if(layer->connectiontype == MS_WMS) {
#ifdef USE_WMS_LYR
retcode = msDrawWMSLayer(map, layer, image_draw);
#else
retcode = MS_FAILURE;
#endif
} else if(layer->type == MS_LAYER_RASTER) {
retcode = msDrawRasterLayer(map, layer, image_draw);
} else if(layer->type == MS_LAYER_CHART) {
retcode = msDrawChartLayer(map, layer, image_draw);
} else { /* must be a Vector layer */
retcode = msDrawVectorLayer(map, layer, image_draw);
}
if (altFormat) {
rendererVTableObj *renderer = MS_IMAGE_RENDERER(image);
rendererVTableObj *altrenderer = MS_IMAGE_RENDERER(image_draw);
rasterBufferObj rb;
int i;
memset(&rb,0,sizeof(rasterBufferObj));
altrenderer->endLayer(image_draw,map,layer);
retcode = altrenderer->getRasterBufferHandle(image_draw,&rb);
if(MS_UNLIKELY(retcode == MS_FAILURE)) {
goto altformat_cleanup;
}
retcode = renderer->mergeRasterBuffer(image,&rb,((layer->compositer)?(layer->compositer->opacity*0.01):(1.0)),0,0,0,0,rb.width,rb.height);
if(MS_UNLIKELY(retcode == MS_FAILURE)) {
goto altformat_cleanup;
}
altformat_cleanup:
/*
* hack to work around bug #3834: if we have use an alternate renderer, the symbolset may contain
* symbols that reference it. We want to remove those references before the altFormat is destroyed
* to avoid a segfault and/or a leak, and so the the main renderer doesn't pick the cache up thinking
* it's for him.
*/
for(i=0; i<map->symbolset.numsymbols; i++) {
if (map->symbolset.symbol[i]!=NULL) {
symbolObj *s = map->symbolset.symbol[i];
if(s->renderer == altrenderer) {
altrenderer->freeSymbol(s);
s->renderer = NULL;
}
}
}
msFreeImage(image_draw);
/* set the imagetype from the original outputformat back (it was removed by msSelectOutputFormat() */
msFree(map->imagetype);
map->imagetype = msStrdup(image->format->name);
} else if( image != image_draw) {
rendererVTableObj *renderer = MS_IMAGE_RENDERER(image_draw);
rasterBufferObj rb;
memset(&rb,0,sizeof(rasterBufferObj));
renderer->endLayer(image_draw,map,layer);
retcode = renderer->getRasterBufferHandle(image_draw,&rb);
if(MS_UNLIKELY(retcode == MS_FAILURE)) {
goto imagedraw_cleanup;
}
if(maskLayer && maskLayer->maskimage) {
rasterBufferObj mask;
unsigned int row,col;
memset(&mask,0,sizeof(rasterBufferObj));
retcode = MS_IMAGE_RENDERER(maskLayer->maskimage)->getRasterBufferHandle(maskLayer->maskimage,&mask);
if(MS_UNLIKELY(retcode == MS_FAILURE)) {
goto imagedraw_cleanup;
}
/* modify the pixels of the overlay */
if(rb.type == MS_BUFFER_BYTE_RGBA) {
for(row=0; row<rb.height; row++) {
unsigned char *ma,*a,*r,*g,*b;
r=rb.data.rgba.r+row*rb.data.rgba.row_step;
g=rb.data.rgba.g+row*rb.data.rgba.row_step;
b=rb.data.rgba.b+row*rb.data.rgba.row_step;
a=rb.data.rgba.a+row*rb.data.rgba.row_step;
ma=mask.data.rgba.a+row*mask.data.rgba.row_step;
for(col=0; col<rb.width; col++) {
if(*ma == 0) {
*a = *r = *g = *b = 0;
}
a+=rb.data.rgba.pixel_step;
r+=rb.data.rgba.pixel_step;
g+=rb.data.rgba.pixel_step;
b+=rb.data.rgba.pixel_step;
ma+=mask.data.rgba.pixel_step;
}
}
}
}
if(!layer->compositer) {
/*we have a mask layer with no composition configured, do a nomral blend */
retcode = renderer->mergeRasterBuffer(image,&rb,1.0,0,0,0,0,rb.width,rb.height);
} else {
retcode = msCompositeRasterBuffer(map,image,&rb,layer->compositer);
}
if(MS_UNLIKELY(retcode == MS_FAILURE)) {
goto imagedraw_cleanup;
}
imagedraw_cleanup:
msFreeImage(image_draw);
}
msImageEndLayer(map,layer,image);
return(retcode);
}
int msDrawVectorLayer(mapObj *map, layerObj *layer, imageObj *image)
{
int status, retcode=MS_SUCCESS;
int drawmode=MS_DRAWMODE_FEATURES;
char annotate=MS_TRUE;
shapeObj shape;
shapeObj savedShape;
rectObj searchrect;
char cache=MS_FALSE;
int maxnumstyles=1;
featureListNodeObjPtr shpcache=NULL, current=NULL;
int nclasses = 0;
int *classgroup = NULL;
double minfeaturesize = -1;
int maxfeatures=-1;
int featuresdrawn=0;
if (image)
maxfeatures=msLayerGetMaxFeaturesToDraw(layer, image->format);
/* TODO TBT: draw as raster layer in vector renderers */
annotate = msEvalContext(map, layer, layer->labelrequires);
if(map->scaledenom > 0) {
if((layer->labelmaxscaledenom != -1) && (map->scaledenom >= layer->labelmaxscaledenom)) annotate = MS_FALSE;
if((layer->labelminscaledenom != -1) && (map->scaledenom < layer->labelminscaledenom)) annotate = MS_FALSE;
}
/* open this layer */
status = msLayerOpen(layer);
if(status != MS_SUCCESS) return MS_FAILURE;