-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs_publish_layers_rest.py
128 lines (104 loc) · 4.08 KB
/
gs_publish_layers_rest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
# Copyright notice
# --------------------------------------------------------------------
# Copyright (C) 2024 Deltares
# Ioanna Micha
#
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------
#
# This tool is part of <a href="http://www.OpenEarth.eu">OpenEarthTools</a>.
# OpenEarthTools is an online collaboration to share and manage data and
# programming tools in an open source, version controlled environment.
# Sign up to recieve regular updates of this function, and to contribute
# your own tools.
# $HeadURL$
# $Keywords: $
import requests
from requests.auth import HTTPBasicAuth
import json
# Configuration
geoserver_url = "https://coclico.avi.deltares.nl/geoserver/rest"
auth = HTTPBasicAuth('admin', 'urx7L3ecZE27-M2k2WNuj7yzmxq-8k')
workspace = "slr"
style_name = "slr_style"
#1. provide the collection.json
f_collection = open('slp_collection.json')
collection = json.load(f_collection)
href_items = [item["href"] for item in collection["links"] if item["rel"] == "item"]
# Define the desired prefix
prefix = 'file:///opt/coclico-data-public/coclico/slp/'
layer_names = []
for item in href_items:
# Remove the '._items/' prefix
layer_name = item.replace('./items/', '')
# Replace '/' with '_'
layer_name = layer_name.replace('/', '_')
# Remove the file extension
layer_name = layer_name.replace('.json', '')
layer_names.append(f"slr:{layer_name}")
""" # Serializing json
json_object = json.dumps({"layers": layer_names}, indent=4)
# Writing to sample.json
with open("sample.json", "w") as outfile:
outfile.write(json_object) """
store_name = layer_name
gtif_path = item.replace('./items/', prefix).replace('.json', '.tif')
# Headers for XML data
headers = {
"Content-type": "text/xml"
}
# Step 1: Create the Coverage Store
data_store_url = f"{geoserver_url}/workspaces/{workspace}/coveragestores"
data_store_data = f"""
<coverageStore>
<name>{store_name}</name>
<workspace>{workspace}</workspace>
<type>GeoTIFF</type>
<url>{gtif_path}</url>
<enabled>true</enabled>
</coverageStore>
"""
response = requests.post(data_store_url, auth=auth, headers=headers, data=data_store_data)
if response.status_code == 201:
print("Store created successfully")
else:
print(f"Failed to create store: {response.content}")
# Step 2: Publish the Layer using PUT
publish_url = f"{geoserver_url}/workspaces/{workspace}/coveragestores/{store_name}/external.geotiff"
publish_params = {
"configure": "first",
"coverageName": layer_name
}
response = requests.put(publish_url, auth=auth, headers={"Content-type": "text/plain"}, data=gtif_path, params=publish_params)
if response.status_code == 201:
print(f"Layer: {workspace}:{layer_name} published successfully")
else:
print(f"Failed to publish layer: {response.content}")
# Step 3: Assign the Style to the Layer
layer_url = f"{geoserver_url}/layers/{workspace}:{layer_name}"
style_data = f"""
<layer>
<defaultStyle>
<name>{style_name}</name>
</defaultStyle>
</layer>
"""
response = requests.put(layer_url, auth=auth, headers=headers, data=style_data)
if response.status_code == 200:
print("Style assigned successfully")
else:
print(f"Failed to assign style: {response.content}")