Coverage Report

Created: 2026-09-14 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/MapServer/src/mapuvraster.cpp
Line
Count
Source
1
/**********************************************************************
2
 * $Id: mapuv.c 12629 2011-10-06 18:06:34Z aboudreault $
3
 *
4
 * Project:  MapServer
5
 * Purpose:  UV Layer
6
 * Author:   Alan Boudreault (aboudreault@mapgears.com)
7
 *
8
 **********************************************************************
9
 * Copyright (c) 2011, Alan Boudreault, MapGears
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a
12
 * copy of this software and associated documentation files (the "Software"),
13
 * to deal in the Software without restriction, including without limitation
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
 * and/or sell copies of the Software, and to permit persons to whom the
16
 * Software is furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies of this Software or works derived from this Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
 * DEALINGS IN THE SOFTWARE.
28
 **********************************************************************/
29
30
#include "mapserver.h"
31
32
#include <assert.h>
33
#include <math.h>
34
#include <stdbool.h>
35
#include "mapows.h"
36
#include "mapresample.h"
37
#include "mapthread.h"
38
39
#include <cmath>
40
#include <limits>
41
42
#define MSUVRASTER_NUMITEMS 6
43
0
#define MSUVRASTER_ANGLE "uv_angle"
44
0
#define MSUVRASTER_ANGLEINDEX -100
45
0
#define MSUVRASTER_MINUS_ANGLE "uv_minus_angle"
46
0
#define MSUVRASTER_MINUSANGLEINDEX -101
47
0
#define MSUVRASTER_LENGTH "uv_length"
48
0
#define MSUVRASTER_LENGTHINDEX -102
49
0
#define MSUVRASTER_LENGTH_2 "uv_length_2"
50
0
#define MSUVRASTER_LENGTH2INDEX -103
51
0
#define MSUVRASTER_U "u"
52
0
#define MSUVRASTER_UINDEX -104
53
0
#define MSUVRASTER_V "v"
54
0
#define MSUVRASTER_VINDEX -105
55
0
#define MSUVRASTER_LON "lon"
56
0
#define MSUVRASTER_LONINDEX -106
57
0
#define MSUVRASTER_LAT "lat"
58
0
#define MSUVRASTER_LATINDEX -107
59
60
struct uvRasterLayerInfo {
61
62
  /* query cache results */
63
  int query_results = 0;
64
65
  int refcount = 0;
66
67
  float *u = nullptr; /* u values */
68
  float *v = nullptr; /* v values */
69
  int width = 0;
70
  int height = 0;
71
  rectObj extent{};
72
  int next_shape = 0;
73
74
  /* To improve performance of GetShape() when queried on increasing shapeindex
75
   */
76
  long last_queried_shapeindex = 0; // value in [0, query_results[ range
77
  size_t last_raster_off = 0;       // value in [0, width*height[ range
78
79
  bool needsLonLat = false;
80
  reprojectionObj *reprojectorToLonLat = nullptr;
81
82
  /* set if the map->extent and map->projection are
83
     valid in msUVRASTERLayerWhichShapes() */
84
  mapObj *mapToUseForWhichShapes = nullptr;
85
86
  std::string timestring{};
87
  std::string timefield{};
88
};
89
90
0
static uvRasterLayerInfo *getLayerInfo(layerObj *layer) {
91
0
  return static_cast<uvRasterLayerInfo *>(layer->layerinfo);
92
0
}
93
94
void msUVRASTERLayerUseMapExtentAndProjectionForNextWhichShapes(layerObj *layer,
95
0
                                                                mapObj *map) {
96
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
97
0
  uvlinfo->mapToUseForWhichShapes = map;
98
0
}
99
100
0
static int msUVRASTERLayerInitItemInfo(layerObj *layer) {
101
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
102
0
  int i;
103
0
  int *itemindexes;
104
0
  int failed = 0;
105
106
0
  if (layer->numitems == 0)
107
0
    return MS_SUCCESS;
108
109
0
  if (uvlinfo == NULL) {
110
0
    msSetError(MS_MISCERR, "Assertion failed: GDAL layer not opened!!!",
111
0
               "msUVRASTERLayerInitItemInfo()");
112
0
    return (MS_FAILURE);
113
0
  }
114
115
0
  if (layer->iteminfo)
116
0
    free(layer->iteminfo);
117
118
0
  if ((layer->iteminfo = (int *)malloc(sizeof(int) * layer->numitems)) ==
119
0
      NULL) {
120
0
    msSetError(MS_MEMERR, NULL, "msUVRASTERLayerInitItemInfo()");
121
0
    return (MS_FAILURE);
122
0
  }
123
124
0
  itemindexes = (int *)layer->iteminfo;
125
0
  for (i = 0; i < layer->numitems; i++) {
126
    /* Special attribute names. */
127
0
    if (EQUAL(layer->items[i], MSUVRASTER_ANGLE))
128
0
      itemindexes[i] = MSUVRASTER_ANGLEINDEX;
129
0
    else if (EQUAL(layer->items[i], MSUVRASTER_MINUS_ANGLE))
130
0
      itemindexes[i] = MSUVRASTER_MINUSANGLEINDEX;
131
0
    else if (EQUAL(layer->items[i], MSUVRASTER_LENGTH))
132
0
      itemindexes[i] = MSUVRASTER_LENGTHINDEX;
133
0
    else if (EQUAL(layer->items[i], MSUVRASTER_LENGTH_2))
134
0
      itemindexes[i] = MSUVRASTER_LENGTH2INDEX;
135
0
    else if (EQUAL(layer->items[i], MSUVRASTER_U))
136
0
      itemindexes[i] = MSUVRASTER_UINDEX;
137
0
    else if (EQUAL(layer->items[i], MSUVRASTER_V))
138
0
      itemindexes[i] = MSUVRASTER_VINDEX;
139
0
    else if (EQUAL(layer->items[i], MSUVRASTER_LON)) {
140
0
      uvlinfo->needsLonLat = true;
141
0
      itemindexes[i] = MSUVRASTER_LONINDEX;
142
0
    } else if (EQUAL(layer->items[i], MSUVRASTER_LAT)) {
143
0
      uvlinfo->needsLonLat = true;
144
0
      itemindexes[i] = MSUVRASTER_LATINDEX;
145
0
    } else {
146
0
      itemindexes[i] = -1;
147
0
      msSetError(MS_MISCERR, "Invalid Field name: %s",
148
0
                 "msUVRASTERLayerInitItemInfo()", layer->items[i]);
149
0
      failed = 1;
150
0
    }
151
0
  }
152
153
0
  return failed ? (MS_FAILURE) : (MS_SUCCESS);
154
0
}
155
156
0
void msUVRASTERLayerFreeItemInfo(layerObj *layer) {
157
0
  if (layer->iteminfo)
158
0
    free(layer->iteminfo);
159
0
  layer->iteminfo = NULL;
160
0
}
161
162
0
static void msUVRasterLayerInfoInitialize(layerObj *layer) {
163
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
164
165
0
  if (uvlinfo != NULL)
166
0
    return;
167
168
0
  uvlinfo = new uvRasterLayerInfo;
169
0
  layer->layerinfo = uvlinfo;
170
171
  /* Set attribute type to Real, unless the user has explicitly set */
172
  /* something else. */
173
0
  {
174
0
    const char *const items[] = {
175
0
        MSUVRASTER_ANGLE,    MSUVRASTER_MINUS_ANGLE, MSUVRASTER_LENGTH,
176
0
        MSUVRASTER_LENGTH_2, MSUVRASTER_U,           MSUVRASTER_V,
177
0
    };
178
0
    size_t i;
179
0
    for (i = 0; i < sizeof(items) / sizeof(items[0]); ++i) {
180
0
      char szTmp[100];
181
0
      snprintf(szTmp, sizeof(szTmp), "%s_type", items[i]);
182
0
      if (msOWSLookupMetadata(&(layer->metadata), "OFG", szTmp) == NULL) {
183
0
        snprintf(szTmp, sizeof(szTmp), "gml_%s_type", items[i]);
184
0
        msInsertHashTable(&(layer->metadata), szTmp, "Real");
185
0
      }
186
0
    }
187
0
  }
188
0
}
189
190
static void msUVRasterLayerInfoFree(layerObj *layer)
191
192
0
{
193
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
194
195
0
  if (uvlinfo == NULL)
196
0
    return;
197
198
0
  free(uvlinfo->u);
199
0
  free(uvlinfo->v);
200
201
0
  if (uvlinfo->reprojectorToLonLat) {
202
0
    msProjectDestroyReprojector(uvlinfo->reprojectorToLonLat);
203
0
  }
204
205
0
  delete uvlinfo;
206
207
0
  layer->layerinfo = NULL;
208
0
}
209
210
0
int msUVRASTERLayerOpen(layerObj *layer) {
211
  /* If we don't have info, initialize an empty one now */
212
0
  if (layer->layerinfo == NULL)
213
0
    msUVRasterLayerInfoInitialize(layer);
214
0
  if (layer->layerinfo == NULL)
215
0
    return MS_FAILURE;
216
217
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
218
219
0
  uvlinfo->refcount = uvlinfo->refcount + 1;
220
221
0
  return MS_SUCCESS;
222
0
}
223
224
0
int msUVRASTERLayerIsOpen(layerObj *layer) {
225
0
  if (layer->layerinfo)
226
0
    return MS_TRUE;
227
0
  return MS_FALSE;
228
0
}
229
230
0
int msUVRASTERLayerClose(layerObj *layer) {
231
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
232
233
0
  if (uvlinfo != NULL) {
234
0
    uvlinfo->refcount--;
235
236
0
    if (uvlinfo->refcount < 1)
237
0
      msUVRasterLayerInfoFree(layer);
238
0
  }
239
0
  return MS_SUCCESS;
240
0
}
241
242
0
int msUVRASTERLayerGetItems(layerObj *layer) {
243
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
244
245
0
  if (uvlinfo == NULL)
246
0
    return MS_FAILURE;
247
248
0
  layer->numitems = 0;
249
0
  layer->items = (char **)msSmallCalloc(sizeof(char *), 10);
250
251
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_ANGLE);
252
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_MINUS_ANGLE);
253
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_LENGTH);
254
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_LENGTH_2);
255
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_U);
256
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_V);
257
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_LON);
258
0
  layer->items[layer->numitems++] = msStrdup(MSUVRASTER_LAT);
259
0
  layer->items[layer->numitems] = NULL;
260
261
0
  return msUVRASTERLayerInitItemInfo(layer);
262
0
}
263
264
/**********************************************************************
265
 *                     msUVRASTERGetValues()
266
 *
267
 * Special attribute names are used to return some UV params: uv_angle,
268
 * uv_length, u and v.
269
 **********************************************************************/
270
static char **msUVRASTERGetValues(layerObj *layer, float u, float v,
271
0
                                  const pointObj *point) {
272
0
  char **values;
273
0
  int i = 0;
274
0
  char tmp[100];
275
0
  float size_scale;
276
0
  int *itemindexes = (int *)layer->iteminfo;
277
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
278
0
  double lon = HUGE_VAL;
279
0
  double lat = HUGE_VAL;
280
281
0
  if (layer->numitems == 0)
282
0
    return (NULL);
283
284
0
  if (!layer->iteminfo) { /* Should not happen... but just in case! */
285
0
    if (msUVRASTERLayerInitItemInfo(layer) != MS_SUCCESS)
286
0
      return NULL;
287
0
    itemindexes = (int *)layer->iteminfo; /* reassign after malloc */
288
0
  }
289
290
0
  if ((values = (char **)malloc(sizeof(char *) * layer->numitems)) == NULL) {
291
0
    msSetError(MS_MEMERR, NULL, "msUVRASTERGetValues()");
292
0
    return (NULL);
293
0
  }
294
295
  /* -------------------------------------------------------------------- */
296
  /*    Determine desired size_scale.  Default to 1 if not otherwise set  */
297
  /* -------------------------------------------------------------------- */
298
0
  size_scale = 1;
299
0
  if (CSLFetchNameValue(layer->processing, "UV_SIZE_SCALE") != NULL) {
300
0
    size_scale = atof(CSLFetchNameValue(layer->processing, "UV_SIZE_SCALE"));
301
0
  }
302
303
0
  if (uvlinfo->needsLonLat) {
304
0
    if (uvlinfo->reprojectorToLonLat == NULL)
305
0
      uvlinfo->reprojectorToLonLat =
306
0
          msProjectCreateReprojector(&layer->projection, NULL);
307
0
    if (uvlinfo->reprojectorToLonLat) {
308
0
      pointObj pointWrk = *point;
309
0
      if (msProjectPointEx(uvlinfo->reprojectorToLonLat, &pointWrk) ==
310
0
          MS_SUCCESS) {
311
0
        lon = pointWrk.x;
312
0
        lat = pointWrk.y;
313
0
      }
314
0
    }
315
0
  }
316
317
0
  for (i = 0; i < layer->numitems; i++) {
318
0
    if (itemindexes[i] == MSUVRASTER_ANGLEINDEX) {
319
0
      snprintf(tmp, 100, "%f", (atan2((double)v, (double)u) * 180 / MS_PI));
320
0
      values[i] = msStrdup(tmp);
321
0
    } else if (itemindexes[i] == MSUVRASTER_MINUSANGLEINDEX) {
322
0
      double minus_angle;
323
0
      minus_angle = (atan2((double)v, (double)u) * 180 / MS_PI) + 180;
324
0
      if (minus_angle >= 360)
325
0
        minus_angle -= 360;
326
0
      snprintf(tmp, 100, "%f", minus_angle);
327
0
      values[i] = msStrdup(tmp);
328
0
    } else if ((itemindexes[i] == MSUVRASTER_LENGTHINDEX) ||
329
0
               (itemindexes[i] == MSUVRASTER_LENGTH2INDEX)) {
330
0
      float length = sqrt((u * u) + (v * v)) * size_scale;
331
332
0
      if (itemindexes[i] == MSUVRASTER_LENGTHINDEX)
333
0
        snprintf(tmp, 100, "%f", length);
334
0
      else
335
0
        snprintf(tmp, 100, "%f", length / 2);
336
337
0
      values[i] = msStrdup(tmp);
338
0
    } else if (itemindexes[i] == MSUVRASTER_UINDEX) {
339
0
      snprintf(tmp, 100, "%f", u);
340
0
      values[i] = msStrdup(tmp);
341
0
    } else if (itemindexes[i] == MSUVRASTER_VINDEX) {
342
0
      snprintf(tmp, 100, "%f", v);
343
0
      values[i] = msStrdup(tmp);
344
0
    } else if (itemindexes[i] == MSUVRASTER_LONINDEX) {
345
0
      snprintf(tmp, sizeof(tmp), "%.18g", lon);
346
0
      values[i] = msStrdup(tmp);
347
0
    } else if (itemindexes[i] == MSUVRASTER_LATINDEX) {
348
0
      snprintf(tmp, sizeof(tmp), "%.18g", lat);
349
0
      values[i] = msStrdup(tmp);
350
0
    } else {
351
0
      values[i] = NULL;
352
0
    }
353
0
  }
354
355
0
  return values;
356
0
}
357
358
0
rectObj msUVRASTERGetSearchRect(layerObj *layer, mapObj *map) {
359
0
  rectObj searchrect = map->extent;
360
0
  int bDone = MS_FALSE;
361
362
  /* For UVRaster, it is important that the searchrect is not too large */
363
  /* to avoid insufficient intermediate raster resolution, which could */
364
  /* happen if we use the default code path, given potential reprojection */
365
  /* issues when using a map extent that is not in the validity area of */
366
  /* the layer projection. */
367
0
  if (!layer->projection.gt.need_geotransform &&
368
0
      !(msProjIsGeographicCRS(&(map->projection)) &&
369
0
        msProjIsGeographicCRS(&(layer->projection)))) {
370
0
    rectObj layer_ori_extent;
371
372
0
    if (msLayerGetExtent(layer, &layer_ori_extent) == MS_SUCCESS) {
373
0
      projectionObj map_proj;
374
375
0
      double map_extent_minx = map->extent.minx;
376
0
      double map_extent_miny = map->extent.miny;
377
0
      double map_extent_maxx = map->extent.maxx;
378
0
      double map_extent_maxy = map->extent.maxy;
379
0
      rectObj layer_extent = layer_ori_extent;
380
381
      /* Create a variant of map->projection without geotransform for */
382
      /* conveniency */
383
0
      msInitProjection(&map_proj);
384
0
      msCopyProjection(&map_proj, &map->projection);
385
0
      map_proj.gt.need_geotransform = MS_FALSE;
386
0
      if (map->projection.gt.need_geotransform) {
387
0
        map_extent_minx =
388
0
            map->projection.gt.geotransform[0] +
389
0
            map->projection.gt.geotransform[1] * map->extent.minx +
390
0
            map->projection.gt.geotransform[2] * map->extent.miny;
391
0
        map_extent_miny =
392
0
            map->projection.gt.geotransform[3] +
393
0
            map->projection.gt.geotransform[4] * map->extent.minx +
394
0
            map->projection.gt.geotransform[5] * map->extent.miny;
395
0
        map_extent_maxx =
396
0
            map->projection.gt.geotransform[0] +
397
0
            map->projection.gt.geotransform[1] * map->extent.maxx +
398
0
            map->projection.gt.geotransform[2] * map->extent.maxy;
399
0
        map_extent_maxy =
400
0
            map->projection.gt.geotransform[3] +
401
0
            map->projection.gt.geotransform[4] * map->extent.maxx +
402
0
            map->projection.gt.geotransform[5] * map->extent.maxy;
403
0
      }
404
405
      /* Reproject layer extent to map projection */
406
0
      msProjectRect(&layer->projection, &map_proj, &layer_extent);
407
408
0
      if (layer_extent.minx <= map_extent_minx &&
409
0
          layer_extent.miny <= map_extent_miny &&
410
0
          layer_extent.maxx >= map_extent_maxx &&
411
0
          layer_extent.maxy >= map_extent_maxy) {
412
        /* do nothing special if area to map is inside layer extent */
413
0
      } else {
414
0
        if (layer_extent.minx >= map_extent_minx &&
415
0
            layer_extent.maxx <= map_extent_maxx &&
416
0
            layer_extent.miny >= map_extent_miny &&
417
0
            layer_extent.maxy <= map_extent_maxy) {
418
          /* if the area to map is larger than the layer extent, then */
419
          /* use full layer extent and add some margin to reflect the */
420
          /* proportion of the useful area over the requested bbox */
421
0
          double extra_x = (map_extent_maxx - map_extent_minx) /
422
0
                           (layer_extent.maxx - layer_extent.minx) *
423
0
                           (layer_ori_extent.maxx - layer_ori_extent.minx);
424
0
          double extra_y = (map_extent_maxy - map_extent_miny) /
425
0
                           (layer_extent.maxy - layer_extent.miny) *
426
0
                           (layer_ori_extent.maxy - layer_ori_extent.miny);
427
0
          searchrect.minx = layer_ori_extent.minx - extra_x / 2;
428
0
          searchrect.maxx = layer_ori_extent.maxx + extra_x / 2;
429
0
          searchrect.miny = layer_ori_extent.miny - extra_y / 2;
430
0
          searchrect.maxy = layer_ori_extent.maxy + extra_y / 2;
431
0
        } else {
432
          /* otherwise clip the map extent with the reprojected layer */
433
          /* extent */
434
0
          searchrect.minx = MS_MAX(map_extent_minx, layer_extent.minx);
435
0
          searchrect.maxx = MS_MIN(map_extent_maxx, layer_extent.maxx);
436
0
          searchrect.miny = MS_MAX(map_extent_miny, layer_extent.miny);
437
0
          searchrect.maxy = MS_MIN(map_extent_maxy, layer_extent.maxy);
438
          /* and reproject into the layer projection */
439
0
          msProjectRect(&map_proj, &layer->projection, &searchrect);
440
0
        }
441
0
        bDone = MS_TRUE;
442
0
      }
443
444
0
      msFreeProjection(&map_proj);
445
0
    }
446
0
  }
447
448
0
  if (!bDone)
449
0
    msProjectRect(&map->projection, &layer->projection,
450
0
                  &searchrect); /* project the searchrect to source coords */
451
452
0
  return searchrect;
453
0
}
454
455
0
int msUVRASTERLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery) {
456
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
457
0
  imageObj *image_tmp;
458
0
  outputFormatObj *outputformat = NULL;
459
0
  mapObj *map_tmp;
460
0
  double map_cellsize;
461
0
  unsigned int spacing;
462
0
  int width, height;
463
0
  char **alteredProcessing = NULL, *saved_layer_mask;
464
0
  char **savedProcessing = NULL;
465
0
  int bHasLonWrap = MS_FALSE;
466
0
  double dfLonWrap = 0.0;
467
0
  rectObj oldLayerExtent;
468
0
  char *oldLayerData = NULL;
469
0
  projectionObj oldLayerProjection;
470
0
  int ret;
471
472
0
  memset(&oldLayerExtent, 0, sizeof(oldLayerExtent));
473
0
  memset(&oldLayerProjection, 0, sizeof(oldLayerProjection));
474
475
0
  if (layer->debug)
476
0
    msDebug("Entering msUVRASTERLayerWhichShapes().\n");
477
478
0
  if (uvlinfo == NULL)
479
0
    return MS_FAILURE;
480
481
0
  if (CSLFetchNameValue(layer->processing, "BANDS") == NULL) {
482
0
    msSetError(MS_MISCERR,
483
0
               "BANDS processing option is required for UV layer. You have to "
484
0
               "specified 2 bands.",
485
0
               "msUVRASTERLayerWhichShapes()");
486
0
    return MS_FAILURE;
487
0
  }
488
489
  /*
490
  ** Allocate mapObj structure
491
  */
492
0
  map_tmp = (mapObj *)msSmallCalloc(sizeof(mapObj), 1);
493
0
  if (initMap(map_tmp) == -1) { /* initialize this map */
494
0
    msFree(map_tmp);
495
0
    return (MS_FAILURE);
496
0
  }
497
498
  /* -------------------------------------------------------------------- */
499
  /*      Determine desired spacing.  Default to 32 if not otherwise set  */
500
  /* -------------------------------------------------------------------- */
501
0
  spacing = 32;
502
0
  if (CSLFetchNameValue(layer->processing, "UV_SPACING") != NULL) {
503
0
    spacing = atoi(CSLFetchNameValue(layer->processing, "UV_SPACING"));
504
0
    if (spacing == 0)
505
0
      spacing = 32;
506
0
  }
507
508
0
  width = (int)(layer->map->width / spacing);
509
0
  height = (int)(layer->map->height / spacing);
510
511
  /* Initialize our dummy map */
512
0
  MS_INIT_COLOR(map_tmp->imagecolor, 255, 255, 255, 255);
513
0
  map_tmp->resolution = layer->map->resolution;
514
0
  map_tmp->defresolution = layer->map->defresolution;
515
516
0
  outputformat = (outputFormatObj *)msSmallCalloc(1, sizeof(outputFormatObj));
517
0
  outputformat->bands = 2;
518
0
  outputformat->name = NULL;
519
0
  outputformat->driver = NULL;
520
0
  outputformat->refcount = 0;
521
0
  outputformat->vtable = NULL;
522
0
  outputformat->device = NULL;
523
0
  outputformat->renderer = MS_RENDER_WITH_RAWDATA;
524
0
  outputformat->imagemode = MS_IMAGEMODE_FLOAT32;
525
0
  msAppendOutputFormat(map_tmp, outputformat);
526
527
0
  msCopyHashTable(&map_tmp->configoptions, &layer->map->configoptions);
528
0
  map_tmp->mappath = msStrdup(layer->map->mappath);
529
0
  map_tmp->shapepath = msStrdup(layer->map->shapepath);
530
0
  map_tmp->gt.rotation_angle = 0.0;
531
532
  /* Custom msCopyProjection() that removes lon_wrap parameter */
533
0
  {
534
0
    int i;
535
536
0
    map_tmp->projection.numargs = 0;
537
0
    map_tmp->projection.gt = layer->projection.gt;
538
539
0
    for (i = 0; i < layer->projection.numargs; i++) {
540
0
      if (strncmp(layer->projection.args[i],
541
0
                  "lon_wrap=", strlen("lon_wrap=")) == 0) {
542
0
        bHasLonWrap = MS_TRUE;
543
0
        dfLonWrap = atof(layer->projection.args[i] + strlen("lon_wrap="));
544
0
      } else {
545
0
        map_tmp->projection.args[map_tmp->projection.numargs++] =
546
0
            msStrdup(layer->projection.args[i]);
547
0
      }
548
0
    }
549
0
    if (map_tmp->projection.numargs != 0) {
550
0
      msProcessProjection(&(map_tmp->projection));
551
0
    }
552
553
0
    map_tmp->projection.wellknownprojection =
554
0
        layer->projection.wellknownprojection;
555
0
  }
556
557
  /* Very special case to improve quality for rasters referenced from lon=0 to
558
   * 360 */
559
  /* We create a temporary VRT that swiches the 2 hemispheres, and then we */
560
  /* modify the georeferencing to be in the more standard [-180, 180] range */
561
  /* and we adjust the layer->data, extent and projection accordingly */
562
0
  if (layer->tileindex == NULL && uvlinfo->mapToUseForWhichShapes &&
563
0
      bHasLonWrap && dfLonWrap == 180.0) {
564
0
    rectObj layerExtent;
565
0
    msLayerGetExtent(layer, &layerExtent);
566
0
    if (layerExtent.minx == 0 && layerExtent.maxx == 360) {
567
0
      GDALDatasetH hDS = NULL;
568
0
      char *decrypted_path;
569
570
0
      if (strncmp(layer->data, "<VRTDataset", strlen("<VRTDataset")) == 0) {
571
0
        decrypted_path = msStrdup(layer->data);
572
0
      } else {
573
0
        char szPath[MS_MAXPATHLEN];
574
0
        msTryBuildPath3(szPath, layer->map->mappath, layer->map->shapepath,
575
0
                        layer->data);
576
0
        decrypted_path = msDecryptStringTokens(layer->map, szPath);
577
0
      }
578
579
0
      if (decrypted_path) {
580
0
        char **connectionoptions;
581
0
        GDALAllRegister();
582
0
        connectionoptions =
583
0
            msGetStringListFromHashTable(&(layer->connectionoptions));
584
0
        hDS = GDALOpenEx(decrypted_path, GDAL_OF_RASTER, NULL,
585
0
                         (const char *const *)connectionoptions, NULL);
586
0
        CSLDestroy(connectionoptions);
587
0
      }
588
0
      if (hDS != NULL) {
589
0
        int iBand;
590
0
        int nXSize = GDALGetRasterXSize(hDS);
591
0
        int nYSize = GDALGetRasterYSize(hDS);
592
0
        int nBands = GDALGetRasterCount(hDS);
593
0
        int nMaxLen = 100 + nBands * (800 + 2 * strlen(decrypted_path));
594
0
        int nOffset = 0;
595
0
        char *pszInlineVRT = static_cast<char *>(msSmallMalloc(nMaxLen));
596
597
0
        snprintf(pszInlineVRT, nMaxLen,
598
0
                 "<VRTDataset rasterXSize=\"%d\" rasterYSize=\"%d\">", nXSize,
599
0
                 nYSize);
600
0
        nOffset = strlen(pszInlineVRT);
601
0
        for (iBand = 1; iBand <= nBands; iBand++) {
602
0
          const char *pszDataType = "Byte";
603
0
          switch (GDALGetRasterDataType(GDALGetRasterBand(hDS, iBand))) {
604
0
          case GDT_Byte:
605
0
            pszDataType = "Byte";
606
0
            break;
607
0
          case GDT_Int16:
608
0
            pszDataType = "Int16";
609
0
            break;
610
0
          case GDT_UInt16:
611
0
            pszDataType = "UInt16";
612
0
            break;
613
0
          case GDT_Int32:
614
0
            pszDataType = "Int32";
615
0
            break;
616
0
          case GDT_UInt32:
617
0
            pszDataType = "UInt32";
618
0
            break;
619
0
          case GDT_Float32:
620
0
            pszDataType = "Float32";
621
0
            break;
622
0
          case GDT_Float64:
623
0
            pszDataType = "Float64";
624
0
            break;
625
0
          default:
626
0
            break;
627
0
          }
628
629
0
          snprintf(pszInlineVRT + nOffset, nMaxLen - nOffset,
630
0
                   "    <VRTRasterBand dataType=\"%s\" band=\"%d\">"
631
0
                   "        <SimpleSource>"
632
0
                   "            <SourceFilename "
633
0
                   "relativeToVrt=\"1\"><![CDATA[%s]]></SourceFilename>"
634
0
                   "            <SourceBand>%d</SourceBand>"
635
0
                   "            <SrcRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" "
636
0
                   "ySize=\"%d\"/>"
637
0
                   "            <DstRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" "
638
0
                   "ySize=\"%d\"/>"
639
0
                   "        </SimpleSource>"
640
0
                   "        <SimpleSource>"
641
0
                   "            <SourceFilename "
642
0
                   "relativeToVrt=\"1\"><![CDATA[%s]]></SourceFilename>"
643
0
                   "            <SourceBand>%d</SourceBand>"
644
0
                   "            <SrcRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" "
645
0
                   "ySize=\"%d\"/>"
646
0
                   "            <DstRect xOff=\"%d\" yOff=\"%d\" xSize=\"%d\" "
647
0
                   "ySize=\"%d\"/>"
648
0
                   "        </SimpleSource>"
649
0
                   "    </VRTRasterBand>",
650
0
                   pszDataType, iBand, decrypted_path, iBand, nXSize / 2, 0,
651
0
                   nXSize - nXSize / 2, nYSize, 0, 0, nXSize - nXSize / 2,
652
0
                   nYSize, decrypted_path, iBand, 0, 0, nXSize / 2, nYSize,
653
0
                   nXSize - nXSize / 2, 0, nXSize / 2, nYSize);
654
655
0
          nOffset += strlen(pszInlineVRT + nOffset);
656
0
        }
657
0
        snprintf(pszInlineVRT + nOffset, nMaxLen - nOffset, "</VRTDataset>");
658
659
0
        oldLayerExtent = layer->extent;
660
0
        oldLayerData = layer->data;
661
0
        oldLayerProjection = layer->projection;
662
0
        layer->extent.minx = -180;
663
0
        layer->extent.maxx = 180;
664
0
        layer->data = pszInlineVRT;
665
0
        layer->projection = map_tmp->projection;
666
667
        /* map_tmp->projection is actually layer->projection without lon_wrap */
668
0
        rect = uvlinfo->mapToUseForWhichShapes->extent;
669
0
        msProjectRect(&uvlinfo->mapToUseForWhichShapes->projection,
670
0
                      &map_tmp->projection, &rect);
671
0
        bHasLonWrap = MS_FALSE;
672
673
0
        GDALClose(hDS);
674
0
      }
675
0
      msFree(decrypted_path);
676
0
    }
677
0
  }
678
679
0
  if (isQuery) {
680
    /* For query mode, use layer->map->extent reprojected rather than */
681
    /* the provided rect. Generic query code will filter returned features. */
682
0
    rect = msUVRASTERGetSearchRect(layer, layer->map);
683
0
  }
684
685
0
  map_cellsize = MS_MAX(MS_CELLSIZE(rect.minx, rect.maxx, layer->map->width),
686
0
                        MS_CELLSIZE(rect.miny, rect.maxy, layer->map->height));
687
0
  map_tmp->cellsize = map_cellsize * spacing;
688
0
  map_tmp->extent.minx =
689
0
      rect.minx - (0.5 * map_cellsize) + (0.5 * map_tmp->cellsize);
690
0
  map_tmp->extent.miny =
691
0
      rect.miny - (0.5 * map_cellsize) + (0.5 * map_tmp->cellsize);
692
0
  map_tmp->extent.maxx =
693
0
      map_tmp->extent.minx + ((width - 1) * map_tmp->cellsize);
694
0
  map_tmp->extent.maxy =
695
0
      map_tmp->extent.miny + ((height - 1) * map_tmp->cellsize);
696
697
0
  if (bHasLonWrap && dfLonWrap == 180.0) {
698
0
    if (map_tmp->extent.minx >= 180) {
699
      /* Request on the right half of the shifted raster (= western hemisphere)
700
       */
701
0
      map_tmp->extent.minx -= 360;
702
0
      map_tmp->extent.maxx -= 360;
703
0
    } else if (map_tmp->extent.maxx >= 180.0) {
704
      /* Request spanning on the 2 hemispheres => drawing whole planet */
705
      /* Take only into account vertical resolution, as horizontal one */
706
      /* will be unreliable (assuming square pixels...) */
707
0
      map_cellsize = MS_CELLSIZE(rect.miny, rect.maxy, layer->map->height);
708
0
      map_tmp->cellsize = map_cellsize * spacing;
709
710
0
      width = 360.0 / map_tmp->cellsize;
711
0
      map_tmp->extent.minx = -180.0 + (0.5 * map_tmp->cellsize);
712
0
      map_tmp->extent.maxx = 180.0 - (0.5 * map_tmp->cellsize);
713
0
    }
714
0
  }
715
716
0
  if (layer->debug)
717
0
    msDebug(
718
0
        "msUVRASTERLayerWhichShapes(): width: %d, height: %d, cellsize: %g\n",
719
0
        width, height, map_tmp->cellsize);
720
721
0
  if (layer->debug == MS_DEBUGLEVEL_VVV)
722
0
    msDebug("msUVRASTERLayerWhichShapes(): extent: %g %g %g %g\n",
723
0
            map_tmp->extent.minx, map_tmp->extent.miny, map_tmp->extent.maxx,
724
0
            map_tmp->extent.maxy);
725
726
  /* important to use that function, to compute map
727
     geotransform, used by the resampling*/
728
0
  msMapSetSize(map_tmp, width, height);
729
730
0
  if (layer->debug == MS_DEBUGLEVEL_VVV)
731
0
    msDebug("msUVRASTERLayerWhichShapes(): geotransform: %g %g %g %g %g %g\n",
732
0
            map_tmp->gt.geotransform[0], map_tmp->gt.geotransform[1],
733
0
            map_tmp->gt.geotransform[2], map_tmp->gt.geotransform[3],
734
0
            map_tmp->gt.geotransform[4], map_tmp->gt.geotransform[5]);
735
736
0
  uvlinfo->extent = map_tmp->extent;
737
738
0
  image_tmp = msImageCreate(width, height, map_tmp->outputformatlist[0], NULL,
739
0
                            NULL, map_tmp->resolution, map_tmp->defresolution,
740
0
                            &(map_tmp->imagecolor));
741
742
  /* Default set to AVERAGE resampling */
743
0
  if (CSLFetchNameValue(layer->processing, "RESAMPLE") == NULL) {
744
0
    alteredProcessing = CSLDuplicate(layer->processing);
745
0
    alteredProcessing =
746
0
        CSLSetNameValue(alteredProcessing, "RESAMPLE", "AVERAGE");
747
0
    savedProcessing = layer->processing;
748
0
    layer->processing = alteredProcessing;
749
0
  }
750
751
  /* disable masking at this level: we don't want to apply the mask at the
752
   * raster level, it will be applied with the correct cellsize and image size
753
   * in the vector rendering phase.
754
   */
755
0
  saved_layer_mask = layer->mask;
756
0
  layer->mask = NULL;
757
758
0
  if (layer->tileindex) {
759
0
    expressionObj old_filter;
760
0
    if (!uvlinfo->timestring.empty()) {
761
0
      msInitExpression(&old_filter);
762
0
      msCopyExpression(&old_filter, &layer->filter); /* save existing filter */
763
0
      msFreeExpression(&layer->filter);
764
0
      msLayerMakeBackticsTimeFilter(layer, uvlinfo->timestring.c_str(),
765
0
                                    uvlinfo->timefield.c_str());
766
0
    }
767
768
0
    ret = msDrawRasterLayerLow(map_tmp, layer, image_tmp, NULL);
769
770
0
    if (!uvlinfo->timestring.empty()) {
771
0
      msCopyExpression(&layer->filter, &old_filter); /* restore old filter */
772
0
      msFreeExpression(&old_filter);
773
0
    }
774
0
  } else {
775
0
    ret = msDrawRasterLayerLow(map_tmp, layer, image_tmp, NULL);
776
0
  }
777
778
  /* restore layer attributes if we went through the above on-the-fly VRT */
779
0
  if (oldLayerData) {
780
0
    msFree(layer->data);
781
0
    layer->data = oldLayerData;
782
0
    layer->extent = oldLayerExtent;
783
0
    layer->projection = oldLayerProjection;
784
0
  }
785
786
  /* restore layer mask */
787
0
  layer->mask = saved_layer_mask;
788
789
  /* restore the saved processing */
790
0
  if (alteredProcessing != NULL) {
791
0
    layer->processing = savedProcessing;
792
0
    CSLDestroy(alteredProcessing);
793
0
  }
794
795
0
  if (ret == MS_FAILURE) {
796
0
    msSetError(MS_MISCERR, "Unable to draw raster data.",
797
0
               "msUVRASTERLayerWhichShapes()");
798
799
0
    msFreeMap(map_tmp);
800
0
    msFreeImage(image_tmp);
801
802
0
    return MS_FAILURE;
803
0
  }
804
805
  /* free old query arrays */
806
0
  free(uvlinfo->u);
807
0
  free(uvlinfo->v);
808
809
  /* Update our uv layer structure */
810
0
  uvlinfo->width = width;
811
0
  uvlinfo->height = height;
812
0
  uvlinfo->query_results = 0;
813
814
0
  uvlinfo->last_queried_shapeindex = 0;
815
0
  uvlinfo->last_raster_off = 0;
816
817
0
  uvlinfo->u = (float *)msSmallMalloc(sizeof(float *) * width * height);
818
0
  uvlinfo->v = (float *)msSmallMalloc(sizeof(float *) * width * height);
819
820
0
  reprojectionObj *reprojectorLayerToMap = nullptr;
821
0
  if (layer->project) {
822
0
    reprojectorLayerToMap =
823
0
        msProjectCreateReprojector(&layer->projection, &layer->map->projection);
824
0
  }
825
  // To reproject the wind direction, use a ~ 10 m long line. This is a value
826
  // not too small and not too big to be able to reproject a small vector.
827
0
  double typicalLength = 10.0;
828
0
  if (msProjIsGeographicCRS(&(layer->projection))) {
829
0
    const double dfDegToMeter =
830
0
        msProjGetSemiMajorAxis(&(layer->projection)) * (MS_PI / 180);
831
0
    const double dfMeterToDeg = 1.0 / dfDegToMeter;
832
0
    typicalLength *= dfMeterToDeg;
833
0
  }
834
835
0
  for (size_t off = 0; off < static_cast<size_t>(width) * height; ++off) {
836
    /* Ignore invalid pixels (at nodata), or (u,v)=(0,0) */
837
0
    if (MS_GET_BIT(image_tmp->img_mask, off)) {
838
0
      uvlinfo->u[off] = image_tmp->img.raw_float[off];
839
0
      uvlinfo->v[off] =
840
0
          image_tmp->img.raw_float[off + static_cast<size_t>(width) * height];
841
0
      if (!(uvlinfo->u[off] == 0 && uvlinfo->v[off] == 0)) {
842
0
        uvlinfo->query_results++;
843
0
        if (reprojectorLayerToMap) {
844
          // Compute coordinates of the pixel in layer projection
845
0
          const int x = static_cast<int>(off % uvlinfo->width);
846
0
          const int y = static_cast<int>(off / uvlinfo->width);
847
0
          const double xLayer =
848
0
              Pix2Georef(x, 0, uvlinfo->width - 1, uvlinfo->extent.minx,
849
0
                         uvlinfo->extent.maxx, MS_FALSE);
850
0
          const double yLayer =
851
0
              Pix2Georef(y, 0, uvlinfo->height - 1, uvlinfo->extent.miny,
852
0
                         uvlinfo->extent.maxy, MS_TRUE);
853
854
          // Creates a vector starting at point.x,point.y, of length
855
          // ~ 10 m, using the angle defined by (u,v)
856
0
          pointObj point1, point2;
857
0
          point1.x = xLayer;
858
0
          point1.y = yLayer;
859
0
          const double lengthInLayerProj =
860
0
              hypotf(uvlinfo->u[off], uvlinfo->v[off]);
861
0
          point2.x =
862
0
              xLayer + typicalLength * (uvlinfo->u[off] / lengthInLayerProj);
863
0
          point2.y =
864
0
              yLayer + typicalLength * (uvlinfo->v[off] / lengthInLayerProj);
865
866
          // Reprojects that vector to map projection
867
0
          if (msProjectPointEx(reprojectorLayerToMap, &point1) == MS_SUCCESS &&
868
0
              msProjectPointEx(reprojectorLayerToMap, &point2) == MS_SUCCESS) {
869
            // Now computes the (u,v) component in map projection,
870
            // preserving its original length
871
0
            const double dx = point2.x - point1.x;
872
0
            const double dy = point2.y - point1.y;
873
0
            const double lengthInMapProj = hypot(dx, dy);
874
0
            uvlinfo->u[off] =
875
0
                (float)(lengthInLayerProj * (dx / lengthInMapProj));
876
0
            uvlinfo->v[off] =
877
0
                (float)(lengthInLayerProj * (dy / lengthInMapProj));
878
0
          }
879
0
        }
880
0
      } else {
881
0
        uvlinfo->u[off] = std::numeric_limits<float>::quiet_NaN();
882
0
        uvlinfo->v[off] = std::numeric_limits<float>::quiet_NaN();
883
0
      }
884
0
    } else {
885
0
      uvlinfo->u[off] = std::numeric_limits<float>::quiet_NaN();
886
0
      uvlinfo->v[off] = std::numeric_limits<float>::quiet_NaN();
887
0
    }
888
0
  }
889
890
0
  msProjectDestroyReprojector(reprojectorLayerToMap);
891
892
0
  msFreeImage(image_tmp); /* we do not need the imageObj anymore */
893
0
  msFreeMap(map_tmp);
894
895
0
  uvlinfo->next_shape = 0;
896
897
0
  return MS_SUCCESS;
898
0
}
899
900
int msUVRASTERLayerGetShape(layerObj *layer, shapeObj *shape,
901
0
                            resultObj *record) {
902
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
903
0
  lineObj line;
904
0
  pointObj point;
905
0
  const long shapeindex = record->shapeindex;
906
907
0
  msFreeShape(shape);
908
0
  shape->type = MS_SHAPE_NULL;
909
910
0
  if (shapeindex < 0 || shapeindex >= uvlinfo->query_results) {
911
0
    msSetError(MS_MISCERR,
912
0
               "Out of range shape index requested.  Requested %ld\n"
913
0
               "but only %d shapes available.",
914
0
               "msUVRASTERLayerGetShape()", shapeindex, uvlinfo->query_results);
915
0
    return MS_FAILURE;
916
0
  }
917
918
  /* loop to the next valid value */
919
0
  size_t raster_off = (shapeindex >= uvlinfo->last_queried_shapeindex)
920
0
                          ? uvlinfo->last_raster_off
921
0
                          : 0;
922
0
  for (long curshapeindex = (shapeindex >= uvlinfo->last_queried_shapeindex)
923
0
                                ? uvlinfo->last_queried_shapeindex
924
0
                                : 0;
925
0
       raster_off < static_cast<size_t>(uvlinfo->width) * uvlinfo->height;
926
0
       ++raster_off) {
927
0
    if (!std::isnan(uvlinfo->u[raster_off])) {
928
0
      if (curshapeindex == shapeindex) {
929
0
        uvlinfo->last_queried_shapeindex = shapeindex;
930
0
        uvlinfo->last_raster_off = raster_off;
931
0
        break;
932
0
      }
933
0
      ++curshapeindex;
934
0
    }
935
0
  }
936
0
  assert(raster_off < static_cast<size_t>(uvlinfo->width) * uvlinfo->height);
937
938
0
  const int x = static_cast<int>(raster_off % uvlinfo->width);
939
0
  const int y = static_cast<int>(raster_off / uvlinfo->width);
940
0
  point.x = Pix2Georef(x, 0, uvlinfo->width - 1, uvlinfo->extent.minx,
941
0
                       uvlinfo->extent.maxx, MS_FALSE);
942
0
  point.y = Pix2Georef(y, 0, uvlinfo->height - 1, uvlinfo->extent.miny,
943
0
                       uvlinfo->extent.maxy, MS_TRUE);
944
0
  if (layer->debug == MS_DEBUGLEVEL_VVV)
945
0
    msDebug("msUVRASTERLayerWhichShapes(): shapeindex: %ld, x: %g, y: %g\n",
946
0
            shapeindex, point.x, point.y);
947
948
0
  point.m = 0.0;
949
950
0
  shape->type = MS_SHAPE_POINT;
951
0
  line.numpoints = 1;
952
0
  line.point = &point;
953
0
  msAddLine(shape, &line);
954
0
  msComputeBounds(shape);
955
956
0
  shape->numvalues = layer->numitems;
957
0
  shape->values = msUVRASTERGetValues(layer, uvlinfo->u[raster_off],
958
0
                                      uvlinfo->v[raster_off], &point);
959
0
  shape->index = shapeindex;
960
0
  shape->resultindex = shapeindex;
961
962
0
  return MS_SUCCESS;
963
0
}
964
965
0
int msUVRASTERLayerNextShape(layerObj *layer, shapeObj *shape) {
966
0
  uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
967
968
0
  if (uvlinfo->next_shape < 0 ||
969
0
      uvlinfo->next_shape >= uvlinfo->query_results) {
970
0
    msFreeShape(shape);
971
0
    shape->type = MS_SHAPE_NULL;
972
0
    return MS_DONE;
973
0
  } else {
974
0
    resultObj record;
975
976
0
    record.shapeindex = uvlinfo->next_shape++;
977
0
    record.tileindex = 0;
978
0
    record.classindex = record.resultindex = -1;
979
980
0
    return msUVRASTERLayerGetShape(layer, shape, &record);
981
0
  }
982
0
}
983
984
/************************************************************************/
985
/*                       msUVRASTERLayerGetExtent()                     */
986
/* Simple copy of the maprasterquery.c file. might change in the future */
987
/************************************************************************/
988
989
int msUVRASTERLayerGetExtent(layerObj *layer, rectObj *extent)
990
991
0
{
992
0
  char szPath[MS_MAXPATHLEN];
993
0
  mapObj *map = layer->map;
994
0
  shapefileObj *tileshpfile;
995
996
0
  if ((!layer->data || strlen(layer->data) == 0) && layer->tileindex == NULL) {
997
    /* should we be issuing a specific error about not supporting
998
       extents for tileindexed raster layers? */
999
0
    return MS_FAILURE;
1000
0
  }
1001
1002
0
  if (map == NULL)
1003
0
    return MS_FAILURE;
1004
1005
  /* If the layer use a tileindex, return the extent of the tileindex
1006
   * shapefile/referenced layer */
1007
0
  if (layer->tileindex) {
1008
0
    const int tilelayerindex = msGetLayerIndex(map, layer->tileindex);
1009
0
    if (tilelayerindex != -1) /* does the tileindex reference another layer */
1010
0
      return msLayerGetExtent(GET_LAYER(map, tilelayerindex), extent);
1011
0
    else {
1012
0
      tileshpfile = (shapefileObj *)malloc(sizeof(shapefileObj));
1013
0
      MS_CHECK_ALLOC(tileshpfile, sizeof(shapefileObj), MS_FAILURE);
1014
1015
0
      if (msShapefileOpen(tileshpfile, "rb",
1016
0
                          msBuildPath3(szPath, map->mappath, map->shapepath,
1017
0
                                       layer->tileindex),
1018
0
                          MS_TRUE) == -1)
1019
0
        if (msShapefileOpen(tileshpfile, "rb",
1020
0
                            msBuildPath(szPath, map->mappath, layer->tileindex),
1021
0
                            MS_TRUE) == -1)
1022
0
          return MS_FAILURE;
1023
1024
0
      *extent = tileshpfile->bounds;
1025
0
      msShapefileClose(tileshpfile);
1026
0
      free(tileshpfile);
1027
0
      return MS_SUCCESS;
1028
0
    }
1029
0
  }
1030
1031
0
  msTryBuildPath3(szPath, map->mappath, map->shapepath, layer->data);
1032
0
  char *decrypted_path = msDecryptStringTokens(map, szPath);
1033
0
  if (!decrypted_path)
1034
0
    return MS_FAILURE;
1035
1036
0
  GDALAllRegister();
1037
1038
0
  char **connectionoptions =
1039
0
      msGetStringListFromHashTable(&(layer->connectionoptions));
1040
0
  GDALDatasetH hDS = GDALOpenEx(decrypted_path, GDAL_OF_RASTER, NULL,
1041
0
                                (const char *const *)connectionoptions, NULL);
1042
0
  CSLDestroy(connectionoptions);
1043
0
  msFree(decrypted_path);
1044
0
  if (hDS == NULL) {
1045
0
    return MS_FAILURE;
1046
0
  }
1047
1048
0
  const int nXSize = GDALGetRasterXSize(hDS);
1049
0
  const int nYSize = GDALGetRasterYSize(hDS);
1050
0
  double adfGeoTransform[6] = {0};
1051
0
  const CPLErr eErr = GDALGetGeoTransform(hDS, adfGeoTransform);
1052
0
  GDALClose(hDS);
1053
0
  if (eErr != CE_None) {
1054
0
    return MS_FAILURE;
1055
0
  }
1056
1057
  /* If this appears to be an ungeoreferenced raster than flip it for
1058
     mapservers purposes. */
1059
0
  if (adfGeoTransform[5] == 1.0 && adfGeoTransform[3] == 0.0) {
1060
0
    adfGeoTransform[5] = -1.0;
1061
0
    adfGeoTransform[3] = nYSize;
1062
0
  }
1063
1064
0
  extent->minx = adfGeoTransform[0];
1065
0
  extent->maxy = adfGeoTransform[3];
1066
1067
0
  extent->maxx = adfGeoTransform[0] + nXSize * adfGeoTransform[1];
1068
0
  extent->miny = adfGeoTransform[3] + nYSize * adfGeoTransform[5];
1069
1070
0
  return MS_SUCCESS;
1071
0
}
1072
1073
/************************************************************************/
1074
/*                     msUVRASTERLayerSetTimeFilter()                   */
1075
/*                                                                      */
1076
/*      This function is actually just used in the context of           */
1077
/*      setting a filter on the tileindex for time based queries.       */
1078
/*      For instance via WMS requests.  So it isn't really related      */
1079
/*      to the "raster query" support at all.                           */
1080
/*                                                                      */
1081
/*      If a local shapefile tileindex is in use, we will set a         */
1082
/*      backtics filter (shapefile compatible).  If another layer is    */
1083
/*      being used as the tileindex then we will forward the            */
1084
/*      SetTimeFilter call to it.  If there is no tileindex in          */
1085
/*      place, we do nothing.                                           */
1086
/************************************************************************/
1087
1088
int msUVRASTERLayerSetTimeFilter(layerObj *layer, const char *timestring,
1089
0
                                 const char *timefield) {
1090
0
  int tilelayerindex;
1091
1092
  /* -------------------------------------------------------------------- */
1093
  /*      If we don't have a tileindex the time filter has no effect.     */
1094
  /* -------------------------------------------------------------------- */
1095
0
  if (layer->tileindex == NULL)
1096
0
    return MS_SUCCESS;
1097
1098
  /* -------------------------------------------------------------------- */
1099
  /*      Find the tileindex layer.                                       */
1100
  /* -------------------------------------------------------------------- */
1101
0
  tilelayerindex = msGetLayerIndex(layer->map, layer->tileindex);
1102
1103
  /* -------------------------------------------------------------------- */
1104
  /*      If we are using a local shapefile as our tileindex (that is     */
1105
  /*      to say, the tileindex name is not of another layer), then we    */
1106
  /*      will install a backtics style filter later.                     */
1107
  /* -------------------------------------------------------------------- */
1108
0
  if (tilelayerindex == -1) {
1109
0
    if (layer->layerinfo == NULL)
1110
0
      msUVRasterLayerInfoInitialize(layer);
1111
0
    if (layer->layerinfo == NULL)
1112
0
      return MS_FAILURE;
1113
0
    uvRasterLayerInfo *uvlinfo = getLayerInfo(layer);
1114
0
    if (timestring)
1115
0
      uvlinfo->timestring = timestring;
1116
0
    if (timefield)
1117
0
      uvlinfo->timefield = timefield;
1118
0
    return MS_SUCCESS;
1119
0
  }
1120
1121
  /* -------------------------------------------------------------------- */
1122
  /*      Otherwise we invoke the tileindex layers SetTimeFilter          */
1123
  /*      method.                                                         */
1124
  /* -------------------------------------------------------------------- */
1125
0
  if (msCheckParentPointer(layer->map, "map") == MS_FAILURE)
1126
0
    return MS_FAILURE;
1127
0
  return msLayerSetTimeFilter(layer->GET_LAYER(map, tilelayerindex), timestring,
1128
0
                              timefield);
1129
0
}
1130
1131
/************************************************************************/
1132
/*                msUVRASTERLayerInitializeVirtualTable()               */
1133
/************************************************************************/
1134
1135
0
int msUVRASTERLayerInitializeVirtualTable(layerObj *layer) {
1136
0
  assert(layer != NULL);
1137
0
  assert(layer->vtable != NULL);
1138
1139
0
  layer->vtable->LayerInitItemInfo = msUVRASTERLayerInitItemInfo;
1140
0
  layer->vtable->LayerFreeItemInfo = msUVRASTERLayerFreeItemInfo;
1141
0
  layer->vtable->LayerOpen = msUVRASTERLayerOpen;
1142
0
  layer->vtable->LayerIsOpen = msUVRASTERLayerIsOpen;
1143
0
  layer->vtable->LayerWhichShapes = msUVRASTERLayerWhichShapes;
1144
0
  layer->vtable->LayerNextShape = msUVRASTERLayerNextShape;
1145
0
  layer->vtable->LayerGetShape = msUVRASTERLayerGetShape;
1146
  /* layer->vtable->LayerGetShapeCount, use default */
1147
0
  layer->vtable->LayerClose = msUVRASTERLayerClose;
1148
0
  layer->vtable->LayerGetItems = msUVRASTERLayerGetItems;
1149
0
  layer->vtable->LayerGetExtent = msUVRASTERLayerGetExtent;
1150
  /* layer->vtable->LayerGetAutoStyle, use default */
1151
  /* layer->vtable->LayerApplyFilterToLayer, use default */
1152
  /* layer->vtable->LayerCloseConnection = msUVRASTERLayerClose; */
1153
  /* we use backtics for proper tileindex shapefile functioning */
1154
0
  layer->vtable->LayerSetTimeFilter = msUVRASTERLayerSetTimeFilter;
1155
  /* layer->vtable->LayerCreateItems, use default */
1156
  /* layer->vtable->LayerGetNumFeatures, use default */
1157
1158
0
  return MS_SUCCESS;
1159
0
}