forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapscale.c
607 lines (526 loc) · 21.3 KB
/
mapscale.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Scale object rendering.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "mapserver.h"
#define VMARGIN 3 /* buffer around the scalebar */
#define HMARGIN 3
#define VSPACING .8 /* spacing (% of font height) between scalebar and text */
#define VSLOP 5 /* makes things fit a bit better vertically */
/*
** Match this with with unit enumerations is mapserver.h
*/
static char *unitText[9]= {"in", "ft", "mi", "m", "km", "dd", "??", "??", "NM"}; /* MS_PIXEL and MS_PERCENTAGE not used */
double inchesPerUnit[9]= {1, 12, 63360.0, 39.3701, 39370.1, 4374754, 1, 1, 72913.3858 };
static double roundInterval(double d)
{
if(d<.001)
return(MS_NINT(d*10000)/10000.0);
if(d<.01)
return(MS_NINT(d*1000)/1000.0);
if(d<.1)
return(MS_NINT(d*100)/100.0);
if(d<1)
return(MS_NINT(d*10)/10.0);
if(d<100)
return(MS_NINT(d));
if(d<1000)
return(MS_NINT(d/10) * 10);
if(d<10000)
return(MS_NINT(d/100) * 100);
if(d<100000)
return(MS_NINT(d/1000) * 1000);
if(d<1000000)
return(MS_NINT(d/10000) * 10000);
if(d<10000000)
return(MS_NINT(d/100000) * 100000);
if(d<100000000)
return(MS_NINT(d/1000000) * 1000000);
return(-1);
}
/*
** Calculate the approximate scale based on a few parameters. Note that this assumes the scale is
** the same in the x direction as in the y direction, so run msAdjustExtent(...) first.
*/
int msCalculateScale(rectObj extent, int units, int width, int height, double resolution, double *scale)
{
double md, gd, center_y;
/* if((extent.maxx == extent.minx) || (extent.maxy == extent.miny)) */
if(!MS_VALID_EXTENT(extent)) {
msSetError(MS_MISCERR, "Invalid image extent, minx=%lf, miny=%lf, maxx=%lf, maxy=%lf.", "msCalculateScale()", extent.minx, extent.miny, extent.maxx, extent.maxy);
return(MS_FAILURE);
}
if((width <= 0) || (height <= 0)) {
msSetError(MS_MISCERR, "Invalid image width or height.", "msCalculateScale()");
return(MS_FAILURE);
}
switch (units) {
case(MS_DD):
case(MS_METERS):
case(MS_KILOMETERS):
case(MS_MILES):
case(MS_NAUTICALMILES):
case(MS_INCHES):
case(MS_FEET):
center_y = (extent.miny+extent.maxy)/2.0;
md = (width-1)/(resolution*msInchesPerUnit(units, center_y)); /* remember, we use a pixel-center to pixel-center extent, hence the width-1 */
gd = extent.maxx - extent.minx;
*scale = gd/md;
break;
default:
*scale = -1; /* this is not an error */
break;
}
return(MS_SUCCESS);
}
double msInchesPerUnit(int units, double center_lat)
{
(void)center_lat;
double lat_adj = 1.0, ipu = 1.0;
switch (units) {
case(MS_METERS):
case(MS_KILOMETERS):
case(MS_MILES):
case(MS_NAUTICALMILES):
case(MS_INCHES):
case(MS_FEET):
ipu = inchesPerUnit[units];
break;
case(MS_DD):
/* With geographical (DD) coordinates, we adjust the inchesPerUnit
* based on the latitude of the center of the view. For this we assume
* we have a perfect sphere and just use cos(lat) in our calculation.
*/
#ifdef ENABLE_VARIABLE_INCHES_PER_DEGREE
if (center_lat != 0.0) {
double cos_lat;
cos_lat = cos(MS_PI*center_lat/180.0);
lat_adj = sqrt(1+cos_lat*cos_lat)/sqrt(2.0);
}
#endif
ipu = inchesPerUnit[units]*lat_adj;
break;
default:
break;
}
return ipu;
}
#define X_STEP_SIZE 5
imageObj *msDrawScalebar(mapObj *map)
{
int status;
char label[32];
double i, msx;
int j;
int isx, sx, sy, ox, oy, state, dsx;
pointObj p;
rectObj r;
imageObj *image = NULL;
double fontWidth, fontHeight;
outputFormatObj *format = NULL;
strokeStyleObj strokeStyle = {0};
shapeObj shape;
lineObj line;
pointObj points[5];
textSymbolObj ts;
rendererVTableObj *renderer;
strokeStyle.patternlength=0;
initTextSymbol(&ts);
if((int)map->units == -1) {
msSetError(MS_MISCERR, "Map units not set.", "msDrawScalebar()");
return(NULL);
}
renderer = MS_MAP_RENDERER(map);
if(!renderer || !MS_MAP_RENDERER(map)->supports_pixel_buffer) {
msSetError(MS_MISCERR, "Outputformat not supported for scalebar", "msDrawScalebar()");
return(NULL);
}
msPopulateTextSymbolForLabelAndString(&ts,&map->scalebar.label,msStrdup("0123456789"),1.0,map->resolution/map->defresolution, 0);
/*
* A string containing the ten decimal digits is rendered to compute an average cell size
* for each number, which is used later to place labels on the scalebar.
*/
if(msGetTextSymbolSize(map,&ts,&r) != MS_SUCCESS) {
return NULL;
}
fontWidth = (r.maxx-r.minx)/10.0;
fontHeight = r.maxy -r.miny;
map->cellsize = msAdjustExtent(&(map->extent), map->width, map->height);
status = msCalculateScale(map->extent, map->units, map->width, map->height, map->resolution, &map->scaledenom);
if(status != MS_SUCCESS) {
return(NULL);
}
dsx = map->scalebar.width - 2*HMARGIN;
do {
msx = (map->cellsize * dsx)/(msInchesPerUnit(map->scalebar.units,0)/msInchesPerUnit(map->units,0));
i = roundInterval(msx/map->scalebar.intervals);
snprintf(label, sizeof(label), "%g", map->scalebar.intervals*i); /* last label */
isx = MS_NINT((i/(msInchesPerUnit(map->units,0)/msInchesPerUnit(map->scalebar.units,0)))/map->cellsize);
sx = (map->scalebar.intervals*isx) + MS_NINT((1.5 + strlen(label)/2.0 + strlen(unitText[map->scalebar.units]))*fontWidth);
if(sx <= (map->scalebar.width - 2*HMARGIN)) break; /* it will fit */
dsx -= X_STEP_SIZE; /* change the desired size in hopes that it will fit in user supplied width */
} while(1);
sy = (2*VMARGIN) + MS_NINT(VSPACING*fontHeight) + fontHeight + map->scalebar.height - VSLOP;
/*Ensure we have an image format representing the options for the scalebar.*/
msApplyOutputFormat( &format, map->outputformat,
map->scalebar.transparent,
map->scalebar.interlace,
MS_NOOVERRIDE );
if(map->scalebar.transparent == MS_OFF) {
if(!MS_VALID_COLOR(map->scalebar.imagecolor))
MS_INIT_COLOR(map->scalebar.imagecolor,255,255,255,255);
}
image = msImageCreate(map->scalebar.width, sy, format,
map->web.imagepath, map->web.imageurl, map->resolution, map->defresolution, &map->scalebar.imagecolor);
/* did we succeed in creating the image? */
if(!image) {
msSetError(MS_MISCERR, "Unable to initialize image.", "msDrawScalebar()");
return NULL;
}
image->map = map;
/* drop this reference to output format */
msApplyOutputFormat( &format, NULL,
MS_NOOVERRIDE, MS_NOOVERRIDE, MS_NOOVERRIDE );
switch(map->scalebar.align) {
case(MS_ALIGN_LEFT):
ox = HMARGIN;
break;
case(MS_ALIGN_RIGHT):
ox = MS_NINT((map->scalebar.width - sx) + fontWidth);
break;
default:
ox = MS_NINT((map->scalebar.width - sx)/2.0 + fontWidth/2.0); /* center the computed scalebar */
}
oy = VMARGIN;
switch(map->scalebar.style) {
case(0): {
line.numpoints = 5;
line.point = points;
shape.line = &line;
shape.numlines = 1;
if(MS_VALID_COLOR(map->scalebar.color)) {
INIT_STROKE_STYLE(strokeStyle);
strokeStyle.color = &map->scalebar.outlinecolor;
strokeStyle.color->alpha = 255;
strokeStyle.width = 1;
}
map->scalebar.backgroundcolor.alpha = 255;
map->scalebar.color.alpha = 255;
state = 1; /* 1 means filled */
for(j=0; j<map->scalebar.intervals; j++) {
points[0].x = points[4].x = points[3].x = ox + j*isx + 0.5;
points[0].y = points[4].y = points[1].y = oy + 0.5;
points[1].x = points[2].x = ox + (j+1)*isx + 0.5;
points[2].y = points[3].y = oy + map->scalebar.height + 0.5;
if(state == 1 && MS_VALID_COLOR(map->scalebar.color))
status = renderer->renderPolygon(image,&shape,&map->scalebar.color);
else if(MS_VALID_COLOR(map->scalebar.backgroundcolor))
status = renderer->renderPolygon(image,&shape,&map->scalebar.backgroundcolor);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto scale_cleanup;
}
if(strokeStyle.color) {
status = renderer->renderLine(image,&shape,&strokeStyle);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto scale_cleanup;
}
}
sprintf(label, "%g", j*i);
map->scalebar.label.position = MS_CC;
p.x = ox + j*isx; /* + MS_NINT(fontPtr->w/2); */
p.y = oy + map->scalebar.height + MS_NINT(VSPACING*fontHeight);
status = msDrawLabel(map,image,p,msStrdup(label),&map->scalebar.label,1.0);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto scale_cleanup;
}
state = -state;
}
sprintf(label, "%g", j*i);
ox = ox + j*isx - MS_NINT((strlen(label)*fontWidth)/2.0);
sprintf(label, "%g %s", j*i, unitText[map->scalebar.units]);
map->scalebar.label.position = MS_CR;
p.x = ox; /* + MS_NINT(fontPtr->w/2); */
p.y = oy + map->scalebar.height + MS_NINT(VSPACING*fontHeight);
status = msDrawLabel(map,image,p,msStrdup(label),&map->scalebar.label,1.0);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto scale_cleanup;
}
break;
}
case(1): {
line.numpoints = 2;
line.point = points;
shape.line = &line;
shape.numlines = 1;
if(MS_VALID_COLOR(map->scalebar.color)) {
strokeStyle.width = 1;
strokeStyle.color = &map->scalebar.color;
}
points[0].y = points[1].y = oy;
points[0].x = ox;
points[1].x = ox + isx*map->scalebar.intervals;
status = renderer->renderLine(image,&shape,&strokeStyle);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto scale_cleanup;
}
points[0].y = oy;
points[1].y = oy + map->scalebar.height;
p.y = oy + map->scalebar.height + MS_NINT(VSPACING*fontHeight);
for(j=0; j<=map->scalebar.intervals; j++) {
points[0].x = points[1].x = ox + j*isx;
status = renderer->renderLine(image,&shape,&strokeStyle);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto scale_cleanup;
}
sprintf(label, "%g", j*i);
if(j!=map->scalebar.intervals) {
map->scalebar.label.position = MS_CC;
p.x = ox + j*isx; /* + MS_NINT(fontPtr->w/2); */
} else {
sprintf(label, "%g %s", j*i, unitText[map->scalebar.units]);
map->scalebar.label.position = MS_CR;
p.x = ox + j*isx - MS_NINT((strlen(label)*fontWidth)/2.0);
}
status = msDrawLabel(map,image,p,msStrdup(label),&map->scalebar.label,1.0);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto scale_cleanup;
}
}
break;
}
default:
msSetError(MS_MISCERR, "Unsupported scalebar style.", "msDrawScalebar()");
return(NULL);
}
scale_cleanup:
freeTextSymbol(&ts);
if(MS_UNLIKELY(status == MS_FAILURE)) {
msFreeImage(image);
return NULL;
}
return(image);
}
int msEmbedScalebar(mapObj *map, imageObj *img)
{
int l,index,s,status = MS_SUCCESS;
pointObj point;
imageObj *image = NULL;
rendererVTableObj *renderer;
symbolObj *embededSymbol;
char* imageType = NULL;
index = msGetSymbolIndex(&(map->symbolset), "scalebar", MS_FALSE);
if(index != -1)
msRemoveSymbol(&(map->symbolset), index); /* remove cached symbol in case the function is called multiple
times with different zoom levels */
if((embededSymbol=msGrowSymbolSet(&map->symbolset)) == NULL)
return MS_FAILURE;
s = map->symbolset.numsymbols;
map->symbolset.numsymbols++;
if(!MS_RENDERER_PLUGIN(map->outputformat) || !MS_MAP_RENDERER(map)->supports_pixel_buffer) {
imageType = msStrdup(map->imagetype); /* save format */
if MS_DRIVER_CAIRO(map->outputformat)
map->outputformat = msSelectOutputFormat( map, "cairopng" );
else
map->outputformat = msSelectOutputFormat( map, "png" );
msInitializeRendererVTable(map->outputformat);
}
renderer = MS_MAP_RENDERER(map);
image = msDrawScalebar(map);
if (imageType) {
map->outputformat = msSelectOutputFormat( map, imageType ); /* restore format */
msFree(imageType);
}
if(!image) {
return MS_FAILURE;
}
embededSymbol->pixmap_buffer = calloc(1,sizeof(rasterBufferObj));
MS_CHECK_ALLOC(embededSymbol->pixmap_buffer, sizeof(rasterBufferObj), MS_FAILURE);
if(MS_SUCCESS != renderer->getRasterBufferCopy(image,embededSymbol->pixmap_buffer)) {
return MS_FAILURE;
}
embededSymbol->type = MS_SYMBOL_PIXMAP; /* intialize a few things */
embededSymbol->name = msStrdup("scalebar");
embededSymbol->sizex = embededSymbol->pixmap_buffer->width;
embededSymbol->sizey = embededSymbol->pixmap_buffer->height;
if(map->scalebar.transparent) {
embededSymbol->transparent = MS_TRUE;
embededSymbol->transparentcolor = 0;
}
switch(map->scalebar.position) {
case(MS_LL):
point.x = MS_NINT(embededSymbol->pixmap_buffer->width/2.0) + map->scalebar.offsetx;
point.y = map->height - MS_NINT(embededSymbol->pixmap_buffer->height/2.0) - map->scalebar.offsety;
break;
case(MS_LR):
point.x = map->width - MS_NINT(embededSymbol->pixmap_buffer->width/2.0) - map->scalebar.offsetx;
point.y = map->height - MS_NINT(embededSymbol->pixmap_buffer->height/2.0) - map->scalebar.offsety;
break;
case(MS_LC):
point.x = MS_NINT(map->width/2.0) + map->scalebar.offsetx;
point.y = map->height - MS_NINT(embededSymbol->pixmap_buffer->height/2.0) - map->scalebar.offsety;
break;
case(MS_UR):
point.x = map->width - MS_NINT(embededSymbol->pixmap_buffer->width/2.0) - map->scalebar.offsetx;
point.y = MS_NINT(embededSymbol->pixmap_buffer->height/2.0) + map->scalebar.offsety;
break;
case(MS_UL):
point.x = MS_NINT(embededSymbol->pixmap_buffer->width/2.0) + map->scalebar.offsetx;
point.y = MS_NINT(embededSymbol->pixmap_buffer->height/2.0) + map->scalebar.offsety;
break;
case(MS_UC):
point.x = MS_NINT(map->width/2.0) + map->scalebar.offsetx;
point.y = MS_NINT(embededSymbol->pixmap_buffer->height/2.0) + map->scalebar.offsety;
break;
}
l = msGetLayerIndex(map, "__embed__scalebar");
if(l == -1) {
if (msGrowMapLayers(map) == NULL)
return(-1);
l = map->numlayers;
map->numlayers++;
if(initLayer((GET_LAYER(map, l)), map) == -1) return(-1);
GET_LAYER(map, l)->name = msStrdup("__embed__scalebar");
GET_LAYER(map, l)->type = MS_LAYER_POINT;
if (msGrowLayerClasses( GET_LAYER(map, l) ) == NULL)
return(-1);
if(initClass(GET_LAYER(map, l)->class[0]) == -1) return(-1);
GET_LAYER(map, l)->numclasses = 1; /* so we make sure to free it */
/* update the layer order list with the layer's index. */
map->layerorder[l] = l;
}
GET_LAYER(map, l)->status = MS_ON;
if(map->scalebar.postlabelcache) { /* add it directly to the image */
if(msMaybeAllocateClassStyle(GET_LAYER(map, l)->class[0], 0)==MS_FAILURE) return MS_FAILURE;
GET_LAYER(map, l)->class[0]->styles[0]->symbol = s;
status = msDrawMarkerSymbol(map, img, &point, GET_LAYER(map, l)->class[0]->styles[0], 1.0);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto embed_cleanup;
}
} else {
if(!GET_LAYER(map, l)->class[0]->labels) {
if(msGrowClassLabels(GET_LAYER(map, l)->class[0]) == NULL) return MS_FAILURE;
initLabel(GET_LAYER(map, l)->class[0]->labels[0]);
GET_LAYER(map, l)->class[0]->numlabels = 1;
GET_LAYER(map, l)->class[0]->labels[0]->force = MS_TRUE;
GET_LAYER(map, l)->class[0]->labels[0]->size = MS_MEDIUM; /* must set a size to have a valid label definition */
GET_LAYER(map, l)->class[0]->labels[0]->priority = MS_MAX_LABEL_PRIORITY;
}
if(GET_LAYER(map, l)->class[0]->labels[0]->numstyles == 0) {
if(msGrowLabelStyles(GET_LAYER(map,l)->class[0]->labels[0]) == NULL)
return(MS_FAILURE);
GET_LAYER(map,l)->class[0]->labels[0]->numstyles = 1;
initStyle(GET_LAYER(map,l)->class[0]->labels[0]->styles[0]);
GET_LAYER(map,l)->class[0]->labels[0]->styles[0]->_geomtransform.type = MS_GEOMTRANSFORM_LABELPOINT;
}
GET_LAYER(map,l)->class[0]->labels[0]->styles[0]->symbol = s;
status = msAddLabel(map, img, GET_LAYER(map, l)->class[0]->labels[0], l, 0, NULL, &point, -1, NULL);
if(MS_UNLIKELY(status == MS_FAILURE)) {
goto embed_cleanup;
}
}
embed_cleanup:
/* Mark layer as deleted so that it doesn't interfere with html legends or with saving maps */
GET_LAYER(map, l)->status = MS_DELETE;
msFreeImage( image );
return status;
}
/************************************************************************/
/* These two functions are used in PHP/Mapscript and Swig/Mapscript */
/************************************************************************/
/************************************************************************/
/* double GetDeltaExtentsUsingScale(double scale, int units, */
/* double centerLat, int width, */
/* double resolution) */
/* */
/* Utility function to return the maximum extent using the */
/* scale and the width of the image. */
/* */
/* Base on the function msCalculateScale (mapscale.c) */
/************************************************************************/
double GetDeltaExtentsUsingScale(double scale, int units, double centerLat, int width, double resolution)
{
double md = 0.0;
double dfDelta = -1.0;
if (scale <= 0 || width <=0)
return -1;
switch (units) {
case(MS_DD):
case(MS_METERS):
case(MS_KILOMETERS):
case(MS_MILES):
case(MS_NAUTICALMILES):
case(MS_INCHES):
case(MS_FEET):
/* remember, we use a pixel-center to pixel-center extent, hence the width-1 */
md = (width-1)/(resolution*msInchesPerUnit(units,centerLat));
dfDelta = md * scale;
break;
default:
break;
}
return dfDelta;
}
/************************************************************************/
/* static double Pix2Georef(int nPixPos, int nPixMin, double nPixMax,*/
/* double dfGeoMin, double dfGeoMax, */
/* bool bULisYOrig) */
/* */
/* Utility function to convert a pixel pos to georef pos. If */
/* bULisYOrig parameter is set to true then the upper left is */
/* considered to be the Y origin. */
/* */
/************************************************************************/
double Pix2Georef(int nPixPos, int nPixMin, int nPixMax,
double dfGeoMin, double dfGeoMax, int bULisYOrig)
{
double dfPosGeo = 0.0;
const double dfWidthGeo = dfGeoMax - dfGeoMin;
const int nWidthPix = nPixMax - nPixMin;
if (dfWidthGeo > 0.0 && nWidthPix > 0) {
const double dfPixToGeo = dfWidthGeo / (double)nWidthPix;
int nDeltaPix;
if (!bULisYOrig)
nDeltaPix = nPixPos - nPixMin;
else
nDeltaPix = nPixMax - nPixPos;
const double dfDeltaGeo = nDeltaPix * dfPixToGeo;
dfPosGeo = dfGeoMin + dfDeltaGeo;
}
return (dfPosGeo);
}
/* This function converts a pixel value in geo ref. The return value is in
* layer units. This function has been added for the purpose of the ticket #1340 */
double Pix2LayerGeoref(mapObj *map, layerObj *layer, int value)
{
double cellsize = MS_MAX(MS_CELLSIZE(map->extent.minx, map->extent.maxx, map->width),
MS_CELLSIZE(map->extent.miny, map->extent.maxy, map->height));
double resolutionFactor = map->resolution/map->defresolution;
double unitsFactor = 1;
if (layer->sizeunits != MS_PIXELS)
unitsFactor = msInchesPerUnit(map->units,0)/msInchesPerUnit(layer->sizeunits,0);
return value*cellsize*resolutionFactor*unitsFactor;
}