Date: Tue, 19 Mar 2024 13:09:37 +0100 (CET) Message-ID: <645097780.175.1710850177398@v-public003.directory.intra> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_174_207176194.1710850177397" ------=_Part_174_207176194.1710850177397 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
GeoServer is an open source server for sharing geospatial da= ta. It is often used to publish shapefiles and PostGIS tables (Vector data)= , ArcGrid and GeoTiff (Raster data). Moreover, it also allows for configura= tion of (cascading) remote WMS. Deltares maintains its own instance of GeoS= erver here. Data layers from GeoServer can be= exposed in via WMS/WFS in many standard formats for subsequent public use.= As NetCDF data format is widely used by the scientific community, ingestin= g that data through GeoServer tools represents a valid and useful alternati= ve of data dissemination. Additional information on NetCDF data format can = be found here.
This tutorial refers to GeoServer <stable> version 2.8.2 on Window=
s. Since the stable version of GeoServer does not allow ingestion of NetCDF=
as input Coverage Format, Geoserver Extension can be installed from <=
a class=3D"external-link" href=3D"http://ares.boundlessgeo.com/geoserver/" =
rel=3D"nofollow">http://ares.boundlessgeo.com/geoserver/ (Select c=
orresponding extension version to Geoserver version downloaded on machine).=
Once the extension has been downloaded, the .jar files have to be cop=
ied to GeoServer 2.8.2\webapps\geoserver\WEB-INF\lib\
. For any=
of the following steps, the easiest way to work with NetCDF Coverage input=
is to use the user interface provided by GeoServer installation. However, =
for reasons of efficiency and process automation a client might prefer to u=
se the REST API GeoServer provides. =
The REST configuration allows you to work with bindings from various progra=
mming languages or simply with cURL command line tool. Python bindings =
are also available from gsconfig.py, but there is no=
implementation of working with NetCDF files yet.
This is the first step before adding any resource, so that your project = is kept tidy and structured. That has to be done prior to the store creatio= n for our NetCDF resources.
First of all, create and add a workspace.
Add a workspace called ws
.
import = os from geoserver.catalog import Catalog geoserverRestURL =3D "http://localhost:8090/geoserver/rest/" username =3D "admin"=20 password =3D "geoserver" workspace =3D 'ws' cat =3D Catalog(geoserverRestURL, username, password) ws =3D cat.create_workspace(workspace, workspace) # # delete workspace # ws =3D cat.get_workspace(workspace) # cat.delete(ws)
curl -u= admin:geoserver -v -POST -H "Content-type: text/xml" -d "<workspace>= <name>ws</name></workspace>" "http://localhost:8090/geose= rver/rest/workspaces"
import = os import requests workspace =3D 'ws' headers_xml =3D {'content-type': 'text/xml'} headers_zip =3D {'content-type': 'application/zip'} headers_sld =3D {'content-type': 'application/vnd.ogc.sld+xml'} r_create_workspace =3D requests.post("http://localhost:8090/geoserver/rest/= workspaces",=20 auth=3D('admin', 'geoserver'),=20 data=3D'<workspace><name>' + workspace + '</name><= /workspace>', headers=3Dheaders_xml)
At the moment of writing this tutorial, the NetCDF plugin supports datas= ets where each variable=E2=80=99s axis is identified by an independent coor= dinate variable, therefore two dimensional non-independent latitude-longitu= de coordinate variables aren=E2=80=99t currently supported.
Once the workspace is created, you can add a NetCDF data store by follow= ing instructions and requirements on this page.
Add a store called st
. If you have a GeoTIFF, using Python =
bindings, cat.create_coveragestore()
is sufficient. As Python =
bindings do not support adding NetCDF data stores yet, cURL commands and Py=
thon requests are shown only.
REM Coo= rdinate reference system CRS can be added as well, although not compulsory,= under nativeCRS tags. Example: ...<nativeCRS>GEOGCS...</nativeCRS= ><srs>EPSG:4326</srs>... curl -u admin:geoserver -v -POST -H "Content-type: text/xml" -d "<covera= geStore><name>st</name><workspace>ws</workspace>= <enabled>true</enable><type>NetCDF</type><url>= ;d:\\YOURPATH\\data.nc</url></coverageStore>" "http://localhost= :8090/geoserver/rest/workspaces/ws/coveragestores?configure=3Dall"
coverag= estore =3D 'st' r_create_coveragestore =3D requests.post('http://localhost:8090/geoserver/r= est/workspaces/ws/coveragestores?configure=3Dall',=20 auth=3D('admin', 'geoserver'),=20 data=3D'<coverageStore><name>' + coveragestore + '</name= ><workspace>' + workspace + "</workspace><enabled>true= </enabled><type>NetCDF</type><url>" + "d:\\YOURPATH= \\data.nc" + '</url></coverageStore>', headers=3Dheaders_xml)
Adding a layer with GeoServer interface does not differ form adding any = other type of layer. You can find a tutorial here.
Before creating a layer, the .nc
file has to be zipped so t=
hat it can be transferred to a Web server via application/zip
=
content type. The zip file will be opened in a folder inside the GeoServer =
data folder. Ancillary files will be created in that exact folder. Later on=
, the zip file can be deleted.
import = zipfile # zip the nc file into a zip zfile =3D 'd:\\YOURPATH\\data.zip' output =3D zipfile.ZipFile(zfile, 'w') output.write('d:\\YOURPATH\\data.nc', coveragestore + '.nc', zipfile.ZIP_DE= FLATED ) output.close()
curl -u= admin:geoserver -XPUT -H "Content-type:application/zip" --data-binary @d:\= \YOURPATH\\data.zip http://localhost:8090/geoserver/rest/workspaces/ws/cove= ragestores/st/file.netcdf
with op= en(output.filename, 'rb') as zip_file: r_create_layer =3D requests.put("http://localhost:8090/geoserver/rest/w= orkspaces/" + workspace + "/coveragestores/" + coveragestore + "/file.net= cdf",=20 auth=3D('admin', 'geoserver'),=20 data=3Dzip_file, headers=3Dheaders_zip)
At the end, remember to delete the zip file created for Web transfer.
import = zipfile # deletes the zip created os.remove(zfile)
If you are using GeoServer interface, the official manual describes very= well how to modify layers, change names, or assign styles. As Restful inte= rface might result harder to use, here are some examples.
Changing name to a layer can become convenient in case you have many Net=
CDF files with different names, though NetCDF variables have the exact same=
name, e.g. Band1
. Say you want to change Band1 into
newBand
.
curl -v= -u admin:geoserver -XPUT -H "Content-type: text/xml" -d "<coverage>&= lt;name>newBand</name><enabled>true</enabled></cove= rage>" "http://localhost:8090/geoserver/rest/workspaces/ws/coveragestore= s/st/coverages/Band1"
old_nam= e_layer =3D "Band1" new_name_layer =3D "newBand" r_change_name =3D requests.put('http://localhost:8090/geoserver/rest/worksp= aces/' + workspace + "/coveragestores/" + coveragestore + "/coverages/" + o= ld_name_layer,=20 auth=3D('admin', 'geoserver'),=20 data=3D'<coverage><name>' + new_name_layer + '</name>= <enabled>true</enabled></coverage>', headers=3Dheaders_xml)
Create a new style, upload it to GeoServer and assign to a workspa=
ce:layer
definition.
stylena= me =3D 'test' stylefilename =3D stylename + '.sld' styleallname =3D 'd:\\ROURPATH\\styles\\' + stylefilename # creates new style r_create_new_style =3D requests.post("http://localhost:8090/geoserver/rest/= styles",=20 auth=3D('admin', 'geoserver'),=20 data=3D'<style><name>' + stylename + '</name><file= name>' + stylefilename + '</filename></style>', headers=3Dheaders_xml) # upload new style with open(styleallname, 'rb') as sld_file: r_upload_new_style =3D requests.put("http://localhost:8090/geoserver/re= st/styles/" + stylename,=20 auth=3D('admin', 'geoserver'),=20 data=3Dsld_file, headers=3Dheaders_sld) # assign it to a layer r_assign_new_style =3D requests.put("http://localhost:8090/geoserver/rest/l= ayers/"+ workspace + ':' + new_name_layer,=20 auth=3D('admin', 'geoserver'),=20 data=3D'<layer><defaultStyle><name>' + stylename + '&= lt;/name></defaultStyle></layer>', headers=3Dheaders_xml)