import requests
import json
# 设置缓存时间
data = json.dumps({
"coverage": {
"metadata": {
"entry": [
{
"@key": "cacheAgeMax",
"$": "1800"
},
{
"@key": "cachingEnabled",
"$": "true"
}
]
}
}
})
resp = requests.put(
f"http://localhost:1231/geoserver/rest/workspaces/seaice/coveragestores/{name}/coverages/{name}.json",
data, headers={'Content-Type': 'application/json'}, auth=('admin', 'geoserver'))
print(resp)
点击查看全部代码
from geoserver.catalog import Catalog
import os
import glob
import requests
import json
def AutoPublicWMSLayer():
"""
Python批量发布WMS服务
"""
for tifFile in glob.glob(f'{tif_out_dir}/*.tif'):
# 示例 ./data/date/Tif/ice_thickness_1020.tif
name = os.path.splitext(os.path.basename(tifFile))[0]
# 我提前在geoserver设置了ice_thickness等样式,这里是根据图层名称取对于样式
style = '_'.join(name.split('_')[:-1])
print(name)
absolute_path = os.path.abspath(tifFile)
cat = Catalog("http://localhost:1231/geoserver/rest/", username="admin", password="geoserver")
cat.create_coveragestore_external_geotiff(name, absolute_path, workspace=cat.get_workspace("seaice"),
overwrite=True)
# 设置默认样式
response = requests.put(f'http://localhost:1231/geoserver/rest/layers/seaice:{name}',
headers={'Content-Type': 'application/xml'},
data=f"<layer><defaultStyle><name>seaice:{style}</name></defaultStyle><queryable>false"
f"</queryable></layer>",
auth=('admin', 'geoserver'))
print(response)
# 设置缓存时间
data = json.dumps({
"coverage": {
"metadata": {
"entry": [
{
"@key": "cacheAgeMax",
"$": "1800"
},
{
"@key": "cachingEnabled",
"$": "true"
}
]
}
}
})
resp = requests.put(
f"http://localhost:1231/geoserver/rest/workspaces/seaice/coveragestores/{name}/coverages/{name}.json",
data, headers={'Content-Type': 'application/json'}, auth=('admin', 'geoserver'))
print(resp)
def AutoDeleteWMSLayer():
"""
Python批量删除WMS服务
"""
for tifFile in glob.glob(f'{tif_out_dir}/*.tif'):
name = os.path.splitext(os.path.basename(tifFile))[0]
try:
url = f"http://localhost:1231/geoserver/rest/workspaces/seaice/coveragestores/{name}.xml?recurse=true"
response = requests.delete(url, auth=('admin', 'geoserver'))
if response.status_code != requests.codes.ok:
raise Exception(response.status_code)
except Exception as e:
print(f"ERROR:{name}>>{e}")
if __name__ == '__main__':
tif_out_dir = '../data/date/Tif'
AutoPublicWMSLayer()
# AutoDeleteWMSLayer()
https://www.osgeo.cn/geoserver-user-manual/rest/index.html#rest
标签:name,WMS,GeoServer,rest,json,API,geoserver,import,path From: https://www.cnblogs.com/echohye/p/17863303.html