You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

places.php 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Takes the latitude and longitude and gets nearby places from OSM.
  4. *
  5. * Uses WGS84 in the DD.DD format, because I say so.
  6. */
  7. require 'required.php';
  8. use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
  9. if (is_empty($VARS['lat'])) {
  10. sendError("Missing required latitude (lat) variable.", true);
  11. }
  12. if (is_empty($VARS['long'])) {
  13. sendError("Missing required longitude (long) variable.", true);
  14. }
  15. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{1,}/', $VARS['lat'])) {
  16. sendError("Latitude (lat) is in the wrong format, or does not have enough precision (DD.DD, at least 2 decimal places.", true);
  17. }
  18. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{1,}/', $VARS['long'])) {
  19. sendError("Longitude (long) is in the wrong format, or does not have enough precision (DD.DD, at least 2 decimal places.", true);
  20. }
  21. $lat = number_format((float) $VARS['lat'], 5, '.', '');
  22. $long = number_format((float) $VARS['long'], 5, '.', '');
  23. $radius = 5;
  24. if (!is_empty($VARS['radius']) && is_numeric($VARS['radius'])) {
  25. $radius = floatval($VARS['radius']);
  26. }
  27. $userlocation = GeoLocation::fromDegrees($VARS['lat'], $VARS['long']);
  28. $searchbounds = $userlocation->boundingCoordinates($radius, 'miles');
  29. // Get the points halfway between the bounds
  30. $lathalf = abs($searchbounds[1]->getLatitudeInDegrees() - $searchbounds[0]->getLatitudeInDegrees()) / 2;
  31. $lonhalf = abs($searchbounds[1]->getLongitudeInDegrees() - $searchbounds[0]->getLongitudeInDegrees()) / 2;
  32. $places1 = $database->select('places', '*', ['AND' => [
  33. 'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees(),
  34. 'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees() - $lathalf, // 0 - .5
  35. 'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees(),
  36. 'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees() - $lonhalf], // 0 - .5
  37. "LIMIT" => 50]);
  38. $places2 = $database->select('places', '*', ['AND' => [
  39. 'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees() + $lathalf, // .5 - 1
  40. 'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees(),
  41. 'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees() + $lonhalf, // .5 - 1
  42. 'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees()],
  43. "LIMIT" => 50]);
  44. $places3 = $database->select('places', '*', ['AND' => [
  45. 'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees(),
  46. 'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees() - $lathalf, // 0 - .5
  47. 'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees() + $lonhalf, // .5 - 1
  48. 'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees()],
  49. "LIMIT" => 50]);
  50. $places4 = $database->select('places', '*', ['AND' => [
  51. 'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees() + $lathalf, // .5 - 1
  52. 'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees(),
  53. 'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees(),
  54. 'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees() - $lonhalf], // 0 - .5
  55. "LIMIT" => 50]);
  56. $places = array_merge($places1, $places2, $places3, $places4);
  57. $geo['name'] = "Places";
  58. $geo['type'] = 'FeatureCollection';
  59. $geo['features'] = [];
  60. foreach ($places as $place) {
  61. $geo['features'][] = array("type" => "Feature",
  62. "geometry" => [
  63. "type" => "Point",
  64. "coordinates" => [
  65. floatval($place['longitude']),
  66. floatval($place['latitude'])
  67. ]
  68. ],
  69. "properties" => [
  70. "osm_id" => intval($place['osmid']),
  71. "name" => ($place['name'] == '' ? null : $place['name'])
  72. ]
  73. );
  74. }
  75. $out = json_encode($geo);
  76. if ($out == false) {
  77. sendError("Server error.");
  78. } else {
  79. echo $out;
  80. }