wms_functions.php
8.46 KB
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
<?php
function xml2html ( $str ) {
#
# Convert XML characters to HTML entities
# for display purposes.
#
$str = ereg_replace("&","&",$str);
$str = ereg_replace("<","<",$str);
$str = ereg_replace(">","><BR>",$str);
return $str;
}
function wms_descricao ( $dom,$xp ) {
$xpath = new DOMXPath($dom);
$query = $xp;
$entries = $xpath->query($query);
$n = "";
foreach ($entries as $entry)
{
$n = $entry->nodeValue;
}
return $n;
}
function wms_descricaov ( $dom,$xp,$attrib ) {
$xpath = new DOMXPath($dom);
$query = $xp;
$entries = $xpath->query($query);
$n = "";
foreach ($entries as $entry)
{
$n = $entry->getAttribute($attrib);
}
return $n;
}
function wms_descricaon ( $dom,$xp,$n ) {
$ctx = xpath_new_context($dom);
$xpnode = xpath_eval($ctx,$xp);
$dtnode = $xpnode->nodeset[$n]->first_child();
return $dtnode->content;
}
function wms_title ( $dom ) {
#
# Read the WMS service title and return it as text.
#
$xpath = new DOMXPath($dom);
$query = '//WMT_MS_Capabilities/Service/Title';
$entries = $xpath->query($query);
foreach ($entries as $entry){$nomeserv = $entry->nodeValue;}
return $nomeserv;
}
function wms_onlineresource ( $dom ) {
#
# Read the WMS online resource URL and return it as text.
#
$xp = "/WMT_MS_Capabilities/Service/OnlineResource";
$ctx = xpath_new_context($dom);
$xpnode = xpath_eval($ctx,$xp);
return $xpnode->nodeset[0]->get_attribute("href");
}
function wms_formats ( $dom )
{
$xpath = new DOMXPath($dom);
$query = '//WMT_MS_Capabilities/Capability/Request/GetMap/Format';
$entries = $xpath->query($query);
$arr = array();
foreach ($entries as $entry)
{
$arr[] = $entry->nodeValue;
}
return $arr;
}
function wms_estilos ( $dom ) {
#
# Read the WMS image formats and return them as an array.
#
//$xp = "/Style";
//$ctx = xpath_new_context($dom);
//$xpnode = xpath_eval($ctx,$xp);
//return $xpnode->nodeset;
$return = $dom->getElementsByTagName("Style");
}
function wms_exceptions ( $dom ) {
#
# Read the WMS exception formats and return them as an array.
#
$xp = "/WMT_MS_Capabilities/Capability/Exception/Format";
$ctx = xpath_new_context($dom);
$xpnode = xpath_eval($ctx,$xp);
$arr = array();
for( $i = 0; $i < sizeof($xpnode->nodeset); ++$i ) {
$dtnode = $xpnode->nodeset[0]->first_child();
array_push($arr,$dtnode->content);
}
return $arr;
}
function wms_layers ( $dom ) {
#
# Read the WMS first level layers and return an
# array of nodes.
#
$xpath = new DOMXPath($dom);
$query = '//WMT_MS_Capabilities/Capability/Layer';
$entries = $xpath->query($query);
return $entries;
}
function wms_xpnode2content( $xp_node ) {
#
# Read the content child node of an element tag
# node.
#
$content = "";
if( $xp_node->nodeset[0] ) {
$node = $xp_node->nodeset[0]->first_child();
$content = $node->content;
}
return $content;
}
function wms_srs( $dom )
{
$xpath = new DOMXPath($dom);
$query = '//WMT_MS_Capabilities/Capability/Layer/SRS';
$entries = $xpath->query($query);
$srs = "";
foreach ($entries as $entry)
{
$s = $entry->nodeValue;
if ($s == "EPSG:4326")
{$srs = "EPSG:4326";}
}
if ($srs == "") {$srs = $s;}
return strtoupper($srs);
}
function wms_bbox( $dom )
{
$xpath = new DOMXPath($dom);
$query = '//WMT_MS_Capabilities/Capability/Layer/LatLonBoundingBox';
$entries = $xpath->query($query);
foreach ($entries as $entry){$bbox = $entry->nodeValue;}
if ($bbox == '-1,-1,-1,-1')
{return '-180,-90,180,90';}
else
{return wms_bbox2txt($bbox);}
}
function wms_bbox2txt( $node ) {
#
# Convert a BoundingBox node into a text string.
#
if( $node ) {
$txt .= 1 * $node->get_attribute("minx");
$txt .= ",";
$txt .= 1 * $node->get_attribute("miny");
$txt .= ",";
$txt .= 1 * $node->get_attribute("maxx");
$txt .= ",";
$txt .= 1 * $node->get_attribute("maxy");
}
else {
$txt = "-180,-90,180,90";
}
return $txt;
}
function wms_layer2html( $node, $tipo , $layer ) {
#
# Convert a Layer node into an HTML representation.
#
$ctx = xpath_new_context($node);
$xp_title = xpath_eval($ctx,"/Title");
$xp_name = xpath_eval($ctx,"/Name");
if (wms_xpnode2content($xp_name) == ""){$xp_name = xpath_eval($ctx,"/name");}
$xp_srs = xpath_eval($ctx,"/SRS");
$xp_llbbox = xpath_eval($ctx,"/LatLonBoundingBox");
$xp_bbox = xpath_eval($ctx,"/BoundingBox");
$txt_title = wms_xpnode2content($xp_title);
$txt_name = wms_xpnode2content($xp_name);
$txt_srs = strtoupper(wms_xpnode2content($xp_srs));
$node_llbbox = $xp_llbbox->nodeset[0];
$node_bbox = $xp_bbox->nodeset[0];
$queryable = 0;
if ( $node->get_attribute("queryable") ) {
$queryable = 1;
}
$opaque = 0;
if ( $node->get_attribute("opaque") ) {
$opaque = 1;
}
//legenda
$xp_legenda = xpath_eval($ctx,"/LegendURL/OnlineResource");
$nodelegenda = $xp_legenda->nodeset[0];
if($nodelegenda)
{ $legenda = $nodelegenda->get_attribute("href");}
$html = "<INPUT TYPE='radio' NAME='checks' VALUE='$txt_name' onClick='toggle(event,\"$tipo\",\"$layer\",\"$legenda\",\"$txt_title\")'>";
$html .= " ";
$html .= $txt_title . "\n";
$html .= wms_hidden("bbox_$txt_name", wms_bbox2txt($node_bbox));
$html .= wms_hidden("llbox_$txt_name", wms_bbox2txt($node_llbbox));
$html .= wms_hidden("srs_$txt_name", $txt_srs);
$html .= wms_hidden("query_$txt_name", $queryable );
$html .= wms_hidden("opaque_$txt_name", $opaque );
$html .= "<BR>";
return $html;
}
function wms_layer3html( $node ) {
#
# Convert a Layer node into an HTML representation sem radio.
#
$ctx = xpath_new_context($node);
$xp_title = xpath_eval($ctx,"/Title");
$xp_abs = xpath_eval($ctx,"/Abstract");
$txt_title = wms_xpnode2content($xp_title);
$txt_abs = wms_xpnode2content($xp_abs);
$html .= "<b>".$txt_title . "</b><i style='color:gray'>" . "-" . $txt_abs . "</i>\n";
$html .= "<BR>";
return $html;
}
function wms_layer4html( $layer ) {
$estilos = wms_estilos($layer);
if (count($estilos) > 0)
{
$ctxl = xpath_new_context($layer);
$xp_namel = xpath_eval($ctxl,"/Name");
if (wms_xpnode2content($xp_namel) == ""){$xp_namel = xpath_eval($ctxl,"/name");}
$txt_namel = wms_xpnode2content($xp_namel);
$html .= wms_layer3html($layer);
foreach ($estilos as $estilo)
{
$html .= wms_layer2html($estilo,"estilo",$txt_namel);
}
}
else
{
$html .= wms_layer2html($layer,"tema","");
}
return $html;
}
function wms_fatal ( $err ) {
print "<P>$err</P>";
exit;
}
function wms_hidden ( $name, $value ) {
return "<INPUT TYPE='hidden' NAME='$name' VALUE='$value'>\n";
}
function wms_pan ( $bbox, $size, $click ) {
#
# Calculate a new extent based on an image size and image
# click coordinates. Move extent linearly so that the
# click becomes the new spatial center.
#
$sw = $bbox[2] - $bbox[0];
$sh = $bbox[3] - $bbox[1];
$iw = $size[0];
$ih = $size[1];
$x = $click[0];
$y = $ih - $click[1] - 1;
$mx = $iw / 2;
$my = $ih / 2;
$bbox[0] = $bbox[0] - $sw * ($mx - $x) / $iw;
$bbox[1] = $bbox[1] - $sh * ($my - $y) / $ih;
$bbox[2] = $bbox[2] - $sw * ($mx - $x) / $iw;
$bbox[3] = $bbox[3] - $sh * ($my - $y) / $ih;
return $bbox;
}
function wms_zoom ( $bbox, $factor ) {
#
# Create a new resized extent based on a spatial extent and
# zoom factor.
#
$sw = $bbox[2] - $bbox[0];
$sh = $bbox[3] - $bbox[1];
$mx = ($bbox[2] + $bbox[0]) / 2;
$my = ($bbox[3] + $bbox[1]) / 2;
$bbox[0] = $mx - $factor * $sw / 2;
$bbox[1] = $my - $factor * $sh / 2;
$bbox[2] = $mx + $factor * $sw / 2;
$bbox[3] = $my + $factor * $sh / 2;
return $bbox;
}
function wms_scale ( $bbox, $size ) {
#
# Resize a spatial extent to have the same aspect ratio
# as an image size.
#
# Spatial width is always preserved and spatial height is
# altered to match the new aspect ratio. There are other
# heuristics for this transformation which might be
# more aesthetically pleasing.
#
$sw = $bbox[2] - $bbox[0];
$sh = $bbox[3] - $bbox[1];
$iw = $size[0];
$ih = $size[1];
$sh = $sw * $ih / $iw;
$bbox[1] = (($bbox[3] + $bbox[1]) / 2) - ($sh / 2);
$bbox[3] = (($bbox[3] + $bbox[1]) / 2) + ($sh / 2);
return $bbox;
}
?>