forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapuvraster.c
1077 lines (912 loc) · 38.6 KB
/
mapuvraster.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: mapuv.c 12629 2011-10-06 18:06:34Z aboudreault $
*
* Project: MapServer
* Purpose: UV Layer
* Author: Alan Boudreault (aboudreault@mapgears.com)
*
**********************************************************************
* Copyright (c) 2011, Alan Boudreault, MapGears
*
* 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 "mapserver.h"
#include <assert.h>
#include <math.h>
#include "mapows.h"
#include "mapresample.h"
#include "mapthread.h"
#define MSUVRASTER_NUMITEMS 6
#define MSUVRASTER_ANGLE "uv_angle"
#define MSUVRASTER_ANGLEINDEX -100
#define MSUVRASTER_MINUS_ANGLE "uv_minus_angle"
#define MSUVRASTER_MINUSANGLEINDEX -101
#define MSUVRASTER_LENGTH "uv_length"
#define MSUVRASTER_LENGTHINDEX -102
#define MSUVRASTER_LENGTH_2 "uv_length_2"
#define MSUVRASTER_LENGTH2INDEX -103
#define MSUVRASTER_U "u"
#define MSUVRASTER_UINDEX -104
#define MSUVRASTER_V "v"
#define MSUVRASTER_VINDEX -105
#define RQM_UNKNOWN 0
#define RQM_ENTRY_PER_PIXEL 1
#define RQM_HIST_ON_CLASS 2
#define RQM_HIST_ON_VALUE 3
typedef struct {
/* query cache results */
int query_results;
/* int query_alloc_max;
int query_request_max;
int query_result_hard_max;
int raster_query_mode; */
int band_count;
int refcount;
/* query bound in force
shapeObj *searchshape;*/
/* Only nearest result to this point.
int range_mode; MS_QUERY_SINGLE, MS_QUERY_MULTIPLE or -1 (skip test)
double range_dist;
pointObj target_point;*/
/* double shape_tolerance; */
float **u; /* u values */
float **v; /* v values */
int width;
int height;
rectObj extent;
int next_shape;
int x, y; /* used internally in msUVRasterLayerNextShape() */
mapObj* mapToUseForWhichShapes; /* set if the map->extent and map->projection are valid in msUVRASTERLayerWhichShapes() */
} uvRasterLayerInfo;
void msUVRASTERLayerUseMapExtentAndProjectionForNextWhichShapes(layerObj* layer,
mapObj* map)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
uvlinfo->mapToUseForWhichShapes = map;
}
static int msUVRASTERLayerInitItemInfo(layerObj *layer)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
int i;
int *itemindexes;
int failed=0;
if (layer->numitems == 0)
return MS_SUCCESS;
if( uvlinfo == NULL ) {
msSetError(MS_MISCERR, "Assertion failed: GDAL layer not opened!!!",
"msUVRASTERLayerInitItemInfo()");
return(MS_FAILURE);
}
if (layer->iteminfo)
free(layer->iteminfo);
if((layer->iteminfo = (int *)malloc(sizeof(int)*layer->numitems))== NULL) {
msSetError(MS_MEMERR, NULL, "msUVRASTERLayerInitItemInfo()");
return(MS_FAILURE);
}
itemindexes = (int*)layer->iteminfo;
for(i=0; i<layer->numitems; i++) {
/* Special case for handling text string and angle coming from */
/* OGR style strings. We use special attribute snames. */
if (EQUAL(layer->items[i], MSUVRASTER_ANGLE))
itemindexes[i] = MSUVRASTER_ANGLEINDEX;
else if (EQUAL(layer->items[i], MSUVRASTER_MINUS_ANGLE))
itemindexes[i] = MSUVRASTER_MINUSANGLEINDEX;
else if (EQUAL(layer->items[i], MSUVRASTER_LENGTH))
itemindexes[i] = MSUVRASTER_LENGTHINDEX;
else if (EQUAL(layer->items[i], MSUVRASTER_LENGTH_2))
itemindexes[i] = MSUVRASTER_LENGTH2INDEX;
else if (EQUAL(layer->items[i], MSUVRASTER_U))
itemindexes[i] = MSUVRASTER_UINDEX;
else if (EQUAL(layer->items[i], MSUVRASTER_V))
itemindexes[i] = MSUVRASTER_VINDEX;
else {
itemindexes[i] = -1;
msSetError(MS_OGRERR,
"Invalid Field name: %s",
"msUVRASTERLayerInitItemInfo()",
layer->items[i]);
failed=1;
}
}
return failed ? (MS_FAILURE) : (MS_SUCCESS);
}
void msUVRASTERLayerFreeItemInfo(layerObj *layer)
{
if (layer->iteminfo)
free(layer->iteminfo);
layer->iteminfo = NULL;
}
static void msUVRasterLayerInfoInitialize(layerObj *layer)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
if( uvlinfo != NULL )
return;
uvlinfo = (uvRasterLayerInfo *) msSmallCalloc(1,sizeof(uvRasterLayerInfo));
layer->layerinfo = uvlinfo;
uvlinfo->band_count = -1;
/* uvlinfo->raster_query_mode = RQM_ENTRY_PER_PIXEL; */
/* uvlinfo->range_mode = -1; /\* inactive *\/ */
/* uvlinfo->refcount = 0; */
/* uvlinfo->shape_tolerance = 0.0; */
uvlinfo->u = NULL;
uvlinfo->v = NULL;
uvlinfo->width = 0;
uvlinfo->height = 0;
/* Set attribute type to Real, unless the user has explicitly set */
/* something else. */
{
const char* const items[] = {
MSUVRASTER_ANGLE,
MSUVRASTER_MINUS_ANGLE,
MSUVRASTER_LENGTH,
MSUVRASTER_LENGTH_2,
MSUVRASTER_U,
MSUVRASTER_V,
};
size_t i;
for( i = 0; i < sizeof(items)/sizeof(items[0]); ++i ) {
char szTmp[100];
snprintf(szTmp, sizeof(szTmp), "%s_type", items[i]);
if (msOWSLookupMetadata(&(layer->metadata), "OFG", szTmp) == NULL) {
snprintf(szTmp, sizeof(szTmp), "gml_%s_type", items[i]);
msInsertHashTable(&(layer->metadata), szTmp, "Real");
}
}
}
/* uvlinfo->query_result_hard_max = 1000000; */
/* if( CSLFetchNameValue( layer->processing, "RASTER_QUERY_MAX_RESULT" ) */
/* != NULL ) */
/* { */
/* uvlinfo->query_result_hard_max = */
/* atoi(CSLFetchNameValue( layer->processing, "RASTER_QUERY_MAX_RESULT" )); */
/* } */
}
static void msUVRasterLayerInfoFree( layerObj *layer )
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
int i;
if( uvlinfo == NULL )
return;
if (uvlinfo->u) {
for (i=0; i<uvlinfo->width; ++i) {
free(uvlinfo->u[i]);
}
free(uvlinfo->u);
}
if (uvlinfo->v) {
for (i=0; i<uvlinfo->width; ++i) {
free(uvlinfo->v[i]);
}
free(uvlinfo->v);
}
free( uvlinfo );
layer->layerinfo = NULL;
}
int msUVRASTERLayerOpen(layerObj *layer)
{
/* If we don't have info, initialize an empty one now */
if( layer->layerinfo == NULL )
msUVRasterLayerInfoInitialize( layer );
if( layer->layerinfo == NULL )
return MS_FAILURE;
uvRasterLayerInfo* uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
uvlinfo->refcount = uvlinfo->refcount + 1;
return MS_SUCCESS;
}
int msUVRASTERLayerIsOpen(layerObj *layer)
{
if (layer->layerinfo)
return MS_TRUE;
return MS_FALSE;
}
int msUVRASTERLayerClose(layerObj *layer)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
if( uvlinfo != NULL ) {
uvlinfo->refcount--;
if( uvlinfo->refcount < 1 )
msUVRasterLayerInfoFree( layer );
}
return MS_SUCCESS;
}
int msUVRASTERLayerGetItems(layerObj *layer)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
if( uvlinfo == NULL )
return MS_FAILURE;
layer->numitems = 0;
layer->items = (char **) msSmallCalloc(sizeof(char *),10);;
layer->items[layer->numitems++] = msStrdup(MSUVRASTER_ANGLE);
layer->items[layer->numitems++] = msStrdup(MSUVRASTER_MINUS_ANGLE);
layer->items[layer->numitems++] = msStrdup(MSUVRASTER_LENGTH);
layer->items[layer->numitems++] = msStrdup(MSUVRASTER_LENGTH_2);
layer->items[layer->numitems++] = msStrdup(MSUVRASTER_U);
layer->items[layer->numitems++] = msStrdup(MSUVRASTER_V);
layer->items[layer->numitems] = NULL;
return msUVRASTERLayerInitItemInfo(layer);
}
/**********************************************************************
* msUVRASTERGetValues()
*
* Special attribute names are used to return some UV params: uv_angle,
* uv_length, u and v.
**********************************************************************/
static char **msUVRASTERGetValues(layerObj *layer, float *u, float *v)
{
char **values;
int i = 0;
char tmp[100];
float size_scale;
int *itemindexes = (int*)layer->iteminfo;
if(layer->numitems == 0)
return(NULL);
if(!layer->iteminfo) { /* Should not happen... but just in case! */
if (msUVRASTERLayerInitItemInfo(layer) != MS_SUCCESS)
return NULL;
itemindexes = (int*)layer->iteminfo; /* reassign after malloc */
}
if((values = (char **)malloc(sizeof(char *)*layer->numitems)) == NULL) {
msSetError(MS_MEMERR, NULL, "msUVRASTERGetValues()");
return(NULL);
}
/* -------------------------------------------------------------------- */
/* Determine desired size_scale. Default to 1 if not otherwise set */
/* -------------------------------------------------------------------- */
size_scale = 1;
if( CSLFetchNameValue( layer->processing, "UV_SIZE_SCALE" ) != NULL ) {
size_scale =
atof(CSLFetchNameValue( layer->processing, "UV_SIZE_SCALE" ));
}
for(i=0; i<layer->numitems; i++) {
if (itemindexes[i] == MSUVRASTER_ANGLEINDEX) {
snprintf(tmp, 100, "%f", (atan2((double)*v, (double)*u) * 180 / MS_PI));
values[i] = msStrdup(tmp);
} else if (itemindexes[i] == MSUVRASTER_MINUSANGLEINDEX) {
double minus_angle;
minus_angle = (atan2((double)*v, (double)*u) * 180 / MS_PI)+180;
if (minus_angle >= 360)
minus_angle -= 360;
snprintf(tmp, 100, "%f", minus_angle);
values[i] = msStrdup(tmp);
} else if ( (itemindexes[i] == MSUVRASTER_LENGTHINDEX) ||
(itemindexes[i] == MSUVRASTER_LENGTH2INDEX) ) {
float length = sqrt((*u**u)+(*v**v))*size_scale;
if (itemindexes[i] == MSUVRASTER_LENGTHINDEX)
snprintf(tmp, 100, "%f", length);
else
snprintf(tmp, 100, "%f", length/2);
values[i] = msStrdup(tmp);
} else if (itemindexes[i] == MSUVRASTER_UINDEX) {
snprintf(tmp, 100, "%f",*u);
values[i] = msStrdup(tmp);
} else if (itemindexes[i] == MSUVRASTER_VINDEX) {
snprintf(tmp, 100, "%f",*v);
values[i] = msStrdup(tmp);
} else {
values[i] = NULL;
}
}
return values;
}
rectObj msUVRASTERGetSearchRect( layerObj* layer, mapObj* map )
{
rectObj searchrect = map->extent;
int bDone = MS_FALSE;
/* For UVRaster, it is important that the searchrect is not too large */
/* to avoid insufficient intermediate raster resolution, which could */
/* happen if we use the default code path, given potential reprojection */
/* issues when using a map extent that is not in the validity area of */
/* the layer projection. */
if( !layer->projection.gt.need_geotransform &&
!(msProjIsGeographicCRS(&(map->projection)) &&
msProjIsGeographicCRS(&(layer->projection))) ) {
rectObj layer_ori_extent;
if( msLayerGetExtent(layer, &layer_ori_extent) == MS_SUCCESS ) {
projectionObj map_proj;
double map_extent_minx = map->extent.minx;
double map_extent_miny = map->extent.miny;
double map_extent_maxx = map->extent.maxx;
double map_extent_maxy = map->extent.maxy;
rectObj layer_extent = layer_ori_extent;
/* Create a variant of map->projection without geotransform for */
/* conveniency */
msInitProjection(&map_proj);
msCopyProjection(&map_proj, &map->projection);
map_proj.gt.need_geotransform = MS_FALSE;
if( map->projection.gt.need_geotransform ) {
map_extent_minx = map->projection.gt.geotransform[0]
+ map->projection.gt.geotransform[1] * map->extent.minx
+ map->projection.gt.geotransform[2] * map->extent.miny;
map_extent_miny = map->projection.gt.geotransform[3]
+ map->projection.gt.geotransform[4] * map->extent.minx
+ map->projection.gt.geotransform[5] * map->extent.miny;
map_extent_maxx = map->projection.gt.geotransform[0]
+ map->projection.gt.geotransform[1] * map->extent.maxx
+ map->projection.gt.geotransform[2] * map->extent.maxy;
map_extent_maxy = map->projection.gt.geotransform[3]
+ map->projection.gt.geotransform[4] * map->extent.maxx
+ map->projection.gt.geotransform[5] * map->extent.maxy;
}
/* Reproject layer extent to map projection */
msProjectRect(&layer->projection, &map_proj, &layer_extent);
if( layer_extent.minx <= map_extent_minx &&
layer_extent.miny <= map_extent_miny &&
layer_extent.maxx >= map_extent_maxx &&
layer_extent.maxy >= map_extent_maxy ) {
/* do nothing special if area to map is inside layer extent */
}
else {
if( layer_extent.minx >= map_extent_minx &&
layer_extent.maxx <= map_extent_maxx &&
layer_extent.miny >= map_extent_miny &&
layer_extent.maxy <= map_extent_maxy ) {
/* if the area to map is larger than the layer extent, then */
/* use full layer extent and add some margin to reflect the */
/* proportion of the useful area over the requested bbox */
double extra_x =
(map_extent_maxx - map_extent_minx) /
(layer_extent.maxx - layer_extent.minx) *
(layer_ori_extent.maxx - layer_ori_extent.minx);
double extra_y =
(map_extent_maxy - map_extent_miny) /
(layer_extent.maxy - layer_extent.miny) *
(layer_ori_extent.maxy - layer_ori_extent.miny);
searchrect.minx = layer_ori_extent.minx - extra_x / 2;
searchrect.maxx = layer_ori_extent.maxx + extra_x / 2;
searchrect.miny = layer_ori_extent.miny - extra_y / 2;
searchrect.maxy = layer_ori_extent.maxy + extra_y / 2;
}
else
{
/* otherwise clip the map extent with the reprojected layer */
/* extent */
searchrect.minx = MS_MAX( map_extent_minx, layer_extent.minx );
searchrect.maxx = MS_MIN( map_extent_maxx, layer_extent.maxx );
searchrect.miny = MS_MAX( map_extent_miny, layer_extent.miny );
searchrect.maxy = MS_MIN( map_extent_maxy, layer_extent.maxy );
/* and reproject into the layer projection */
msProjectRect(&map_proj, &layer->projection, &searchrect);
}
bDone = MS_TRUE;
}
msFreeProjection(&map_proj);
}
}
if( !bDone )
msProjectRect(&map->projection, &layer->projection, &searchrect); /* project the searchrect to source coords */
return searchrect;
}
int msUVRASTERLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
imageObj *image_tmp;
outputFormatObj *outputformat = NULL;
mapObj *map_tmp;
double map_cellsize;
unsigned int spacing;
int width, height, u_src_off, v_src_off, i, x, y;
char **alteredProcessing = NULL, *saved_layer_mask;
char **savedProcessing = NULL;
int bHasLonWrap = MS_FALSE;
double dfLonWrap = 0.0;
rectObj oldLayerExtent = {0};
char* oldLayerData = NULL;
projectionObj oldLayerProjection={0};
int ret;
if (layer->debug)
msDebug("Entering msUVRASTERLayerWhichShapes().\n");
if( uvlinfo == NULL )
return MS_FAILURE;
if( CSLFetchNameValue( layer->processing, "BANDS" ) == NULL ) {
msSetError( MS_MISCERR, "BANDS processing option is required for UV layer. You have to specified 2 bands.",
"msUVRASTERLayerWhichShapes()" );
return MS_FAILURE;
}
/*
** Allocate mapObj structure
*/
map_tmp = (mapObj *)msSmallCalloc(sizeof(mapObj),1);
if(initMap(map_tmp) == -1) { /* initialize this map */
msFree(map_tmp);
return(MS_FAILURE);
}
/* -------------------------------------------------------------------- */
/* Determine desired spacing. Default to 32 if not otherwise set */
/* -------------------------------------------------------------------- */
spacing = 32;
if( CSLFetchNameValue( layer->processing, "UV_SPACING" ) != NULL ) {
spacing =
atoi(CSLFetchNameValue( layer->processing, "UV_SPACING" ));
if( spacing == 0 )
spacing = 32;
}
width = (int)(layer->map->width/spacing);
height = (int)(layer->map->height/spacing);
/* Initialize our dummy map */
MS_INIT_COLOR(map_tmp->imagecolor, 255,255,255,255);
map_tmp->resolution = layer->map->resolution;
map_tmp->defresolution = layer->map->defresolution;
outputformat = (outputFormatObj *) msSmallCalloc(1,sizeof(outputFormatObj));
outputformat->bands = uvlinfo->band_count = 2;
outputformat->name = NULL;
outputformat->driver = NULL;
outputformat->refcount = 0;
outputformat->vtable = NULL;
outputformat->device = NULL;
outputformat->renderer = MS_RENDER_WITH_RAWDATA;
outputformat->imagemode = MS_IMAGEMODE_FLOAT32;
msAppendOutputFormat(map_tmp, outputformat);
msCopyHashTable(&map_tmp->configoptions, &layer->map->configoptions);
map_tmp->mappath = msStrdup(layer->map->mappath);
map_tmp->shapepath = msStrdup(layer->map->shapepath);
map_tmp->gt.rotation_angle = 0.0;
/* Custom msCopyProjection() that removes lon_wrap parameter */
{
int i;
map_tmp->projection.numargs = 0;
map_tmp->projection.gt = layer->projection.gt;
map_tmp->projection.automatic = layer->projection.automatic;
for (i = 0; i < layer->projection.numargs; i++) {
if( strncmp(layer->projection.args[i], "lon_wrap=",
strlen("lon_wrap=")) == 0 ) {
bHasLonWrap = MS_TRUE;
dfLonWrap = atof( layer->projection.args[i] + strlen("lon_wrap=") );
}
else {
map_tmp->projection.args[map_tmp->projection.numargs ++] =
msStrdup(layer->projection.args[i]);
}
}
if (map_tmp->projection.numargs != 0) {
msProcessProjection(&(map_tmp->projection));
}
map_tmp->projection.wellknownprojection = layer->projection.wellknownprojection;
}
/* Very special case to improve quality for rasters referenced from lon=0 to 360 */
/* We create a temporary VRT that swiches the 2 hemispheres, and then we */
/* modify the georeferncing to be in the more standard [-180, 180] range */
/* and we adjust the layer->data, extent and projection accordingly */
if( layer->tileindex == NULL &&
uvlinfo->mapToUseForWhichShapes && bHasLonWrap && dfLonWrap == 180.0 )
{
rectObj layerExtent;
msLayerGetExtent(layer, &layerExtent);
if( layerExtent.minx == 0 && layerExtent.maxx == 360 )
{
GDALDatasetH hDS = NULL;
char* decrypted_path;
if( strncmp(layer->data, "<VRTDataset", strlen("<VRTDataset")) == 0 )
{
decrypted_path = msStrdup(layer->data);
}
else
{
char szPath[MS_MAXPATHLEN];
msTryBuildPath3(szPath, layer->map->mappath,
layer->map->shapepath, layer->data);
decrypted_path = msDecryptStringTokens( layer->map, szPath );
}
if( decrypted_path )
{
char** connectionoptions;
GDALAllRegister();
connectionoptions = msGetStringListFromHashTable(&(layer->connectionoptions));
hDS = GDALOpenEx(decrypted_path,
GDAL_OF_RASTER,
NULL,
(const char* const*)connectionoptions,
NULL);
CSLDestroy(connectionoptions);
}
if( hDS != NULL )
{
int iBand;
int nXSize = GDALGetRasterXSize( hDS );
int nYSize = GDALGetRasterYSize( hDS );
int nBands = GDALGetRasterCount( hDS );
int nMaxLen = 100 + nBands * (800 + 2 * strlen(decrypted_path));
int nOffset = 0;
char* pszInlineVRT = msSmallMalloc( nMaxLen );
snprintf(pszInlineVRT, nMaxLen,
"<VRTDataset rasterXSize=\"%d\" rasterYSize=\"%d\">",
nXSize, nYSize);
nOffset = strlen(pszInlineVRT);
for( iBand = 1; iBand <= nBands; iBand++ )
{
const char* pszDataType = "Byte";
switch( GDALGetRasterDataType(GDALGetRasterBand(hDS, iBand)) )
{
case GDT_Byte: pszDataType = "Byte"; break;
case GDT_Int16: pszDataType = "Int16"; break;
case GDT_UInt16: pszDataType = "UInt16"; break;
case GDT_Int32: pszDataType = "Int32"; break;
case GDT_UInt32: pszDataType = "UInt32"; break;
case GDT_Float32: pszDataType = "Float32"; break;
case GDT_Float64: pszDataType = "Float64"; break;
default: break;
}
snprintf( pszInlineVRT + nOffset, nMaxLen - nOffset,
" <VRTRasterBand dataType=\"%s\" band=\"%d\">"
" <SimpleSource>"
" <SourceFilename relativeToVrt=\"1\"><![CDATA[%s]]></SourceFilename>"
" <SourceBand>%d</SourceBand>"
" <SrcRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" ySize=\"%d\"/>"
" <DstRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" ySize=\"%d\"/>"
" </SimpleSource>"
" <SimpleSource>"
" <SourceFilename relativeToVrt=\"1\"><![CDATA[%s]]></SourceFilename>"
" <SourceBand>%d</SourceBand>"
" <SrcRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" ySize=\"%d\"/>"
" <DstRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" ySize=\"%d\"/>"
" </SimpleSource>"
" </VRTRasterBand>",
pszDataType, iBand,
decrypted_path, iBand,
nXSize / 2, 0, nXSize - nXSize / 2, nYSize,
0, 0, nXSize - nXSize / 2, nYSize,
decrypted_path, iBand,
0, 0, nXSize / 2, nYSize,
nXSize - nXSize / 2, 0, nXSize / 2, nYSize );
nOffset += strlen(pszInlineVRT + nOffset);
}
snprintf(pszInlineVRT + nOffset, nMaxLen - nOffset,
"</VRTDataset>");
oldLayerExtent = layer->extent;
oldLayerData = layer->data;
oldLayerProjection = layer->projection;
layer->extent.minx = -180;
layer->extent.maxx = 180;
layer->data = pszInlineVRT;
layer->projection = map_tmp->projection;
/* map_tmp->projection is actually layer->projection without lon_wrap */
rect = uvlinfo->mapToUseForWhichShapes->extent;
msProjectRect(&uvlinfo->mapToUseForWhichShapes->projection,
&map_tmp->projection, &rect);
bHasLonWrap = MS_FALSE;
GDALClose(hDS);
}
msFree( decrypted_path );
}
}
if( isQuery )
{
/* For query mode, use layer->map->extent reprojected rather than */
/* the provided rect. Generic query code will filter returned features. */
rect = msUVRASTERGetSearchRect(layer, layer->map);
}
map_cellsize = MS_MAX(MS_CELLSIZE(rect.minx, rect.maxx,layer->map->width),
MS_CELLSIZE(rect.miny,rect.maxy,layer->map->height));
map_tmp->cellsize = map_cellsize*spacing;
map_tmp->extent.minx = rect.minx-(0.5*map_cellsize)+(0.5*map_tmp->cellsize);
map_tmp->extent.miny = rect.miny-(0.5*map_cellsize)+(0.5*map_tmp->cellsize);
map_tmp->extent.maxx = map_tmp->extent.minx+((width-1)*map_tmp->cellsize);
map_tmp->extent.maxy = map_tmp->extent.miny+((height-1)*map_tmp->cellsize);
if( bHasLonWrap && dfLonWrap == 180.0) {
if( map_tmp->extent.minx >= 180 ) {
/* Request on the right half of the shifted raster (= western hemisphere) */
map_tmp->extent.minx -= 360;
map_tmp->extent.maxx -= 360;
}
else if( map_tmp->extent.maxx >= 180.0 ) {
/* Request spanning on the 2 hemispheres => drawing whole planet */
/* Take only into account vertical resolution, as horizontal one */
/* will be unreliable (assuming square pixels...) */
map_cellsize = MS_CELLSIZE(rect.miny,rect.maxy,layer->map->height);
map_tmp->cellsize = map_cellsize*spacing;
width = 360.0 / map_tmp->cellsize;
map_tmp->extent.minx = -180.0+(0.5*map_tmp->cellsize);
map_tmp->extent.maxx = 180.0-(0.5*map_tmp->cellsize);
}
}
if (layer->debug)
msDebug("msUVRASTERLayerWhichShapes(): width: %d, height: %d, cellsize: %g\n",
width, height, map_tmp->cellsize);
if (layer->debug == 5)
msDebug("msUVRASTERLayerWhichShapes(): extent: %g %g %g %g\n",
map_tmp->extent.minx, map_tmp->extent.miny,
map_tmp->extent.maxx, map_tmp->extent.maxy);
/* important to use that function, to compute map
geotransform, used by the resampling*/
msMapSetSize(map_tmp, width, height);
if (layer->debug == 5)
msDebug("msUVRASTERLayerWhichShapes(): geotransform: %g %g %g %g %g %g\n",
map_tmp->gt.geotransform[0], map_tmp->gt.geotransform[1],
map_tmp->gt.geotransform[2], map_tmp->gt.geotransform[3],
map_tmp->gt.geotransform[4], map_tmp->gt.geotransform[5]);
uvlinfo->extent = map_tmp->extent;
image_tmp = msImageCreate(width, height, map_tmp->outputformatlist[0],
NULL, NULL, map_tmp->resolution, map_tmp->defresolution,
&(map_tmp->imagecolor));
/* Default set to AVERAGE resampling */
if( CSLFetchNameValue( layer->processing, "RESAMPLE" ) == NULL ) {
alteredProcessing = CSLDuplicate( layer->processing );
alteredProcessing =
CSLSetNameValue( alteredProcessing, "RESAMPLE",
"AVERAGE");
savedProcessing = layer->processing;
layer->processing = alteredProcessing;
}
/* disable masking at this level: we don't want to apply the mask at the raster level,
* it will be applied with the correct cellsize and image size in the vector rendering
* phase.
*/
saved_layer_mask = layer->mask;
layer->mask = NULL;
ret = msDrawRasterLayerLow(map_tmp, layer, image_tmp, NULL );
/* restore layer attributes if we went through the above on-the-fly VRT */
if( oldLayerData )
{
msFree(layer->data);
layer->data = oldLayerData;
layer->extent = oldLayerExtent;
layer->projection = oldLayerProjection;
}
/* restore layer mask */
layer->mask = saved_layer_mask;
/* restore the saved processing */
if (alteredProcessing != NULL) {
layer->processing = savedProcessing;
CSLDestroy(alteredProcessing);
}
if( ret == MS_FAILURE) {
msSetError(MS_MISCERR, "Unable to draw raster data.", "msUVRASTERLayerWhichShapes()");
msFreeMap(map_tmp);
msFreeImage(image_tmp);
return MS_FAILURE;
}
/* free old query arrays */
if (uvlinfo->u) {
for (i=0; i<uvlinfo->width; ++i) {
free(uvlinfo->u[i]);
}
free(uvlinfo->u);
}
if (uvlinfo->v) {
for (i=0; i<uvlinfo->width; ++i) {
free(uvlinfo->v[i]);
}
free(uvlinfo->v);
}
/* Update our uv layer structure */
uvlinfo->width = width;
uvlinfo->height = height;
uvlinfo->query_results = width*height;
uvlinfo->u = (float **)msSmallMalloc(sizeof(float *)*width);
uvlinfo->v = (float **)msSmallMalloc(sizeof(float *)*width);
for (x = 0; x < width; ++x) {
uvlinfo->u[x] = (float *)msSmallMalloc(height * sizeof(float));
uvlinfo->v[x] = (float *)msSmallMalloc(height * sizeof(float));
for (y = 0; y < height; ++y) {
u_src_off = v_src_off = x + y * width;
v_src_off += width*height;
uvlinfo->u[x][y] = image_tmp->img.raw_float[u_src_off];
uvlinfo->v[x][y] = image_tmp->img.raw_float[v_src_off];
/* null vector? update the number of results */
if (uvlinfo->u[x][y] == 0 && uvlinfo->v[x][y] == 0)
--uvlinfo->query_results;
}
}
msFreeImage(image_tmp); /* we do not need the imageObj anymore */
msFreeMap(map_tmp);
uvlinfo->next_shape = 0;
return MS_SUCCESS;
}
int msUVRASTERLayerGetShape(layerObj *layer, shapeObj *shape, resultObj *record)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
lineObj line ;
pointObj point;
int i, j, k, x=0, y=0;
long shapeindex = record->shapeindex;
msFreeShape(shape);
shape->type = MS_SHAPE_NULL;
if( shapeindex < 0 || shapeindex >= uvlinfo->query_results ) {
msSetError(MS_MISCERR,
"Out of range shape index requested. Requested %ld\n"
"but only %d shapes available.",
"msUVRASTERLayerGetShape()",
shapeindex, uvlinfo->query_results );
return MS_FAILURE;
}
/* loop to the next non null vector */
k = 0;
for (i=0, x=-1; i<uvlinfo->width && k<=shapeindex; ++i, ++x) {
for (j=0, y=-1; j<uvlinfo->height && k<=shapeindex; ++j, ++k, ++y) {
if (uvlinfo->u[i][j] == 0 && uvlinfo->v[i][j] == 0)
--k;
}
}
point.x = Pix2Georef(x, 0, uvlinfo->width-1,
uvlinfo->extent.minx, uvlinfo->extent.maxx, MS_FALSE);
point.y = Pix2Georef(y, 0, uvlinfo->height-1,
uvlinfo->extent.miny, uvlinfo->extent.maxy, MS_TRUE);
if (layer->debug == 5)
msDebug("msUVRASTERLayerWhichShapes(): shapeindex: %ld, x: %g, y: %g\n",
shapeindex, point.x, point.y);
point.m = 0.0;
shape->type = MS_SHAPE_POINT;
line.numpoints = 1;
line.point = &point;
msAddLine( shape, &line );
msComputeBounds( shape );
shape->numvalues = layer->numitems;
shape->values = msUVRASTERGetValues(layer, &uvlinfo->u[x][y], &uvlinfo->v[x][y]);
shape->index = shapeindex;
shape->resultindex = shapeindex;
return MS_SUCCESS;
}
int msUVRASTERLayerNextShape(layerObj *layer, shapeObj *shape)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
if( uvlinfo->next_shape < 0
|| uvlinfo->next_shape >= uvlinfo->query_results ) {
msFreeShape(shape);
shape->type = MS_SHAPE_NULL;
return MS_DONE;
} else {
resultObj record;
record.shapeindex = uvlinfo->next_shape++;
record.tileindex = 0;
record.classindex = record.resultindex = -1;
return msUVRASTERLayerGetShape( layer, shape, &record);
}
}
/************************************************************************/
/* msUVRASTERLayerGetExtent() */
/* Simple copy of the maprasterquery.c file. might change in the future */
/************************************************************************/
int msUVRASTERLayerGetExtent(layerObj *layer, rectObj *extent)
{
char szPath[MS_MAXPATHLEN];
mapObj *map = layer->map;
shapefileObj *tileshpfile;
if( (!layer->data || strlen(layer->data) == 0)
&& layer->tileindex == NULL) {
/* should we be issuing a specific error about not supporting
extents for tileindexed raster layers? */
return MS_FAILURE;
}
if( map == NULL )
return MS_FAILURE;
/* If the layer use a tileindex, return the extent of the tileindex shapefile/referenced layer */
if (layer->tileindex) {
const int tilelayerindex = msGetLayerIndex(map, layer->tileindex);
if(tilelayerindex != -1) /* does the tileindex reference another layer */
return msLayerGetExtent(GET_LAYER(map, tilelayerindex), extent);
else {
tileshpfile = (shapefileObj *) malloc(sizeof(shapefileObj));
MS_CHECK_ALLOC(tileshpfile, sizeof(shapefileObj), MS_FAILURE);
if(msShapefileOpen(tileshpfile, "rb", msBuildPath3(szPath, map->mappath, map->shapepath, layer->tileindex), MS_TRUE) == -1)
if(msShapefileOpen(tileshpfile, "rb", msBuildPath(szPath, map->mappath, layer->tileindex), MS_TRUE) == -1)
return MS_FAILURE;
*extent = tileshpfile->bounds;
msShapefileClose(tileshpfile);
free(tileshpfile);
return MS_SUCCESS;
}
}
msTryBuildPath3(szPath, map->mappath, map->shapepath, layer->data);
char* decrypted_path = msDecryptStringTokens( map, szPath );
if( !decrypted_path )
return MS_FAILURE;
GDALAllRegister();
char** connectionoptions = msGetStringListFromHashTable(&(layer->connectionoptions));
GDALDatasetH hDS = GDALOpenEx(decrypted_path,
GDAL_OF_RASTER,
NULL,
(const char* const*)connectionoptions,
NULL);
CSLDestroy(connectionoptions);
msFree( decrypted_path );
if( hDS == NULL ) {
return MS_FAILURE;
}
const int nXSize = GDALGetRasterXSize( hDS );
const int nYSize = GDALGetRasterYSize( hDS );
double adfGeoTransform[6] = {0};
const CPLErr eErr = GDALGetGeoTransform( hDS, adfGeoTransform );
if( eErr != CE_None ) {
GDALClose( hDS );
return MS_FAILURE;
}
/* If this appears to be an ungeoreferenced raster than flip it for
mapservers purposes. */
if( adfGeoTransform[5] == 1.0 && adfGeoTransform[3] == 0.0 ) {
adfGeoTransform[5] = -1.0;
adfGeoTransform[3] = nYSize;
}
extent->minx = adfGeoTransform[0];
extent->maxy = adfGeoTransform[3];
extent->maxx = adfGeoTransform[0] + nXSize * adfGeoTransform[1];
extent->miny = adfGeoTransform[3] + nYSize * adfGeoTransform[5];
return MS_SUCCESS;
}
/************************************************************************/
/* msUVRASTERLayerSetTimeFilter() */
/* */
/* This function is actually just used in the context of */
/* setting a filter on the tileindex for time based queries. */
/* For instance via WMS requests. So it isn't really related */