kgr/autobahn/autobahn_graph.ipynb

133 lines
3.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 78,
"id": "3d4827d9",
"metadata": {},
"outputs": [],
"source": [
"from rdflib import Graph, URIRef, Literal, Namespace\n",
"from rdflib.namespace import RDF, RDFS, XSD, GEO\n",
"import json\n",
"\n",
"g = Graph()\n",
"thm = Namespace(\"https://th-mannheim.de/\")\n",
"thm_ont = Namespace(\"https://th-mannheim.de/ont/\")"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "80c6fad0",
"metadata": {},
"outputs": [],
"source": [
"def open_json(name : str):\n",
" with open(name, mode=\"r\", encoding=\"utf8\") as jsonfile:\n",
" return json.loads(jsonfile.read())"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "21dac562",
"metadata": {},
"outputs": [],
"source": [
"roads = open_json(\"data/autobahn.json\")\n",
"chargings = open_json(\"data/chargingstations.json\")\n",
"parkings = open_json(\"data/parkinglorries.json\")\n",
"roadworks = open_json(\"data/roadworks.json\")"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "cecd4a23",
"metadata": {},
"outputs": [],
"source": [
"autobahn_urls = {}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72ac9b11",
"metadata": {},
"outputs": [],
"source": [
"for autobahn in roads[\"roads\"]:\n",
" autobahn_url = thm[f\"autobahn/{autobahn}\"]\n",
" autobahn_urls[autobahn] = autobahn_url\n",
"\n",
" for parking in parkings[autobahn][\"parking_lorry\"]:\n",
"\n",
" parking_url = thm[f\"parkings/{parking[\"identifier\"]}\"]\n",
" geometry_url = thm_ont[f\"geometry/{parking[\"identifier\"]}\"]\n",
"\n",
" # Grundlegende Daten\n",
" g.add((parking_url, RDF.type, thm_ont.Parking))\n",
" g.add((parking_url, thm_ont.title, Literal(parking[\"title\"], lang=\"de\")))\n",
" g.add((parking_url, thm_ont.subtitle, Literal(parking[\"subtitle\"], lang=\"de\")))\n",
" g.add((parking_url, thm_ont.description, Literal(parking[\"subtitle\"], lang=\"de\")))\n",
"\n",
" # Zuordnung zur Autobahn\n",
" g.add((parking_url, thm.belongs, autobahn_urls[autobahn]))\n",
"\n",
" # Koordinaten\n",
" g.add((parking_url, GEO.hasGeometry, geometry_url))\n",
" g.add((parking_url, GEO.asWKT, Literal(f\"POINT({parking[\"coordinate\"][\"long\"]} {parking[\"coordinate\"][\"lat\"]})\", datatype=GEO.wktLiteratl)))\n",
"\n",
" # Zusätzliche Features (wenn mal Zeit ist \n",
" # kann man die auch in Objekte überführen, anstatt Literale zu verwenden)\n",
" for features in parking[\"lorryParkingFeatureIcons\"]:\n",
" g.add((parking_url, thm_ont.has_feature, Literal(features[\"description\"], lang=\"de\")))"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "a8ae8a6f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Graph identifier=Nfd9dbadf281b46dbbae0fa147c9bb242 (<class 'rdflib.graph.Graph'>)>"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.serialize(\"autobahn.ttl\", format=\"turtle\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv (3.14.4)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}