Basic Service Calls

back to geoserver home...

This section below explores some of the basic functionality of the Geoserver. Making basic requests to some of the services on offer. I'll likely do detailed demos on specific services / apis in due course.

WMS GetCapabilities

The GetCapabilities endpoint provides details about the services, metadata and details of the layers available. Note that the response can be quite large and will include a list of all available spatial refrence sysems. This can be addressed by limiting the srs available for the workspace via the admin ui (WMS -> select workspace -> limit srs).

    
payload = {
    "service": "wms",
    "version2": "1.3.0",
    "request": "GetCapabilities",
    "namespace": "xmlns(myprefix=https://fearnought.club/geoserver/sheffield)", 
}

url = "https://fearnought.club/geoserver/fearnought_sheffield/wms"

r = requests.get(url, params=payload)

context['xml_response'] = r.text
       

WMS GetMap

The "GetMap" endpoint returns a pre-rendered map in a variety of outputs (e.g. png, jpeg, pdf). Easy enough, although the bounding box is mandatory so we need to call "GetCapabilities" first and then parse the returned XML in order to the find the bbox values.

"should just take 5 mins - will be like json, maybe even easier" he said, having never worked with XML before.....

Three hours later...ok so we've been on a merry dance looking into XML schemas and hey presto! Got there in the end with a little help from chatGPT - first time it's actually been useful for me. Will take that as a massive W!.

Fun times to parse the xml using a schema.
    
    import xml.etree.ElementTree as ET
    schema = {'wms': 'http://www.opengis.net/wms'}   
    root = ET.fromstring(xml_content)
    layers = root.findall('.//wms:Layer/wms:Layer', schema)
    ## then for each layer find the bboxes..
    bbox_els = layer.findall('wms:BoundingBox', namespaces)
       
The standard request in the view and template snippet
    
    payload = {
        "service": "WMS",
        "version": "1.3.0",
        "request": "GetMap",
        "layers": "fearnought_enschede:geo_enschedeboundary",
        "bbox": "248635.984375,464779.15625,263820.15625,478458.59375",
        "styles": "",
        "width": "300",
        "height": "150",
        "srs": "EPSG:28992",
        "format": "image/png",
    }

    url = "https://fearnought.club/geoserver/fearnought_enschede/wms"
    r = requests.get(url, params=payload)

    context["image"] = b64encode(r.content).decode()

    ## In the template 
    <img src="data:image/png;base64,">
       

WFS GetFeature

In this example the Web Feature Service (WFS) is used. The GetFeature endpoint is called to get the boundary for Enschede. This is transformed to WGS84 to be overlaid on the basemap.

The WMS GetCapabilities returns two sets of bounding box - the native using the spatial reference of the data and the transformed into WG84. This was parsed to get the relevant bbox and then used to set the focus of the map.

    
    payload = {
        "service": "wfs",
        "version2": "2.0.0",
        "request": "GetFeature",
        "typeNames": "fearnought_enschede:geo_enschedeboundary",
        "srsName": "EPSG:4326",
        "outputFormat": "application/json",
    }

    url = "https://fearnought.club/geoserver/fearnought_enschede/wfs"

    # make the request and parse the returned json
    r = requests.get(url, params=payload)
    parsed = json.loads(r.content)

    # setup a map and use bounding box to fit
    m = folium.Map(zoom_start=3)
    m.fit_bounds([[mybbox[1], mybbox[0]], [mybbox[3], mybbox[2]]])

    folium.GeoJson(parsed).add_to(m)
    context["map"] = m._repr_html_()