Hey there,
I am back from my ‘summer hibernation’ with a tutorial on the Geoserver REST API. This API is very useful if you want to upload large amounts of data into your geoserver at once. Of course you could use the Geoserver UI, but this allows you only to add one layer at a time. Here the REST API comes into play:
GeoServer’s graphical administrative tools offer a convenient UI for managing data as workspaces, stores, layers and styles but will only handle them one at a time. If you have a significant amount of data to upload into GeoServer anew, or migrate from an existing implementation from another vendor, one-at-a-time just won’t cut it.
(from boundless.com)
Here is a working example on how to add a new workspace called “myworkingdir” to your geoserver programmatically using the REST API from your terminal/shell:
> curl -u admin:geoserver -v -XPOST -H 'Content-type:text/xml' > -d 'myworkingdir' > http://localhost:8080/geoserver/rest/workspaces
Wrap it up in R
I like to use R for the processing of my spatial data. Switching between software and different windows is often annyoing and slows down the workflow… so I thought to myself that it would be nice, if I could upload layers to geoserver, create workspaces and datastores directly from inside R. The results are four very simple R functions that allow you to handle basic geoserver tasks form inside R:
- Add new workspace: createWorkspace()
- Add new datastore: createDatastore()
- Add new layer: createLayer()
- Apply style: applyStyle()
You can download all functions from my github repository.
Example
Here is how the code works: First we need to specify some parameters: Username, password, the url to your geoserver etc…
#Specify parameters user <- "admin" #username password <- "geoserver" #password geoserver.url <- "http://localhost:8080/geoserver/" #url to geoserver (http://<your.geoserver.url>:<port>/geoserver/) workspace.name <- "myworkspace" #workspace where Tifs are going to be uploaded img.path <- "/Users/martin/Downloads/img.tif" #tif file path img.name <- sub(".tif", "", basename(img.path)) #tif name
If we for example want to create a new datastore form inside R, here is how the function looks like. Simply plug in your parameters above and run the code below. Voila, a new datastore was added to your geoserver.
Create Datastore function
#Function definition createDatastore <- function(user, password, workspace.name, img.path, img.name, geoserver.url) { system(paste('curl -v -u ', user,':',password, ' -XPOST -H "Content-type: text/xml" ', '-d "<coverageStore><name>',img.name,'</name>', '<workspace>',workspace.name,'</workspace>', '<enabled>true</enabled>', '<type>GeoTIFF</type>', '<url>',img.path,'</url></coverageStore>" ', '"', geoserver.url,'rest/workspaces/',workspace.name,'/coveragestores?configure=all"', sep="")) } #Execute function createDatastore(user = user, password = password, workspace.name = workspace.name, img.path = img.path, img.name = img.name, geoserver.url = geoserver.url )
I hope this was useful to some of you. If you have any questions, please leave me a comment below.
Cheers
Martin
1 Comment
You can post comments in this post.
Hi,
Can you please help me to upload several layers (tif files) in geoserver?
Harikrishnan k.v 3 years ago
Post A Reply