Created
January 5, 2024 17:08
-
-
Save bennyistanto/db84c6b0bc5f704b648bd4f9a8e75f0a to your computer and use it in GitHub Desktop.
Geoserver batch upload for netCDF and SLD file in a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<StyledLayerDescriptor version="1.0.0" | |
xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" | |
xmlns="http://www.opengis.net/sld" | |
xmlns:ogc="http://www.opengis.net/ogc" | |
xmlns:xlink="http://www.w3.org/1999/xlink" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<NamedLayer> | |
<Name>ERA5Land_Temperature</Name> | |
<UserStyle> | |
<Title>Global 2m Mean Temperature</Title> | |
<FeatureTypeStyle> | |
<Rule> | |
<RasterSymbolizer> | |
<ChannelSelection> | |
<GrayChannel> | |
<SourceChannelName>tmean</SourceChannelName> <!-- Replace with your specific band name --> | |
</GrayChannel> | |
</ChannelSelection> | |
<ColorMap type="intervals"> | |
<ColorMapEntry color="#313695" quantity="-50" label="below -30°C"/> | |
<ColorMapEntry color="#4575b4" quantity="-30" label="-30°C to -15°C"/> | |
<ColorMapEntry color="#74add1" quantity="-15" label="-15°C to 0°C"/> | |
<ColorMapEntry color="#abd9e9" quantity="0" label="0°C to 15°C"/> | |
<ColorMapEntry color="#ffffbf" quantity="15" label="15°C to 30°C"/> | |
<ColorMapEntry color="#fee090" quantity="30" label="30°C to 40°C"/> | |
<ColorMapEntry color="#fdae61" quantity="40" label="40°C to 45°C"/> | |
<ColorMapEntry color="#f46d43" quantity="45" label="45°C to 50°C"/> | |
<ColorMapEntry color="#d73027" quantity="50" label="above 50°C"/> | |
</ColorMap> | |
</RasterSymbolizer> | |
</Rule> | |
</FeatureTypeStyle> | |
</UserStyle> | |
</NamedLayer> | |
</StyledLayerDescriptor> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from requests.auth import HTTPBasicAuth | |
import os | |
# GeoServer details | |
geoserver_url = "http://localhost:8080/geoserver" | |
workspace = "your_workspace" | |
username = "admin" | |
password = "geoserver" | |
data_directory = "path_to_your_data_folder" # Folder containing .nc and .sld files | |
def upload_nc_file(nc_file, store_name): | |
rest_url = f"{geoserver_url}/rest/workspaces/{workspace}/coveragestores/{store_name}/file.nc?configure=first&coverageName={store_name}" | |
headers = {"Content-type": "application/x-netcdf"} | |
with open(nc_file, 'rb') as data: | |
response = requests.put(rest_url, headers=headers, data=data, auth=HTTPBasicAuth(username, password)) | |
if response.status_code == 201: | |
print(f"NetCDF file {nc_file} uploaded successfully") | |
else: | |
print(f"Error uploading NetCDF file {nc_file}: {response.content}") | |
def upload_sld(sld_file, style_name): | |
sld_url = f"{geoserver_url}/rest/styles" | |
sld_headers = {"Content-type": "application/vnd.ogc.sld+xml"} | |
sld_data = f"<style><name>{style_name}</name><filename>{sld_file}</filename></style>" | |
response = requests.post(sld_url, headers=sld_headers, data=sld_data, auth=HTTPBasicAuth(username, password)) | |
if response.status_code == 201: | |
with open(sld_file, 'r') as file: | |
sld_content = file.read() | |
style_url = f"{geoserver_url}/rest/styles/{style_name}" | |
response = requests.put(style_url, headers=sld_headers, data=sld_content, auth=HTTPBasicAuth(username, password)) | |
if response.status_code == 200: | |
print(f"SLD file {sld_file} uploaded successfully") | |
else: | |
print(f"Error uploading SLD file {sld_file}: {response.content}") | |
else: | |
print(f"Error creating SLD style for {sld_file}: {response.content}") | |
def associate_style(layer_name, style_name): | |
layer_url = f"{geoserver_url}/rest/layers/{workspace}:{layer_name}" | |
xml = f"<layer><defaultStyle><name>{style_name}</name></defaultStyle></layer>" | |
headers = {"Content-type": "text/xml"} | |
response = requests.put(layer_url, headers=headers, data=xml, auth=HTTPBasicAuth(username, password)) | |
if response.status_code == 200: | |
print(f"Style {style_name} associated with NetCDF layer {layer_name} successfully") | |
else: | |
print(f"Error associating style {style_name}: {response.content}") | |
# Scan the directory for NetCDF files and process each one | |
for file in os.listdir(data_directory): | |
if file.endswith(".nc"): | |
nc_file = os.path.join(data_directory, file) | |
sld_file = os.path.join(data_directory, file.replace(".nc", ".sld")) | |
store_name = file[:-3] # Remove .nc extension for store name | |
if os.path.exists(sld_file): | |
upload_nc_file(nc_file, store_name) | |
upload_sld(sld_file, store_name) | |
associate_style(store_name, store_name) | |
else: | |
print(f"No matching SLD file found for {nc_file}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment