...
Code Block |
---|
|
coveragestore = 'st'
r_create_coveragestore = requests.post('http://localhost:8090/geoserver/rest/workspaces/ws/coveragestores?configure=all',
auth=('admin', 'geoserver'),
data='<coverageStore><name>' + coveragestore + '</name><workspace>' + workspace + "</workspace><enabled>true</enabled><type>NetCDF</type><url>" + "d:\\YOURPATH\\data.nc" + '</url></coverageStore>',
headers=headers_xml) |
Adding a layer
GeoServer User interface
Adding a layer with GeoServer interface does not differ form adding any other type of layer. You can find a tutorial here.
RESTful interface
Before creating a layer, the .nc
file has to be zipped so that 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.
Code Block |
---|
import zipfile
# zip the nc file into a zip
zfile = 'd:\\YOURPATH\\data.zip'
output = zipfile.ZipFile(zfile, 'w')
output.write('d:\\YOURPATH\\data.nc', coveragestore + '.nc', zipfile.ZIP_DEFLATED )
output.close()
|
Code Block |
---|
|
curl -u admin:geoserver -XPUT -H "Content-type:application/zip" --data-binary @d:\\YOURPATH\\data.zip http://localhost:8090/geoserver/rest/workspaces/ws/coveragestores/st/file.netcdf
|
Code Block |
---|
|
with open(output.filename, 'rb') as zip_file:
r_create_create_layer = requests.put("http://localhost:8090/geoserver/rest/workspaces/" + workspace + "/coveragestores/" + coveragestore + "/file.netcdf",
auth=('admin', 'geoserver'),
data=zip_file,
headers=headers_zip) |
At the end, remember to delete the zip file created for Web transfer.
Code Block |
---|
import zipfile
# deletes the zip created
os.remove(zfile)
|
Performing actions using RESTful API
If you are using GeoServer interface, the official manual describes very well how to modify layers, change names, or assign styles. As Restful interface might result harder to use, here are some examples.
Change layer name
Changing name to a layer can become convenient in case you have many NetCDF files with different names, though NetCDF variables have the exact same name, e.g. Band1
. Say you want to change Band1
into newBand
.
Code Block |
---|
|
curl -v -u admin:geoserver -XPUT -H "Content-type: text/xml" -d "<coverage><name>newBand</name><enabled>true</enabled></coverage>" "http://localhost:8090/geoserver/rest/workspaces/ws/coveragestores/st/coverages/Band1"
|
Code Block |
---|
|
old_name_layer = "Band1"
new_name_layer = "newBand"
r_change_name = requests.put('http://localhost:8090/geoserver/rest/workspaces/' + workspace + "/coveragestores/" + coveragestore + "/coverages/" + old_name_layer,
auth=('admin', 'geoserver'),
data='<coverage><name>' + new_name_layer + '</name><enabled>true</enabled></coverage>',
headers=headers_xml) |
Create and assign a style to a layer
Create a new style, upload it to GeoServer and assign to a workspace:layer
definition.
Code Block |
---|
|
stylename = 'test'
stylefilename = stylename + '.sld'
styleallname = 'd:\\ROURPATH\\styles\\' + stylefilename
# creates new style
r_create_new_style = requests.post("http://localhost:8090/geoserver/rest/styles",
auth=('admin', 'geoserver'),
data='<style><name>' + stylename + '</name><filename>' + stylefilename + '</filename></style>',
headers=headers_xml)
# upload new style
with open(styleallname, 'rb') as sld_file:
r_upload_new_style = requests.put("http://localhost:8090/geoserver/rest/styles/" + stylename,
auth=('admin', 'geoserver'),
data=sld_file,
headers=headers_sld)
# assign it to a layer
r_assign_new_style = requests.put("http://localhost:8090/geoserver/rest/layers/"+ workspace + ':' + new_name_layer,
auth=('admin', 'geoserver'),
data='<layer><defaultStyle><name>' + stylename + '</name></defaultStyle></layer>',
headers=headers_xml)
|
ToBeContinued.