This page last changed on Jul 06, 2006 by yecarrillo.

With basic PHP knowledge you can play with WMS capabilities and create list of advertized layers.

This tutorial will teach how to make these lists and use that values to other purpouses.

Prerequisites

wms-parser.php class library. This file is part of Carto Module for Drupal by John Pulles.
You can download the whole module and extract only this file, or get the latest code from CVS web at http://cvs.drupal.org/viewcvs/drupal/contributions/modules/carto/wms-parser.php

Steps

  • Include the file
  • Read WMS capabilities and store it into a variable
  • Parse it using objects provided by this the class library and store it into a new object
  • Expose the object contents as you desire

Sample Code

This sample code shows how to catch the WMS capabilities to create a list of layers:

<?php

include ('/var/www/html/wms-parser.php');

$nombre_archivo = "/var/www/html/wmscap.xml";
$gestor = fopen($nombre_archivo, "r");
$contenido = fread($gestor, filesize($nombre_archivo));
fclose($gestor);

$caps = & new CapabilitiesParser( );
$caps->parse($contenido);
$caps->free_parser( );

?>

<h1>WMS Services Description</h1>

<strong>WMS version:</strong> <?php echo $caps->version ?><br />
<strong>Layers count:</strong> <?php echo sizeof($caps->layers) ?> <br />

<h2>Layers list</h2>
<ol>
<?php foreach ($caps->layers as $l) { ?>
  <li><strong><?php echo $l['Title'] ?></strong><br />
  <?php echo $l['Abstract'] ?><br />
  Limits: (<i><?php echo $l['LatLonBoundingBox']['minx'] . ',' .
                         $l['LatLonBoundingBox']['miny'] . ',' .
                         $l['LatLonBoundingBox']['maxx'] . ',' .
                         $l['LatLonBoundingBox']['maxy'];  ?>)</i>
  </p></li>
<?php } ?>
</ol>

You can obtain capabilities directly from the server (PHP 5 and up)

<?php

include ('/var/www/html/wms-parser.php');

$nombre_archivo = "http://example.com:8080/geoserver/wms?request=GetCapabilities&service=WMS";
$gestor = fopen($nombre_archivo, "rb");
$contenido = stream_get_contents($gestor);
fclose($gestor);

$caps = & new CapabilitiesParser( );
$caps->parse($contenido);
$caps->free_parser( );

?>

<h1>WMS Services Description</h1>

<strong>WMS version:</strong> <?php echo $caps->version ?><br />
<strong>Layers count:</strong> <?php echo sizeof($caps->layers) ?> <br />

<h2>Layers list</h2>
<ol>
<?php foreach ($caps->layers as $l) { ?>
  <li><strong><?php echo $l['Title'] ?></strong><br />
  <?php echo $l['Abstract'] ?><br />
  Limits: (<i><?php echo $l['LatLonBoundingBox']['minx'] . ',' .
                         $l['LatLonBoundingBox']['miny'] . ',' .
                         $l['LatLonBoundingBox']['maxx'] . ',' .
                         $l['LatLonBoundingBox']['maxy'];  ?>)</i>
  </p></li>
<?php } ?>
</ol>

The layers array is a recursive list of layers. We can exclude layers 0 and 1 form the list using the queryable property, and extract the root service name and abstract from the layer 1.

<?php

include ('/var/www/html/wms-parser.php');

$nombre_archivo = "http://example.com:8080/geoserver/wms?request=GetCapabilities&service=WMS";
$gestor = fopen($nombre_archivo, "rb");
$contenido = stream_get_contents($gestor);
fclose($gestor);

$caps = & new CapabilitiesParser( );
$caps->parse($contenido);
$caps->free_parser( );

?>

<h1>WMS Services Description</h1>

<strong>Service Name:</strong> <?php echo $caps->layers[1]['Title'] ?> <br />
<i><?php echo $caps->layers[1]['Abstract'] ?></i>  <br />
<strong>WMS version:</strong> <?php echo $caps->version ?><br />
<strong>Layers count:</strong> <?php echo (sizeof($caps->layers) - 2) ?> <br />

<h2>Layers list</h2>
<ol>
<?php foreach ($caps->layers as $l) { 
if ($l['queryable']) {
?>
  <li><strong><?php echo $l['Title'] ?></strong><br />
  <?php echo $l['Abstract'] ?><br />
  Limits: (<i><?php echo $l['LatLonBoundingBox']['minx'] . ',' .
                         $l['LatLonBoundingBox']['miny'] . ',' .
                         $l['LatLonBoundingBox']['maxx'] . ',' .
                         $l['LatLonBoundingBox']['maxy'];  ?>)</i>
  </p></li>
<?php } } ?>
</ol>

Available Properties

This class provides you a small but usefull amount of properties:

  • version : String with WMS version of the service
  • root : String with the root name of the service
  • layers : Array of layers containing this items:
  1. Name : String
  2. Title : String
  3. Abstract : String
  4. SRS: String of space separated list of SRSs available for this layer
  5. LatLonBoundingBox : Array of bounding box coordinates: minx,miny,maxx,maxy
  6. Style : Array of named styles assigned to this layer
  7. queryable : 0 for not queryable layers and 1 for queryable layers

You can look the whole object with this piece of code:

<?php

include ('/var/www/html/wms-parser.php');

$nombre_archivo = "http://example.com:8080/geoserver/wms?request=GetCapabilities&service=WMS";
$gestor = fopen($nombre_archivo, "rb");
$contenido = stream_get_contents($gestor);
fclose($gestor);

$caps = & new CapabilitiesParser( );
$caps->parse($contenido);
$caps->free_parser( );

echo "<pre>";
var_dump($caps);
echo "</pre>";

?>
Document generated by Confluence on Jan 16, 2008 23:28