Agrimetrics’ GraphQL API provides a convenient interface through which data hosted in the Agrimetrics Data Platform can be queried. Since some of these datasets are geospatially indexed (geospatialMeasures), querying the API with a region of interest, whether a polygon or point, will yield data that are implicitly geospatially related. Since all geospatialMeasures data are queried in the same manner - for a given region, return all selected data for the specified datasets - to reduce some of the boiler-plate code that would otherwise be needed, some helper functions have been written and included in this repository. This notebook aims to demonstrate how to query for and visualise data using these helper functions.
In this notebook, you will find a few GraphQL queries that have been copied directly from the Query Builder webapp. The queries used here have been crafted within the webapp and pasted into the notebook to demonstrate how you might generate quick visualisations of data of interest. Should there be an alternate set of data of interest, replace the queries below as needed.
You will need a subscription key to run this example. Your subscription key is available through the Agrimetrics developer portal:
In the following cell utils, transformData, and graphqlClient helper functions have been imported. These files are included in the Github repository along with this notebook. If you want to tweak the notebook in any way and host it locally, be sure to copy the notebook along with these helper function source files.
# To reduce time when installing packages, use all cores available
options("stringsAsFactors" = FALSE)
# disable warnings
options(warn=-1)
# source the helper functions used to simplify access to graphQL data
# utils.R - some core utilities
source("utils.R")
# transformData.R - simplifies the transformation of JSON response objects served by Agrimetrics' graphQL Query API
# into more useful dataframes
source("transformData.R")
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: knitr
# graphqlClient.R - a thin wrapper around the [ghql](https://cloud.r-project.org/web/packages/ghql/index.html) library
# providing a connection with which the queries in this notebook are executed.
source("graphqlClient.R")
# installPackages is a helper function optimising time needed to install packages. It does so by making use of
# all your machine's CPU cores.
packagesRequired <- c('stringr', 'dplyr', 'jsonlite', 'geojsonsf', 'sf', 'tmap', 'ggplot2')
installPackages(packagesRequired)
library(geojsonsf)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(ggplot2)
library(tmap)
The following configures the GraphQL client used for all queries within this notebook. Once Jupyter has been installed along with the R kernel, the only other requirement is your API key.
You can provide your API key in one of two ways:
$ API_KEY=abcdefghijklmnopqrstuvwxyz jupyter notebook
apiKey <- "xxxxxxxxx"
Important: Note that using the second method above will embed your API key in the evaluated notebook, so it should not be distributed or published afterwards.
apiKey <- Sys.getenv("API_KEY", "UNSET")
# use the helper function to acquire a connection to the Agrimetrics Query API. This connection is
# used throughout the notebook for all subsequent queries.
connection <- getConnection(apiKey)
## [1] "Connection acquired: https://api.agrimetrics.co.uk/graphql"
GraphQL queries have their own query language. The query selects which elements of the schema are to be returned.
For a more comprehensive description of the query language, see the GraphQL introduction. For interactive online documentation and exploration of the GraphQL schema, visit the Graph Explorer tool.
For demonstration purposes, the queries throughout will be performed for a region covering Rothamsted Farm, one of our Founding Partners, for which we make most of our data free of charge.
# For all the following queries, we will be requesting data for an area of Surrey and Sussex
polygon <- c(
"type"="Polygon",
"coordinates"=list(list(list(
c(-0.401073, 51.80076),
c(-0.356222, 51.80076),
c(-0.356222, 51.819771),
c(-0.401073, 51.819771),
c(-0.401073, 51.80076)
)))
)
The Query Builder serves as a great interactive tool to aid in crafting queries. The following query was produced in this way, retrieving data for soilPh, Abundance of Invertebrates and monthly temperature values. The query has been tailored to demonstrate some of the basic concepts available when writing queries; variables and fragments feature in the query to both reduce some code duplication and provide a means of parameterisation of the query.
testQuery <- '
fragment monthlyWeatherMeasure on GeospatialTimeSeriesMeasure {
datapoints {
date
value
}
}
fragment location on GeospatialMeasure {
location {
shape
}
}
query getGridsIntersectingPolygon ($polygon: LocationFilter!, $startDate: Date!, $endDate: Date!) {
geospatialMeasures(geoFilter: {location: $polygon, operation: INTERSECTS}) {
soilPH {
unit
value
...location
}
soilTotalAbundanceOfInvertebrates {
unit
value
...location
}
temperatureMeanMonthly (where: {
datapoints: {
date: {
GE: $startDate,
LE: $endDate
}
}
}) {
...monthlyWeatherMeasure
...location
}
temperatureMaxMonthly (where: {
datapoints: {
date: {
GE: $startDate,
LE: $endDate
}
}
}) {
...monthlyWeatherMeasure
...location
}
temperatureMinMonthly (where: {
datapoints: {
date: {
GE: $startDate,
LE: $endDate
}
}
}) {
...monthlyWeatherMeasure
...location
}
}
}'
variables <- list(polygon=polygon, startDate="2017-01-01", endDate="2018-12-01")
# use library function to get data from Query API / GraphQL API
response <- getData(connection, testQuery, variables)
# convert the data into a dataFrame
data <- geospatialMeasuresToDataFrame(response)
print(attributes(data))
## $names
## [1] "soilPH" "soilTotalAbundanceOfInvertebrates"
## [3] "temperatureMeanMonthly" "temperatureMaxMonthly"
## [5] "temperatureMinMonthly"
print(str(data))
## List of 5
## $ soilPH :'data.frame': 7 obs. of 3 variables:
## ..$ unit : chr [1:7] "http://data.agrimetrics.co.uk/units/ph" "http://data.agrimetrics.co.uk/units/ph" "http://data.agrimetrics.co.uk/units/ph" "http://data.agrimetrics.co.uk/units/ph" ...
## ..$ value : num [1:7] 6.07 7.05 7.05 7.23 8.21 ...
## ..$ location:'data.frame': 7 obs. of 1 variable:
## .. ..$ shape:'data.frame': 7 obs. of 2 variables:
## .. .. ..$ coordinates:List of 7
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.406 -0.406 -0.391 -0.392 -0.406 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.392 -0.392 -0.377 -0.377 -0.392 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.392 -0.391 -0.377 -0.377 -0.392 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.377 -0.377 -0.363 -0.363 -0.377 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.377 -0.377 -0.362 -0.363 -0.377 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.363 -0.362 -0.348 -0.348 -0.363 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.391 -0.391 -0.376 -0.377 -0.391 ...
## .. .. ..$ type : chr [1:7] "Polygon" "Polygon" "Polygon" "Polygon" ...
## $ soilTotalAbundanceOfInvertebrates:'data.frame': 7 obs. of 3 variables:
## ..$ unit : chr [1:7] "http://data.agrimetrics.co.uk/units/count" "http://data.agrimetrics.co.uk/units/count" "http://data.agrimetrics.co.uk/units/count" "http://data.agrimetrics.co.uk/units/count" ...
## ..$ value : num [1:7] 56.2 30.7 30.7 48.8 23.3 ...
## ..$ location:'data.frame': 7 obs. of 1 variable:
## .. ..$ shape:'data.frame': 7 obs. of 2 variables:
## .. .. ..$ coordinates:List of 7
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.406 -0.406 -0.391 -0.392 -0.406 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.392 -0.392 -0.377 -0.377 -0.392 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.392 -0.391 -0.377 -0.377 -0.392 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.377 -0.377 -0.363 -0.363 -0.377 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.377 -0.377 -0.362 -0.363 -0.377 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.363 -0.362 -0.348 -0.348 -0.363 ...
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.391 -0.391 -0.376 -0.377 -0.391 ...
## .. .. ..$ type : chr [1:7] "Polygon" "Polygon" "Polygon" "Polygon" ...
## $ temperatureMeanMonthly :'data.frame': 1 obs. of 2 variables:
## ..$ datapoints:List of 1
## .. ..$ :'data.frame': 24 obs. of 2 variables:
## .. .. ..$ date : chr [1:24] "2017-01-01" "2017-02-01" "2017-03-01" "2017-04-01" ...
## .. .. ..$ value: num [1:24] 3.1 5.9 8.8 9 13.2 16.8 17.6 16.1 13.6 12.3 ...
## ..$ location :'data.frame': 1 obs. of 1 variable:
## .. ..$ shape:'data.frame': 1 obs. of 2 variables:
## .. .. ..$ coordinates:List of 1
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.407 -0.335 -0.333 -0.405 -0.407 ...
## .. .. ..$ type : chr "Polygon"
## $ temperatureMaxMonthly :'data.frame': 1 obs. of 2 variables:
## ..$ datapoints:List of 1
## .. ..$ :'data.frame': 24 obs. of 2 variables:
## .. .. ..$ date : chr [1:24] "2017-01-01" "2017-02-01" "2017-03-01" "2017-04-01" ...
## .. .. ..$ value: num [1:24] 6.4 8.6 12.7 13.9 17.9 21.6 22 20.4 17.5 15.5 ...
## ..$ location :'data.frame': 1 obs. of 1 variable:
## .. ..$ shape:'data.frame': 1 obs. of 2 variables:
## .. .. ..$ coordinates:List of 1
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.407 -0.335 -0.333 -0.405 -0.407 ...
## .. .. ..$ type : chr "Polygon"
## $ temperatureMinMonthly :'data.frame': 1 obs. of 2 variables:
## ..$ datapoints:List of 1
## .. ..$ :'data.frame': 24 obs. of 2 variables:
## .. .. ..$ date : chr [1:24] "2017-01-01" "2017-02-01" "2017-03-01" "2017-04-01" ...
## .. .. ..$ value: num [1:24] -0.1 3.3 4.9 4.2 8.7 12 13.2 11.8 9.7 9.1 ...
## ..$ location :'data.frame': 1 obs. of 1 variable:
## .. ..$ shape:'data.frame': 1 obs. of 2 variables:
## .. .. ..$ coordinates:List of 1
## .. .. .. ..$ : num [1, 1:5, 1:2] -0.407 -0.335 -0.333 -0.405 -0.407 ...
## .. .. ..$ type : chr "Polygon"
## NULL
The helper function griddedTemporalMeasuresToDataFrame can be used to transform the data into a geospatial dataframe. The source for this function can be found within transformData.R within this folder of the repository.
# transform to a dataframe
griddedMonthlyWeatherData <- griddedTemporalMeasuresToDataFrame(
response, c("temperatureMeanMonthly", "temperatureMinMonthly", "temperatureMaxMonthly")
#response, c("temperatureMeanMonthly")
#response, c("soilTotalAbundanceOfInvertebrates")
# response
)
geospatialMeasuresToDataFrame(response)
## $soilPH
## unit value
## 1 http://data.agrimetrics.co.uk/units/ph 6.068252
## 2 http://data.agrimetrics.co.uk/units/ph 7.046670
## 3 http://data.agrimetrics.co.uk/units/ph 7.046670
## 4 http://data.agrimetrics.co.uk/units/ph 7.231666
## 5 http://data.agrimetrics.co.uk/units/ph 8.210084
## 6 http://data.agrimetrics.co.uk/units/ph 6.044802
## 7 http://data.agrimetrics.co.uk/units/ph 7.023221
## location.shape.coordinates
## 1 -0.4060127, -0.4056957, -0.3911954, -0.3915153, -0.4060127, 51.8048296, 51.8138172, 51.8136197, 51.8046322, 51.8048296
## 2 -0.3918351, -0.3915153, -0.3770181, -0.3773408, -0.3918351, 51.7956448, 51.8046322, 51.8044330, 51.7954456, 51.7956448
## 3 -0.3915153, -0.3911954, -0.3766952, -0.3770181, -0.3915153, 51.8046322, 51.8136197, 51.8134204, 51.8044330, 51.8046322
## 4 -0.3773408, -0.3770181, -0.3625210, -0.3628466, -0.3773408, 51.7954456, 51.8044330, 51.8042320, 51.7952447, 51.7954456
## 5 -0.3770181, -0.3766952, -0.3621953, -0.3625210, -0.3770181, 51.8044330, 51.8134204, 51.8132194, 51.8042320, 51.8044330
## 6 -0.3625210, -0.3621953, -0.3476955, -0.3480241, -0.3625210, 51.8042320, 51.8132194, 51.8130165, 51.8040293, 51.8042320
## 7 -0.3911954, -0.3908752, -0.3763722, -0.3766952, -0.3911954, 51.8136197, 51.8226071, 51.8224078, 51.8134204, 51.8136197
## location.shape.type
## 1 Polygon
## 2 Polygon
## 3 Polygon
## 4 Polygon
## 5 Polygon
## 6 Polygon
## 7 Polygon
##
## $soilTotalAbundanceOfInvertebrates
## unit value
## 1 http://data.agrimetrics.co.uk/units/count 56.20854
## 2 http://data.agrimetrics.co.uk/units/count 30.71813
## 3 http://data.agrimetrics.co.uk/units/count 30.71813
## 4 http://data.agrimetrics.co.uk/units/count 48.82695
## 5 http://data.agrimetrics.co.uk/units/count 23.33654
## 6 http://data.agrimetrics.co.uk/units/count 36.80287
## 7 http://data.agrimetrics.co.uk/units/count 11.31245
## location.shape.coordinates
## 1 -0.4060127, -0.4056957, -0.3911954, -0.3915153, -0.4060127, 51.8048296, 51.8138172, 51.8136197, 51.8046322, 51.8048296
## 2 -0.3918351, -0.3915153, -0.3770181, -0.3773408, -0.3918351, 51.7956448, 51.8046322, 51.8044330, 51.7954456, 51.7956448
## 3 -0.3915153, -0.3911954, -0.3766952, -0.3770181, -0.3915153, 51.8046322, 51.8136197, 51.8134204, 51.8044330, 51.8046322
## 4 -0.3773408, -0.3770181, -0.3625210, -0.3628466, -0.3773408, 51.7954456, 51.8044330, 51.8042320, 51.7952447, 51.7954456
## 5 -0.3770181, -0.3766952, -0.3621953, -0.3625210, -0.3770181, 51.8044330, 51.8134204, 51.8132194, 51.8042320, 51.8044330
## 6 -0.3625210, -0.3621953, -0.3476955, -0.3480241, -0.3625210, 51.8042320, 51.8132194, 51.8130165, 51.8040293, 51.8042320
## 7 -0.3911954, -0.3908752, -0.3763722, -0.3766952, -0.3911954, 51.8136197, 51.8226071, 51.8224078, 51.8134204, 51.8136197
## location.shape.type
## 1 Polygon
## 2 Polygon
## 3 Polygon
## 4 Polygon
## 5 Polygon
## 6 Polygon
## 7 Polygon
##
## $temperatureMeanMonthly
## datapoints
## 1 2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01, 2018-01-01, 2018-02-01, 2018-03-01, 2018-04-01, 2018-05-01, 2018-06-01, 2018-07-01, 2018-08-01, 2018-09-01, 2018-10-01, 2018-11-01, 2018-12-01, 3.1, 5.9, 8.8, 9, 13.2, 16.8, 17.6, 16.1, 13.6, 12.3, 6.6, 4.6, 5.2, 2.4, 5, 10.3, 13.3, 16.3, 20, 17.5, 14.1, 10.9, 7.9, 6.5
## location.shape.coordinates
## 1 -0.4069630, -0.3345208, -0.3328642, -0.4053784, -0.4069630, 51.7778669, 51.7768629, 51.8217989, 51.8228045, 51.7778669
## location.shape.type
## 1 Polygon
##
## $temperatureMaxMonthly
## datapoints
## 1 2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01, 2018-01-01, 2018-02-01, 2018-03-01, 2018-04-01, 2018-05-01, 2018-06-01, 2018-07-01, 2018-08-01, 2018-09-01, 2018-10-01, 2018-11-01, 2018-12-01, 6.4, 8.6, 12.7, 13.9, 17.9, 21.6, 22, 20.4, 17.5, 15.5, 9.7, 7.3, 8.2, 5.3, 8.2, 14, 18.8, 21.7, 26.3, 22.5, 19.1, 15, 10.9, 9.3
## location.shape.coordinates
## 1 -0.4069630, -0.3345208, -0.3328642, -0.4053784, -0.4069630, 51.7778669, 51.7768629, 51.8217989, 51.8228045, 51.7778669
## location.shape.type
## 1 Polygon
##
## $temperatureMinMonthly
## datapoints
## 1 2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01, 2018-01-01, 2018-02-01, 2018-03-01, 2018-04-01, 2018-05-01, 2018-06-01, 2018-07-01, 2018-08-01, 2018-09-01, 2018-10-01, 2018-11-01, 2018-12-01, -0.1, 3.3, 4.9, 4.2, 8.7, 12, 13.2, 11.8, 9.7, 9.1, 3.5, 1.9, 2.2, -0.6, 1.7, 6.6, 7.8, 10.8, 13.7, 12.4, 9.2, 6.9, 5, 3.7
## location.shape.coordinates
## 1 -0.4069630, -0.3345208, -0.3328642, -0.4053784, -0.4069630, 51.7778669, 51.7768629, 51.8217989, 51.8228045, 51.7778669
## location.shape.type
## 1 Polygon
print(attributes(griddedMonthlyWeatherData))
## $names
## [1] "Date" "location" "temperatureMeanMonthly"
## [4] "temperatureMinMonthly" "temperatureMaxMonthly"
##
## $row.names
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
##
## $class
## [1] "data.frame"
# use the geojson_sf library to generate geometries for each of the 5km grids returned
weatherGridShapeGeoms <- griddedMonthlyWeatherData$location %>% geojson_sf
# create a spatial features collection
monthlyTemperature <- st_sf(
cbind(
"date"=griddedMonthlyWeatherData$Date,
"temperatureMeanMonthly"=griddedMonthlyWeatherData$temperatureMeanMonthly,
"temperatureMinMonthly"=griddedMonthlyWeatherData$temperatureMinMonthly,
"temperatureMaxMonthly"=griddedMonthlyWeatherData$temperatureMaxMonthly,
weatherGridShapeGeoms
)
)
print(head(monthlyTemperature))
## Simple feature collection with 6 features and 4 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -0.407 ymin: 51.7769 xmax: -0.3329 ymax: 51.8228
## geographic CRS: WGS 84
## date temperatureMeanMonthly temperatureMinMonthly temperatureMaxMonthly
## 1 2017-01-01 3.1 -0.1 6.4
## 2 2017-02-01 5.9 3.3 8.6
## 3 2017-03-01 8.8 4.9 12.7
## 4 2017-04-01 9.0 4.2 13.9
## 5 2017-05-01 13.2 8.7 17.9
## 6 2017-06-01 16.8 12.0 21.6
## geometry
## 1 POLYGON ((-0.407 51.7779, -...
## 2 POLYGON ((-0.407 51.7779, -...
## 3 POLYGON ((-0.407 51.7779, -...
## 4 POLYGON ((-0.407 51.7779, -...
## 5 POLYGON ((-0.407 51.7779, -...
## 6 POLYGON ((-0.407 51.7779, -...
# Since this data is timeseries, to simplify the plots we will only display min, mean and max for January 2018
filteredDataByDate <- filter(monthlyTemperature, as.Date(date) == as.Date("2018-01-01"))
# Generate an interactive leaflet map of all grids
tmap_mode("plot")
## tmap mode set to plotting
tmap_leaflet(
tm_shape(filteredDataByDate, name="Max Temp") +
tm_polygons(
col="temperatureMaxMonthly",
palette="YlOrRd"
) +
tm_shape(filteredDataByDate, name="Min Temp") +
tm_polygons(
col="temperatureMinMonthly",
palette="YlGnBu"
) +
tm_shape(filteredDataByDate, name="Mean Temp") +
tm_polygons(
col="temperatureMeanMonthly",
palette="Oranges"
) +
tm_scale_bar()
)
#Testing GraphQL API with Python
https://developer.agrimetrics.co.uk/docs/services/graphql/operations/General-GraphQL-query
########### Python 3.2 #############
import http.client, urllib.request, urllib.parse, urllib.error, base64
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '6f5289c1db774e7ab18f38e9b7acd157',
}
params = urllib.parse.urlencode({
})
try:
conn = http.client.HTTPSConnection('api.agrimetrics.co.uk')
conn.request("POST", "/graphql/v1/?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
####################################
## b'{ "statusCode": 401, "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription." }'
grid_temperatures.png
Shapefiles impose some limitations including the string length of the attributes’ names. See here for more details.
# Duplicate the geospatial dataframe as it will be tweaked for output as a shapefile
tempDf <- monthlyTemperature
# shapefiles cannot have attributes with names longer than 10 characters so the columns are renamed here.
names(tempDf) <- c('DATE', 'MEAN', 'MIN', 'MAX', 'geometry')
# the dataframe is written to weatherData.shp
st_write(tempDf, dsn=paste0("weatherData.shp"), delete_layer = TRUE)
## Writing layer `weatherData' to data source `weatherData.shp' using driver `ESRI Shapefile'
## Writing 24 features with 4 fields and geometry type Polygon.
Since the data acquired is of a timeseries format, a x-y chart is used to provide a basic visualisation of this data.
# plot the dataframe as timeseries chart
df <- monthlyTemperature
df$date <- as.Date(df$date)
ggplot(data=monthlyTemperature, aes(x = date)) +
geom_line(aes(y = temperatureMeanMonthly, group=1), color="orange") +
geom_line(aes(y = temperatureMinMonthly, group=1), color="blue") +
geom_line(aes(y = temperatureMaxMonthly, group=1), color="red") +
labs(
x = "Date",
y = "Temperature (°C)",
title = "Historical Temperature Data"
) +
theme(text = element_text(size=12), axis.text.x = element_text(angle = 75))
Soil information is data derived from modelling appropriately distributed samples across the UK. The data is available at a 1km grid from the CEH. See here for more details.
# get the Total Abundance of Invertebrates for the soil grids within the searched area
soilTAOI <- data$soilTotalAbundanceOfInvertebrates
# magic one liner to convert the GeoJSON location object into a list of simple features
geometry <- soilTAOI$location$shape %>% toJSON %>% geojson_sf
# bind the data to these geometries
soilTAOIData <- st_sf(cbind(soilTAOI$value, geometry))
# create a static plot, colouring the cells by Total Abundance Of Invertebrates
ggplot(soilTAOIData) + geom_sf(aes(fill = soilTAOI.value))
# get the Total Abundance of Invertebrates for the soil grids within the searched area
soilPH <- data$soilPH
# magic one liner to convert the GeoJSON location object into a dataframe of simple features
geometry <- soilPH$location$shape %>% toJSON %>% geojson_sf
# create a soilData "simple feature collection"
soilPHData <- st_sf(cbind(soilPH$value, geometry))
# create a static plot, colouring the cells by pH
ggplot(soilPHData) + geom_sf(aes(fill = soilPH.value))
Without performing any analysis of the data, this cell simply presents all the following geospatial data in a single plot: * soil pH * total abundance of invertebrates * monthly mean temperature for January 2018 (using previously filtered dataframe)
# Putting it all together
# we are only going to plot the mean temperature along with soil data
tmap_mode("plot")
## tmap mode set to plotting
tmap_leaflet(
tm_shape(filteredDataByDate) +
tm_polygons("temperatureMeanMonthly", title="Temperature Mean Monthly (°C)", palette="-Greys", alpha=0.5) +
tm_shape(soilPHData) +
tm_polygons("soilPH.value", title="Soil pH", palette="-Greys", alpha=0.5) +
tm_shape(soilTAOIData) +
tm_polygons("soilTAOI.value", title="Total Abundance Of Invertebrates", palette="-Greys", alpha=0.5)
)
all_together.png