DBNetz_railwayStationNodes.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // head of html
  3. echo '<!DOCTYPE html>
  4. <html lang="cs" dir="ltr">
  5. <head>
  6. <title>Deutschen Bahn Streckennetz to RDF</title>
  7. <meta charset="utf-8">
  8. <meta name="robots" content="index,follow">
  9. <meta name="author" content="Jáchym Kellar">
  10. <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"> -->
  11. <style>
  12. body{
  13. margin-top: 80px;
  14. text-align: center;
  15. }
  16. .start{
  17. font-size: 28px;
  18. text-decoration: none;
  19. letter-spacing: 1px;
  20. }
  21. a{
  22. text-decoration: none;
  23. }
  24. a:hover{
  25. text-decoration: underline;
  26. }
  27. .pozn{
  28. font-size: 13px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <h2>Deutsche Bahn dataset &rarr; SPOI data model</h2>
  34. <h4>Deutsche Bahn: <a href="http://data.deutschebahn.com/dataset/data-streckennetz" target="_blank">http://data.deutschebahn.com/dataset/data-streckennetz</a></h4>
  35. <h4>SPOI: <a href="http://sdi4apps.eu/spoi/" target="_blank">http://sdi4apps.eu/spoi</a></h4>
  36. <br><br>
  37. ';
  38. // click to start transformation -> reload page
  39. echo '<a href="railwayStationNodes.php?transform=start" class="start">Start</a>';
  40. echo '<br><span class="pozn">(this may take a while)</span>';
  41. // should start the transformation? - get method
  42. if (IsSet($_GET['transform'])){
  43. $status=$_GET['transform'];
  44. if($status == "start"){
  45. transform();
  46. }
  47. }
  48. // end of html document
  49. echo ' </body>
  50. </html>
  51. ';
  52. // download and transform data - main
  53. function transform(){
  54. // download data
  55. file_put_contents("DB-Netz_INSPIRE.zip", fopen("http://download-data.deutschebahn.com/static/datasets/streckennetz/DB_Inspire_GeoJSON.zip", 'r'));
  56. // filesize of downloaded archive
  57. $filesize = filesize("DB-Netz_INSPIRE.zip");
  58. echo "<br><br>Zip archive file size: ".format_size($filesize)."<br>";
  59. // unzip archive
  60. $zip = new ZipArchive;
  61. $res = $zip->open('DB-Netz_INSPIRE.zip');
  62. if ($res === TRUE) {
  63. // extract only railwayStationNodes
  64. $zip->extractTo(getcwd()."/", "railwayStationNodes.geojson");
  65. $zip->close();
  66. } else {
  67. echo 'Extract - error<br>';
  68. }
  69. // delete archive
  70. unlink('DB-Netz_INSPIRE.zip');
  71. // filesize of geojson
  72. $filesize = filesize("railwayStationNodes.geojson");
  73. echo "GeoJson file size: ".format_size($filesize)."<br>";
  74. // create rdf
  75. $soubor = fopen("railwayStationNodes.rdf", "w");
  76. // write head of rdf
  77. fwrite($soubor, '<?xml version="1.0" encoding="utf-8"?');
  78. fwrite($soubor, ">\r\n");
  79. fwrite($soubor, '<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/"
  80. xmlns:dcterms="http://purl.org/dc/terms/1.1/"
  81. xmlns:foaf="http://xmlns.com/foaf/0.1/"
  82. xmlns:geos="http://www.opengis.net/ont/geosparql#"
  83. xmlns:owl="http://www.w3.org/2002/07/owl#"
  84. xmlns:poi="http://www.openvoc.eu/poi#"
  85. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  86. xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  87. xmlns:sf="http://www.opengis.net/ont/sf#"
  88. xmlns:skos="http://www.w3.org/2004/02/skos/core#">
  89. ');
  90. // load geojson
  91. $string = file_get_contents("railwayStationNodes.geojson");
  92. $json = json_decode($string, true);
  93. echo 'Memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB<br>";
  94. // loop a transform data
  95. foreach($json['features'] as $item) {
  96. $coordinate1 = $item['geometry']['coordinates'][0];
  97. $coordinate2 = $item['geometry']['coordinates'][1];
  98. $id = $item['properties']['id'];
  99. fwrite($soubor, ' <rdf:Description rdf:about="http://www.sdi4apps.eu/poi/#'.$id.'">');
  100. fwrite($soubor, "\r\n");
  101. $label = $item['properties']['geographicalName'];
  102. $label = trim($label);
  103. fwrite($soubor, ' <rdfs:label xml:lang="de">'.$label.'</rdfs:label>');
  104. fwrite($soubor, "\r\n");
  105. fwrite($soubor, ' <geos:asWKT rdf:datatype="http://www.openlinksw.com/schemas/virtrdf#Geometry">POINT('.$coordinate1.' '.$coordinate2.')</geos:asWKT>');
  106. fwrite($soubor, "\r\n");
  107. fwrite($soubor, ' <poi:class rdf:resource="http://gis.zcu.cz/SPOI/Ontology#transportation"/>');
  108. //fwrite($soubor, ' <poi:category>railway</poi:category>');
  109. fwrite($soubor, "\r\n");
  110. fwrite($soubor, ' <poi:class rdf:resource="http://gis.zcu.cz/SPOI/Ontology#railway_station"/>');
  111. //fwrite($soubor, ' <poi:categoryOSM>railway.station</poi:categoryOSM>');
  112. fwrite($soubor, "\r\n");
  113. //fwrite($soubor, ' <poi:categoryWaze rdf:resource="http://www.openvoc.eu/waze_classification#Transportation"/>');
  114. //fwrite($soubor, "\r\n");
  115. fwrite($soubor, ' <geos:sfWithin rdf:resource="http://dbpedia.org/resource/Germany"/>');
  116. fwrite($soubor, "\r\n");
  117. fwrite($soubor, ' <geos:sfWithin rdf:resource="http://www.geonames.org/2921044"/>');
  118. fwrite($soubor, "\r\n");
  119. fwrite($soubor, ' <dc:identifier rdf:resource="http://www.sdi4apps.eu/poi/#'.$id.'"/>');
  120. fwrite($soubor, "\r\n");
  121. fwrite($soubor, ' <dc:publisher>SPOI (http://sdi4apps.eu/spoi)</dc:publisher>');
  122. fwrite($soubor, "\r\n");
  123. fwrite($soubor, ' <dc:title xml:lang="de">'.$label.'</dc:title>');
  124. fwrite($soubor, "\r\n");
  125. fwrite($soubor, ' <dc:rights rdf:resource="https://creativecommons.org/licenses/by/4.0/"/>');
  126. fwrite($soubor, "\r\n");
  127. fwrite($soubor, ' <dc:source rdf:resource="http://data.deutschebahn.com/"/> ');
  128. fwrite($soubor, "\r\n");
  129. $date = getdate();
  130. fwrite($soubor, ' <dcterms:created rdf:datatype="http://www.w3.org/2001/XMLSchema#date">'.$date['year']."-".$date['mon']."-".$date['mday'].'</dcterms:created>');
  131. fwrite($soubor, "\r\n");
  132. fwrite($soubor, ' </rdf:Description>');
  133. fwrite($soubor, "\r\n");
  134. }
  135. fwrite($soubor, "</rdf:RDF>");
  136. fclose($soubor);
  137. // delete json
  138. unlink('railwayStationNodes.geojson');
  139. // link to download rdf
  140. echo '<a href="railwayStationNodes.rdf" target="_blank">Download RDF file</a>';
  141. // filesize of rdf
  142. $filesize = filesize("railwayStationNodes.rdf");
  143. echo " (".format_size($filesize).")<br>";
  144. }
  145. // filesize function
  146. function format_size($size) {
  147. $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
  148. if ($size == 0) { return('n/a'); } else {
  149. return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
  150. }
  151. ?>