knitr::opts_chunk$set( collapse = TRUE, comment = "#>", cache = TRUE ) options(rmarkdown.html_vignette.check_title = FALSE) options(rdf_print_format = "turtle")
This article describes how to access the Marine Regions Gazetteer entries and geometries as RDF, which are loaded into R via rdflib::rdf
. Mirroring the Marine Regions Gazetteer via Linked Data Event Streams is discussed.
The following libraries are required
library(mregions2) library(rdflib) # Use the pipe operator `%>%` library(magrittr) # For visualizing library(mapview) library(jsonlite)
Known an ?MRGID
, you can get the gazetter entry as RDF.
E.g. the Belgian Exclusive Economic Zone or Belgian Part of the North Sea has the ?MRGID
: <https://marineregions.org/mrgid/3293>
gaz_search(3293) %>% gaz_geometry() %>% mapview()
Using gaz_search()
or the underlying service gaz_rest_record_by_mrgid()
with the argument rdf = TRUE
will:
rdflib
R package (?rdflib::rdflib
) HTTP requests are handled with httr2
(?httr2::httr2
)
bpns <- gaz_search(3293, rdf = TRUE) class(bpns) bpns
There are many stores returned. This is because the returned object includes not only info about the Gazetteer record, but also about its hierarchy. The Marine Regions Gazetteer is hierarchical and there are many relations between their records
The relations are explained the Marine Regions Ontology:
http://marineregions.org/ns/ontology
There is also information about the Place Type. The Place Types are defined in:
http://marineregions.org/ns/placetypes
Both the ontology and the placetypes are available as HTML when using a web browser, but also as Turtle or JSON-LD by using content negotiation.
You can then use rdflib::rdflib
to apply common RDF applications.
You can serialize the record in other formats, e.g. JSON-LD
# Create a placeholder file bpns_as_jsonld <- tempfile(fileext = ".rdf") # Serialize bpns %>% rdf_serialize( doc = bpns_as_jsonld, format = "jsonld" ) readLines(bpns_as_jsonld) %>% jsonlite::prettify()
Show Output
readLines(bpns_as_jsonld) %>% jsonlite::prettify() %>% cat()
SPARQL queries can be applied to the RDF document. Note: The ?MRGID
of the Belgian Part of the North Sea is <http://marineregions.org/mrgid/3293>
sparql <- " SELECT ?p ?o WHERE { <http://marineregions.org/mrgid/3293> ?p ?o }" rdf_query(bpns, query = sparql)
The following example extracts all the predicates with the prefix mr:
, pointing to the Marine Regions Ontology at http://marineregions.org/ns/ontology.
sparql <- " PREFIX mr: <http://marineregions.org/ns/ontology#> SELECT ?s ?p ?o WHERE { ?s ?p ?o . FILTER( STRSTARTS(STR(?p), str(mr:)) ) }" rdf_query(bpns, query = sparql)
You can see in the predicates that one of the relationships is mr:hasGeometry
, pointing to the object http://marineregions.org/mrgid/3293/geometries?source=79&attributeValue=3293
This URI can be requested as Turtle with content negotiation and load as an rdflib::rdf
object of the rdflib::rdflib
package
But you could also use gaz_geometry()
, which already takes care of this:
bpns_geom <- gaz_geometry(3293, format = "rdf") bpns_geom
The geometry is provided as WKT. This is the same logic that gaz_geometry(format = "wkt")
uses to extract the geometry:
sparql <- " PREFIX gsp: <http://www.opengis.net/ont/geosparql#> SELECT ?o WHERE { ?s gsp:asWKT ?o . }" rdf_query(bpns_geom, query = sparql)
Using the SPARQL syntax you can extract the label of the record in a specific language, if this is available. If not, the preferred label will be returned
Example: Get the Gazetteer entry for "Belgium" with MRGID <http://marineregions.org/mrgid/14>
and extract the label in Dutch (nl) if available.
belgium <- gaz_search(14, rdf = TRUE) sparql <- ' PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT (COALESCE(?altLabel, ?prefLabel) AS ?o) WHERE { OPTIONAL { ?s skos:altLabel ?altLabel . filter langMatches(lang(?altLabel), "nl") } ?s skos:prefLabel ?prefLabel . } ' rdf_query(belgium, query = sparql)
The Marine Regions Gazetteer is accessible as RDF, and can be mirrored and synchronized via Linked Data Event Streams (LDES). This is described with high detail in:
Lonneville B. et al. (2021) Publishing the Marine Regions Gazetteer as a Linked Data Event Stream. S4BioDiv 2021. http://ceur-ws.org/Vol-2969/paper8-s4biodiv.pdf
Building such API in R is, at the moment, out of the scope of mregions2. However, an interested user could make use of mregions2 and rdflib::rdflib
to mirror the Marine Regions Gazetteer. The general approach is described below:
By design, mregions2 stores the RDF triples in memory. This is because the package does not aim at replicating the Marine Regions Gazetteer, but only consult small subsets.
rdflib::rdflib
allows to store the triples in a database. This is further explained in vignette("storage", package = "rdflib")
.
As an example, using a Virtuoso database backend is possible. Virtuoso is a popular open source database for RDF.
triplestore <- rdf(storage = "virtuoso", user = "dba", password = "dba", dsn = "Local Virtuoso" ) # Turn bpns into nquads tmp <- tempfile(fileext = ".nq") write_nquads(bpns, tmp) # Parse bpns as nquads into the store rdf_parse(tmp, format = "nquads", rdf = triplestore)
The LDES feed is available at:
https://marineregions.org/feed
This offers fragmentation of the whole Marine Regions Gazetteer based on the last modification time of the ?MRGID
. It primary use is to make it easy to have external caches, replications, or derived indexes in an efficient and incremental way.
The implementation would start by fetching the feed as start page and then follow the tree:relation
paths down to pages that contain only tree:member
entries that are older than the requested date.
Source: https://github.com/ropensci/mregions/pull/62#issuecomment-1091837752
library(mregions2) # # # # # # # # # # # cuba_eez_relations <- mr_relations(cuba_eez) # # cuba_eez %>% tidyr::as_tibble() %>% View() # # cuba_eez <- mr_get(8406) # # # test <- rdflib::rdf_query(cuba_eez, query = paste0( # mr_prefixes(), # " # SELECT * # WHERE { # <http://marineregions.org/mrgid/8406> ?p ?o # # } # " # # )) # # # ## SHOW TO THE TEAM DIFFERENT OPTIONS AND DECIDE WHAT TO DO # ## OPTION 1: STICK TO REST WEBSERVICES AND PROVIDE LINKED DATA AS VIGNETTE # ## OPTION 2: PROVIDE ALL STUFF IN A GEOOBJECT AS A TABLE # ## OPTION 3: CURATE ALL STUFF IN A GEOOBJECT AS TABLES # # # test = cbind(s = "http://marineregions.org/mrgid/8406", test) # # test = rbind(test, c("http://marineregions.org/mrgid/845456", # "foo_name", # "foo value")) # # colnames(test) # # tidyr::pivot_wider(test, names_from = p, values_from = o) %>% View() # # # # # # # cuba_eez %>% # rdflib::rdf_serialize(format = "turtle", namespace = c( # mr = "http://marineregions.org/ns/ontology#", # mrt = "http://marineregions.org/ns/placetypes#", # dc = "http://purl.org/dc/terms/", # xsd = "http://www.w3.org/2001/XMLSchema#", # rdfs = "http://www.w3.org/2000/01/rdf-schema#", # skos = "http://www.w3.org/2004/02/skos/core#", # dcat = "http://www.w3.org/ns/dcat#", # gsp = "http://www.opengis.net/ont/geosparql#", # prov = "http://www.w3.org/ns/prov#" # )) %>% # cat() # # as_tibble.MRGeoObject(cuba_eez) %>% View() # # # # View(cuba_eez_relations) # # # # Do something with replaced by # bouvet <- mr_get(8399) %>% as.data.frame() # # # # # mr_get("http://marineregions.org/mrgid/8406") %>% # rdflib::rdf_serialize(format = "turtle", namespace = c( # mr = "http://marineregions.org/ns/ontology#", # mrt = "http://marineregions.org/ns/placetypes#", # dc = "http://purl.org/dc/terms/", # xsd = "http://www.w3.org/2001/XMLSchema#", # rdfs = "http://www.w3.org/2000/01/rdf-schema#", # skos = "http://www.w3.org/2004/02/skos/core#", # dcat = "http://www.w3.org/ns/dcat#", # gsp = "http://www.opengis.net/ont/geosparql#", # prov = "http://www.w3.org/ns/prov#" # )) %>% # cat() # # # # belgium <- mr_get("http://marineregions.org/mrgid/26567") # # # # # # # # # # # query <- paste0( # " # prefix mr: <http://marineregions.org/ns/ontology#> # prefix mrt: <http://marineregions.org/ns/placetypes#> # prefix dc: <http://purl.org/dc/terms/> # prefix xsd: <http://www.w3.org/2001/XMLSchema#> # prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> # prefix skos: <http://www.w3.org/2004/02/skos/core#> # prefix dcat: <http://www.w3.org/ns/dcat#> # prefix gsp: <http://www.opengis.net/ont/geosparql#> # prefix prov: <http://www.w3.org/ns/prov#> # # SELECT ?subject ?predicate ?relation # # # ?inContextOf ?isRelatedTo ?isPartOf # # ?contains ?isAdministrativePartOf ?hasAdministrativePart # # ?isPartlyPartOf ?partlyContains ?flowsInto ?hasTributary # # ?risesIn ?containsSourceOf ?flowsThrough ?containsWatercourse # # ?isInfluencedBy ?influences # # # WHERE { # # # Get label # ?mrgid a ?placetype ; # skos:prefLabel ?label ; # dc:modified ?modified . # # # Restrict to only the placetype # ?placetype skos:inScheme <http://marineregions.org/ns/placetypes> . # # # modified is the filter - it is or should be in all mrgid # # # OPTIONAL { ?mrgid mr:inContextOf ?inContextOf } # # OPTIONAL { ?mrgid mr:isRelatedTo ?isRelatedTo } # # OPTIONAL { ?mrgid mr:isPartOf ?isPartOf } # # OPTIONAL { ?mrgid mr:contains ?contains } # # OPTIONAL { ?mrgid mr:isAdministrativePartOf ?isAdministrativePartOf } # # OPTIONAL { ?mrgid mr:hasAdministrativePart ?hasAdministrativePart } # # OPTIONAL { ?mrgid mr:isPartlyPartOf ?isPartlyPartOf } # # OPTIONAL { ?mrgid mr:partlyContains ?partlyContains } # # OPTIONAL { ?mrgid mr:flowsInto ?flowsInto } # # OPTIONAL { ?mrgid mr:hasTributary ?hasTributary } # # OPTIONAL { ?mrgid mr:risesIn ?risesIn } # # OPTIONAL { ?mrgid mr:containsSourceOf ?containsSourceOf } # # OPTIONAL { ?mrgid mr:flowsThrough ?flowsThrough } # # OPTIONAL { ?mrgid mr:containsWatercourse ?containsWatercourse } # # OPTIONAL { ?mrgid mr:isInfluencedBy ?isInfluencedBy } # # OPTIONAL { ?mrgid mr:influences ?influences } # # OPTIONAL { # { <http://marineregions.org/mrgid/8406> mr:contains ?relation } UNION { <http://marineregions.org/mrgid/8406> mr:partlyContains ?relation } # } # # OPTIONAL { # { ?mrgid mr:contains ?relation } UNION { ?mrgid mr:partlyContains ?relation } # } # # }" # ) # # # # # query <- paste0( # " # prefix mr: <http://marineregions.org/ns/ontology#> # prefix mrt: <http://marineregions.org/ns/placetypes#> # prefix dc: <http://purl.org/dc/terms/> # prefix xsd: <http://www.w3.org/2001/XMLSchema#> # prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> # prefix skos: <http://www.w3.org/2004/02/skos/core#> # prefix dcat: <http://www.w3.org/ns/dcat#> # prefix gsp: <http://www.opengis.net/ont/geosparql#> # prefix prov: <http://www.w3.org/ns/prov#> # # SELECT ?mrgid # (COALESCE(?altLabel, ?prefLabel) AS ?label) # ?placetype # ?modified # ?source # ?seeAlso # ?centroid # ?bbox # ?the_geom # # WHERE { # # # Restrict to only the placetype # ?mrgid a ?placetype . # ?placetype skos:inScheme <http://marineregions.org/ns/placetypes> . # # # Get some vars # OPTIONAL { ?mrgid dc:modified ?modified } # OPTIONAL { ?mrgid rdfs:seeAlso ?seeAlso } # OPTIONAL { ?mrgid dcat:centroid ?centroid } # OPTIONAL { ?mrgid dcat:bbox ?bbox } # OPTIONAL { ?mrgid mr:hasGeometry ?the_geom } # # # # Get the language # OPTIONAL { # ?mrgid skos:altLabel ?altLabel . # filter langMatches(lang(?altLabel), \"nl\") # } # ?mrgid skos:prefLabel ?prefLabel . # # # Get the source # OPTIONAL { ?mrgid prov:hadPrimarySource ?primarySource } # OPTIONAL { ?primarySource prov:wasAttributedTo ?wasAttributedTo } # OPTIONAL { ?wasAttributedTo rdfs:label ?source } # # # Approach using Property Paths, but seem like they are not supported # # ?mrgid prov:hadPrimarySource/prov:wasAttributedTo/rdfs:label ?source . # # # # }" # ) # # x <- rdflib::rdf_query(belgium, query) # # # # Add base_graph # # # # # # # # # # # debug(mr_get_rdf) # options(verbose = F) # # belgium <- mr_get("http://marineregions.org/mrgid/14") # tidyr::as_tibble # # # # # # france <- mr_get("http://marineregions.org/mrgid/17") # # mr_ # # # ## CREATE GENERAL FUNCTION TO MERGE MRGIDS # ## CREATE GENERAL FUNCTION TO APPLY SPARQL QUERY # # ## QUERIES # ## GET ALL BASIC INFO # # # # # # print(out, n = 100) # # # # # " # # prov:hadPrimarySource [ # # prov:wasAttributedTo [ # # rdfs:label # # \"(2001). The Times comprehensive atlas of the world. 10th ed. Times Books: # # London. ISBN 0-7230-0792-6. 67, 220, 124 plates pp.\"^^xsd:string # # ] # # ] # # # # " # # # # # # # # ## GET ALL RELATIONSHIPS # query <- paste0( # prefix, # " # SELECT ?mrgid ?placetype ?prefLabel # WHERE { # ?mrgid a ?placetype ; # skos:prefLabel ?prefLabel . # # }" # ) # # cat(query) # out <- rdflib::rdf_query(rdf, query, data.frame = FALSE) # out <- subset(out, out$placetype != 'http://marineregions.org/ns/ontology#MRGeoObject') # out <- subset(out, out$mrgid != 'http://marineregions.org/mrgid/14') # print(out, n = 100) # # query <- paste0( # prefix, # " # SELECT ?relationship ?value # WHERE { # <http://marineregions.org/mrgid/14> ?relationship ?value . # }" # ) # # relations <- rdflib::rdf_query(rdf, query, data.frame = FALSE) # relations <- subset(relations, grepl("http://marineregions.org/ns/ontology#", relations$relationship, fixed = TRUE)) # # out <- subset(out, out$placetype != 'http://marineregions.org/ns/ontology#MRGeoObject') # out <- subset(out, out$mrgid != 'http://marineregions.org/mrgid/14') # print(out, n = 100) # # # # # ## GET GEOMETRY # # # # # # # # # ## GET PREFERRED LANGUAGE # query <- paste0( # prefix, # " # SELECT (<http://marineregions.org/mrgid/14> as ?mrgid) # (coalesce(?altLabel, ?prefLabel) as ?label) # WHERE { # # # OPTIONAL { # <http://marineregions.org/mrgid/14> skos:altLabel ?altLabel # filter langMatches(lang(?altLabel), \"nl\") # } # OPTIONAL { # <http://marineregions.org/mrgid/14> skos:prefLabel ?prefLabel # } # # } # # # " # ) # # cat(query) # out <- rdflib::rdf_query(rdf, query, data.frame = FALSE) # out # # # # # # # cat(cont) # # query <- paste0( # # prefix, # # " # # SELECT ?mrgid ?name ?placetype # # WHERE { # # ?mrgid a ?placetype . # # ?mrgid skos:prefLabel ?name . # # }" # # ) # # # # cat(query) # # rdflib::rdf_query(rdf, query, data.frame = FALSE) # # # cat(cont) # query <- paste0( # prefix, # " # SELECT ?mrgid ?label ?geoobject # WHERE { # ?mrgid a ?geoobject . # ?mrgid skos:altLabel ?label . # FILTER (lang(?label) = 'en') # } # LIMIT 15 # " # ) # # cat(query) # rdflib::rdf_query(rdf, query, data.frame = FALSE) # # # # <http:\/\/marineregions.org\/ns\/ontology#(\w+)> # # FILTER (regex(?predicate, '/<http:\\/\\/marineregions.org\\/ns\\/ontology#(\\w+)>/g')) . # # FILTER (regex(?predicate, '/mr:(\\w+)/g')) . # # # # Set prefixes # todelete <- gsub("@prefix(.*?)\n", "", resp) # prefix <- gsub(todelete, "", resp, fixed = TRUE) # prefix <- gsub("@", "", prefix, fixed = TRUE) # prefix <- gsub(" .", "", prefix, fixed = TRUE) # Sys.setenv(mr_prefix = prefix) # # # # query <- # " # PREFIX mr: <http://marineregions.org/ns/ontology#> # SELECT ?mrgid ?the_geom # WHERE { ?mrgid mr:hasGeometry ?the_geom .}" # # # uri_geom <- rdflib::rdf_query(rdf, query) # # # # query <- # # " # # PREFIX mr: <http://marineregions.org/ns/ontology#> # # SELECT ?a ?c # # WHERE { ?a mr:contains ?c .}" # # # # contains <- rdflib::rdf_query(rdf, query, data.frame = FALSE) # # # query <- # " # PREFIX mr: <http://marineregions.org/ns/ontology#> # PREFIX skos: <http://www.w3.org/2004/02/skos/core#> # SELECT ?mrgid ?contains ?name ?other_names ?the_geom # WHERE { ?mrgid mr:contains ?contains . # ?mrgid skos:prefLabel ?name . # ?mrgid skos:altLabel ?other_names . # ?mrgid mr:hasGeometry ?the_geom . # } # " # # contains <- rdflib::rdf_query(rdf, query, data.frame = T) # # # # How to select the skos:prefLabel from the relationships # # How to get the placetype? # # Create method to jump to next? # # # "http://marineregions.org/ns/ontology" # no cacheable # "http://marineregions.org/ns/placetypes.ttl" # no cacheable # "https://www.w3.org/ns/dcat2" # cache-control + expires # "http://www.w3.org/ns/prov" # cache-control + expires # "http://www.w3.org/2000/01/rdf-schema" # cache-control + expires # "http://www.opengis.net/ont/geosparql" # Last-Modified + ETag # # 'http://purl.org/dc/terms/' # "http://purl.org/dc/elements/1.1/" # #' #' Create a new S3 object of class "MRGeoObject" #' #' #' #' Inherits the class "rdf" from the package "rdflib" #' #' #' #' @param x #' #' #' #' @return #' #' #' #' @examples #' new_MRGeoObject <- function(x = list()){ #' #' structure( #' x, #' class = c("MRGeoObject", "rdf") #' ) #' #' } #' #' #' Get a Marine Regions GeoObject as RDF #' #' #' #' @param x a url, a mrgid or a name of a Marine Regions Geoobject as a string #' #' @param ... aditional params for furter inheritance #' #' #' #' @return an object of class c("MRGeoObject", "rdf") #' #' This is completely compatible with the rdflib package. #' #' For instance, you can apply sparql queries with rdflib::rdf_query() #' #' #' #' Or you can serialize the file with rdflib::rdf_serialize() #' #' #' #' Add extra RDF documents to and create your own knowledge graph #' #' #' #' @export #' #' #' #' @examples #' #' x <- mr_get("Belgium") #' #' x <- mr_get("http://marineregions.org/mrgid/14) #' #' x <- mr_get(14) #' mr_get <- function(x, ...){ #' UseMethod("mr_get") #' } #' #' #' @rdname mr_get #' mr_get.default <- function(url, ...){ #' #' resp <- mr_request(url) %>% #' httr2::req_headers(accept = "text/turtle") %>% #' httr2::req_perform("GET") %>% #' httr2::resp_body_string() #' #' verbose <- getOption("verbose", default = FALSE) #' if(verbose){ #' cat(resp) #' } #' #' out <- resp %>% #' rdflib::rdf_parse("turtle") #' #' new_MRGeoObject(out) #' #' } #' #' #' @rdname mr_get #' mr_get.numeric <- function(mrgid, ...){ #' url <- glue::glue("http://marineregions.org/mrgid/{mrgid}") #' mr_get.default(url) #' } #' #' #' @rdname mr_get #' mr_get.character <- function(string, ...){ #' #' is_mr_url <- grepl("http://marineregions.org", string, fixed = TRUE) #' #' if(is_mr_url){ #' out <- mr_get.default(string) #' return(out) #' } #' #' if(!is_mr_url){ #' #' search <- mregions2::mr_gaz_records_by_names(string) #' #' if(nrow(search) == 0){ #' warning(glue::glue("No matches found for \"{string}\""), .call = FALSE) #' return(invisible(NULL)) #' } #' #' verbose <- getOption("verbose", default = FALSE) #' if(verbose){ #' cli::cli_alert_warning(glue::glue("{nrow(search)} match(es) found for \"{string}\": '{paste0(search$preferredGazetteerName, collapse = '\', \'')}'")) #' } #' #' return(mr_get.numeric(search$MRGID)) #' } #' } #' #' #' #' Returns all the prefixes for sparql queries #' #' #' #' @examples #' #' mr_prefixes() #' mr_prefixes <- function(){ #' prefixes <- c( #' "prefix mr: <http://marineregions.org/ns/ontology#>", #' "prefix mrt: <http://marineregions.org/ns/placetypes#>", #' "prefix dc: <http://purl.org/dc/terms/>", #' "prefix xsd: <http://www.w3.org/2001/XMLSchema#>", #' "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>", #' "prefix skos: <http://www.w3.org/2004/02/skos/core#>", #' "prefix dcat: <http://www.w3.org/ns/dcat#>", #' "prefix gsp: <http://www.opengis.net/ont/geosparql#>", #' "prefix prov: <http://www.w3.org/ns/prov#>" #' ) #' #' paste0(prefixes, collapse = "\n") #' #' } #' #' #' #' #' Extract information from a Marine Regions Geo Object as tabular data #' #' #' #' @param x an object of class c("MRGeoObject", "rdf") #' #' @param lang preferred language as 2c ISO_639_1. See details. #' #' @param ... aditional params for furter inheritance #' #' #' #' @details #' #' #' #' The Marine Regions Geo Objects can be labels in several #' #' languages. Choose your preferred language and, if the label exists #' #' in your language, it will be returned. Otherwise the label will be in the preferred language. #' #' #' #' @return a tibble with one row per Marine Regions Geo Object #' #' @export #' #' #' #' @examples #' as_tibble.MRGeoObject <- function(x, lang = "en", ...){ #' #' checkmate::assert_choice(lang, c("en", "es", "fr", "de", "nl", "it")) #' #' query <- paste0( #' mr_prefixes(), #' " #' SELECT ?mrgid #' (COALESCE(?altLabel, ?prefLabel) AS ?label) #' ?placetype #' ?modified #' ?source #' ?seeAlso #' ?centroid #' ?bbox #' ?the_geom #' #' WHERE { #' #' # Restrict to only the placetype #' ?mrgid a ?placetype . #' FILTER( STRSTARTS(STR(?placetype),str(mrt:)) ) #' #' # modified is the filter - it is or should be in all mrgid #' ?mrgid dc:modified ?modified #' OPTIONAL { ?mrgid rdfs:seeAlso ?seeAlso } #' OPTIONAL { ?mrgid dcat:centroid ?centroid } #' OPTIONAL { ?mrgid dcat:bbox ?bbox } #' OPTIONAL { ?mrgid mr:hasGeometry ?the_geom } #' #' #' # Get the language #' OPTIONAL { #' ?mrgid skos:altLabel ?altLabel . #' filter langMatches(lang(?altLabel), \"nl\") #' } #' ?mrgid skos:prefLabel ?prefLabel . #' #' # Get the source #' OPTIONAL { ?mrgid prov:hadPrimarySource ?primarySource } #' OPTIONAL { ?primarySource prov:wasAttributedTo ?wasAttributedTo } #' OPTIONAL { ?wasAttributedTo rdfs:label ?source } #' #' }" #' ) #' #' # Perform Query #' out <- rdflib::rdf_query(x, query) %>% #' suppressMessages() #' #' out #' } #' #' #' options(verbose = TRUE) #' #' cuba_eez <- mr_get("Cuban Exclusive Economic Zone") #' #' tidyr::as_tibble(cuba_eez) %>% View() #' #' mr_relations(cuba_eez) %>% View() #' #' test1 <- mregions2::mr_gaz_relations_by_MRGID(8406, #' direction = "both", #' type = "all") #' View(test1) #' #' #' @rdname as_tibble.MRGeoObject #' as.data.frame.MRGeoObject <- function(x, ...){ #' x <- as_tibble.MRGeoObject(x) #' as.data.frame(x) #' } #' #' #' #' Extract the relationships of a given MRGeoObject #' #' #' #' @param x a MRGeoObject #' #' @param ... #' #' #' #' @return #' #' @export #' #' #' #' @examples #' #' cuba_eez_relationships <- mr_get(8406) %>% mr_relationships() #' mr_relations <- function(x, ...){ #' # 1. Get all predictes of ontology mr: #' # These are the relations plus hasGeometry #' query <- paste0( #' mr_prefixes(), #' " #' SELECT ?s ?relation ?mrgid #' WHERE { #' ?s ?relation ?mrgid . #' FILTER( STRSTARTS(STR(?relation),str(mr:)) ) #' #' }" #' ) #' #' suppressMessages(out <- rdflib::rdf_query(x, query, data.frame = FALSE)) #' #' # Leave out geom, no idea how to do it in SPARQL #' # out <- subset(out, out$relation != "http://marineregions.org/ns/ontology#hasGeometry") #' #' # 2. Get all placetypes and labels of relations #' query2 <- paste0( #' mr_prefixes(), #' " #' SELECT ?mrgid ?placetype ?label #' WHERE { #' ?mrgid a ?placetype. #' FILTER( STRSTARTS(STR(?placetype),str(mrt:)) ) #' #' ?mrgid skos:prefLabel ?label. #' #' #' } #' " #' ) #' #' suppressMessages(labels <- rdflib::rdf_query(x, query2, data.frame = FALSE)) #' #' #' # 3. Merge dataframes, rename and rearrange to provide human-readable dataset #' out <- merge(out, labels) #' #' out <- data.frame( #' relation = gsub("http://marineregions.org/ns/ontology#", "mr:", out$relation), #' label = out$label, #' placetype = gsub("http://marineregions.org/ns/placetypes#", "mrt:", out$placetype), #' mrgid = out$mrgid #' ) #' #' out <- out[order(out$relation, decreasing = FALSE), ] #' #' # End #' tidyr::as_tibble(out) #' #' }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.