When you want to connect to a WMS server and visualize data, you need to know which data sets are available.
First, you need to create a
TLcdWMSClient
instance:
String serverURL = "https://sampleservices.luciad.com/wms";
TLcdWMSClient wmsClient = TLcdWMSClient.createWMSClient(new URI(serverURL));
The client offers API to perform a GetCapabilities
request:
ALcdOGCWMSCapabilities capabilities = wmsClient.getCachedCapabilities();
The return value is a Java object representing the capabilities exposed by the server.
If we now loop over the capabilities, we can collect all available WMS layers:
List<String> allAvailableLayerNames = new ArrayList<>();
//On a WMS server, the layers are stored in a hierarchy
//Loop over the hierarchy to collect them all
for (int i = 0; i < capabilities.getWMSRootNamedLayerCount(); i++) {
ALcdWMSNamedLayer rootNamedLayer = capabilities.getWMSRootNamedLayer(i);
allAvailableLayerNames.addAll(getAvailableLayerNames(rootNamedLayer));
}
Because a WMS server stores the layers in a hierarchical structure, we use recursion to implement the getAvailableLayerNames
method:
private static List<String> getAvailableLayerNames(ALcdWMSNamedLayer aWMSNamedLayer) {
List<String> result = new ArrayList<>();
//Add the current layer
String layerName = aWMSNamedLayer.getNamedLayerName();
if (layerName != null) {
result.add(layerName);
}
//Use recursion to add all the child layers
for (int i = 0; i < aWMSNamedLayer.getChildWMSNamedLayerCount(); i++) {
result.addAll(getAvailableLayerNames(aWMSNamedLayer.getChildWMSNamedLayer(i)));
}
return result;
}
Find below a runnable example which lists the available data sets on our sample server:
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import com.luciad.wms.client.model.ALcdOGCWMSCapabilities;
import com.luciad.wms.client.model.ALcdWMSNamedLayer;
import com.luciad.wms.client.model.TLcdWMSClient;
class FindAvailableDataTutorial {
public static void main(String[] args) throws Exception {
String serverURL = "https://sampleservices.luciad.com/wms";
TLcdWMSClient wmsClient = TLcdWMSClient.createWMSClient(new URI(serverURL));
ALcdOGCWMSCapabilities capabilities = wmsClient.getCachedCapabilities();
List<String> allAvailableLayerNames = new ArrayList<>();
//On a WMS server, the layers are stored in a hierarchy
//Loop over the hierarchy to collect them all
for (int i = 0; i < capabilities.getWMSRootNamedLayerCount(); i++) {
ALcdWMSNamedLayer rootNamedLayer = capabilities.getWMSRootNamedLayer(i);
allAvailableLayerNames.addAll(getAvailableLayerNames(rootNamedLayer));
}
System.out.println("Available WMS layers on " + serverURL + ": \n");
allAvailableLayerNames.stream()
.sorted()
.forEach(System.out::println);
}
private static List<String> getAvailableLayerNames(ALcdWMSNamedLayer aWMSNamedLayer) {
List<String> result = new ArrayList<>();
//Add the current layer
String layerName = aWMSNamedLayer.getNamedLayerName();
if (layerName != null) {
result.add(layerName);
}
//Use recursion to add all the child layers
for (int i = 0; i < aWMSNamedLayer.getChildWMSNamedLayerCount(); i++) {
result.addAll(getAvailableLayerNames(aWMSNamedLayer.getChildWMSNamedLayer(i)));
}
return result;
}
}