Guide to Terradue Open Source projects

Terradue / rOpenSearch

Language Created Last update Stars Forks
R 2014-05-29T06:37:53Z 2014-09-04T07:47:46Z 2 0


R interface to OpenSearch


The rOpenSearch documentation is live at:

The rOpenSearch documentation source is available at:

Inside R, use ? to view the function's help page. Example:


Citing this package

To cite rOpenSearch use its DOI

Installing a release

The releases are available at:

Releases can be installed using devtools

Start an R session and run:


Note the example above install the v0.1-SNAPSHOT release, adapt it to the current release

Building and installing the development version

The rOpenSearch package is built using maven.

From a terminal:

git clone
cd rOpenSearch
mvn compile

That generates a compressed archive with the rOpenSearch package in:


To install the package, start an R session and run:

install.packages("~/rOpenSearch/target/R/src/rOpenSearch_x.y.z.tar.gz", repos=NULL, type="source")

Note x.y.z is the development version number.

Then load the library:


Getting Started

Get Envisat MERIS Earth Observation data best coverage for the white shark habitat analysis

This example:



# set the catalogue and retrieve the queryables
osd.description <- ''
response.type <- "application/rdf+xml"
q <- GetOSQueryables(osd.description, response.type)
q$value[q$type == "count"] <- 200

# get the occurrences from GBIF using rgbif
key <- name_backbone(name='Carcharodon carcharias', kingdom='animalia')$speciesKey
occ <- occ_search(taxonKey=key, limit=1000, return='data', hasCoordinate=TRUE)
occ <- occ[complete.cases(occ),]
# bind the lon/lat columns
occ <- cbind(occ$decimalLongitude, occ$decimalLatitude)

### dbscan clustering 
eps <- 5
minpts <- 20
ds <- dbscan(occ, eps=eps, MinPts=minpts)

dataset <- list()
mbr <- list()
sp <- list()

# query the catalog using the geometry of the minimum bounding box for each cluster 
for(i in 1:max(ds$cluster)) {

  print(paste("Dealing with cluster", i))

  # get the cluster i
  cl <- (occ[ds$cluster %in% i,])

  # create the matrix with the cluster minimum bounding box
  coords <- matrix(nrow=5, ncol=2, byrow=TRUE, data=c(
    min(cl[,1]), min(cl[,2]), 
    max(cl[,1]), min(cl[,2]), 
    max(cl[,1]), max(cl[,2]), 
    min(cl[,1]), max(cl[,2]), 
    min(cl[,1]), min(cl[,2])))

  # get the cluster geospatial envelope
  mbr[[i]] <- gEnvelope(SpatialPoints(coords))

  # update the queryables value with the WKT of the cluster envelope 
  q$value[q$type == "geo:geometry"] <- writeWKT(gEnvelope(SpatialPoints(coords)))

  # query the catalogue
  res <- Query(osd.description, response.type, q)

  # from the result, get the Datasets
  dataset[[i]] <- xmlToDataFrame(nodes = getNodeSet(xmlParse(res), 
    "//dclite4g:DataSet"), stringsAsFactors = FALSE)

  # create the SpatialPolygonDataFrame
  # add the first element  
  poly.sp <- SpatialPolygonsDataFrame(readWKT(data.frame(dataset[[i]]$spatial)[1,]), dataset[[i]][1,])
  # bind the remaining elements
  for (n in 2:nrow(dataset[[i]])) {
    poly.sp <- rbind(poly.sp, SpatialPolygonsDataFrame(readWKT(data.frame(dataset[[i]]$spatial)[n,],id=n), dataset[[i]][n,]))  

  # evaluate the coverage index between each MERIS product and the cluster area
  for (n in 1:nrow(dataset[[i]])) {
    poly.sp[n,"coverage_index"] <- gArea(gIntersection(mbr[[i]], poly.sp[n,])) / gArea(mbr[[i]]) 

  # take the MERIS products with more than 80% coverage index
  sp[[i]] <- poly.sp[poly.sp$coverage_index > 0.8,]

# what's the cluster with the best coverage?
index <- which.max(lapply(sp, function(x) nrow(x)))

# do a plot
newmap <- getMap() #resolution = "low")
plot(sp[[index]], add=TRUE, col=brewer.pal( 8 , "RdBu")[index])
plot(mbr[[index]], add=TRUE, col="blue")

# list the MERIS file to download from the European Space Agency

This returns the image below:

alt tag

Query the European Space Agency ERS-1/2 SAR and Envisat ASAR virtual archive

Query the Envisat ASAR Image Mode source packets Level 0 (ASA_IM__0P) series

Return the 100 first dataset spanning time interval 2010-01-01 to 2010-01-31

# load the libraries

install_github("rOpenSearch", username="Terradue", subdir="/src/main/R/rOpenSearch")

# define the OpenSearch description URL
osd.url <- ""
response.type <- "application/rdf+xml"

# get the queryables dataframe from the OpenSearch description URL
df.params <- GetOSQueryables(osd.url, response.type)

# define the values for the queryables
df.params$value[df.params$type == "count"] <- 30 
df.params$value[df.params$type == "time:start"] <- "2010-01-01"
df.params$value[df.params$type == "time:end"] <- "2010-01-31"

# submit the query
res <- Query(osd.url, response.type, df.params)

# get the dataset
dataset <- xmlToDataFrame(nodes = getNodeSet(xmlParse(res), 
  "//dclite4g:DataSet"), stringsAsFactors = FALSE)

# create a SpatialPolygonsDataFrame with the first element of res$dataset
poly.sp <- SpatialPolygonsDataFrame(readWKT(data.frame(dataset$spatial)[1,]), dataset[1,])

# iterate through the remaining dataset
for (n in 2:nrow(dataset)) {
  poly.sp <- rbind(poly.sp,
    SpatialPolygonsDataFrame(readWKT(data.frame(dataset$spatial)[n,],id=n), dataset[n,]))

# write the geojson file
writeOGR(poly.sp, 'example1.geojson','dataMap', driver='GeoJSON')

The GeoJSON file can be see here:

Questions, bugs, and suggestions

Please file any bugs or questions as issues or send in a pull request.