0

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.

1
  • The short answer to your question is 'Yes' But you need to provide more details, so that we can understand what you are trying to achieve and why. That will help this question get detailed answers. Please see this post on how you can improve your question: meta.gis.stackexchange.com/questions/3349/… Jan 10, 2014 at 2:42

1 Answer 1

1

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.

3
  • Thanks for the quick response. They layers are all held and loaded from Geoserver and currently done via javascript. The layers are then toggled on and off using a custom menu that identifies the state of the layer and sends "layerid.setVisibility(Insert correct state)". My reason for moving everything server side is an attempt to hide all of the code and I thought loading the layers would be an easy start. (I may be barking up the wrong tree)
    – Phill
    Jan 9, 2014 at 15:20
  • What is the purpose of hiding the code? You can't effectively hide Geoserver URL using this technique, as even if the code isn't readable, it's not a problem to extract the URL using JavaScript console in the browser. It would be wiser to secure the layers using GeoServer authentication instead, and use a proxy (for example PHP/cURL based) between OpenLayers and GeoServer. Jan 9, 2014 at 15:40
  • I have everything secured as suggested. I'm looking at starting a piece of work for a company that want as much code hidden as possible to avoid people easily copying. Mainly code that calculates / constructs things like cql filters, sketchSymbolizer properties etc. I thought starting with something simple like loading the layers into an array would be the best start point as proof of concept that the variables can be constructed and loaded in from an external source.
    – Phill
    Jan 9, 2014 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.