Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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

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...

share|improve this answer
    
Thanks @Aragon. I'll check it out right away. –  okello Jul 23 '12 at 8:19
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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