1

How can I get a list of all layers deployed in Geoserver separated by Vector/Raster? Similar to how Geoserver itself does this when one uses the WPS Request Builder (it then shows a complete list of all available Vector or Raster layers - depending on the service one wants to use).

I can only get a list of all layers when I know the workspace (the name of the WMS) they are running in by calling the capabilities of the WMS. I want to be able to recieve the workspave and the name of the layers.

Edit: I just found out one can use the rest API. Thats a nice thing. I can get all layer names by calling http://localhost:8080/geoserver/rest/layers.json but how can I than seperate those by Vector/Raster?

  • Do you plan to work with your own GeoServer or with any GeoServer in the web? – user30184 Nov 30 '18 at 7:45
  • No with my own Geoserver. Maybe on the same machine but maybe in my LAN. Not decided yet – Leo.1906 Nov 30 '18 at 12:41
1

So this is my final code using ajax and the REST api. Works good so far.

var layerList = [];
var loginInfo = ["admin", "geoserver"];
var geoserverURL = "http://127.0.0.1/geoserver";
var raslist = document.getElementById("rasList");
var veclist = document.getElementById("vecList");


$.ajax({
    url: geoserverURL + '/rest/layers.json',
    type: 'GET',
    dataType: 'json',
    contentType: "application/json",
    beforeSend: function(xhr) {
         xhr.setRequestHeader ("Authorization", "Basic " + btoa(loginInfo[0] + ":" + loginInfo[1]));
    },
    success: function(data){
        for (var i = 0; i < data.layers.layer.length; i++) {
            layerList.push([data.layers.layer[i].name, data.layers.layer[i].href]);
        }
    },
    async: false
});

for (var i = 0; i < layerList.length; i++) {
    getType(layerList[i][1], i)
}

// wait for all ajax elements to stop to proceed with the COMPLETED array - otherwise the asynchronus style of ajax will destroy it all ..
$(document).ajaxStop(function() {
    for (var i = 0; i < layerList.length; i++) {
        var option = document.createElement("option");
        //option.setAttribute("value", "test" + i);
        option.text = layerList[i][0];
        if (layerList[i][1] == "RASTER") {
            raslist.appendChild(option);
        } else {
            veclist.appendChild(option);
        }
    }
});


function getType(url, i) {
    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'json',
        contentType: "application/json",
        beforeSend: function(xhr) {
             xhr.setRequestHeader ("Authorization", "Basic " + btoa(loginInfo[0] + ":" + loginInfo[1]));
        },
        success: function(data){
            layerList[i][1] = data.layer.type;
        }
    });
}

rasList and vecList are defined in html of course. This is only the js file to be included in the html.

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.