Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

an initial implementation of wrb codelists import from csv #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions code-lists/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WRB codelists

[World Reference Base](https://wrb.isric.org/) working group of IUSS maintains a set of code lists to describe soils, as part of the World Reference Base for Soil Resources (current: 4th edition 2022). This repository presents a home for identification of the concepts in those lists, as well as facilites maintenance of these lists in preparation of upcoming releases.

## csv2skos

The [code lists](./wrb-codelists.ttl) are described in RDF using the SKOS ontology. The RDF is generated from a [CSV](./wrb-codelists.csv) using a [conversion script](./csv2skos/csv2skos.py). MS Excel has been used to prepare the initial version of the CSV (Note that Excel uses ';' as a separator, where this initiative uses ',' as a separator).

## skos2html

[skos2html](./csv2skos/skos2html.py) is a small utility to generate html of the skos file for human readability.
11 changes: 11 additions & 0 deletions code-lists/csv2skos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# csv2skos

A tool to load csv data into skos

```
virtualenv .
. bin/activate
pip install -r requirements.txt
python csv2skos.py
```

42 changes: 42 additions & 0 deletions code-lists/csv2skos/csv2skos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import csv
from rdflib import Graph, URIRef, Literal, Namespace
from rdflib.namespace import DC, DCTERMS, FOAF, OWL, RDF, RDFS, SKOS, XMLNS, XSD

def valuri(data):
return "".join(x for x in data.replace(' ','-') if (x.isalnum() or x =='-'))

g = Graph()
WRB = Namespace("http://iuss-wrb.github.io/wrb#")

ont = URIRef("http://iuss-wrb.github.io/wrb#")
g.add((ont, RDF.type, OWL.Ontology))
g.add((ont, DCTERMS.description, Literal("Code lists to describe soil properties as defined by the IUSS WRB working group")))
g.add((ont, DCTERMS.creator, URIRef("https://orcid.org/0000-0003-1499-618X")))
g.add((ont, DCTERMS.rights, Literal("This ontology is distributed under Creative Commons Attribution 4.0 License - https://creativecommons.org/licenses/by/4.0")))
g.add((ont, DCTERMS.source, URIRef("https://wrb.isric.org/files/WRB_fourth_edition_2022-12-18.pdf")))
g.add((ont, DCTERMS.title, Literal("WRB code lists")))
g.add((ont, FOAF.logo, URIRef("https://wrb.isric.org/images/logo.png")))

with open('../wrb-codelists.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
cs = ""
for row in reader:
if cs != valuri(row['attribute']):
cs = valuri(row['attribute'])
cs2 = URIRef(WRB[valuri(row['attribute'])])
g.add((cs2, RDF.type, SKOS.ConceptScheme))
g.add((cs2, SKOS.prefLabel, Literal(row['attribute'])))

concept = URIRef(WRB[valuri(row['attribute'])+'/'+valuri(row['id'])])
g.add((concept, RDF.type, SKOS.Concept))
g.add((concept, SKOS.prefLabel, Literal(row['label'])))
g.add((concept, SKOS.inScheme, cs2))
g.add((concept, SKOS.notation, Literal(row['notation'])))
g.add((concept, SKOS.definition, Literal(row['definition'])))

g.bind("skos", SKOS)
g.bind("dcterms", DCTERMS)
g.bind("owl", OWL)
g.bind("wrb", WRB)
g.serialize(destination="../wrb-codelists.ttl")

1 change: 1 addition & 0 deletions code-lists/csv2skos/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rdflib
33 changes: 33 additions & 0 deletions code-lists/csv2skos/skos2html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from rdflib import Graph
from rdflib.namespace import RDF, SKOS, DCTERMS, OWL

g = Graph()
g.parse("../wrb-codelists.ttl")

ttl = "Codelists" # default title
desc = "A series of codelists" # default description

for s, p, o in g.triples((None, RDF.type, OWL.Ontology)):
ttl = g.value(s,DCTERMS.title)
desc = g.value(s,DCTERMS.description) + "<br/>"
for t in ["creator","rights","source"]:
if g.value(s,DCTERMS[t]):
desc += "<br/><b>"+t+"</b>: " + str(g.value(s,DCTERMS[t]))

html = "<h1>"+ttl+"</h1><p>"+desc+"</p>"

html += "<h2 id='#top'>Contents</h2><p><ul>"
for s, p, o in g.triples((None, RDF.type, SKOS.ConceptScheme)):
html += "<li><a href='#"+str(s)+"'>" + g.value(s,SKOS.prefLabel) + "</a></li>"
html += "</ul></p><hr/>\n"

for s, p, o in g.triples((None, RDF.type, SKOS.ConceptScheme)):
html += "<h2 id='"+str(s)+"'>" + g.value(s,SKOS.prefLabel) + "</h2>\n"
html += "<table><tr><th>Code</th><th>Label</th><th>Definition</th></tr>\n"
for s2, p2, o2 in g.triples((None, SKOS.inScheme, s)):
html += "<tr><td>"+g.value(s2,SKOS.notation)+"</td><td>"+ g.value(s2,SKOS.prefLabel) + "</td><td>" + g.value(s2,SKOS.definition) +"</td></tr>\n"
html += "<br/><a href='#top'>&#8657; Index</a><hr/>"

f = open("../index.html", "w")
f.write("<html>\n<head><title>"+ttl+"</title></head>\n<body>"+html+"</body>\n<style> body {font-family: arial} td, th {border: 1px solid gray;padding:5px} th { background-color: #efefef } table{border-collapse: collapse}</style></html>")
f.close()
Loading