9

I have strict requirement to retrieve names of all layers in a GeoServer store using OpenLayers. I've gone through the post here, but it doesn't answer my question. I've also gone through GeoServer's REST API, which would be ideal if I'm using another language, say C#, Java, or PHP. But I want to do this entirely in JavaScript.

UPDATE: From what I can see in the WFS GetCapabilities document, I can retrieve names of all layers by sending a WFS GetCapabilities request using filters. But I'm not sure if its possible to incorporate filters in a GetCapabilities request. Neither do I have any idea how such a filter will look like.

Any advice on how I can go about achieving this will be highly appreciated. Any alternative approach will also be appreciated.

UPDATE:

I believe I've found a solution here; "WFS GetCapabilities requests can be filtered to only return layers corresponding to a particular namespace. To do this, add the following code to your request: namespace=<namespace>". I hope this assists someone else.

  • An extended question. The method you mention only return xml of already published layers and other published information. Is there any way to return xml information of unpublished/pending layers? I have try wms/wfs/wcs/REST and even python package gsconfig and none of them would do this. – swap0 Aug 12 '14 at 14:50
18

I notice you have already found a solution that works for you, but I thought I might add some additional options that you or others may find useful.

In GeoServer there is a capability known as Virtual OWS Services. The WMS, WFS, and WCS services are collectively known as the OWS services. When you make a request to one of these services you are making a global request, so all registered layers are returned in the capabilities document (unless you have data security settings). By contrast, a virtual service is a filtered view of the global service. The filtering is done on workspaces and is accessed through a slightly different url.

Lets take an example. Suppose you have a workspace called myws and it is this workspace that you would like to list layers for. In that case you could make a request to the url:

http://www.yourgeoserver.com/geoserver/myws/ows?SERVICE=WFS&REQUEST=GetCapabilities

The returned capabilities document will only list the layers registered against the myws workspace.

That is one approach, however you had originally asked about accessing the layers list through JavaScript. Happily there is a way to do this using the GeoServer REST API. You can get a list of featuretypes for a given workspace and datastore using something like:

http://www.yourgeoserver.com/geoserver/rest/workspaces/myws/featuretypes.json

This will return you a JSON object listing all of the featuretypes within the workspace, you can also substitute .json for .xml or .html to get the response in those formats. So, how to do this in OpenLayers? Fortunately OpenLayers provides an encapsulation of the XMLHttpRequest object in the form of OpenLayers.Request object. A simple example using the REST url above would be:

var request = OpenLayers.Request.GET({
  url: "http://www.yourgeoserver.com/geoserver/rest/workspaces/myws/featuretypes.json",
  callback: function(request) {
    // Code here to handle the response, the request object contains the data
  }
});

There is a good document on requesting remote data with OpenLayers here.

Hope that gives you some other approaches to consider.

  • I've tested each and every suggestion in this post, @CHenderson. They work perfectly. Thanks a lot. This was extremely helpful. – okello Jul 24 '12 at 11:51
  • No problem, it's a pleasure to be able to help! – CHenderson Jul 24 '12 at 18:29
  • That virtual OWS services link is dead now. Can be found here – wchatx May 7 '16 at 13:26
  • works like a charm – AhammadaliPK Apr 4 '18 at 4:58
1

Try to check out this answer. it also gives a demo. Dont forget that layers serve via various services as it mentioned there.

i hope it helps you...

  • I've checked it out. It's in line with my thought that I can achieve this by using WFS GetCapabilities request. But I only want to retrieve names of layers, nothing else. This means I have to incorporate filters in my request. But I still don't have any idea on how I can append filters to a WFS GetCapabilties request that would return only the names of the layers. Refer to my update above. Any pointers? – okello Jul 23 '12 at 8:24
1

This answer might help someone. I wanted to build a list of my layers automatically using Javascript and Leaflet. Using CHenderson answer, I made the following code:



    url = "https://YOUSERVER/geoserver/STORE/ows?service=wfs&version=2.0.0&request=GetCapabilities"
    layerList = [];
    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        success: function(xml) {
            $(xml).find('FeatureType').each(function(){
                name = $(this).find("Name").text();
                title = $(this).find("Title").text();
                group = "NO_GROUP";
                $(this).find('ows\\:Keywords').each(function(){
                    keyword = $(this).find('ows\\:Keyword').text();
                    if(keyword.indexOf("group:")!=-1)
                    {
                        group=keyword.split(":")[1];
                        return false;
                    }
                });
                layerList.push({"name":name,"title":title,"group":group});          
            });
        }
    });

This way, I can add a keyword "group:GEOGRAPHY"enter image description here on the Geoserver layer and I don´t have to manually code each layer and its group. The name is used to create the layer (maybe using L.WMS.source) and the title is the html label.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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