Geoserver REST API via R

geoserver logo

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.


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.


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/) <- "myworkspace" #workspace where Tifs are going to be uploaded

img.path       <- "/Users/martin/Downloads/img.tif" #tif file path       <- 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,, img.path,, geoserver.url) {
  system(paste('curl -v -u ', user,':',password, ' -XPOST -H "Content-type: text/xml" ',
               '-d "<coverageStore><name>',,'</name>',
               '<url>',img.path,'</url></coverageStore>" ', 
               '"', geoserver.url,'rest/workspaces/',,'/coveragestores?configure=all"',

#Execute function
createDatastore(user           = user,
                password       = password,
                img.path       = img.path,
                geoserver.url  = geoserver.url

I hope this was useful to some of you. If you have any questions, please leave me a comment below.



About This Author

Martin was born in Czech Republic and studied at the University of Natural Resources and Life Sciences, Vienna. He is currently working at GeoVille - an Earth Observation Company based in Austria, specialised in Land Monitoring. His main interests are: Open-source applications like R, (geospatial) statistics and data-management, web-mapping and visualization. He loves travelling, geocaching, photography and sports.

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 4 years ago Reply

Post A Reply