gwtopenlayerswmsgwt-openlayers

GWT-OpenLayers and OpenLayers.Format.WMSCapabilities


I am working with the Google Web Toolkit wrapper for OpenLayers. I'm attempting to add a WMS layer to a map, but I need to parse a Capabilities document in order to get the available layer names. I see that a WMSCapabilities class is available in OpenLayers http://dev.openlayers.org/releases/OpenLayers-2.12/doc/apidocs/files/OpenLayers/Format/WMSCapabilities-js.html, but I can't seem to find the implementation in GWT. Is this feature not yet available, or is it hiding, undocumented somewhere? Thanks in advance!


Solution

  • I still don't know if the GWT implementation is available, but it's actually rather easy to wrap native javascript code in Java. Here is my solution:

    import com.google.gwt.core.client.JsArrayString;
    
    native JsArrayString getLayerNames(String capDoc) /*-{
        var toReturn = [];
        var parser = new $wnd.OpenLayers.Format.WMSCapabilities();
        var doc = parser.read(capDoc);
        for (var i in doc.capability["layers"]) {
            toReturn.push(doc.capability["layers"][i].name);
        }
        return toReturn;
    }-*/;
    

    You can then access them using:

    JsArrayString layers = getLayerNames(getMyCapabilitiesDocumentAsString());
    for (int i = 0; i < layers.length(); i++) {
        Window.alert("A layer name is " + layers.get(i));
    }
    

    The variable doc is a javascript array containing the entire contents of the capabilities document, so it's possible to access more than just the layer names; simply pull out what you need. Also, it's probably better to create a single parser rather than create a new one each time the method is called, but that's a different exercise ;)