[GIS] OpenLayers 2.1x load WMS layers from PHP

openlayers-2PHPwms

I have been looking to see if it's possible to add WMS layers from a PHP script. I'd like to do this to move a lot of the code from the client side to the server.

Currently I load the layers in the initialize function with:

/*Declare layers + properties:*/
var layer1= new OpenLayers.Layer.WMS("Layer1", "/geoserver/wms", {layers: 'Workspace:Layer1', styles: '', srs: 'EPSG:27700', format: format, tiled: 'true',transparent: 'TRUE'}, {tileSize: new OpenLayers.Size(540,640), buffer: 0, displayOutsideMaxExtent: true,isBaseLayer: false,visibility : false,transitionEffect: 'none' } );
var layer2= new OpenLayers.Layer.WMS("Layer2", "/geoserver/wms", {layers: 'Workspace:Layer2', styles: '', srs: 'EPSG:27700', format: format, tiled: 'true',transparent: 'TRUE'}, {tileSize: new OpenLayers.Size(540,640), buffer: 0, displayOutsideMaxExtent: true,isBaseLayer: false,visibility : false,transitionEffect: 'none' } );

/*Add layers to layer array*/
layerArray.push({key : 'layer1', lyr : layer1});
layerArray.push({key : 'layer2', lyr : layer2});

/*Add layer to the map*/
for(var n =0; n<layerArray.length; n++)
{
    obj = layerArray[n];
    map.addLayer(obj.lyr);
}

Is there a way to move the layer information and layer array population to PHP so the only code in the initialize function is the code to add the layers to the map?

** Edit **

More details as requested:

I have been asked to produce a site that contains an OpenLayers 2.1x map that utilises vector drawing, select, measure, geolocate, CQL filters and all mapping layers are stored in GeoServer 2.33 and are accessed using WMS. GeoServer is run on the web server.

I have completed all of this using JavaScript to populate the layer names (as indicated above) and sketchSymbolizer properties etc. I also use JavaScript to create any calculations and CQL filters.

The client who has asked for the site has expressed concerns with the ease of taking the JavaScript and replicating the site, plus he feels large calculation in JavaScript are affecting the performance of older smart phones.

I have moved calculations to PHP and got them running smoothly (if not a little slower for fast machines, but adding a loader and everyone is happy again), but there is still a want to move all of the OpenLayers initialize items into PHP as well. I can not get OpenLayers to initialize, I have used the code below to create an array, but feel I'm missing the point completely.

<!--Demo code held in html for proof of concept before moving to an external file-->
<?php

$al = array(
array(
"key" => "layer1",
"lyr" => "new OpenLayers.Layer.WMS("Layer1", "/geoserver/wms", {layers: 'Workspace:Layer1', styles: '', srs: 'EPSG:27700', format: format, tiled: 'true',transparent: 'TRUE'}, {tileSize: new OpenLayers.Size(540,640), buffer: 0, displayOutsideMaxExtent: true,isBaseLayer: false,visibility : false,transitionEffect: 'none' } )"
),
 array(
 "key" => "layer2",
 "lyr" => "new OpenLayers.Layer.WMS("Layer2", "/geoserver/wms", {layers: 'Workspace:Layer2', styles: '', srs: 'EPSG:27700', format: format, tiled: 'true',transparent: 'TRUE'}, {tileSize: new OpenLayers.Size(540,640), buffer: 0, displayOutsideMaxExtent: true,isBaseLayer: false,visibility : false,transitionEffect: 'none' } )"
)
);
$arrayObject = new ArrayObject($al);
?>
<script type="text/javascript">
    var layerArray= <?php echo json_encode($arrayObject); ?>;
</script>

This produces the error "annot read property 'wrapDateLine' of null".

In light of "why" do I want to, my hope was to hide variables used for creating the layers and other elements, but if there is a more effective / better way, I'm very open to trying anything.

Best Answer

Yes, it is possible. But what is the source of layer list? If that information is stored in a some kind of database, then PHP will be the right way. But when you want to load all layers from WMS server/workspace, then AJAX call to GetCapabilities or GeoServer API will be better. Last but not least, if layer list is immutable, this could be done entirely in JavaScript. If you provide some details about the layer list source, I'll post some code examples.

Related Question