Jan
Jan

Reputation: 11

How can I set the default style of a layer using the geoserver restful API in Python?

I am creating a layer using the restful API of geoserver. This is done by the following code:

    application_json = {
        "featureType": {
            "name": store_name,  # Set your desired layer name here
            "nativeName": store_name,  # Set the same name as above
            "title": f"Surface Water Depth ({self.scenario.id})",  # Set your desired title here
            "abstract": f"Surface Water Depth for scenario {self.scenario.id}",  # Set your desired abstract here
            "enabled": "true",
            "srs": "EPSG:32632",
            "nativeBoundingBox": {
                "minx": total_bounds[0],
                "maxx": total_bounds[2],
                "miny": total_bounds[1],
                "maxy": total_bounds[3]
            },
            "latLonBoundingBox": {
                "minx": minlon,
                "maxx": maxlon,
                "miny": minlat,
                "maxy": maxlat
            },
            "defaultStyle": "hazard_class_1"
        }
    }
    response = requests.post(feature_type_url, auth=HTTPBasicAuth(self.username, self.password), json=application_json)

Everything except for stting the default style works fine. In the geoserver-log, I cannot find any hints. The default style is still set to polygon. When I change the style manually in geoserver, everything is as desired.

I read the documentation for the API but did not find any hints there. Also, I reviewed the log and it did not throw any errors or warnings

Upvotes: 0

Views: 319

Answers (1)

Jan
Jan

Reputation: 11

I finally figured out how it works. I created the layer with a post to the URL /workspaces/{workspaceName}/datastores/{storeName}/featuretypes/{featureTypeName}. Afterwards, I change the default style with a put to /workspaces/{workspaceName}/layers/{layerName}.

Here is the code for the put request:

layer_url = f"{self.workspace_url}/layers/{store_name}"
update_style_json = {
    "layer": {
        "defaultStyle": {
            "name": "water_level_1"
        }
    }
}

response = requests.put(
    layer_url,
    auth=HTTPBasicAuth(self.username, self.password),
    json=update_style_json,
    headers=headers
)

Upvotes: 1

Related Questions