forked from dmorissette/mapserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapml.c
775 lines (632 loc) · 31.4 KB
/
mapml.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: MapML support implementation.
* See https://maps4html.github.io/HTML-Map-Element/spec/
* Author: Daniel Morissette
*
******************************************************************************
* Copyright (c) 1996-2019 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.
*****************************************************************************/
#define _GNU_SOURCE
#include "mapserver.h"
#include "mapows.h"
#include "cpl_string.h" /* CPLSPrintf() */
/* There is a dependency on libxml2 for XML handling */
#ifdef USE_LIBXML2
#include "maplibxml2.h"
#endif
/*
* Utility function to output MapML doc via msIO
*/
static int _msIO_MapMLDump(FILE *fp, xmlDocPtr psDoc)
{
int status = MS_SUCCESS;
/* Note: we want to avoid the <?xml declaration in the output,
* so we dump the root node instead of the document itself */
xmlBufferPtr buf = xmlBufferCreate();
if (xmlNodeDump(buf, psDoc, xmlDocGetRootElement(psDoc), 0, 1) > 0) {
msIO_printf("%s\n", buf->content);
}
else {
// TODO: Produce an exception
msSetError(MS_WMSERR, "Writing MapML XML output failed.", "_msIO_MapMLDump()");
status = MS_FAILURE;
}
xmlBufferFree (buf);
return status;
}
/*
* A couple of useful XML-generation shortcuts (libxml2 wrappers)
*/
static xmlNodePtr _xmlNewChild1Prop(xmlNodePtr parent_node, const char *node_name, const char *node_value, const char *prop1, const char *value1)
{
xmlNodePtr _node = xmlNewChild(parent_node, pszNamespaces, BAD_CAST node_name, BAD_CAST node_value);
if (value1) xmlNewProp(_node, BAD_CAST prop1, BAD_CAST value1);
return _node;
}
static xmlNodePtr _xmlNewChild2Prop(xmlNodePtr parent_node, const char *node_name, const char *node_value, const char *prop1, const char *value1, const char *prop2, const char *value2)
{
xmlNodePtr _node = xmlNewChild(parent_node, pszNamespaces, BAD_CAST node_name, BAD_CAST node_value);
if (value1) xmlNewProp(_node, BAD_CAST prop1, BAD_CAST value1);
if (value2) xmlNewProp(_node, BAD_CAST prop2, BAD_CAST value2);
return _node;
}
static xmlNodePtr _xmlNewChild3Prop(xmlNodePtr parent_node, const char *node_name, const char *node_value, const char *prop1, const char *value1, const char *prop2, const char *value2, const char *prop3, const char *value3)
{
xmlNodePtr _node = xmlNewChild(parent_node, pszNamespaces, BAD_CAST node_name, BAD_CAST node_value);
if (value1) xmlNewProp(_node, BAD_CAST prop1, BAD_CAST value1);
if (value2) xmlNewProp(_node, BAD_CAST prop2, BAD_CAST value2);
if (value3) xmlNewProp(_node, BAD_CAST prop3, BAD_CAST value3);
return _node;
}
static xmlNodePtr _xmlNewChild4Prop(xmlNodePtr parent_node, const char *node_name, const char *node_value, const char *prop1, const char *value1, const char *prop2, const char *value2, const char *prop3, const char *value3, const char *prop4, const char *value4)
{
xmlNodePtr _node = xmlNewChild(parent_node, pszNamespaces, BAD_CAST node_name, BAD_CAST node_value);
if (value1) xmlNewProp(_node, BAD_CAST prop1, BAD_CAST value1);
if (value2) xmlNewProp(_node, BAD_CAST prop2, BAD_CAST value2);
if (value3) xmlNewProp(_node, BAD_CAST prop3, BAD_CAST value3);
if (value4) xmlNewProp(_node, BAD_CAST prop4, BAD_CAST value4);
return _node;
}
static xmlNodePtr _xmlNewChild5Prop(xmlNodePtr parent_node, const char *node_name, const char *node_value, const char *prop1, const char *value1, const char *prop2, const char *value2, const char *prop3, const char *value3, const char *prop4, const char *value4, const char *prop5, const char *value5)
{
xmlNodePtr _node = xmlNewChild(parent_node, pszNamespaces, BAD_CAST node_name, BAD_CAST node_value);
if (value1) xmlNewProp(_node, BAD_CAST prop1, BAD_CAST value1);
if (value2) xmlNewProp(_node, BAD_CAST prop2, BAD_CAST value2);
if (value3) xmlNewProp(_node, BAD_CAST prop3, BAD_CAST value3);
if (value4) xmlNewProp(_node, BAD_CAST prop4, BAD_CAST value4);
if (value5) xmlNewProp(_node, BAD_CAST prop5, BAD_CAST value5);
return _node;
}
static void _xmlNewPropInt(xmlNodePtr _node, const char *prop1, int value1)
{
xmlNewProp(_node, BAD_CAST prop1, BAD_CAST CPLSPrintf("%d", value1));
}
static void _xmlNewPropDouble(xmlNodePtr _node, const char *prop1, double value1)
{
xmlNewProp(_node, BAD_CAST prop1, BAD_CAST CPLSPrintf("%g", value1));
}
/*
* Map MapML Projection name to EPSG CRS codes.
*
* Returns corresponding EPSG/CRS code or
* Returns NULL if projection is invalid or not enabled.
*
* Set bQuietMode = TRUE to silently return NULL if SRS not enabled.
* Set bQuietMode = FALSE to produce an error if SRS is not enabled
*/
static const char * msIsMapMLProjectionEnabled(mapObj *map, layerObj *lp, const char *pszNamespaces, const char *pszProjection, int bQuietMode)
{
const char *pszCRS=NULL;
/* Validate PROJECTION and map it to WMS CRS */
if (pszProjection && strcasecmp(pszProjection, "OSMTILE") == 0)
pszCRS = "EPSG:3857"; // Web Mercator
else if (pszProjection && strcasecmp(pszProjection, "CBMTILE") == 0)
pszCRS = "EPSG:3978"; // Canada LCC
else if (pszProjection && strcasecmp(pszProjection, "APSTILE") == 0)
pszCRS = "EPSG:5936"; // Alaska Polar Stereographic
else if (pszProjection && strcasecmp(pszProjection, "WGS84") == 0) {
if (msOWSIsCRSValid2(map, lp, pszNamespaces, "CRS:84"))
pszCRS = "CRS:84";
else
pszCRS = "EPSG:4326";
}
else {
msSetError(MS_WMSERR, "Invalid PROJECTION parameter", "msMapMLProjection2EPSG()");
return NULL;
}
if (!msOWSIsCRSValid2(map, lp, pszNamespaces, pszCRS)) {
if (!bQuietMode)
msSetError(MS_WMSERR, "PROJECTION %s requires CRS %s to be enabled for this layer.", "msMapMLProjection2EPSG()", pszProjection, pszCRS);
return NULL;
}
return pszCRS;
}
/*
* Return list of styles for selected layer as a CPL StringList The
* list of styles is actually the list of class group names.
* Returns a CSL string list that should be freed by the caller or NULL if no
* class name found (i.e. just one default style applies)
*
* Note: This works only for a single layer and won't work for grouped layers
* at the moment.
*
* Note: Should probably be moved to mapwms.c with a refactoring of
* WMS code to reuse it.
*/
static char **msWMSGetStyleList(layerObj *lp)
{
char **papszStyles = NULL;
for (int i=0; i<lp->numclasses; i++) {
if (lp->class[i]->group &&
CSLFindString(papszStyles, lp->class[i]->group) == -1)
papszStyles = CSLAddString(papszStyles, lp->class[i]->group);
}
return papszStyles;
}
/*
** msMapMLException()
**
** Report current MapServer error in requested format.
*/
int msMapMLException(mapObj *map, const char *exception_code)
{
msIO_setHeader("Content-Type","text/xml; charset=UTF-8");
msIO_sendHeaders();
msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>\n");
msIO_printf("<ServiceExceptionReport>\n");
if (exception_code)
msIO_printf("<ServiceException code=\"%s\">\n", exception_code);
else
msIO_printf("<ServiceException>\n");
msWriteErrorXML(stdout);
msIO_printf("</ServiceException>\n");
msIO_printf("</ServiceExceptionReport>\n");
return MS_FAILURE; /* so that we can call 'return msMapMLException();' anywhere */
}
/*
** msWriteMapMLLayer()
**
** Return specified layer info in response to GetMapML requests.
**
** URL Parameters:
** MAP=...
** SERVICE=WMS (only WMS supported for now)
** REQUEST=GetMapML
** LAYER=...
** STYLE=...
** PROJECTION= one of OSMTILE, CBMTILE, APSTILE, WGS84
** defaults to OSMTILE if not specified (as per spec)
** MAPML_MODE= image (default) or tile or cgitile or features
**
**
** Returns MS_SUCCESS/MS_FAILURE
*/
int msWriteMapMLLayer(FILE *fp, mapObj *map, cgiRequestObj *req, owsRequestObj *ows_request, const char *pszService)
{
#ifdef USE_MAPML
int i = 0;
const char *pszLayer = NULL, *pszStyle = "";
layerObj *lp;
int nLayers =0;
int iLayerIndex = -1;
char ***nestedGroups = NULL;
int *numNestedGroups = NULL;
int *isUsedInNestedGroup = NULL;
const char *pszProjection = "OSMTILE", *pszCRS=NULL;
char *script_url = NULL;
const char *pszVal1=NULL, *pszVal2=NULL;
const char *pszEncoding = "UTF-8";
const char *pszNamespaces = "http://www.w3.org/1999/xhtml";
const char *pszMapMLMode = NULL;
const char *pszImageFormat = NULL;
projectionObj proj;
rectObj ext;
/* We need this server's onlineresource. It will come with the trailing "?" or "&" */
/* the returned string should be freed once we're done with it. */
if ((script_url=msOWSGetOnlineResource(map, pszNamespaces, "onlineresource", req)) == NULL) {
msSetError(MS_WMSERR, "Missing OnlineResource.", "msWriteMapMLLayer()");
return MS_FAILURE;
}
/* Process URL Parameters */
for(i=0; map && req && i<req->NumParams; i++) {
if(strcasecmp(req->ParamNames[i], "LAYER") == 0) {
pszLayer = req->ParamValues[i];
}
if(strcasecmp(req->ParamNames[i], "PROJECTION") == 0) {
pszProjection = req->ParamValues[i];
}
if(strcasecmp(req->ParamNames[i], "STYLE") == 0) {
// TODO ideally we should validate that supplied style exists, see mapwms.c
pszStyle = req->ParamValues[i];
}
if(strcasecmp(req->ParamNames[i], "MAPML_LINK_MODE") == 0) {
pszMapMLMode = req->ParamValues[i];
}
}
if (!pszLayer) {
msSetError(MS_WMSERR, "Mandatory LAYER parameter missing in GetMapML request.", "msWriteMapMLLayer()");
return MS_FAILURE;
}
/* Look for requested layer. we check for layer's and group's name */
/* as well as wms_layer_group names */
nestedGroups = (char***)msSmallCalloc(map->numlayers, sizeof(char**));
numNestedGroups = (int*)msSmallCalloc(map->numlayers, sizeof(int));
isUsedInNestedGroup = (int*)msSmallCalloc(map->numlayers, sizeof(int));
msWMSPrepareNestedGroups(map, OWS_1_3_0, nestedGroups, numNestedGroups, isUsedInNestedGroup);
for (i=0; i<map->numlayers; i++) {
lp = GET_LAYER(map, i);
if ( ((map->name && strcasecmp(map->name, pszLayer) == 0) ||
(lp->name && strcasecmp(lp->name, pszLayer) == 0) ||
(lp->group && strcasecmp(lp->group, pszLayer) == 0) ||
((numNestedGroups[i] >0) && (msStringInArray(pszLayer, nestedGroups[i], numNestedGroups[i]))) ) &&
(msIntegerInArray(lp->index, ows_request->enabled_layers, ows_request->numlayers)) ) {
nLayers++;
lp->status = MS_ON;
iLayerIndex = i;
break; /* We only care about the first match */
} else
lp->status = MS_OFF;
}
/* free the stuff used for nested layers */
for (i = 0; i < map->numlayers; i++) {
if (numNestedGroups[i] > 0) {
msFreeCharArray(nestedGroups[i], numNestedGroups[i]);
}
}
msFree(nestedGroups);
msFree(numNestedGroups);
msFree(isUsedInNestedGroup);
if (nLayers != 1) {
msSetError(MS_WMSERR, "Invalid layer given in the LAYER parameter. A layer might be disabled for \
this request. Check wms/ows_enable_request settings.", "msWriteMapMLLayer()");
return MS_FAILURE;
}
/* Validate MapML PROJECTION and map it to WMS CRS (EPSG code) */
if ((pszCRS = msIsMapMLProjectionEnabled(map, lp, pszNamespaces, pszProjection, FALSE)) == NULL)
return MS_FAILURE; // msSetError already called
/* Fetch and reproject layer extent to requested CRS */
// TODO: For now just using map extent... need to look up layer/group extent if applicable
memcpy(&ext, &(map->extent), sizeof(rectObj));
msInitProjection(&proj);
if (msLoadProjectionStringEPSG(&proj, pszCRS) != 0) {
/* Failed to load projection, msSetError shoudlshould already have been called */
return MS_FAILURE;
}
if (msProjectionsDiffer(&(map->projection), &proj) == MS_TRUE) {
msProjectRect(&(map->projection), &proj, &ext);
}
/*
* We're good to go. Create a new <mapml> document and start populating the <head> and <body>
*/
xmlDocPtr psMapMLDoc = NULL; /* libxml2 document pointer */
xmlNodePtr root_node = NULL, psMapMLHead = NULL, psMapMLBody = NULL, psNode = NULL;
psMapMLDoc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(pszNamespaces, BAD_CAST "mapml-");
xmlDocSetRootElement(psMapMLDoc, root_node);
psMapMLHead = xmlNewChild(root_node, pszNamespaces, BAD_CAST "map-head", NULL);
psMapMLBody = xmlNewChild(root_node, pszNamespaces, BAD_CAST "map-body", NULL);
/* *** mapmp/head *** */
/* <title>: If LAYER name is the top-level map then return map title, otherwise return the first matching layer's title */
if (map->name && EQUAL(map->name, pszLayer))
pszVal1 = msOWSLookupMetadata3( &(map->web.metadata), NULL, pszNamespaces, "map-title", map->name );
else
pszVal1 = msOWSLookupMetadata3( &(lp->metadata), NULL, pszNamespaces, "map-title", lp->name );
if (pszVal1)
psNode = xmlNewChild(psMapMLHead, pszNamespaces, BAD_CAST "map-title", BAD_CAST pszVal1);
/* <meta> */
_xmlNewChild1Prop(psMapMLHead, "map-meta", NULL, "charset", pszEncoding);
_xmlNewChild2Prop(psMapMLHead, "map-meta", NULL, "http-equiv", "Content-Type",
"content", CPLSPrintf("text/mapml;projection=%s", pszProjection) );
/* link rel=license - mapped to *_attribution_* metadata */
pszVal1 = msOWSLookupMetadata2( &(lp->metadata), &(map->web.metadata), pszNamespaces, "attribution_onlineresource" );
pszVal2 = msOWSLookupMetadata2( &(lp->metadata), &(map->web.metadata), pszNamespaces, "attribution_title" );
if (pszVal1 || pszVal2)
_xmlNewChild3Prop(psMapMLHead, "map-link", NULL, "rel", "license", "href", pszVal1, "title", pszVal2);
/* link rel=legend */
pszVal1 = CPLSPrintf("%sSERVICE=WMS&REQUEST=GetLegendGraphic&VERSION=1.3.0&FORMAT=image/png&LAYER=%s&STYLE=%s&SLD_VERSION=1.1.0", script_url, pszLayer, pszStyle);
_xmlNewChild2Prop(psMapMLHead, "map-link", NULL, "rel", "legend", "href", pszVal1);
/* link rel=alternate projection */
const char *papszAllProj[] = {"OSMTILE", "CBMTILE", "APSTILE", "WGS84", NULL};
const char *pszProj;
for (int i=0; (pszProj=papszAllProj[i])!= NULL; i++) {
if (!EQUAL(pszProjection, pszProj) &&
msIsMapMLProjectionEnabled(map, lp, pszNamespaces, pszProj, TRUE) != NULL) {
pszVal1 = CPLSPrintf("%sSERVICE=%s&REQUEST=GetMapML&LAYER=%s&STYLE=%s&PROJECTION=%s", script_url, pszService, pszLayer, pszStyle, pszProj);
_xmlNewChild3Prop(psMapMLHead, "map-link", NULL, "rel", "alternate", "projection", pszProj, "href", pszVal1);
}
}
/* link rel=style / self style */
/* Works only in the single-layer / single-style case at the moment */
char **papszStyles = msWMSGetStyleList(lp);
const char *pszAltStyle;
for (int i=0; papszStyles && (pszAltStyle=papszStyles[i])!= NULL; i++) {
pszVal1 = CPLSPrintf("%sSERVICE=%s&REQUEST=GetMapML&LAYER=%s&STYLE=%s&PROJECTION=%s", script_url, pszService, pszLayer, pszAltStyle, pszProjection);
if (EQUAL(pszAltStyle, pszStyle))
_xmlNewChild3Prop(psMapMLHead, "map-link", NULL, "rel", "self style", "title", pszAltStyle, "href", pszVal1);
else
_xmlNewChild3Prop(psMapMLHead, "map-link", NULL, "rel", "style", "title", pszAltStyle, "href", pszVal1);
}
CSLDestroy(papszStyles);
/* *** mapml/body *** */
/* What type of output do we want, controlled by mapml_link_mode metadata, or request MAPML_LINK_MODE param
* - image: (the default) produces full page WMS GetMap images
* - tile: produces link-rel = tile with tiled WMS GetMap requests
* - cgitile: produces mode=tile mapserv CGI requests
* - features: produces link-ref=features pointing to WFS GetFeature
*/
if (pszMapMLMode == NULL)
pszMapMLMode = msOWSLookupMetadata2( &(lp->metadata), &(map->web.metadata), NULL, "mapml_link_mode" );
if (pszMapMLMode == NULL)
pszMapMLMode = "image"; // The default
/* Check for preferred image format */
pszImageFormat = msOWSLookupMetadata2( &(lp->metadata), &(map->web.metadata), NULL, "mapml_wms_image_format" );
if (pszImageFormat == NULL)
pszImageFormat = "image/png";
/* <extent> */
xmlNodePtr psMapMLExtent = _xmlNewChild1Prop(psMapMLBody, "map-extent", NULL, "units", pszProjection);
if (EQUAL(pszMapMLMode, "tile")) {
// MAPML TILE mode: Serve requested layer as tiled WMS GetMap requests
// TODO: Should we allow this mode with WGS84?
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "txmin", "type", "location", "units", "tilematrix", "position", "top-left", "axis", "easting");
_xmlNewPropDouble(psNode, "min", ext.minx);
_xmlNewPropDouble(psNode, "max", ext.maxx);
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "tymin", "type", "location", "units", "tilematrix", "position", "bottom-left", "axis", "northing");
_xmlNewPropDouble(psNode, "min", ext.miny);
_xmlNewPropDouble(psNode, "max", ext.maxy);
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "txmax", "type", "location", "units", "tilematrix", "position", "top-right", "axis", "easting");
_xmlNewPropDouble(psNode, "min", ext.minx);
_xmlNewPropDouble(psNode, "max", ext.maxx);
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "tymax", "type", "location", "units", "tilematrix", "position", "top-left", "axis", "northing");
_xmlNewPropDouble(psNode, "min", ext.miny);
_xmlNewPropDouble(psNode, "max", ext.maxy);
/* WMS BBOX format, coordinate default to X,Y, except for EPSG:4326 where it is lat,lon */
const char *pszBBOX = "{txmin},{tymin},{txmax},{tymax}";
if (EQUAL(pszCRS, "EPSG:4326"))
pszBBOX = "{tymin},{txmin},{tymax},{txmax}";
/* GetMap URL */
pszVal1 = CPLSPrintf("%sSERVICE=WMS&REQUEST=GetMap&FORMAT=%s&TRANSPARENT=TRUE&VERSION=1.3.0&LAYERS=%s&STYLES=%s&WIDTH=256&HEIGHT=256&CRS=%s&BBOX=%s&m4h=t", script_url, pszImageFormat, pszLayer, pszStyle, pszCRS, pszBBOX);
psNode = _xmlNewChild2Prop(psMapMLExtent, "map-link", NULL, "rel", "tile", "tref", pszVal1);
// TODO: Add WMS GetFeatureInfo (share code with "image" case)
}
else if (EQUAL(pszMapMLMode, "cgitile")) {
// MAPML CGITILE mode: Serve requested layer as tiles using mapserv CGI mode=tile&tilemode=gmap
// TODO: is this use case valid only for the OSMTILE projection???
// Special case to map top-level map layer in WMS to special keyword "all" in mapserv CGI syntax
if (map->name && EQUAL(map->name, pszLayer))
pszLayer = "all";
// TODO: map to real zoom/axis values here
psNode = _xmlNewChild2Prop(psMapMLExtent, "map-input", NULL, "name", "z", "type", "zoom");
_xmlNewPropInt(psNode, "value", 10);
_xmlNewPropInt(psNode, "min", 4);
_xmlNewPropInt(psNode, "max", 15);
psNode = _xmlNewChild4Prop(psMapMLExtent, "map-input", NULL, "name", "y", "type", "location", "units", "tilematrix", "axis", "row");
_xmlNewPropInt(psNode, "min", 0);
_xmlNewPropInt(psNode, "max", 32768);
psNode = _xmlNewChild4Prop(psMapMLExtent, "map-input", NULL, "name", "x", "type", "location", "units", "tilematrix", "axis", "column");
_xmlNewPropInt(psNode, "min", 0);
_xmlNewPropInt(psNode, "max", 32768);
pszVal1 = CPLSPrintf("%smode=tile&tilemode=gmap&FORMAT=%s&LAYERS=%s&tile={x}+{y}+{z}&m4h=t", script_url, pszImageFormat, pszLayer);
psNode = _xmlNewChild2Prop(psMapMLExtent, "map-link", NULL, "rel", "tile", "tref", pszVal1);
}
else if (EQUAL(pszMapMLMode, "image")) {
// Produce full screen WMS GetMap requests
_xmlNewChild2Prop(psMapMLExtent, "map-input", NULL, "name", "w", "type", "width");
_xmlNewChild2Prop(psMapMLExtent, "map-input", NULL, "name", "h", "type", "height");
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "xmin", "type", "location", "units", "pcrs", "position", "top-left", "axis", "easting");
_xmlNewPropDouble(psNode, "min", ext.minx);
_xmlNewPropDouble(psNode, "max", ext.maxx);
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "ymin", "type", "location", "units", "pcrs", "position", "bottom-left", "axis", "northing");
_xmlNewPropDouble(psNode, "min", ext.miny);
_xmlNewPropDouble(psNode, "max", ext.maxy);
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "xmax", "type", "location", "units", "pcrs", "position", "top-right", "axis", "easting");
_xmlNewPropDouble(psNode, "min", ext.minx);
_xmlNewPropDouble(psNode, "max", ext.maxx);
psNode = _xmlNewChild5Prop(psMapMLExtent, "map-input", NULL, "name", "ymax", "type", "location", "units", "pcrs", "position", "top-left", "axis", "northing");
_xmlNewPropDouble(psNode, "min", ext.miny);
_xmlNewPropDouble(psNode, "max", ext.maxy);
/* WMS BBOX format, coordinate default to X,Y, except for EPSG:4326 where it is lat,lon */
const char *pszBBOX = "{xmin},{ymin},{xmax},{ymax}";
if (EQUAL(pszCRS, "EPSG:4326"))
pszBBOX = "{ymin},{xmin},{ymax},{xmax}";
/* GetMap URL */
pszVal1 = CPLSPrintf("%sSERVICE=WMS&REQUEST=GetMap&FORMAT=%s&TRANSPARENT=TRUE&VERSION=1.3.0&LAYERS=%s&STYLES=%s&WIDTH={w}&HEIGHT={h}&CRS=%s&BBOX=%s&m4h=t", script_url, pszImageFormat, pszLayer, pszStyle, pszCRS, pszBBOX);
psNode = _xmlNewChild2Prop(psMapMLExtent, "map-link", NULL, "rel", "image", "tref", pszVal1);
/* If layer is queryable then enable GetFeatureInfo */
// TODO Check if layer is queryable (also need to check top-level map, groups, nested groups)
// TODO: Check if wms_getfeatureinfo_formatlist includes text/mapml */
// TODO: handle optional feature count
psNode = _xmlNewChild4Prop(psMapMLExtent, "map-input", NULL, "name", "i", "type", "location", "axis", "i", "units", "map");
_xmlNewPropInt(psNode, "min", 0);
_xmlNewPropInt(psNode, "max", 0);
psNode = _xmlNewChild4Prop(psMapMLExtent, "map-input", NULL, "name", "j", "type", "location", "axis", "j", "units", "map");
_xmlNewPropInt(psNode, "min", 0);
_xmlNewPropInt(psNode, "max", 0);
pszVal1 = CPLSPrintf("%sSERVICE=WMS&REQUEST=GetFeatureInfo&INFO_FORMAT=text/mapml&FEATURE_COUNT=1&TRANSPARENT=TRUE&VERSION=1.3.0&LAYERS=%s&STYLES=%s&QUERY_LAYERS=%s&WIDTH={w}&HEIGHT={h}&CRS=%s&BBOX=%s&x={i}&y={j}&m4h=t", script_url, pszLayer, pszStyle, pszLayer, pszCRS, pszBBOX);
psNode = _xmlNewChild2Prop(psMapMLExtent, "map-link", NULL, "rel", "query", "tref", pszVal1);
}
else if (EQUAL(pszMapMLMode, "features")) {
// MAPML FEATURES mode: Serve link to WFS GetFeature requests
// TODO: WFS GetFeature not available yet
}
else {
msSetError(MS_WMSERR, "Requested MapML output mode not supported. Use one of image, tile, cgitile or features.", "msWriteMapMLLayer()");
return msMapMLException(map, "InvalidRequest");
}
/* Generate output */
msIO_setHeader("Content-Type","text/mapml");
msIO_setHeader("Access-Control-Allow-Origin","*");
msIO_sendHeaders();
_msIO_MapMLDump(fp, psMapMLDoc);
/* Cleanup */
xmlFreeDoc(psMapMLDoc);
msFree(script_url);
return MS_SUCCESS;
#else
msSetError(MS_WMSERR, "MapML support is not available.", "msWriteMapMLLayer()");
return(MS_FAILURE);
#endif /* USE_MAPML */
}
/*
** msWriteMapMLLayer()
**
** Dump MapML query results for WMS GetFeatureInfo
**
**
** Returns MS_SUCCESS/MS_FAILURE
*/
//TODO: This is just a temporary implementation derived from msGMLWriteQuery. It will be rewritten once the OGR/mapml driver is available
int msWriteMapMLQuery(mapObj *map, FILE *fp, const char *namespaces)
{
#if defined(USE_MAPML)
int status;
int i,j,k;
layerObj *lp=NULL;
shapeObj shape;
char szPath[MS_MAXPATHLEN];
char *value;
char *pszMapSRS = NULL;
gmlGroupListObj *groupList=NULL;
gmlItemListObj *itemList=NULL;
gmlConstantListObj *constantList=NULL;
gmlGeometryListObj *geometryList=NULL;
gmlItemObj *item=NULL;
gmlConstantObj *constant=NULL;
msInitShape(&shape);
msIO_setHeader("Content-Type","text/mapml");
msIO_setHeader("Access-Control-Allow-Origin","*");
msIO_sendHeaders();
msIO_fprintf(fp, "<mapml- xmlns=\"http://www.w3.org/1999/xhtml\">\n");
msIO_fprintf(fp, " <map-head>\n");
msIO_fprintf(fp, " <map-title>GetFeatureInfo Results</map-title>\n");
//msIO_fprintf(fp, " <map-base href=\"TODO-href\" />\n");
msIO_fprintf(fp, " <map-meta charset=\"utf-8\" ></map-meta>\n");
// TODO: Add PROJECTION param to Content-Type value
msIO_fprintf(fp, " <map-meta http-equiv=\"Content-Type\" content=\"text/mapml\" ></map-meta>\n");
msIO_fprintf(fp, " </map-head>\n");
msIO_fprintf(fp, " <map-body>\n");
msIO_fprintf(fp, " <map-extent />\n"); // Mandatory extent element (empty)
/* Look up map SRS. We need an EPSG code for GML, if not then we get null and we'll fall back on the layer's SRS */
msOWSGetEPSGProj(&(map->projection), NULL, namespaces, MS_TRUE, &pszMapSRS);
/* step through the layers looking for query results */
for(i=0; i<map->numlayers; i++) {
char *pszOutputSRS = NULL;
int nSRSDimension = 2;
const char* geomtype;
char *layername=NULL, *layertitle=NULL;
lp = (GET_LAYER(map, map->layerorder[i]));
if(lp->resultcache && lp->resultcache->numresults > 0) { /* found results */
#ifdef USE_PROJ
/* Determine output SRS, if map has none, then try using layer's native SRS */
if ((pszOutputSRS = pszMapSRS) == NULL) {
msOWSGetEPSGProj(&(lp->projection), NULL, namespaces, MS_TRUE, &pszOutputSRS);
if (pszOutputSRS == NULL) {
msSetError(MS_WMSERR, "No valid EPSG code in map or layer projection for GML output", "msGMLWriteQuery()");
continue; /* No EPSG code, cannot output this layer */
}
}
#endif
/* start this collection (layer) */
// TODO: Lookup layername metadata, and chack for NULL lp->name
/* if no layer name provided fall back on the layer name + "_layer" */
layername = (char*) msSmallMalloc(strlen(lp->name)+7);
sprintf(layername, "%s", lp->name);
//msOWSPrintValidateMetadata(fp, &(lp->metadata), namespaces, "layername", OWS_NOERR, "\t<%s>\n", layername);
layertitle = (char *) msOWSLookupMetadata(&(lp->metadata), "OM", "map-title");
if (layertitle) {
//msOWSPrintEncodeMetadata(fp, &(lp->metadata), namespaces, "title", OWS_NOERR, "\t<gml:name>%s</gml:name>\n", layertitle);
}
geomtype = msOWSLookupMetadata(&(lp->metadata), "OFG", "geomtype");
if( geomtype != NULL && (strstr(geomtype, "25d") != NULL || strstr(geomtype, "25D") != NULL) )
{
msIO_fprintf(fp, "<!-- WARNING: 25d requested for layer '%s' but MapML only supports 2D. -->\n", lp->name);
}
/* populate item and group metadata structures */
itemList = msGMLGetItems(lp, namespaces);
constantList = msGMLGetConstants(lp, namespaces);
groupList = msGMLGetGroups(lp, namespaces);
geometryList = msGMLGetGeometries(lp, namespaces, MS_FALSE);
if (itemList == NULL || constantList == NULL || groupList == NULL || geometryList == NULL) {
msSetError(MS_MISCERR, "Unable to populate item and group metadata structures", "msGMLWriteQuery()");
return MS_FAILURE;
}
for(j=0; j<lp->resultcache->numresults; j++) {
status = msLayerGetShape(lp, &shape, &(lp->resultcache->results[j]));
if(status != MS_SUCCESS) {
msGMLFreeGroups(groupList);
msGMLFreeConstants(constantList);
msGMLFreeItems(itemList);
msGMLFreeGeometries(geometryList);
return(status);
}
#ifdef USE_PROJ
/* project the shape into the map projection (if necessary), note that this projects the bounds as well */
if(pszOutputSRS == pszMapSRS && msProjectionsDiffer(&(lp->projection), &(map->projection))) {
status = msProjectShape(&lp->projection, &map->projection, &shape);
if(status != MS_SUCCESS) {
msIO_fprintf(fp, "<!-- Warning: Failed to reproject shape: %s -->\n",msGetErrorString(","));
continue;
}
}
#endif
/* start this feature */
msIO_fprintf(fp, " <map-feature id=\"%s.%ld\" class=\"%s\">\n", layername, shape.index, layername);
// TODO: handle geomeotry output...
//
/* Write the feature geometry and bounding box unless 'none' was requested. */
/* Default to bbox only if nothing specified and output full geometry only if explicitly requested */
/*
if(!(geometryList && geometryList->numgeometries == 1 && strcasecmp(geometryList->geometries[0].name, "none") == 0)) {
gmlWriteBounds(fp, OWS_GML2, &(shape.bounds), pszOutputSRS, "\t\t\t", "gml");
if (geometryList && geometryList->numgeometries > 0 )
gmlWriteGeometry(fp, geometryList, OWS_GML2, &(shape), pszOutputSRS, NULL, "\t\t\t", "", nSRSDimension);
}
*/
/* write properties */
msIO_fprintf(fp, " <map-properties>\n");
msIO_fprintf(fp, " <table>\n");
msIO_fprintf(fp, " <thead>\n");
msIO_fprintf(fp, " <tr>\n");
msIO_fprintf(fp, " <th role=\"columnheader\" scope=\"col\">Property Name</th>\n");
msIO_fprintf(fp, " <th role=\"columnheader\" scope=\"col\">Property Value</th>\n");
msIO_fprintf(fp, " </tr>\n");
msIO_fprintf(fp, " </thead>\n");
for(k=0; k<itemList->numitems; k++) {
item = &(itemList->items[k]);
if(item->visible && msItemInGroups(item->name, groupList) == MS_FALSE) {
msIO_fprintf(fp, " <tbody>\n");
msIO_fprintf(fp, " <tr>\n");
msIO_fprintf(fp, " <th scope=\"row\">%s</th>\n", item->name);
msIO_fprintf(fp, " <td itemprop=\"%s\">%s</td>\n", item->name, shape.values[k]);
msIO_fprintf(fp, " </tr>\n");
msIO_fprintf(fp, " </tbody>\n");
}
}
msIO_fprintf(fp, " </table>\n");
msIO_fprintf(fp, " </map-properties>\n");
/* end this feature */
msIO_fprintf(fp, " </map-feature>\n");
msFreeShape(&shape); /* init too */
}
/* end this collection (layer) */
msFree(layername);
msFree(layertitle);
msGMLFreeGroups(groupList);
msGMLFreeConstants(constantList);
msGMLFreeItems(itemList);
msGMLFreeGeometries(geometryList);
/* msLayerClose(lp); */
}
if(pszOutputSRS!=pszMapSRS) {
msFree(pszOutputSRS);
}
} /* next layer */
/* end this document */
msIO_fprintf(fp, " </map-body>\n");
msIO_fprintf(fp, "</mapml->\n");
return(MS_SUCCESS);
#else
msSetError(MS_MISCERR, "MapML support not enabled", "msWriteMapMLQuery()");
return MS_FAILURE;
#endif
}