forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapwmslayer.c
executable file
·1653 lines (1422 loc) · 59.6 KB
/
mapwmslayer.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$
*
* Project: MapServer
* Purpose: Implementation of WMS CONNECTIONTYPE - client to WMS servers
* Author: Daniel Morissette, DM Solutions Group (morissette@dmsolutions.ca)
*
*****************************************************************************
* Copyright (c) 2001-2004, Daniel Morissette, DM Solutions Group Inc
*
* 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 "maperror.h"
#include "mapogcsld.h"
#include "mapows.h"
#include <time.h>
#include <ctype.h>
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <process.h>
#include <stdio.h>
#endif
#include "cpl_vsi.h"
/**********************************************************************
* msInitWmsParamsObj()
*
**********************************************************************/
int msInitWmsParamsObj(wmsParamsObj *wmsparams)
{
wmsparams->onlineresource = NULL;
wmsparams->params = msCreateHashTable();
wmsparams->numparams=0;
wmsparams->httpcookiedata = NULL;
return MS_SUCCESS;
}
/**********************************************************************
* msFreeWmsParamsObj()
*
* Frees the contents of the object, but not the object itself.
**********************************************************************/
void msFreeWmsParamsObj(wmsParamsObj *wmsparams)
{
msFree(wmsparams->onlineresource);
wmsparams->onlineresource = NULL;
msFreeHashTable(wmsparams->params);
wmsparams->params = NULL;
msFree(wmsparams->httpcookiedata);
wmsparams->numparams=0;
}
/**********************************************************************
* msSetWMSParamString()
*
**********************************************************************/
#ifdef USE_WMS_LYR
static int msSetWMSParamString(wmsParamsObj *psWMSParams,
const char *name, const char * value,
int urlencode, int nVersion)
{
if (urlencode) {
char *pszTmp;
/*
* Special case handling for characters the WMS specification
* says should not be encoded, when they occur in certain
* parameters.
*
* Note: WMS 1.3 removes SRS and FORMAT from the set of
* exceptional cases, but renames SRS as CRS in any case.
*/
if( strcmp(name,"LAYERS") == 0 ||
strcmp(name,"STYLES") == 0 ||
strcmp(name,"BBOX") == 0 ) {
pszTmp = msEncodeUrlExcept(value,',');
} else if ( strcmp(name,"SRS") == 0 ) {
pszTmp = msEncodeUrlExcept(value,':');
} else if ( nVersion < OWS_1_3_0 && strcmp(name,"FORMAT") == 0 ) {
pszTmp = msEncodeUrlExcept(value,'/');
} else {
pszTmp = msEncodeUrl(value);
}
msInsertHashTable(psWMSParams->params, name, pszTmp);
msFree(pszTmp);
} else {
msInsertHashTable(psWMSParams->params, name, value);
}
psWMSParams->numparams++;
return MS_SUCCESS;
}
#endif /* def USE_WMS_LYR */
/**********************************************************************
* msSetWMSParamInt()
*
**********************************************************************/
#ifdef USE_WMS_LYR
static int msSetWMSParamInt(wmsParamsObj *wmsparams,
const char *name, int value)
{
char szBuf[100];
snprintf(szBuf, sizeof(szBuf), "%d", value);
msInsertHashTable(wmsparams->params, name, szBuf);
wmsparams->numparams++;
return MS_SUCCESS;
}
#endif /* def USE_WMS_LYR */
/**********************************************************************
* msBuildWMSParamsUrl()
*
**********************************************************************/
static char *msBuildURLFromWMSParams(wmsParamsObj *wmsparams)
{
const char *key, *value;
size_t bufferSize = 0;
int nLen;
char *pszURL;
/* Compute size required for URL buffer
*/
nLen = strlen(wmsparams->onlineresource) + 3;
key = msFirstKeyFromHashTable(wmsparams->params);
while (key != NULL) {
value = msLookupHashTable(wmsparams->params, key);
nLen += strlen(key) + strlen(value) + 2;
key = msNextKeyFromHashTable(wmsparams->params, key);
}
bufferSize = nLen+1;
pszURL = (char*)msSmallMalloc(bufferSize);
/* Start with the onlineresource value and append trailing '?' or '&'
* if missing.
*/
strcpy(pszURL, wmsparams->onlineresource);
if (strchr(pszURL, '?') == NULL)
strcat(pszURL, "?");
else {
char *c;
c = pszURL+strlen(pszURL)-1;
if (*c != '?' && *c != '&')
strcpy(c+1, "&");
}
/* Now add all the parameters
*/
nLen = strlen(pszURL);
key = msFirstKeyFromHashTable(wmsparams->params);
while (key != NULL) {
value = msLookupHashTable(wmsparams->params, key);
snprintf(pszURL+nLen, bufferSize-nLen, "%s=%s&", key, value);
nLen += strlen(key) + strlen(value) + 2;
key = msNextKeyFromHashTable(wmsparams->params, key);
}
/* Get rid of trailing '&'*/
pszURL[nLen-1] = '\0';
return pszURL;
}
#ifdef USE_WMS_LYR
/**********************************************************************
* msBuildWMSLayerURLBase()
*
* Build the base of a GetMap or GetFeatureInfo URL using metadata.
* The parameters to set are:
* VERSION
* LAYERS
* FORMAT
* TRANSPARENT
* STYLES
* QUERY_LAYERS (for queriable layers only)
*
* Returns a reference to a newly allocated string that should be freed
* by the caller.
**********************************************************************/
static int msBuildWMSLayerURLBase(mapObj *map, layerObj *lp,
wmsParamsObj *psWMSParams, int nRequestType)
{
const char *pszOnlineResource, *pszVersion, *pszName, *pszFormat;
const char *pszFormatList, *pszStyle, /* *pszStyleList,*/ *pszTime;
const char *pszBgColor, *pszTransparent;
const char *pszSLD=NULL, *pszStyleSLDBody=NULL, *pszVersionKeyword=NULL;
const char *pszSLDBody=NULL, *pszSLDURL = NULL;
char *pszSLDGenerated = NULL;
int nVersion=OWS_VERSION_NOTSET;
/* If lp->connection is not set then use wms_onlineresource metadata */
pszOnlineResource = lp->connection;
if (pszOnlineResource == NULL)
pszOnlineResource = msOWSLookupMetadata(&(lp->metadata),
"MO", "onlineresource");
pszVersion = msOWSLookupMetadata(&(lp->metadata), "MO", "server_version");
pszName = msOWSLookupMetadata(&(lp->metadata), "MO", "name");
pszFormat = msOWSLookupMetadata(&(lp->metadata), "MO", "format");
pszFormatList = msOWSLookupMetadata(&(lp->metadata), "MO", "formatlist");
pszStyle = msOWSLookupMetadata(&(lp->metadata), "MO", "style");
/*pszStyleList = msOWSLookupMetadata(&(lp->metadata), "MO", "stylelist");*/
pszTime = msOWSLookupMetadata(&(lp->metadata), "MO", "time");
pszSLDBody = msOWSLookupMetadata(&(lp->metadata), "MO", "sld_body");
pszSLDURL = msOWSLookupMetadata(&(lp->metadata), "MO", "sld_url");
pszBgColor = msOWSLookupMetadata(&(lp->metadata), "MO", "bgcolor");
pszTransparent = msOWSLookupMetadata(&(lp->metadata), "MO", "transparent");
if (pszOnlineResource==NULL || pszVersion==NULL || pszName==NULL) {
msSetError(MS_WMSCONNERR,
"One of wms_onlineresource, wms_server_version, wms_name "
"metadata is missing in layer %s. "
"Please either provide a valid CONNECTION URL, or provide "
"those values in the layer's metadata.\n",
"msBuildWMSLayerURLBase()", lp->name);
return MS_FAILURE;
}
psWMSParams->onlineresource = msStrdup(pszOnlineResource);
if (strncmp(pszVersion, "1.0.7", 5) < 0)
pszVersionKeyword = "WMTVER";
else
pszVersionKeyword = "VERSION";
nVersion = msOWSParseVersionString(pszVersion);
/* WMS 1.0.8 is really just 1.1.0 */
if (nVersion == OWS_1_0_8) nVersion = OWS_1_1_0;
msSetWMSParamString(psWMSParams, pszVersionKeyword, pszVersion, MS_FALSE, nVersion);
msSetWMSParamString(psWMSParams, "SERVICE", "WMS", MS_FALSE, nVersion);
msSetWMSParamString(psWMSParams, "LAYERS", pszName, MS_TRUE, nVersion);
if (pszFormat==NULL && pszFormatList==NULL) {
msSetError(MS_WMSCONNERR,
"At least wms_format or wms_formatlist is required for "
"layer %s. "
"Please either provide a valid CONNECTION URL, or provide "
"those values in the layer's metadata.\n",
"msBuildWMSLayerURLBase()", lp->name);
return MS_FAILURE;
}
if (pszFormat != NULL) {
msSetWMSParamString(psWMSParams, "FORMAT", pszFormat, MS_TRUE, nVersion);
} else {
/* Look for the first format in list that matches */
char **papszTok;
int i, n;
papszTok = msStringSplit(pszFormatList, ',', &n);
for(i=0; pszFormat==NULL && i<n; i++) {
if (0
#if defined USE_PNG
|| strcasecmp(papszTok[i], "PNG")
|| strcasecmp(papszTok[i], "image/png")
#endif
#if defined USE_JPEG
|| strcasecmp(papszTok[i], "JPEG")
|| strcasecmp(papszTok[i], "image/jpeg")
#endif
) {
pszFormat = papszTok[i];
}
}
if (pszFormat) {
msSetWMSParamString(psWMSParams, "FORMAT", pszFormat, MS_TRUE, nVersion);
msFreeCharArray(papszTok, n);
} else {
msSetError(MS_WMSCONNERR,
"Could not find a format that matches supported input "
"formats in wms_formatlist metdata in layer %s. "
"Please either provide a valid CONNECTION URL, or "
"provide the required layer metadata.\n",
"msBuildWMSLayerURLBase()", lp->name);
msFreeCharArray(papszTok, n);
return MS_FAILURE;
}
}
if (pszStyle==NULL) {
/* When no style is selected, use "" which is a valid default. */
pszStyle = "";
} else {
/* Was a wms_style_..._sld URL provided? */
char szBuf[100];
snprintf(szBuf, sizeof(szBuf), "style_%.80s_sld", pszStyle);
pszSLD = msOWSLookupMetadata(&(lp->metadata), "MO", szBuf);
snprintf(szBuf, sizeof(szBuf), "style_%.80s_sld_body", pszStyle);
pszStyleSLDBody = msOWSLookupMetadata(&(lp->metadata), "MO", szBuf);
if (pszSLD || pszStyleSLDBody) {
/* SLD URL is set. If this defn. came from a map context then */
/* the style name may just be an internal name: "Style{%d}" if */
/* that's the case then we should not pass this name via the URL */
if (strncmp(pszStyle, "Style{", 6) == 0)
pszStyle = "";
}
}
/* set STYLE parameter no matter what, even if it's empty (i.e. "STYLES=")
* GetLegendGraphic doesn't support multiple styles and is named STYLE
*/
if (nRequestType == WMS_GETLEGENDGRAPHIC) {
msSetWMSParamString(psWMSParams, "STYLE", pszStyle, MS_TRUE, nVersion);
} else {
msSetWMSParamString(psWMSParams, "STYLES", pszStyle, MS_TRUE, nVersion);
}
if (pszSLD != NULL) {
/* Only SLD is set */
msSetWMSParamString(psWMSParams, "SLD", pszSLD, MS_TRUE, nVersion);
} else if (pszStyleSLDBody != NULL) {
/* SLDBODY are set */
msSetWMSParamString(psWMSParams, "SLD_BODY", pszStyleSLDBody, MS_TRUE, nVersion);
}
if (msIsLayerQueryable(lp)) {
msSetWMSParamString(psWMSParams, "QUERY_LAYERS", pszName, MS_TRUE, nVersion);
}
if (pszTime && strlen(pszTime) > 0) {
msSetWMSParamString(psWMSParams, "TIME", pszTime, MS_TRUE, nVersion);
}
/* if the metadata wms_sld_body is set to AUTO, we generate
* the sld based on classes found in the map file and send
* it in the URL. If diffrent from AUTO, we are assuming that
* it is a valid sld.
*/
if (pszSLDBody) {
if (strcasecmp(pszSLDBody, "AUTO") == 0) {
if (pszVersion && strncmp(pszVersion, "1.3.0", 5) == 0)
pszSLDGenerated = msSLDGenerateSLD(map, lp->index, "1.1.0");
else
pszSLDGenerated = msSLDGenerateSLD(map, lp->index, NULL);
if (pszSLDGenerated) {
msSetWMSParamString(psWMSParams, "SLD_BODY",
pszSLDGenerated, MS_TRUE, nVersion);
free(pszSLDGenerated);
}
} else {
msSetWMSParamString(psWMSParams, "SLD_BODY", pszSLDBody, MS_TRUE, nVersion);
}
}
if (pszSLDURL) {
msSetWMSParamString(psWMSParams, "SLD", pszSLDURL, MS_TRUE, nVersion);
}
if (pszBgColor) {
msSetWMSParamString(psWMSParams, "BGCOLOR", pszBgColor, MS_TRUE, nVersion);
}
if (pszTransparent) {
msSetWMSParamString(psWMSParams, "TRANSPARENT", pszTransparent, MS_TRUE, nVersion);
} else {
msSetWMSParamString(psWMSParams, "TRANSPARENT", "TRUE", MS_TRUE, nVersion);
}
return MS_SUCCESS;
}
#endif /* USE_WMS_LYR */
/**********************************************************************
* msBuildWMSLayerURL()
*
* Build a GetMap or GetFeatureInfo URL.
*
* Returns a reference to a newly allocated string that should be freed
* by the caller.
**********************************************************************/
static int
msBuildWMSLayerURL(mapObj *map, layerObj *lp, int nRequestType,
int nClickX, int nClickY, int nFeatureCount,
const char *pszInfoFormat, rectObj *bbox_ret,
int *width_ret, int *height_ret,
wmsParamsObj *psWMSParams)
{
#ifdef USE_WMS_LYR
char *pszEPSG = NULL;
const char *pszVersion, *pszRequestParam,
*pszSrsParamName="SRS", *pszLayer=NULL, *pszQueryLayers=NULL,
*pszUseStrictAxisOrder;
rectObj bbox;
int bbox_width = map->width, bbox_height = map->height;
int nVersion=OWS_VERSION_NOTSET;
int bUseStrictAxisOrder = MS_FALSE; /* this is the assumption up to 1.1.0 */
int bFlipAxisOrder = MS_FALSE;
const char *pszTmp;
int bIsEssential = MS_FALSE;
if (lp->connectiontype != MS_WMS) {
msSetError(MS_WMSCONNERR, "Call supported only for CONNECTIONTYPE WMS",
"msBuildWMSLayerURL()");
return MS_FAILURE;
}
/* ------------------------------------------------------------------
* Find out request version
* ------------------------------------------------------------------ */
if (lp->connection == NULL ||
((pszVersion = strstr(lp->connection, "VERSION=")) == NULL &&
(pszVersion = strstr(lp->connection, "version=")) == NULL &&
(pszVersion = strstr(lp->connection, "WMTVER=")) == NULL &&
(pszVersion = strstr(lp->connection, "wmtver=")) == NULL ) ) {
/* CONNECTION missing or seems incomplete... try to build from metadata */
if (msBuildWMSLayerURLBase(map, lp, psWMSParams, nRequestType) != MS_SUCCESS)
return MS_FAILURE; /* An error already produced. */
/* If we received MS_SUCCESS then version must have been set */
pszVersion = msLookupHashTable(psWMSParams->params, "VERSION");
if (pszVersion ==NULL)
pszVersion = msLookupHashTable(psWMSParams->params, "WMTVER");
nVersion = msOWSParseVersionString(pszVersion);
} else {
/* CONNECTION string seems complete, start with that. */
char *pszDelimiter;
psWMSParams->onlineresource = msStrdup(lp->connection);
/* Fetch version info */
pszVersion = strchr(pszVersion, '=')+1;
pszDelimiter = strchr(pszVersion, '&');
if (pszDelimiter != NULL)
*pszDelimiter = '\0';
nVersion = msOWSParseVersionString(pszVersion);
if (pszDelimiter != NULL)
*pszDelimiter = '&';
}
switch (nVersion) {
case OWS_1_0_8:
nVersion = OWS_1_1_0; /* 1.0.8 == 1.1.0 */
break;
case OWS_1_0_0:
case OWS_1_0_1:
case OWS_1_0_7:
case OWS_1_1_0:
case OWS_1_1_1:
/* All is good, this is a supported version. */
break;
case OWS_1_3_0:
/* 1.3.0 introduces a few changes... */
pszSrsParamName = "CRS";
bUseStrictAxisOrder = MS_TRUE; /* this is the assumption for 1.3.0 */
break;
default:
/* Not a supported version */
msSetError(MS_WMSCONNERR, "MapServer supports only WMS 1.0.0 to 1.3.0 (please verify the VERSION parameter in the connection string).", "msBuildWMSLayerURL()");
return MS_FAILURE;
}
/* ------------------------------------------------------------------
* For GetFeatureInfo requests, make sure QUERY_LAYERS is included
* ------------------------------------------------------------------ */
if (nRequestType == WMS_GETFEATUREINFO &&
strstr(psWMSParams->onlineresource, "QUERY_LAYERS=") == NULL &&
strstr(psWMSParams->onlineresource, "query_layers=") == NULL &&
msLookupHashTable(psWMSParams->params, "QUERY_LAYERS") == NULL) {
pszQueryLayers = msOWSLookupMetadata(&(lp->metadata), "MO", "name");
if (pszQueryLayers == NULL) {
msSetError(MS_WMSCONNERR, "wms_name not set or WMS Connection String must contain the QUERY_LAYERS parameter to support GetFeatureInfo requests (with name in uppercase).", "msBuildWMSLayerURL()");
return MS_FAILURE;
}
}
/* ------------------------------------------------------------------
* For GetLegendGraphic requests, make sure LAYER is included
* ------------------------------------------------------------------ */
if (nRequestType == WMS_GETLEGENDGRAPHIC &&
strstr(psWMSParams->onlineresource, "LAYER=") == NULL &&
strstr(psWMSParams->onlineresource, "layer=") == NULL &&
msLookupHashTable(psWMSParams->params, "LAYER") == NULL) {
pszLayer = msOWSLookupMetadata(&(lp->metadata), "MO", "name");
if (pszLayer == NULL) {
msSetError(MS_WMSCONNERR, "wms_name not set or WMS Connection String must contain the LAYER parameter to support GetLegendGraphic requests (with name in uppercase).", "msBuildWMSLayerURL()");
return MS_FAILURE;
}
}
/* ------------------------------------------------------------------
* Figure the SRS we'll use for the request.
* - Fetch the map SRS (if it's EPSG)
* - Check if map SRS is listed in layer wms_srs metadata
* - If map SRS is valid for this layer then use it
* - Otherwise request layer in its default SRS and we'll reproject later
* ------------------------------------------------------------------ */
msOWSGetEPSGProj(&(map->projection),NULL, NULL, MS_TRUE, &pszEPSG);
if ( pszEPSG &&
(strncasecmp(pszEPSG, "EPSG:", 5) == 0 ||
strncasecmp(pszEPSG, "AUTO:", 5) == 0) ) {
const char *pszFound;
char *pszLyrEPSG;
int nLen;
char *pszPtr = NULL;
/* If it's an AUTO projection then keep only id and strip off */
/* the parameters for now (we'll restore them at the end) */
if (strncasecmp(pszEPSG, "AUTO:", 5) == 0) {
if ((pszPtr = strchr(pszEPSG, ',')))
*pszPtr = '\0';
}
nLen = strlen(pszEPSG);
msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "MO", MS_FALSE, &pszLyrEPSG);
if (pszLyrEPSG == NULL ||
(pszFound = strstr(pszLyrEPSG, pszEPSG)) == NULL ||
! ((*(pszFound+nLen) == '\0') || isspace(*(pszFound+nLen))) ) {
/* Not found in Layer's list of SRS (including projection object) */
free(pszEPSG);
pszEPSG = NULL;
}
msFree(pszLyrEPSG);
if (pszEPSG && pszPtr)
*pszPtr = ','; /* Restore full AUTO:... definition */
}
if (pszEPSG == NULL) {
msOWSGetEPSGProj(&(lp->projection), &(lp->metadata),"MO", MS_TRUE, &pszEPSG);
if( pszEPSG == NULL || (strncasecmp(pszEPSG, "EPSG:", 5) != 0 && strncasecmp(pszEPSG, "AUTO:", 5) != 0) ) {
msSetError(MS_WMSCONNERR, "Layer must have an EPSG or AUTO projection code (in its PROJECTION object or wms_srs metadata)", "msBuildWMSLayerURL()");
msFree(pszEPSG);
return MS_FAILURE;
}
}
/* ------------------------------------------------------------------
* For an AUTO projection, set the Units,lon0,lat0 if not already set
* ------------------------------------------------------------------ */
if (strncasecmp(pszEPSG, "AUTO:", 5) == 0 &&
strchr(pszEPSG, ',') == NULL) {
pointObj oPoint;
char *pszNewEPSG;
/* Use center of map view for lon0,lat0 */
oPoint.x = (map->extent.minx + map->extent.maxx)/2.0;
oPoint.y = (map->extent.miny + map->extent.maxy)/2.0;
msProjectPoint(&(map->projection), &(map->latlon), &oPoint);
pszNewEPSG = (char*)msSmallMalloc(101*sizeof(char));
snprintf(pszNewEPSG, 100, "%s,9001,%.16g,%.16g",
pszEPSG, oPoint.x, oPoint.y);
pszNewEPSG[100]='\0';
free(pszEPSG);
pszEPSG=pszNewEPSG;
}
/*
* Work out whether we'll be wanting to flip the axis order for the request
*/
pszUseStrictAxisOrder = msOWSLookupMetadata(&(lp->metadata), "MO", "strict_axis_order");
if (pszUseStrictAxisOrder != NULL) {
if (strncasecmp(pszUseStrictAxisOrder, "1", 1) == 0 ||
strncasecmp(pszUseStrictAxisOrder, "true", 4) == 0) {
bUseStrictAxisOrder = MS_TRUE;
} else if (strncasecmp(pszUseStrictAxisOrder, "0", 1) == 0 ||
strncasecmp(pszUseStrictAxisOrder, "false", 5) == 0) {
bUseStrictAxisOrder = MS_FALSE;
}
}
if (bUseStrictAxisOrder == MS_TRUE && pszEPSG &&
strncasecmp(pszEPSG, "EPSG:", 5) == 0 &&
msIsAxisInverted(atoi(pszEPSG + 5))) {
bFlipAxisOrder = MS_TRUE;
}
/* ------------------------------------------------------------------
* Set layer SRS.
* ------------------------------------------------------------------ */
/* No need to set lp->proj if it's already set to the right EPSG code */
{
char* pszEPSGCodeFromLayer = NULL;
msOWSGetEPSGProj(&(lp->projection), NULL, "MO", MS_TRUE, &pszEPSGCodeFromLayer);
if (pszEPSGCodeFromLayer == NULL || strcasecmp(pszEPSG, pszEPSGCodeFromLayer) != 0) {
char *ows_srs = NULL;
msOWSGetEPSGProj(NULL, &(lp->metadata), "MO", MS_FALSE, &ows_srs);
/* no need to set lp->proj if it is already set and there is only
one item in the _srs metadata for this layer - we will assume
the projection block matches the _srs metadata (the search for ' '
in ows_srs is a test to see if there are multiple EPSG: codes) */
if( lp->projection.numargs == 0 || ows_srs == NULL || (strchr(ows_srs,' ') != NULL) ) {
if (strncasecmp(pszEPSG, "EPSG:", 5) == 0) {
char szProj[20];
snprintf(szProj, sizeof(szProj), "init=epsg:%s", pszEPSG+5);
if (msLoadProjectionString(&(lp->projection), szProj) != 0) {
msFree(pszEPSGCodeFromLayer);
msFree(ows_srs);
return MS_FAILURE;
}
} else {
if (msLoadProjectionString(&(lp->projection), pszEPSG) != 0) {
msFree(pszEPSGCodeFromLayer);
msFree(ows_srs);
return MS_FAILURE;
}
}
}
msFree(ows_srs);
}
msFree(pszEPSGCodeFromLayer);
}
/* ------------------------------------------------------------------
* Adjust for MapServer EXTENT being center of pixel and WMS BBOX being
* edge of pixel (#2843).
* ------------------------------------------------------------------ */
bbox = map->extent;
bbox.minx -= map->cellsize * 0.5;
bbox.maxx += map->cellsize * 0.5;
bbox.miny -= map->cellsize * 0.5;
bbox.maxy += map->cellsize * 0.5;
/* -------------------------------------------------------------------- */
/* Reproject if needed. */
/* -------------------------------------------------------------------- */
if (msProjectionsDiffer(&(map->projection), &(lp->projection))) {
msProjectRect(&(map->projection), &(lp->projection), &bbox);
/* -------------------------------------------------------------------- */
/* Sometimes our remote WMS only accepts square pixel */
/* requests. If this is the case adjust adjust the number of */
/* pixels or lines in the request so that the pixels are */
/* square. */
/* -------------------------------------------------------------------- */
{
const char *nonsquare_ok =
msOWSLookupMetadata(&(lp->metadata),
"MO", "nonsquare_ok");
/* assume nonsquare_ok is false */
if( nonsquare_ok != NULL
&& (strcasecmp(nonsquare_ok,"no") == 0
|| strcasecmp(nonsquare_ok,"false") == 0) ) {
double cellsize_x = (bbox.maxx-bbox.minx) / bbox_width;
double cellsize_y = (bbox.maxy-bbox.miny) / bbox_height;
if( cellsize_x < cellsize_y * 0.999999 ) {
int new_bbox_height =
ceil((cellsize_y/cellsize_x) * bbox_height);
if (lp->debug)
msDebug("NONSQUARE_OK=%s, adjusted HEIGHT from %d to %d to equalize cellsize at %g.\n",
nonsquare_ok,
bbox_height, new_bbox_height, cellsize_x );
bbox_height = new_bbox_height;
} else if( cellsize_y < cellsize_x * 0.999999 ) {
int new_bbox_width =
ceil((cellsize_x/cellsize_y) * bbox_width);
if (lp->debug)
msDebug("NONSQUARE_OK=%s, adjusted WIDTH from %d to %d to equalize cellsize at %g.\n",
nonsquare_ok,
bbox_width, new_bbox_width, cellsize_y );
bbox_width = new_bbox_width;
} else {
if (lp->debug)
msDebug("NONSQUARE_OK=%s, but cellsize was already square - no change.\n",
nonsquare_ok );
}
}
}
}
/* -------------------------------------------------------------------- */
/* If the layer has predefined extents, and a predefined */
/* projection that matches the request projection, then */
/* consider restricting the BBOX to match the limits. */
/* -------------------------------------------------------------------- */
if( bbox_width != 0 ) {
char *ows_srs;
rectObj layer_rect;
msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "MO", MS_FALSE, &ows_srs);
if( ows_srs && strchr(ows_srs,' ') == NULL
&& msOWSGetLayerExtent( map, lp, "MO", &layer_rect) == MS_SUCCESS ) {
/* fulloverlap */
if( msRectContained( &bbox, &layer_rect ) ) {
/* no changes */
}
/* no overlap */
else if( !msRectOverlap( &layer_rect, &bbox ) ) {
bbox_width = 0;
bbox_height = 0;
}
else {
double cellsize_x = (bbox.maxx-bbox.minx) / bbox_width;
double cellsize_y = (bbox.maxy-bbox.miny) / bbox_height;
double cellsize = MS_MIN(cellsize_x,cellsize_y);
msRectIntersect( &bbox, &layer_rect );
bbox_width = round((bbox.maxx - bbox.minx) / cellsize);
bbox_height = round((bbox.maxy - bbox.miny) / cellsize);
/* Force going through the resampler if we're going to receive a clipped BBOX (#4931) */
if(msLayerGetProcessingKey(lp, "RESAMPLE") == NULL) {
msLayerSetProcessingKey(lp, "RESAMPLE", "nearest");
}
}
}
msFree(ows_srs);
}
/* -------------------------------------------------------------------- */
/* Potentially return the bbox. */
/* -------------------------------------------------------------------- */
if (bbox_ret != NULL)
*bbox_ret = bbox;
if( width_ret != NULL )
*width_ret = bbox_width;
if( height_ret != NULL )
*height_ret = bbox_height;
/* ------------------------------------------------------------------
* Build the request URL.
* At this point we set only the following parameters for GetMap:
* REQUEST
* SRS (or CRS)
* BBOX
*
* And for GetFeatureInfo:
* X (I for 1.3.0)
* Y (J for 1.3.0)
* INFO_FORMAT
* FEATURE_COUNT (only if nFeatureCount > 0)
*
* The connection string should contain all other required params,
* including:
* VERSION
* LAYERS
* FORMAT
* TRANSPARENT
* STYLES
* QUERY_LAYERS (for queryable layers only)
* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------
* Sometimes a requested layer is essential for the map, so if the
* request fails or an error is delivered, the map has not to be drawn
* ------------------------------------------------------------------ */
if ((pszTmp = msOWSLookupMetadata(&(lp->metadata),
"MO", "essential")) != NULL) {
if( strcasecmp(pszTmp,"true") == 0
|| strcasecmp(pszTmp,"on") == 0
|| strcasecmp(pszTmp,"yes") == 0 )
bIsEssential = MS_TRUE;
else
bIsEssential = atoi(pszTmp);
}
if (nRequestType == WMS_GETFEATUREINFO) {
char szBuf[100] = "";
if (nVersion >= OWS_1_0_7)
pszRequestParam = "GetFeatureInfo";
else
pszRequestParam = "feature_info";
const char* pszExceptionsParam;
if (nVersion >= OWS_1_3_0)
pszExceptionsParam = "XML";
else if (nVersion >= OWS_1_1_0) /* 1.1.0 to 1.1.0 */
pszExceptionsParam = "application/vnd.ogc.se_xml";
else if (nVersion > OWS_1_0_0) /* 1.0.1 to 1.0.7 */
pszExceptionsParam = "SE_XML";
else
pszExceptionsParam = "WMS_XML";
msSetWMSParamString(psWMSParams, "REQUEST", pszRequestParam, MS_FALSE, nVersion);
msSetWMSParamInt( psWMSParams, "WIDTH", bbox_width);
msSetWMSParamInt( psWMSParams, "HEIGHT", bbox_height);
msSetWMSParamString(psWMSParams, pszSrsParamName, pszEPSG, MS_FALSE, nVersion);
if (bFlipAxisOrder == MS_TRUE) {
snprintf(szBuf, sizeof(szBuf), "%.15g,%.15g,%.15g,%.15g",
bbox.miny, bbox.minx, bbox.maxy, bbox.maxx);
} else {
snprintf(szBuf, sizeof(szBuf), "%.15g,%.15g,%.15g,%.15g",
bbox.minx, bbox.miny, bbox.maxx, bbox.maxy);
}
msSetWMSParamString(psWMSParams, "BBOX", szBuf, MS_TRUE, nVersion);
if (nVersion >= OWS_1_3_0) {
msSetWMSParamInt( psWMSParams, "I", nClickX);
msSetWMSParamInt( psWMSParams, "J", nClickY);
} else {
msSetWMSParamInt( psWMSParams, "X", nClickX);
msSetWMSParamInt( psWMSParams, "Y", nClickY);
}
msSetWMSParamString(psWMSParams, "EXCEPTIONS", pszExceptionsParam, MS_FALSE, nVersion);
msSetWMSParamString(psWMSParams, "INFO_FORMAT", pszInfoFormat, MS_TRUE, nVersion);
if (pszQueryLayers) { /* not set in CONNECTION string */
msSetWMSParamString(psWMSParams, "QUERY_LAYERS", pszQueryLayers, MS_FALSE, nVersion);
}
/* If FEATURE_COUNT <= 0 then don't pass this parameter */
/* The spec states that FEATURE_COUNT must be greater than zero */
/* and if not passed then the behavior is up to the server */
if (nFeatureCount > 0) {
msSetWMSParamInt(psWMSParams, "FEATURE_COUNT", nFeatureCount);
}
} else if (nRequestType == WMS_GETLEGENDGRAPHIC) {
if(map->extent.maxx > map->extent.minx && map->width > 0 && map->height > 0) {
char szBuf[20] = "";
double scaledenom;
msCalculateScale(map->extent, map->units, map->width, map->height,
map->resolution, &scaledenom);
snprintf(szBuf, 20, "%g",scaledenom);
msSetWMSParamString(psWMSParams, "SCALE", szBuf, MS_FALSE, nVersion);
}
pszRequestParam = "GetLegendGraphic";
/*
const char* pszExceptionsParam = msOWSLookupMetadata(&(lp->metadata),
"MO", "exceptions_format");
if (pszExceptionsParam == NULL) {
if (nVersion >= OWS_1_1_0 && nVersion < OWS_1_3_0)
pszExceptionsParam = "application/vnd.ogc.se_inimage";
else
pszExceptionsParam = "INIMAGE";
}*/
if (pszLayer) { /* not set in CONNECTION string */
msSetWMSParamString(psWMSParams, "LAYER", pszLayer, MS_FALSE, nVersion);
}
msSetWMSParamString(psWMSParams, "REQUEST", pszRequestParam, MS_FALSE, nVersion);
msSetWMSParamString(psWMSParams, pszSrsParamName, pszEPSG, MS_FALSE, nVersion);
if (nVersion >= OWS_1_3_0) {
msSetWMSParamString(psWMSParams, "SLD_VERSION", "1.1.0", MS_FALSE, nVersion);
}
} else { /* if (nRequestType == WMS_GETMAP) */
char szBuf[100] = "";
if (nVersion >= OWS_1_0_7)
pszRequestParam = "GetMap";
else
pszRequestParam = "map";
const char* pszExceptionsParam = msOWSLookupMetadata(&(lp->metadata),
"MO", "exceptions_format");
if (!bIsEssential) {
if (pszExceptionsParam == NULL) {
if (nVersion >= OWS_1_1_0 && nVersion < OWS_1_3_0)
pszExceptionsParam = "application/vnd.ogc.se_inimage";
else
pszExceptionsParam = "INIMAGE";
}
} else {
/* if layer is essential, do not emit EXCEPTIONS parameter (defaults to XML) */
pszExceptionsParam = NULL;
}
msSetWMSParamString(psWMSParams, "REQUEST", pszRequestParam, MS_FALSE, nVersion);
msSetWMSParamInt( psWMSParams, "WIDTH", bbox_width);
msSetWMSParamInt( psWMSParams, "HEIGHT", bbox_height);
msSetWMSParamString(psWMSParams, pszSrsParamName, pszEPSG, MS_FALSE, nVersion);
if (bFlipAxisOrder == MS_TRUE) {
snprintf(szBuf, sizeof(szBuf), "%.15g,%.15g,%.15g,%.15g",
bbox.miny, bbox.minx, bbox.maxy, bbox.maxx);
} else {
snprintf(szBuf, sizeof(szBuf), "%.15g,%.15g,%.15g,%.15g",
bbox.minx, bbox.miny, bbox.maxx, bbox.maxy);
}
msSetWMSParamString(psWMSParams, "BBOX", szBuf, MS_TRUE, nVersion);
if( pszExceptionsParam ) {
msSetWMSParamString(psWMSParams, "EXCEPTIONS", pszExceptionsParam, MS_FALSE, nVersion);
}
}
free(pszEPSG);
return MS_SUCCESS;
#else
/* ------------------------------------------------------------------
* WMS CONNECTION Support not included...
* ------------------------------------------------------------------ */
msSetError(MS_WMSCONNERR, "WMS CLIENT CONNECTION support is not available.",
"msBuildWMSLayerURL()");
return MS_FAILURE;
#endif /* USE_WMS_LYR */
}
/**********************************************************************
* msWMSGetFeatureInfoURL()
*
* Build a GetFeatureInfo URL for this layer.
*
* Returns a reference to a newly allocated string that should be freed
* by the caller.
**********************************************************************/
char *msWMSGetFeatureInfoURL(mapObj *map, layerObj *lp,
int nClickX, int nClickY, int nFeatureCount,
const char *pszInfoFormat)
{
wmsParamsObj sThisWMSParams;
char *pszURL;
msInitWmsParamsObj(&sThisWMSParams);
if (msBuildWMSLayerURL(map, lp, WMS_GETFEATUREINFO,
nClickX, nClickY, nFeatureCount,
pszInfoFormat, NULL, NULL, NULL,
&sThisWMSParams)!= MS_SUCCESS) {
return NULL;
}
pszURL = msBuildURLFromWMSParams(&sThisWMSParams);
msFreeWmsParamsObj(&sThisWMSParams);
return pszURL;
}
/**********************************************************************
* msPrepareWMSLayerRequest()
*
**********************************************************************/
int msPrepareWMSLayerRequest(int nLayerId, mapObj *map, layerObj *lp,
int nRequestType, enum MS_CONNECTION_TYPE lastconnectiontype,
wmsParamsObj *psLastWMSParams,
int nClickX, int nClickY, int nFeatureCount, const char *pszInfoFormat,
httpRequestObj *pasReqInfo, int *numRequests)
{
#ifdef USE_WMS_LYR
char *pszURL = NULL, *pszHTTPCookieData = NULL;
const char *pszTmp;
rectObj bbox = { 0 };
int bbox_width = 0, bbox_height = 0;
int nTimeout, bOkToMerge, bForceSeparateRequest, bCacheToDisk;
wmsParamsObj sThisWMSParams;
int ret = MS_FAILURE;
if (lp->connectiontype != MS_WMS)
return MS_FAILURE;