govdata mit SPARQL anfragen#

https://rdflib.dev/sparqlwrapper/doc/1.8.5/main.html

#!pip install sparqlwrapper

SELECT#

SELECT gibt “Zeilen” einer Datenbank zurück.

from SPARQLWrapper import SPARQLWrapper, JSON, JSONLD, CSV, TURTLE
from rdflib import Graph

queryString_SELECT = """
SELECT * 
WHERE { ?s ?p ?o. }
LIMIT 3
"""

sparql_SELECT = SPARQLWrapper("https://www.govdata.de/sparql")
sparql_SELECT.setReturnFormat(CSV)
sparql_SELECT.setQuery(queryString_SELECT)
ret_SELECT = sparql_SELECT.query()
print(ret_SELECT)
<SPARQLWrapper.Wrapper.QueryResult object at 0x00007FEAD07CACD0>
{"requestedFormat" : 'csv',
"response (a file-like object, as return by the urllib2.urlopen library call)" : {
	"url" : "https://www.govdata.de/sparql?query=%0ASELECT+%2A+%0AWHERE+%7B+%3Fs+%3Fp+%3Fo.+%7D%0ALIMIT+3%0A&format=csv&output=csv&results=csv",
	"code" : "200",
	"headers" : Date: Wed, 05 Jul 2023 04:57:06 GMT
Server: Apache
Strict-Transport-Security: max-age=31536000; includeSubDomains
Fuseki-Request-Id: 300678
Vary: Accept,Accept-Encoding,Accept-Charset,Origin,Access-Control-Request-Method,Access-Control-Request-Headers
Cache-Control: must-revalidate,no-cache,no-store
Pragma: no-cache
Content-Type: text/csv; charset=utf-8
Connection: close
Transfer-Encoding: chunked

}}
results_SELECT = sparql_SELECT.query().convert()
results_SELECT
b's,p,o\r\nurn:uuid:a7eab660-7a93-506b-0ae0-12a7aeff289b,http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://www.w3.org/2006/vcard/ns#Organization\r\nurn:uuid:a7eab660-7a93-506b-0ae0-12a7aeff289b,http://www.w3.org/2006/vcard/ns#fn,GISAdminChemnitz\r\nurn:uuid:732b6cb7-ff7f-066b-d5e8-a0732695c0a3,http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://www.w3.org/2006/vcard/ns#Organization\r\n'
# alternativ query und convert in einem Rutsch
results_SELECT = sparql_SELECT.queryAndConvert()
results_SELECT
b's,p,o\r\nurn:uuid:a7eab660-7a93-506b-0ae0-12a7aeff289b,http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://www.w3.org/2006/vcard/ns#Organization\r\nurn:uuid:a7eab660-7a93-506b-0ae0-12a7aeff289b,http://www.w3.org/2006/vcard/ns#fn,GISAdminChemnitz\r\nurn:uuid:732b6cb7-ff7f-066b-d5e8-a0732695c0a3,http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://www.w3.org/2006/vcard/ns#Organization\r\n'

CONSTRUCT#

CONSTRUCT gibt einen Graphen zurück. Für uns die sauberere Version, da wir in der Graphenwelt bleiben, und insbesondere auch den Rückgabe-Graphen mit SPARQL weiter anfragen können.

queryString_CONSTRUCT = """
CONSTRUCT { ?s ?p ?o. } 
WHERE { ?s ?p ?o. }
LIMIT 3
"""

sparql_CONSTRUCT = SPARQLWrapper("https://www.govdata.de/sparql")
sparql_CONSTRUCT.setReturnFormat(JSONLD)
sparql_CONSTRUCT.setQuery(queryString_CONSTRUCT)
results_CONSTRUCT = sparql_CONSTRUCT.query().convert()
results_CONSTRUCT
<Graph identifier=Neb63d7561c314ed5a7fcd7682df5f638 (<class 'rdflib.graph.ConjunctiveGraph'>)>
for row in results_CONSTRUCT:
    print(row)
(rdflib.term.URIRef('urn:uuid:732b6cb7-ff7f-066b-d5e8-a0732695c0a3'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/2006/vcard/ns#Organization'))
(rdflib.term.URIRef('urn:uuid:a7eab660-7a93-506b-0ae0-12a7aeff289b'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/2006/vcard/ns#Organization'))
(rdflib.term.URIRef('urn:uuid:a7eab660-7a93-506b-0ae0-12a7aeff289b'), rdflib.term.URIRef('http://www.w3.org/2006/vcard/ns#fn'), rdflib.term.Literal('GISAdminChemnitz', lang='de'))

Anfrage zu “Abfall”#

queryString_abfall = """PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dct: <http://purl.org/dc/terms/> 
PREFIX dcatde: <http://dcat-ap.de/def/dcatde/>

CONSTRUCT {
?expression
  <#access> ?a_url . 
}

WHERE {
    ?expression a dcat:Dataset ;
        dcat:keyword "Abfall" .
    ?expression
        dcat:distribution ?manifestation . 
    ?manifestation
        dcat:accessURL ?a_url ; 
        dct:format <http://publications.europa.eu/resource/authority/file-type/CSV> .
}
LIMIT 10"""
sparql_abfall = SPARQLWrapper("https://www.govdata.de/sparql")
sparql_abfall.setReturnFormat(JSONLD)
sparql_abfall.setQuery(queryString_abfall)

results_abfall = sparql_abfall.queryAndConvert()
type(results_abfall), len(results_abfall)
(rdflib.graph.ConjunctiveGraph, 10)
for row in results_abfall:
    print(row)
(rdflib.term.URIRef('https://www.opendata-hro.de/dataset/41e083a5-862a-4c9e-abe8-3427ac61b83b'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://geo.sv.rostock.de/download/opendata/glascontainer/glascontainer.csv'))
(rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/38c41ad8-c47b-4da8-9f2c-df80db453c54'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/38c41ad8-c47b-4da8-9f2c-df80db453c54/resource/d64024fd-d40d-448f-bada-318f478335d1/download/2019_sh_abfall_bilanz.csv'))
(rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/89acc463-746c-4976-b276-1580cb924bfd'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/89acc463-746c-4976-b276-1580cb924bfd/resource/df75ea3b-f1ce-404f-ae37-60adf88c1955/download/2021_sh_abfall_bilanz.xlsx'))
(rdflib.term.URIRef('https://www.opendata-hro.de/dataset/e20b32ea-1e32-45c4-8305-2c2b8110a5bc'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://geo.sv.rostock.de/download/opendata/altkleidercontainer/altkleidercontainer.csv'))
(rdflib.term.URIRef('https://www.opendata-hro.de/dataset/3d57acf5-d768-497a-81ff-14a77ce59d85'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://geo.sv.rostock.de/download/opendata/papiercontainer/papiercontainer.csv'))
(rdflib.term.URIRef('https://www.opendata-hro.de/dataset/934ec65d-398b-49d0-9833-9aa713c0ff87'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://geo.sv.rostock.de/download/opendata/abfallbehaelter/abfallbehaelter.csv'))
(rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/bd97d5bd-9178-4ffa-b9b9-e2aff8dc93b2'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/bd97d5bd-9178-4ffa-b9b9-e2aff8dc93b2/resource/37c6af49-1f68-498b-915a-11940c91860b/download/2019_sh_abfall_bilanz.csv'))
(rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/a4f4433a-e7c7-3903-9644-b49ec0b70c42'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://www.kiel.de/opendata/kiel_umwelt_klima_abfall_in_tonnen.csv'))
(rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/89acc463-746c-4976-b276-1580cb924bfd'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://opendata.schleswig-holstein.de/dataset/89acc463-746c-4976-b276-1580cb924bfd/resource/9508bc2f-79af-4aed-8fd6-c43162ac2b63/download/2021_sh_abfall_bilanz.csv'))
(rdflib.term.URIRef('https://www.opendata-hro.de/dataset/efdcff26-4066-40ad-9c30-6b4d24b9024b'), rdflib.term.URIRef('http://server/unset-base/#access'), rdflib.term.URIRef('https://geo.sv.rostock.de/download/opendata/recyclinghoefe/recyclinghoefe.csv'))