forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaptile.c
525 lines (437 loc) · 16.8 KB
/
maptile.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: MapServer Tile Access API
* Author: Paul Ramsey <pramsey@cleverelephant.ca>
*
******************************************************************************
* Copyright (c) 2008, Paul Ramsey
*
* 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 "maptile.h"
#include "mapproject.h"
#ifdef USE_TILE_API
static void msTileResetMetatileLevel(mapObj *map)
{
hashTableObj *meta = &(map->web.metadata);
const char *zero = "0";
/* Is the tile_metatile_levetl set... */
if(msLookupHashTable(meta, "tile_metatile_level") != NULL) {
msRemoveHashTable(meta, "tile_metatile_level");
}
msInsertHashTable(meta, "tile_metatile_level", zero);
}
#endif
/************************************************************************
* msTileGetGMapCoords *
************************************************************************/
static int msTileGetGMapCoords(const char *coordstring, int *x, int *y, int *zoom)
{
int num_coords = 0;
char **coords = NULL;
if( coordstring ) {
coords = msStringSplit(coordstring, ' ', &(num_coords));
if( num_coords != 3 ) {
msFreeCharArray(coords, num_coords);
msSetError(MS_WEBERR, "Invalid number of tile coordinates (should be three).", "msTileSetup()");
return MS_FAILURE;
}
} else {
msSetError(MS_WEBERR, "Tile parameter not set.", "msTileSetup()");
return MS_FAILURE;
}
if( x )
*x = strtol(coords[0], NULL, 10);
if( y )
*y = strtol(coords[1], NULL, 10);
if( zoom )
*zoom = strtol(coords[2], NULL, 10);
msFreeCharArray(coords, 3);
return MS_SUCCESS;
}
/************************************************************************
* msTileSetParams *
************************************************************************/
static void msTileGetParams(mapObj *map, tileParams *params)
{
const char *value = NULL;
hashTableObj *meta = &(map->web.metadata);
params->tile_size = SPHEREMERC_IMAGE_SIZE;
/* Check for tile buffer, set to buffer==0 as default */
if((value = msLookupHashTable(meta, "tile_map_edge_buffer")) != NULL) {
params->map_edge_buffer = atoi(value);
if(map->debug)
msDebug("msTileSetParams(): tile_map_edge_buffer = %d\n", params->map_edge_buffer);
} else
params->map_edge_buffer = 0;
/* Check for metatile size, set to tile==metatile as default */
if((value = msLookupHashTable(meta, "tile_metatile_level")) != NULL) {
params->metatile_level = atoi(value);
/* Quietly force metatile_level to be sane */
if( params->metatile_level < 0 )
params->metatile_level = 0;
if( params->metatile_level > 2 )
params->metatile_level = 2;
if(map->debug)
msDebug("msTileSetParams(): tile_metatile_level = %d\n", params->metatile_level);
} else
params->metatile_level = 0;
}
/************************************************************************
* msTileExtractSubTile *
* *
************************************************************************/
static imageObj* msTileExtractSubTile(const mapservObj *msObj, const imageObj *img)
{
int width, mini, minj;
int zoom = 2;
imageObj* imgOut = NULL;
tileParams params;
rendererVTableObj *renderer;
rasterBufferObj imgBuffer;
if( !MS_RENDERER_PLUGIN(msObj->map->outputformat)
|| msObj->map->outputformat->renderer != img->format->renderer ||
! MS_MAP_RENDERER(msObj->map)->supports_pixel_buffer ) {
msSetError(MS_MISCERR,"unsupported or mixed renderers","msTileExtractSubTile()");
return NULL;
}
renderer = MS_MAP_RENDERER(msObj->map);
if (renderer->getRasterBufferHandle((imageObj*)img,&imgBuffer) != MS_SUCCESS) {
return NULL;
}
/*
** Load the metatiling information from the map file.
*/
msTileGetParams(msObj->map, ¶ms);
/*
** Initialize values for the metatile clip area.
*/
width = img->width - 2*params.map_edge_buffer;
mini = params.map_edge_buffer;
minj = params.map_edge_buffer;
if( msObj->TileMode == TILE_GMAP ) {
int x, y, zoom;
if( msObj->TileCoords ) {
if( msTileGetGMapCoords(msObj->TileCoords, &x, &y, &zoom) == MS_FAILURE )
return NULL;
} else {
msSetError(MS_WEBERR, "Tile parameter not set.", "msTileSetup()");
return NULL;
}
if(msObj->map->debug)
msDebug("msTileExtractSubTile(): gmaps coords (x: %d, y: %d)\n",x,y);
/*
** The bottom N bits of the coordinates give us the subtile
** location relative to the metatile.
*/
x = (0xffff ^ (0xffff << params.metatile_level)) & x;
y = (0xffff ^ (0xffff << params.metatile_level)) & y;
if(msObj->map->debug)
msDebug("msTileExtractSubTile(): gmaps image coords (x: %d, y: %d)\n",x,y);
mini = mini + x * params.tile_size;
minj = minj + y * params.tile_size;
} else if( msObj->TileMode == TILE_VE ) {
int tsize;
const int lenTileCoords = (int)strlen( msObj->TileCoords );
if( lenTileCoords - params.metatile_level < 0 ) {
return(NULL);
}
/*
** Process the last elements of the VE coordinate string to place the
** requested tile in the context of the metatile
*/
for( int i = lenTileCoords - params.metatile_level;
i < lenTileCoords;
i++ ) {
char j = msObj->TileCoords[i];
tsize = width / zoom;
if( j == '1' || j == '3' ) mini += tsize;
if( j == '2' || j == '3' ) minj += tsize;
zoom *= 2;
}
} else {
return(NULL); /* Huh? Should have a mode. */
}
imgOut = msImageCreate(params.tile_size, params.tile_size, msObj->map->outputformat, NULL, NULL, msObj->map->resolution, msObj->map->defresolution, NULL);
if( imgOut == NULL ) {
return NULL;
}
if(msObj->map->debug)
msDebug("msTileExtractSubTile(): extracting (%d x %d) tile, top corner (%d, %d)\n",params.tile_size,params.tile_size,mini,minj);
if(MS_UNLIKELY(MS_FAILURE == renderer->mergeRasterBuffer(imgOut,&imgBuffer,1.0,mini, minj,0, 0,params.tile_size, params.tile_size))) {
msFreeImage(imgOut);
return NULL;
}
return imgOut;
}
/************************************************************************
* msTileSetup *
* *
* Called from mapserv.c, this is where the fun begins *
* Set up projections and test the parameters for legality. *
************************************************************************/
int msTileSetup(mapservObj* msObj)
{
#ifdef USE_TILE_API
char *outProjStr = NULL;
tileParams params;
/*
** Load the metatiling information from the map file.
*/
msTileGetParams(msObj->map, ¶ms);
/*
** Ensure all the LAYERs have a projection.
*/
if( msMapSetLayerProjections(msObj->map) != 0 ) {
return(MS_FAILURE);
}
/*
** Set the projection string for this mode.
*/
if( msObj->TileMode == TILE_GMAP || msObj->TileMode == TILE_VE ) {
outProjStr = SPHEREMERC_PROJ4;
} else {
return MS_FAILURE; /* Huh? No mode? */
}
if( msLoadProjectionString(&(msObj->map->projection), outProjStr) != 0 ) {
msSetError(MS_CGIERR, "Unable to load projection string.", "msTileSetup()");
return MS_FAILURE;
}
/*
** Set up the output extents for this tilemode and tile coordinates
*/
if( msObj->TileMode == TILE_GMAP ) {
int x, y, zoom;
double zoomfactor;
if( msObj->TileCoords ) {
if( msTileGetGMapCoords(msObj->TileCoords, &x, &y, &zoom) == MS_FAILURE )
return MS_FAILURE;
} else {
msSetError(MS_WEBERR, "Tile parameter not set.", "msTileSetup()");
return MS_FAILURE;
}
if( params.metatile_level >= zoom ) {
msTileResetMetatileLevel(msObj->map);
}
zoomfactor = pow(2.0, (double)zoom);
/*
** Check the input request for sanity.
*/
if( x >= zoomfactor || y >= zoomfactor ) {
msSetError(MS_CGIERR, "GMap tile coordinates are too large for supplied zoom.", "msTileSetup()");
return(MS_FAILURE);
}
if( x < 0 || y < 0 ) {
msSetError(MS_CGIERR, "GMap tile coordinates should not be less than zero.", "msTileSetup()");
return(MS_FAILURE);
}
} else if ( msObj->TileMode == TILE_VE ) {
if( strspn( msObj->TileCoords, "0123" ) < strlen( msObj->TileCoords ) ) {
msSetError(MS_CGIERR, "VE tile name should only include characters 0, 1, 2 and 3.", "msTileSetup()");
return(MS_FAILURE);
}
if( params.metatile_level >= (int)strlen(msObj->TileCoords) ) {
msTileResetMetatileLevel(msObj->map);
}
} else {
return(MS_FAILURE); /* Huh? Should have a mode. */
}
return MS_SUCCESS;
#else
msSetError(MS_CGIERR, "Tile API is not available.", "msTileSetup()");
return(MS_FAILURE);
#endif
}
/************************************************************************
* msTileSetExtent *
* *
* Based on the input parameters, set the output extent for this *
* tile. *
************************************************************************/
int msTileSetExtent(mapservObj* msObj)
{
#ifdef USE_TILE_API
mapObj *map = msObj->map;
double dx, dy, buffer;
tileParams params;
/* Read the tile-mode map file parameters */
msTileGetParams(msObj->map, ¶ms);
if( msObj->TileMode == TILE_GMAP ) {
int x, y, zoom;
double zoomfactor, tilesize, xmin, xmax, ymin, ymax;
if( msObj->TileCoords ) {
if( msTileGetGMapCoords(msObj->TileCoords, &x, &y, &zoom) == MS_FAILURE )
return MS_FAILURE;
} else {
msSetError(MS_WEBERR, "Tile parameter not set.", "msTileSetup()");
return MS_FAILURE;
}
if(map->debug)
msDebug("msTileSetExtent(): gmaps coords (x: %d, y: %d, z: %d)\n",x,y,zoom);
/*
** If we are metatiling, adjust the zoom level appropriately,
** then scale back the x/y coordinates to match the new level.
*/
if( params.metatile_level > 0 ) {
zoom = zoom - params.metatile_level;
x = x >> params.metatile_level;
y = y >> params.metatile_level;
}
if(map->debug)
msDebug("msTileSetExtent(): gmaps metacoords (x: %d, y: %d, z: %d)\n",x,y,zoom);
zoomfactor = pow(2.0, (double)zoom);
/*
** Calculate the ground extents of the tile request.
*/
/* printf("X: %i Y: %i Z: %i\n",x,y,zoom); */
tilesize = SPHEREMERC_GROUND_SIZE / zoomfactor;
xmin = (x * tilesize) - (SPHEREMERC_GROUND_SIZE / 2.0);
xmax = ((x + 1) * tilesize) - (SPHEREMERC_GROUND_SIZE / 2.0);
ymin = (SPHEREMERC_GROUND_SIZE / 2.0) - ((y + 1) * tilesize);
ymax = (SPHEREMERC_GROUND_SIZE / 2.0) - (y * tilesize);
map->extent.minx = xmin;
map->extent.maxx = xmax;
map->extent.miny = ymin;
map->extent.maxy = ymax;
} else if( msObj->TileMode == TILE_VE ) {
double minx = SPHEREMERC_GROUND_SIZE / -2.0;
double miny = SPHEREMERC_GROUND_SIZE / -2.0;
double maxx = SPHEREMERC_GROUND_SIZE / 2.0;
double maxy = SPHEREMERC_GROUND_SIZE / 2.0;
double zoom = 2.0;
double tsize;
/*
** Walk down the VE URL string, adjusting the extent each time.
** For meta-tiling cases, we stop early, to draw a larger image.
*/
for( int i = 0; i < (int)strlen( msObj->TileCoords ) - params.metatile_level; i++ ) {
char j = msObj->TileCoords[i];
tsize = SPHEREMERC_GROUND_SIZE / zoom;
if( j == '1' || j == '3' ) minx += tsize;
if( j == '0' || j == '2' ) maxx -= tsize;
if( j == '2' || j == '3' ) maxy -= tsize;
if( j == '0' || j == '1' ) miny += tsize;
zoom *= 2.0;
}
map->extent.minx = minx;
map->extent.maxx = maxx;
map->extent.miny = miny;
map->extent.maxy = maxy;
} else {
return(MS_FAILURE); /* Huh? Should have a mode. */
}
/*
** Set the output tile size.
*/
msObj->ImgCols = SPHEREMERC_IMAGE_SIZE << params.metatile_level;
msObj->ImgRows = SPHEREMERC_IMAGE_SIZE << params.metatile_level;
map->width = SPHEREMERC_IMAGE_SIZE << params.metatile_level;
map->height = SPHEREMERC_IMAGE_SIZE << params.metatile_level;
if(map->debug)
msDebug("msTileSetExtent(): base image size (%d x %d)\n",map->width,map->height);
/*
** Add the gutters
** First calculate ground units in the buffer at current extent
*/
buffer = params.map_edge_buffer * (map->extent.maxx - map->extent.minx) / (double)map->width;
/*
** Then adjust the map extents out by that amount
*/
map->extent.minx -= buffer;
map->extent.maxx += buffer;
map->extent.miny -= buffer;
map->extent.maxy += buffer;
/*
** Finally adjust the map image size by the pixel buffer
*/
map->width += 2 * params.map_edge_buffer;
map->height += 2 * params.map_edge_buffer;
msObj->ImgCols += 2 * params.map_edge_buffer;
msObj->ImgRows += 2 * params.map_edge_buffer;
if(map->debug)
msDebug("msTileSetExtent(): buffered image size (%d x %d)\n",map->width,map->height);
/*
** Adjust the extents inwards by 1/2 pixel so they are from
** center-of-pixel to center-of-pixel, instead of edge-to-edge.
** This is the way mapserver does it.
*/
dx = (map->extent.maxx - map->extent.minx) / map->width;
map->extent.minx += dx*0.5;
map->extent.maxx -= dx*0.5;
dy = (map->extent.maxy - map->extent.miny) / map->height;
map->extent.miny += dy*0.5;
map->extent.maxy -= dy*0.5;
/*
** Ensure the labelcache buffer is greater than the tile buffer.
*/
if( params.map_edge_buffer > 0 ) {
const char *value;
hashTableObj *meta = &(map->web.metadata);
char tilebufferstr[64];
/* Write the tile buffer to a string */
snprintf(tilebufferstr, sizeof(tilebufferstr), "-%d", params.map_edge_buffer);
/* Hm, the labelcache buffer is set... */
if((value = msLookupHashTable(meta, "labelcache_map_edge_buffer")) != NULL) {
/* If it's too small, replace with a bigger one */
if( params.map_edge_buffer > abs(atoi(value)) ) {
msRemoveHashTable(meta, "labelcache_map_edge_buffer");
msInsertHashTable(meta, "labelcache_map_edge_buffer", tilebufferstr);
}
}
/* No labelcache buffer value? Then we use the tile buffer. */
else {
msInsertHashTable(meta, "labelcache_map_edge_buffer", tilebufferstr);
}
}
if(map->debug) {
msDebug( "msTileSetExtent (%f, %f) (%f, %f)\n", map->extent.minx, map->extent.miny, map->extent.maxx, map->extent.maxy);
}
return MS_SUCCESS;
#else
msSetError(MS_CGIERR, "Tile API is not available.", "msTileSetExtent()");
return(MS_FAILURE);
#endif
}
/************************************************************************
* msDrawTile *
* *
* Draw the tile once with gutters, metatiling and buffers, then *
* clip out the final tile. *
* WARNING: Call msTileSetExtent() first or this will be a pointless *
* fucnction call. *
************************************************************************/
imageObj* msTileDraw(mapservObj *msObj)
{
imageObj *img;
tileParams params;
msTileGetParams(msObj->map, ¶ms);
img = msDrawMap(msObj->map, MS_FALSE);
if( img == NULL )
return NULL;
if( params.metatile_level > 0 || params.map_edge_buffer > 0 ) {
imageObj *tmp = msTileExtractSubTile(msObj, img);
msFreeImage(img);
if( tmp == NULL )
return NULL;
img = tmp;
}
return img;
}