]> marstr Code Repo - orthographic-api/commitdiff
Initial commit with PHP script
authorMarStr <marcus@marstr.online>
Mon, 19 Aug 2024 13:15:38 +0000 (15:15 +0200)
committerMarStr <marcus@marstr.online>
Mon, 19 Aug 2024 13:15:38 +0000 (15:15 +0200)
index.php [new file with mode: 0644]

diff --git a/index.php b/index.php
new file mode 100644 (file)
index 0000000..9b6768e
--- /dev/null
+++ b/index.php
@@ -0,0 +1,43 @@
+<?php
+
+// Our API endpoint to either acquire non-existing, or update existing OSM data files.
+// At the end, provide the XML data to the Orthographic Python client.
+
+
+// The folder where we keep all OSM XMLs
+$root_folder = "/server/osm/";
+
+if (isset($_POST))
+{
+       // Convert the JSON in POST to an array
+       $json = json_decode(file_get_contents('php://input'), true);
+       $filename = $root_folder . $json["tile_lat"] . "-" . $json["square_lat"] . "_" . $json["tile_lng"] . "-" . $json["square_lng"] . ".xml";
+       $url = "http://www.overpass-api.de/api/xapi?*[bbox=".$json["bbox"]["lng"].",".$json["bbox"]["lat"].",".$json["bbox"]["lng_b"].",".$json["bbox"]["lat_b"]."]";
+
+       // Check if the file we are looking for already exists
+       $t_exists = file_exists($filename);
+       
+       // If the file exists, check for its age. If it is older than 30 days, update the file.
+       if ($t_exists == true)
+       {
+               $t = filemtime($filename);
+               if(strtotime($t) < (time()-2592000))
+               {
+                       $ovp = file_get_contents($url);
+                       file_put_contents($filename, $ovp);
+               }
+       }
+
+       // If, however, the file does not exist, acquire it from Overpass API
+       if ($t_exists == false)
+       {
+               $ovp = file_get_contents($url);
+               file_put_contents($filename, $ovp);
+       }
+
+       // Offer the content as stream for Orthographic to download
+       $xml = file_get_contents($filename);
+       echo $xml;
+}
+
+?>