forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapraster.c
1138 lines (974 loc) · 40.6 KB
/
mapraster.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: mapraster.c 10772 2010-11-29 18:27:02Z aboudreault $
*
* Project: MapServer
* Purpose: msDrawRasterLayer(): generic raster layer drawing.
* Author: Frank Warmerdam, warmerdam@pobox.com
* Pete Olson (LMIC)
* Steve Lime
*
******************************************************************************
* Copyright (c) 1996-2010 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 "mapserver.h"
#include "mapfile.h"
#include "mapresample.h"
#include "mapthread.h"
extern int msyylex_destroy(void);
extern int yyparse(parseObj *);
extern parseResultObj yypresult; /* result of parsing, true/false */
#include "gdal.h"
#include "cpl_string.h"
#include "mapraster.h"
#define MAXCOLORS 256
#define BUFLEN 1024
#define HDRLEN 8
#define CVT(x) ((x) >> 8) /* converts to 8-bit color value */
#define NUMGRAYS 16
/************************************************************************/
/* msGetClass_String() */
/************************************************************************/
static int msGetClass_String( layerObj *layer, colorObj *color, const char *pixel_value, int firstClassToTry )
{
int i;
const char *tmpstr1=NULL;
int numitems;
char *item_names[4] = { "pixel", "red", "green", "blue" };
char *item_values[4];
char red_value[8], green_value[8], blue_value[8];
/* -------------------------------------------------------------------- */
/* No need to do a lookup in the case of one default class. */
/* -------------------------------------------------------------------- */
if((layer->numclasses == 1) && !(layer->class[0]->expression.string)) /* no need to do lookup */
return(0);
/* -------------------------------------------------------------------- */
/* Setup values list for expressions. */
/* -------------------------------------------------------------------- */
numitems = 4;
if( color->red == -1 && color->green == -1 && color->blue == -1 )
{
strcpy(red_value, "-1");
strcpy(green_value, "-1");
strcpy(blue_value, "-1");
}
else
{
sprintf( red_value, "%d", color->red );
sprintf( green_value, "%d", color->green );
sprintf( blue_value, "%d", color->blue );
}
item_values[0] = (char *)pixel_value;
item_values[1] = red_value;
item_values[2] = green_value;
item_values[3] = blue_value;
/* -------------------------------------------------------------------- */
/* Loop over classes till we find a match. */
/* -------------------------------------------------------------------- */
if( firstClassToTry >= layer->numclasses )
firstClassToTry = -1;
for(i=0; i<layer->numclasses; i++) {
const int idx = firstClassToTry < 0 ? i :
i == 0 ? firstClassToTry :
i <= firstClassToTry ? i - 1:
i;
/* check for correct classgroup, if set */
if ( layer->class[idx]->group && layer->classgroup &&
strcasecmp(layer->class[idx]->group, layer->classgroup) != 0 )
continue;
/* Empty expression - always matches */
if (layer->class[idx]->expression.string == NULL)
return(i);
switch(layer->class[idx]->expression.type) {
/* -------------------------------------------------------------------- */
/* Simple string match */
/* -------------------------------------------------------------------- */
case(MS_STRING):
/* trim junk white space */
tmpstr1= pixel_value;
while( *tmpstr1 == ' ' )
tmpstr1++;
if(strcmp(layer->class[idx]->expression.string, tmpstr1) == 0) return(idx); /* matched */
break;
/* -------------------------------------------------------------------- */
/* Regular expression. Rarely used for raster. */
/* -------------------------------------------------------------------- */
case(MS_REGEX):
if(!layer->class[idx]->expression.compiled) {
if(ms_regcomp(&(layer->class[idx]->expression.regex), layer->class[idx]->expression.string, MS_REG_EXTENDED|MS_REG_NOSUB) != 0) { /* compile the expression */
msSetError(MS_REGEXERR, "Invalid regular expression.", "msGetClass()");
return(-1);
}
layer->class[idx]->expression.compiled = MS_TRUE;
}
if(ms_regexec(&(layer->class[idx]->expression.regex), pixel_value, 0, NULL, 0) == 0) return(idx); /* got a match */
break;
/* -------------------------------------------------------------------- */
/* Parsed expression. */
/* -------------------------------------------------------------------- */
case(MS_EXPRESSION): {
int status;
parseObj p;
shapeObj dummy_shape;
expressionObj *expression = &(layer->class[idx]->expression);
dummy_shape.numvalues = numitems;
dummy_shape.values = item_values;
if( expression->tokens == NULL )
msTokenizeExpression( expression, item_names, &numitems );
p.shape = &dummy_shape;
p.expr = expression;
p.expr->curtoken = p.expr->tokens; /* reset */
p.type = MS_PARSE_TYPE_BOOLEAN;
status = yyparse(&p);
if (status != 0) {
msSetError(MS_PARSEERR, "Failed to parse expression: %s", "msGetClass_FloatRGB", expression->string);
return -1;
}
if( p.result.intval )
return idx;
break;
}
}
}
return(-1); /* not found */
}
/************************************************************************/
/* msGetClass() */
/************************************************************************/
int msGetClass(layerObj *layer, colorObj *color, int colormap_index)
{
char pixel_value[12];
snprintf( pixel_value, sizeof(pixel_value), "%d", colormap_index );
return msGetClass_String( layer, color, pixel_value, -1 );
}
/************************************************************************/
/* msGetClass_FloatRGB() */
/* */
/* Returns the class based on classification of a floating */
/* pixel value. */
/************************************************************************/
int msGetClass_FloatRGB(layerObj *layer, float fValue, int red, int green, int blue )
{
return msGetClass_FloatRGB_WithFirstClassToTry(
layer, fValue, red, green, blue, -1);
}
int msGetClass_FloatRGB_WithFirstClassToTry(layerObj *layer, float fValue, int red, int green, int blue, int firstClassToTry )
{
char pixel_value[100];
colorObj color;
color.red = red;
color.green = green;
color.blue = blue;
snprintf( pixel_value, sizeof(pixel_value), "%18g", fValue );
return msGetClass_String( layer, &color, pixel_value, firstClassToTry );
}
/************************************************************************/
/* msRasterSetupTileLayer() */
/* */
/* Setup the tile layer. */
/************************************************************************/
int msDrawRasterSetupTileLayer(mapObj *map, layerObj *layer,
rectObj* psearchrect,
int is_query,
int* ptilelayerindex, /* output */
int* ptileitemindex, /* output */
int* ptilesrsindex, /* output */
layerObj **ptlp /* output */ )
{
int i;
char* requested_fields;
int status;
layerObj* tlp = NULL;
*ptilelayerindex = msGetLayerIndex(layer->map, layer->tileindex);
if(*ptilelayerindex == -1) { /* the tileindex references a file, not a layer */
/* so we create a temporary layer */
tlp = (layerObj *) malloc(sizeof(layerObj));
MS_CHECK_ALLOC(tlp, sizeof(layerObj), MS_FAILURE);
initLayer(tlp, map);
*ptlp = tlp;
/* set a few parameters for a very basic shapefile-based layer */
tlp->name = msStrdup("TILE");
tlp->type = MS_LAYER_TILEINDEX;
tlp->data = msStrdup(layer->tileindex);
if( is_query )
{
tlp->map = map; /*needed when scaletokens are applied, to extract current map scale */
for(i = 0; i < layer->numscaletokens; i++) {
if(msGrowLayerScaletokens(tlp) == NULL) {
return MS_FAILURE;
}
initScaleToken(&tlp->scaletokens[i]);
msCopyScaleToken(&layer->scaletokens[i],&tlp->scaletokens[i]);
tlp->numscaletokens++;
}
}
if (layer->projection.numargs > 0 &&
EQUAL(layer->projection.args[0], "auto"))
{
tlp->projection.numargs = 1;
tlp->projection.args[0] = msStrdup("auto");
}
if (layer->filteritem)
tlp->filteritem = msStrdup(layer->filteritem);
if (layer->filter.string) {
if (layer->filter.type == MS_EXPRESSION) {
char* pszTmp =
(char *)msSmallMalloc(sizeof(char)*(strlen(layer->filter.string)+3));
sprintf(pszTmp,"(%s)",layer->filter.string);
msLoadExpressionString(&tlp->filter, pszTmp);
free(pszTmp);
} else if (layer->filter.type == MS_REGEX ||
layer->filter.type == MS_IREGEX) {
char* pszTmp =
(char *)msSmallMalloc(sizeof(char)*(strlen(layer->filter.string)+3));
sprintf(pszTmp,"/%s/",layer->filter.string);
msLoadExpressionString(&tlp->filter, pszTmp);
free(pszTmp);
} else
msLoadExpressionString(&tlp->filter, layer->filter.string);
tlp->filter.type = layer->filter.type;
}
} else {
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return MS_FAILURE;
tlp = (GET_LAYER(layer->map, *ptilelayerindex));
*ptlp = tlp;
}
status = msLayerOpen(tlp);
if(status != MS_SUCCESS) {
return status;
}
/* fetch tileitem and tilesrs fields */
requested_fields = (char*) msSmallMalloc(sizeof(char)*(strlen(layer->tileitem)+1+
(layer->tilesrs ? strlen(layer->tilesrs) : 0) + 1));
if( layer->tilesrs != NULL )
sprintf(requested_fields, "%s,%s", layer->tileitem, layer->tilesrs);
else
strcpy(requested_fields, layer->tileitem);
status = msLayerWhichItems(tlp, MS_FALSE, requested_fields);
msFree(requested_fields);
if(status != MS_SUCCESS) {
return status;
}
/* get the tileitem and tilesrs index */
for(i=0; i<tlp->numitems; i++) {
if(strcasecmp(tlp->items[i], layer->tileitem) == 0) {
*ptileitemindex = i;
}
if(layer->tilesrs != NULL &&
strcasecmp(tlp->items[i], layer->tilesrs) == 0) {
*ptilesrsindex = i;
}
}
if(*ptileitemindex < 0) { /* didn't find it */
msSetError(MS_MEMERR,
"Could not find attribute %s in tileindex.",
"msDrawRasterLayerLow()",
layer->tileitem);
return MS_FAILURE;
}
if(layer->tilesrs != NULL && *ptilesrsindex < 0) { /* didn't find it */
msSetError(MS_MEMERR,
"Could not find attribute %s in tileindex.",
"msDrawRasterLayerLow()",
layer->tilesrs);
return MS_FAILURE;
}
/* if necessary, project the searchrect to source coords */
if((map->projection.numargs > 0) && (layer->projection.numargs > 0) &&
!EQUAL(layer->projection.args[0], "auto")) {
if( msProjectRect(&map->projection, &layer->projection, psearchrect)
!= MS_SUCCESS ) {
msDebug( "msDrawRasterLayerLow(%s): unable to reproject map request rectangle into layer projection, canceling.\n", layer->name );
return MS_FAILURE;
}
}
else if((map->projection.numargs > 0) && (tlp->projection.numargs > 0) &&
!EQUAL(tlp->projection.args[0], "auto")) {
if( msProjectRect(&map->projection, &tlp->projection, psearchrect)
!= MS_SUCCESS ) {
msDebug( "msDrawRasterLayerLow(%s): unable to reproject map request rectangle into layer projection, canceling.\n", layer->name );
return MS_FAILURE;
}
}
return msLayerWhichShapes(tlp, *psearchrect, MS_FALSE);
}
/************************************************************************/
/* msDrawRasterCleanupTileLayer() */
/* */
/* Cleanup the tile layer. */
/************************************************************************/
void msDrawRasterCleanupTileLayer(layerObj* tlp,
int tilelayerindex)
{
msLayerClose(tlp);
if(tilelayerindex == -1) {
freeLayer(tlp);
free(tlp);
}
}
/************************************************************************/
/* msDrawRasterIterateTileIndex() */
/* */
/* Iterate over the tile layer. */
/************************************************************************/
int msDrawRasterIterateTileIndex(layerObj *layer,
layerObj* tlp,
shapeObj* ptshp, /* input-output */
int tileitemindex,
int tilesrsindex,
char* tilename, /* output */
size_t sizeof_tilename,
char* tilesrsname, /* output */
size_t sizeof_tilesrsname)
{
int status;
status = msLayerNextShape(tlp, ptshp);
if( status == MS_FAILURE || status == MS_DONE ) {
return status;
}
if(layer->data == NULL || strlen(layer->data) == 0 ) { /* assume whole filename is in attribute field */
strlcpy( tilename, ptshp->values[tileitemindex], sizeof_tilename);
} else
snprintf(tilename, sizeof_tilename, "%s/%s", ptshp->values[tileitemindex], layer->data);
tilesrsname[0] = '\0';
if( tilesrsindex >= 0 )
{
if(ptshp->values[tilesrsindex] != NULL )
strlcpy( tilesrsname, ptshp->values[tilesrsindex], sizeof_tilesrsname );
}
msFreeShape(ptshp); /* done with the shape */
return status;
}
/************************************************************************/
/* msDrawRasterBuildRasterPath() */
/* */
/* Build the path of the raster to open. */
/************************************************************************/
int msDrawRasterBuildRasterPath(mapObj *map, layerObj *layer,
const char* filename,
char szPath[MS_MAXPATHLEN] /* output */)
{
/*
** If using a tileindex then build the path relative to that file if SHAPEPATH is not set.
*/
if(layer->tileindex && !map->shapepath) {
char tiAbsFilePath[MS_MAXPATHLEN];
char *tiAbsDirPath = NULL;
msTryBuildPath(tiAbsFilePath, map->mappath, layer->tileindex); /* absolute path to tileindex file */
tiAbsDirPath = msGetPath(tiAbsFilePath); /* tileindex file's directory */
msBuildPath(szPath, tiAbsDirPath, filename);
free(tiAbsDirPath);
} else {
msTryBuildPath3(szPath, map->mappath, map->shapepath, filename);
}
return MS_SUCCESS;
}
/************************************************************************/
/* msDrawRasterGetCPLErrorMsg() */
/* */
/* Return the CPL error message, and filter out sensitive info. */
/************************************************************************/
const char* msDrawRasterGetCPLErrorMsg(const char* decrypted_path,
const char* szPath)
{
const char *cpl_error_msg = CPLGetLastErrorMsg();
/* we wish to avoid reporting decrypted paths */
if( cpl_error_msg != NULL
&& strstr(cpl_error_msg,decrypted_path) != NULL
&& strcmp(decrypted_path,szPath) != 0 )
cpl_error_msg = NULL;
/* we wish to avoid reporting the stock GDALOpen error messages */
if( cpl_error_msg != NULL
&& (strstr(cpl_error_msg,"not recognised as a supported") != NULL
|| strstr(cpl_error_msg,"does not exist") != NULL) )
cpl_error_msg = NULL;
if( cpl_error_msg == NULL )
cpl_error_msg = "";
return cpl_error_msg;
}
/************************************************************************/
/* msDrawRasterLoadProjection() */
/* */
/* Handle TILESRS or PROJECTION AUTO for each tile. */
/************************************************************************/
int msDrawRasterLoadProjection(layerObj *layer,
GDALDatasetH hDS,
const char* filename,
int tilesrsindex,
const char* tilesrsname)
{
/*
** Generate the projection information if using TILESRS.
*/
if( tilesrsindex >= 0 )
{
const char *pszWKT;
if( tilesrsname[0] != '\0' )
pszWKT = tilesrsname;
else
pszWKT = GDALGetProjectionRef( hDS );
if( pszWKT != NULL && strlen(pszWKT) > 0 ) {
if( msOGCWKT2ProjectionObj(pszWKT, &(layer->projection),
layer->debug ) != MS_SUCCESS ) {
char szLongMsg[MESSAGELENGTH*2];
errorObj *ms_error = msGetErrorObj();
snprintf( szLongMsg, sizeof(szLongMsg),
"%s\n"
"PROJECTION '%s' cannot be used for this "
"GDAL raster (`%s').",
ms_error->message, pszWKT, filename);
szLongMsg[MESSAGELENGTH-1] = '\0';
msSetError(MS_OGRERR, "%s","msDrawRasterLayer()",
szLongMsg);
return MS_FAILURE;
}
}
}
/*
** Generate the projection information if using AUTO.
*/
else if (layer->projection.numargs > 0 &&
EQUAL(layer->projection.args[0], "auto")) {
const char *pszWKT;
pszWKT = GDALGetProjectionRef( hDS );
if( pszWKT != NULL && strlen(pszWKT) > 0 ) {
if( msOGCWKT2ProjectionObj(pszWKT, &(layer->projection),
layer->debug ) != MS_SUCCESS ) {
char szLongMsg[MESSAGELENGTH*2];
errorObj *ms_error = msGetErrorObj();
snprintf( szLongMsg, sizeof(szLongMsg),
"%s\n"
"PROJECTION AUTO cannot be used for this "
"GDAL raster (`%s').",
ms_error->message, filename);
szLongMsg[MESSAGELENGTH-1] = '\0';
msSetError(MS_OGRERR, "%s","msDrawRasterLayer()",
szLongMsg);
return MS_FAILURE;
}
}
}
return MS_SUCCESS;
}
typedef enum
{
CDRT_OK,
CDRT_RETURN_MS_FAILURE,
CDRT_CONTINUE_NEXT_TILE
} CheckDatasetReturnType;
/************************************************************************/
/* msDrawRasterLayerLowCheckDataset() */
/************************************************************************/
static
CheckDatasetReturnType msDrawRasterLayerLowCheckDataset(mapObj *map,
layerObj *layer,
GDALDatasetH hDS,
const char* decrypted_path,
const char* szPath)
{
/*
** If GDAL doesn't recognise it, and it wasn't successfully opened
** Generate an error.
*/
if(hDS == NULL) {
int ignore_missing = msMapIgnoreMissingData(map);
const char *cpl_error_msg = msDrawRasterGetCPLErrorMsg(decrypted_path, szPath);
if(ignore_missing == MS_MISSING_DATA_FAIL) {
msSetError(MS_IOERR, "Corrupt, empty or missing file '%s' for layer '%s'. %s", "msDrawRasterLayerLow()", szPath, layer->name, cpl_error_msg );
return(CDRT_RETURN_MS_FAILURE);
} else if( ignore_missing == MS_MISSING_DATA_LOG ) {
if( layer->debug || layer->map->debug ) {
msDebug( "Corrupt, empty or missing file '%s' for layer '%s' ... ignoring this missing data. %s\n", szPath, layer->name, cpl_error_msg );
}
return(CDRT_CONTINUE_NEXT_TILE);
} else if( ignore_missing == MS_MISSING_DATA_IGNORE ) {
return(CDRT_CONTINUE_NEXT_TILE);
} else {
/* never get here */
msSetError(MS_IOERR, "msIgnoreMissingData returned unexpected value.", "msDrawRasterLayerLow()");
return(CDRT_RETURN_MS_FAILURE);
}
}
return CDRT_OK;
}
/************************************************************************/
/* msDrawRasterLayerLowOpenDataset() */
/************************************************************************/
void* msDrawRasterLayerLowOpenDataset(mapObj *map, layerObj *layer,
const char* filename,
char szPath[MS_MAXPATHLEN],
char** p_decrypted_path)
{
const char* pszPath;
msGDALInitialize();
if(layer->debug == MS_TRUE)
msDebug( "msDrawRasterLayerLow(%s): Filename is: %s\n", layer->name, filename);
if( strncmp(filename, "<VRTDataset", strlen("<VRTDataset")) == 0 )
{
pszPath = filename;
}
else
{
msDrawRasterBuildRasterPath(map, layer, filename, szPath);
pszPath = szPath;
}
if(layer->debug == MS_TRUE)
msDebug("msDrawRasterLayerLow(%s): Path is: %s\n", layer->name, pszPath);
/*
** Note: because we do decryption after the above path expansion
** which depends on actually finding a file, it essentially means that
** fancy path manipulation is essentially disabled when using encrypted
** components. But that is mostly ok, since stuff like sde,postgres and
** oracle georaster do not use real paths.
*/
*p_decrypted_path = msDecryptStringTokens( map, pszPath );
if( *p_decrypted_path == NULL )
return NULL;
msAcquireLock( TLOCK_GDAL );
if( !layer->tileindex )
{
char** connectionoptions = msGetStringListFromHashTable(&(layer->connectionoptions));
GDALDatasetH hDS = GDALOpenEx( *p_decrypted_path,
GDAL_OF_RASTER | GDAL_OF_SHARED,
NULL,
(const char* const*)connectionoptions,
NULL);
CSLDestroy(connectionoptions);
return hDS;
}
else
{
return GDALOpenShared( *p_decrypted_path, GA_ReadOnly );
}
}
/************************************************************************/
/* msDrawRasterLayerLowCloseDataset() */
/************************************************************************/
void msDrawRasterLayerLowCloseDataset(layerObj *layer, void* hDS)
{
if( hDS )
{
const char *close_connection;
close_connection = msLayerGetProcessingKey( layer,
"CLOSE_CONNECTION" );
if( close_connection == NULL && layer->tileindex == NULL )
close_connection = "DEFER";
{
/* Due to how GDAL processes OVERVIEW_LEVEL, datasets returned are */
/* not shared, despite being asked to, so close them for real */
char** connectionoptions = msGetStringListFromHashTable(&(layer->connectionoptions));
if( CSLFetchNameValue(connectionoptions, "OVERVIEW_LEVEL") )
close_connection = NULL;
CSLDestroy(connectionoptions);
}
if( close_connection != NULL
&& strcasecmp(close_connection,"DEFER") == 0 ) {
GDALDereferenceDataset( (GDALDatasetH)hDS );
} else {
GDALClose( (GDALDatasetH)hDS );
}
msReleaseLock( TLOCK_GDAL );
}
}
/************************************************************************/
/* msDrawRasterLayerLowCheckIfMustDraw() */
/* */
/* Return 1 if the layer should be drawn. */
/************************************************************************/
int msDrawRasterLayerLowCheckIfMustDraw(mapObj *map, layerObj *layer)
{
if(!layer->data && !layer->tileindex && !(layer->connectiontype==MS_KERNELDENSITY || layer->connectiontype==MS_IDW)) {
if(layer->debug == MS_TRUE)
msDebug( "msDrawRasterLayerLow(%s): layer data and tileindex NULL ... doing nothing.", layer->name );
return(0);
}
if((layer->status != MS_ON) && (layer->status != MS_DEFAULT)) {
if(layer->debug == MS_TRUE)
msDebug( "msDrawRasterLayerLow(%s): not status ON or DEFAULT, doing nothing.", layer->name );
return(0);
}
if(map->scaledenom > 0) {
if((layer->maxscaledenom > 0) && (map->scaledenom > layer->maxscaledenom)) {
if(layer->debug == MS_TRUE)
msDebug( "msDrawRasterLayerLow(%s): skipping, map scale %.2g > MAXSCALEDENOM=%g\n",
layer->name, map->scaledenom, layer->maxscaledenom );
return(0);
}
if((layer->minscaledenom > 0) && (map->scaledenom <= layer->minscaledenom)) {
if(layer->debug == MS_TRUE)
msDebug( "msDrawRasterLayerLow(%s): skipping, map scale %.2g < MINSCALEDENOM=%g\n",
layer->name, map->scaledenom, layer->minscaledenom );
return(0);
}
}
if(layer->maxscaledenom <= 0 && layer->minscaledenom <= 0) {
if((layer->maxgeowidth > 0) && ((map->extent.maxx - map->extent.minx) > layer->maxgeowidth)) {
if(layer->debug == MS_TRUE)
msDebug( "msDrawRasterLayerLow(%s): skipping, map width %.2g > MAXSCALEDENOM=%g\n", layer->name,
(map->extent.maxx - map->extent.minx), layer->maxgeowidth );
return(0);
}
if((layer->mingeowidth > 0) && ((map->extent.maxx - map->extent.minx) < layer->mingeowidth)) {
if(layer->debug == MS_TRUE)
msDebug( "msDrawRasterLayerLow(%s): skipping, map width %.2g < MINSCALEDENOM=%g\n", layer->name,
(map->extent.maxx - map->extent.minx), layer->mingeowidth );
return(0);
}
}
return 1;
}
/************************************************************************/
/* msDrawRasterLayerLow() */
/* */
/* Check for various file types and act appropriately. Handle */
/* tile indexing. */
/************************************************************************/
int msDrawRasterLayerLow(mapObj *map, layerObj *layer, imageObj *image,
rasterBufferObj *rb )
{
return msDrawRasterLayerLowWithDataset(map, layer, image, rb, NULL);
}
int msDrawRasterLayerLowWithDataset(mapObj *map, layerObj *layer, imageObj *image,
rasterBufferObj *rb, void* hDatasetIn )
{
/* -------------------------------------------------------------------- */
/* As of MapServer 6.0 GDAL is required for rendering raster */
/* imagery. */
/* -------------------------------------------------------------------- */
int status, done;
char *filename=NULL, tilename[MS_MAXPATHLEN], tilesrsname[1024];
layerObj *tlp=NULL; /* pointer to the tile layer either real or temporary */
int tileitemindex=-1, tilelayerindex=-1, tilesrsindex=-1;
shapeObj tshp;
char szPath[MS_MAXPATHLEN] = { 0 };
char *decrypted_path = NULL;
int final_status = MS_SUCCESS;
rectObj searchrect;
GDALDatasetH hDS;
double adfGeoTransform[6];
void *kernel_density_cleanup_ptr = NULL;
if(layer->debug > 0 || map->debug > 1)
msDebug( "msDrawRasterLayerLow(%s): entering.\n", layer->name );
if( hDatasetIn == NULL &&
!msDrawRasterLayerLowCheckIfMustDraw(map, layer) ) {
return MS_SUCCESS;
}
msGDALInitialize();
if(layer->tileindex) { /* we have an index file */
msInitShape(&tshp);
searchrect = map->extent;
status = msDrawRasterSetupTileLayer(map, layer,
&searchrect,
MS_FALSE,
&tilelayerindex,
&tileitemindex,
&tilesrsindex,
&tlp);
if(status != MS_SUCCESS) {
if (status != MS_DONE)
final_status = status;
goto cleanup;
}
}
done = MS_FALSE;
while(done != MS_TRUE) {
if(layer->tileindex) {
status = msDrawRasterIterateTileIndex(layer, tlp, &tshp,
tileitemindex, tilesrsindex,
tilename, sizeof(tilename),
tilesrsname, sizeof(tilesrsname));
if( status == MS_FAILURE) {
final_status = MS_FAILURE;
break;
}
if(status == MS_DONE) break; /* no more tiles/images */
filename = tilename;
} else {
filename = layer->data;
done = MS_TRUE; /* only one image so we're done after this */
}
if(layer->connectiontype == MS_KERNELDENSITY || layer->connectiontype == MS_IDW) {
msAcquireLock( TLOCK_GDAL );
status = msInterpolationDataset(map, image, layer, &hDS, &kernel_density_cleanup_ptr);
if(status != MS_SUCCESS) {
msReleaseLock( TLOCK_GDAL );
final_status = status;
goto cleanup;
}
done = MS_TRUE;
if(msProjectionsDiffer(&map->projection,&layer->projection)) {
char *mapProjStr = msGetProjectionString(&(map->projection));
/* Set the projection to the map file projection */
if (msLoadProjectionString(&(layer->projection), mapProjStr) != 0) {
GDALClose( hDS );
msReleaseLock( TLOCK_GDAL );
msSetError(MS_CGIERR, "Unable to set projection on interpolation layer.", "msDrawRasterLayerLow()");
return(MS_FAILURE);
}
free(mapProjStr);
}
} else {
if(strlen(filename) == 0) continue;
if( hDatasetIn )
{
hDS = (GDALDatasetH)hDatasetIn;
}
else
{
hDS = (GDALDatasetH)msDrawRasterLayerLowOpenDataset(
map, layer, filename, szPath, &decrypted_path);
}
}
if( hDatasetIn == NULL )
{
CheckDatasetReturnType eRet =
msDrawRasterLayerLowCheckDataset(map,layer,hDS,decrypted_path,szPath);
msFree( decrypted_path );
decrypted_path = NULL;
if( eRet == CDRT_CONTINUE_NEXT_TILE )
{
msReleaseLock( TLOCK_GDAL );
continue;
}
if( eRet == CDRT_RETURN_MS_FAILURE )
{
msReleaseLock( TLOCK_GDAL );
return MS_FAILURE;
}
}
if( msDrawRasterLoadProjection(layer, hDS, filename, tilesrsindex, tilesrsname) != MS_SUCCESS )
{
if( hDatasetIn == NULL )
{
GDALClose( hDS );
msReleaseLock( TLOCK_GDAL );
}
final_status = MS_FAILURE;
break;
}
msGetGDALGeoTransform( hDS, map, layer, adfGeoTransform );
/*
** We want to resample if the source image is rotated, if
** the projections differ or if resampling has been explicitly
** requested, or if the image has north-down instead of north-up.
*/
if( ((adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0
|| adfGeoTransform[5] > 0.0 || adfGeoTransform[1] < 0.0 )
&& layer->transform )
|| msProjectionsDiffer( &(map->projection),
&(layer->projection) )
|| CSLFetchNameValue( layer->processing, "RESAMPLE" ) != NULL ) {
status = msResampleGDALToMap( map, layer, image, rb, hDS );
}
else
{
if( adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0 ) {
if( layer->debug || map->debug )
msDebug(
"Layer %s has rotational coefficients but we\n"
"are unable to use them, projections support\n"
"needs to be built in.",
layer->name );
}
status = msDrawRasterLayerGDAL(map, layer, image, rb, hDS );
}
if( status == -1 ) {
if( hDatasetIn == NULL )
{
GDALClose( hDS );
msReleaseLock( TLOCK_GDAL );
}
final_status = MS_FAILURE;
break;
}
/*
** Should we keep this file open for future use?
** default to keeping open for single data files, and
** to closing for tile indexes
*/
if(layer->connectiontype == MS_KERNELDENSITY || layer->connectiontype == MS_IDW) {
/*
** Fix issue #5330
** The in-memory kernel density heatmap gdal dataset handle (hDS) gets re-used
** but the associated rasterband cache doesn't get flushed, which causes old data
** to be rendered instead of the newly generated imagery. To fix, simply close the
** the handle and prevent further re-use.
** Note that instead of this workaround, we could explicitely set
** CLOSE_CONNECTION=ALWAYS on the kerneldensity layer.
*/
GDALClose( hDS );
msReleaseLock( TLOCK_GDAL );
}
else {
if( hDatasetIn == NULL)
msDrawRasterLayerLowCloseDataset(layer, hDS);
}
} /* next tile */
cleanup:
if(layer->tileindex) { /* tiling clean-up */
msDrawRasterCleanupTileLayer(tlp, tilelayerindex);
}
if(kernel_density_cleanup_ptr) {
msCleanupInterpolationDataset(map, image, layer, kernel_density_cleanup_ptr);
}
return final_status;
}
/************************************************************************/
/* msDrawReferenceMap() */
/************************************************************************/
imageObj *msDrawReferenceMap(mapObj *map)
{
double cellsize;
int x1,y1,x2,y2;
char szPath[MS_MAXPATHLEN];
int status = MS_SUCCESS;
imageObj *image = NULL;
styleObj style;
/* check to see if we have enough information to actually proceed */
if(!map->reference.image || map->reference.height == 0 || map->reference.width == 0) {
msSetError(MS_MISCERR, "Reference map configuration error.", "msDrawReferenceMap()");
return NULL;
}
rendererVTableObj *renderer = MS_MAP_RENDERER(map);
rasterBufferObj *refImage = (rasterBufferObj*)calloc(1,sizeof(rasterBufferObj));
MS_CHECK_ALLOC(refImage, sizeof(rasterBufferObj), NULL);