Reisezentren.php 6.7 KB

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