= Building a WPS client using !OpenLayers = [[TOC(heading= )]] [wiki:ZooWorkshop/FOSS4GJapan/CreatingOGRBasedWebServices Prev.] | [wiki:ZooWorkshop/FOSS4GJapan WorkShop table of content] | [wiki:ZooWorkshop/FOSS4GJapan/Exercice Next] The next step of our workshop is to connect to the OGR Services we have created from an !OpenLayers map. This will allow to apply single or multiple geometries processes on user-selected polygons and to display the new generated geometries. == Creating a simple map showing the dataset as WMS == !OpenLayers is also included in OSGeoLive default distribution, so it is convenient to use it for our needs. Please open your text editor and write the following HTML snippet: {{{ #!text/html ZOO WPS Client example
}}} The following !JavaScript code must then be added in a {{{}}}} section within the {{{}}} one. This will setup a map showing the United States data as WMS. {{{ #!js var map, layer, select, hover, multi, control; function init(){ OpenLayers.ProxyHost= "../cgi-bin/proxy.cgi?url="; map = new OpenLayers.Map('map', { controls: [ new OpenLayers.Control.PanZoom(), new OpenLayers.Control.Permalink(), new OpenLayers.Control.Navigation() ] }); layer = new OpenLayers.Layer.WMS( "States WMS/WFS", "http://localhost:8082/geoserver/ows", {layers: 'topp:states', format: 'image/png'}, {buffer:1, singleTile:true} ); map.addLayers([layer]); map.zoomToExtent(new OpenLayers.Bounds(-140.444336,25.115234,-44.438477,50.580078)); } }}} Once done, please save your HTML file as zoo-ogr.html in your workshop directory, then copy it in {{{/var/www}}} and visualize it with your favorite Web browser using this link : http://localhost/zoo-ogr.html. You should obtain a map centered on the USA with the WMS layer activated. [[Image(OL-1.png,nolink,width=500px,margin=5)]] == Fetching the data layer as WFS and adding selection controls == Before accessing the displayed data via WFS, you first have to create new vector layers dedicated to host the several interactions we are going to create. Please add the following lines within the {{{init()}}} function, and do not forget to add the newly created layer in the {{{map.addLayers}}} method: {{{ #!js select = new OpenLayers.Layer.Vector("Selection", {styleMap: new OpenLayers.Style(OpenLayers.Feature.Vector.style["select"]) }); hover = new OpenLayers.Layer.Vector("Hover"); multi = new OpenLayers.Layer.Vector("Multi", {styleMap: new OpenLayers.Style({ fillColor:"red", fillOpacity:0.4, strokeColor:"red", strokeOpacity:1, strokeWidth:2 }) }); map.addLayers([layer, select, hover, multi]); }}} Then, you can now access tdata by creating new controls to select polygons, as the following. Please note that {{{OpenLayers.Protocol.WFS.fromWMSLayer(layer)}}} is used to access geometries and that several state of selection are declared and append to the control variable. {{{ #!js control = new OpenLayers.Control.GetFeature({ protocol: OpenLayers.Protocol.WFS.fromWMSLayer(layer) }); control.events.register("featureselected", this, function(e) { select.addFeatures([e.feature]); }); control.events.register("featureunselected", this, function(e) { select.removeFeatures([e.feature]); }); control.events.register("hoverfeature", this, function(e) { hover.addFeatures([e.feature]); }); control.events.register("outfeature", this, function(e) { hover.removeFeatures([e.feature]); }); map.addControl(control); control.activate(); }}} Please save your HTML file again. You should now be able to select a polygon only by clicking on it. The selected polygon should appear in blue color. [[Image(OL-2.png,width=500px,nolink,margin: 5)]] == Calling the single geometry seervices from !JavaScript == Now that everything is setup, we can go on and call our OGR ZOO services with !JavaScript. Please add the following lines after the {{{init()}}} function, which will call the single geometry processes. {{{ #!c function simpleProcessing(aProcess) { if (select.features.length == 0) return alert("No feature selected!"); var url = '/zoo/?request=Execute&service=WPS&version=1.0.0&'; if (aProcess == 'Buffer') { var dist = document.getElementById('bufferDist').value; if (isNaN(dist)) return alert("Distance is not a Number!"); url+='Identifier=Buffer&DataInputs=BufferDistance='+dist+'@datatype=interger;InputPolygon=Reference@xlink:href='; } else url += 'Identifier='+aProcess+'&DataInputs=InputPolygon=Reference@xlink:href='; var xlink = control.protocol.url +"?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.0.0"; xlink += '&typename='+control.protocol.featurePrefix; xlink += ':'+control.protocol.featureType; xlink += '&SRS='+control.protocol.srsName; xlink += '&FeatureID='+select.features[0].fid; url += encodeURIComponent(xlink); url += '&RawDataOutput=Result@mimeType=application/json'; var request = new OpenLayers.Request.XMLHttpRequest(); request.open('GET',url,true); request.onreadystatechange = function() { if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) { var GeoJSON = new OpenLayers.Format.GeoJSON(); var features = GeoJSON.read(request.responseText); hover.removeFeatures(hover.features); hover.addFeatures(features); } } request.send(); } }}} Then, some specific buttons must be added in the HTML, in order to be able to call the different processes we just declared. You can add them on top of the map by writing the following lines before the {{{
}}}. {{{ #!xml

Single geometry processing

}}} Save your HTML file again. You should now be able to select a polygon and to launch a Buffer, !ConvexHull, Boundary or Centroid on it by clicking one of the button. The result of the process should appear as GeoJSON layer on the map, in orange color. [[Image(OL-3.png,width=500px,nolink,margin: 5)]] == Calling the multiples geometries processes from !JavaScript == Using the same technique, you can now write a function dedicated to the multiple geometries processes. Please add the following lines after the {{{simpleProcessing()}}} function, we will guide you during the exercise in section 5 on how to create such a function. {{{ #!js function multiProcessing(aProcess) { if (select.features.length == 0 || hover.features.length == 0) return alert("No feature created!"); var url = '/zoo/'; var xlink = control.protocol.url +"?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.0.0"; xlink += '&typename='+control.protocol.featurePrefix; xlink += ':'+control.protocol.featureType; xlink += '&SRS='+control.protocol.srsName; xlink += '&FeatureID='+select.features[0].fid; var GeoJSON = new OpenLayers.Format.GeoJSON(); try { var params = ''; params += ' '+aProcess+''; params += ' '; params += ' '; params += ' InputEntity1'; params += ' '; params += ' '; params += ' '; params += ' InputEntity2'; params += ' '; params += ' '+GeoJSON.write(hover.features[0].geometry)+' '; params += ' '; params += ' '; params += ' '; params += ' '; params += ' '; params += ' Result'; params += ' '; params += ' '; params += ' '; } catch(e) { alert(e); return false; } var request = new OpenLayers.Request.XMLHttpRequest(); request.open('POST',url,true); request.setRequestHeader('Content-Type','text/xml'); request.onreadystatechange = function() { if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) { var GeoJSON = new OpenLayers.Format.GeoJSON(); var features = GeoJSON.read(request.responseText); multi.removeFeatures(multi.features); multi.addFeatures(features); } } request.send(params); } }}} Note that this time we didn't use the GET method to request the ZOO Kernel but a XML POST. We did that because if you use the GET method you will get error due to the HTTP GET method limitation based on the length of your request. Using JSON string representing the geometry will make your request longer. Once you get the functions to call your multiple geometries processes, you' must add some buttons to fire the request call. Here is the HTML code to add to your current {{{zoo-ogr.html}}} file : {{{ #!xml

Multiple geometries processing

}}} Please reload the page. You should then be able to run your multiple geometries services and you should get results displayed in red as shown by the following screenshots : ||[[Image(OL-4.png,width=250px,align=left,margin: 5px)]]||[[Image(OL-5.png,width=250px,align=left,margin: 5px)]]|| ||[[Image(OL-6.png,width=250px,align=left,margin: 5px)]]||[[Image(OL-7.png,width=250px,align=left,margin: 5px)]]|| It seems that something is missing in your Services Provider to get the same results … The multiple geometries Services ! This is what we are going to do together in the next section. It seems that something is missing in your Services Provider to get the same results … The multiple geometries Services ! This is what we are going to do together in the next section. It seems that something is missing in your Services Provider to get the same results … The multiple geometries Services ! This is what we are going to do together in the next section. [wiki:ZooWorkshop/FOSS4GJapan/CreatingOGRBasedWebServices Prev.] | [wiki:ZooWorkshop/FOSS4GJapan WorkShop table of content] | [wiki:ZooWorkshop/FOSS4GJapan/Exercice Next] It seems that something is missing in your Services Provider to get the same results … The multiple geometries Services ! This is what we are going to do together in the next section.