locator.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/sh
  2. PHPBIN="php -d error_reporting=0 ";
  3. if [ -f "$1" ]; then
  4. CSV=$(echo $1 | sed s/".txt"/".csv"/g);
  5. echo -n > $CSV;
  6. while read f; do
  7. ADDRESS=$(echo $f)
  8. export ADDRESS;
  9. $PHPBIN << 'EOF' >> $CSV;
  10. <?php
  11. $address = ucwords(trim(getenv("ADDRESS")));
  12. # Nominatim Usage Policy
  13. # https://operations.osmfoundation.org/policies/nominatim/
  14. sleep(1);
  15. # OpenStreetMap Nominatim API
  16. $url = 'https://nominatim.openstreetmap.org/search?q=' . urlencode($address) . '&limit=1&format=json';
  17. $ch = curl_init();
  18. # Set cURL User Agent
  19. curl_setopt($ch, CURLOPT_USERAGENT, "PostmanRuntime/7.28.4");
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. curl_setopt($ch, CURLOPT_URL, $url);
  22. $response = curl_exec($ch);
  23. curl_close($ch);
  24. # Response
  25. $a = json_decode($response, true);
  26. echo "\"$address\", ";
  27. foreach ($a as $array) {
  28. foreach ($array as $k => $v) {
  29. switch ("$k") {
  30. case "lat":
  31. echo "$v, ";
  32. break;
  33. case "lon":
  34. echo "$v";
  35. break;
  36. }
  37. }
  38. }
  39. ?>
  40. EOF
  41. echo >> $CSV;
  42. done < $1
  43. # Remove newline character at the end of a file
  44. truncate -s -1 $CSV
  45. else
  46. echo "Usage: $0 <file>";
  47. fi