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.

terrain.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. require 'required.php';
  3. use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
  4. // Validate input
  5. if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
  6. sendError("Missing information.", true);
  7. }
  8. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) {
  9. sendError("Latitude (lat) is in the wrong format.", true);
  10. }
  11. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) {
  12. sendError("Longitude (long) is in the wrong format.", true);
  13. }
  14. // Round to 2 digits (approx. 1.1km)
  15. $lat = (float) number_format((float) $VARS['lat'], 2, '.', '');
  16. $long = (float) number_format((float) $VARS['long'], 2, '.', '');
  17. if (abs($lat) > 90.00 || abs($long) > 180.00) {
  18. sendError("Coordinates out-of-bounds. Are you sure you're on Earth?", true);
  19. }
  20. $userlocation = GeoLocation::fromDegrees($lat, $long);
  21. $searchbounds = $userlocation->boundingCoordinates(1.2, 'km');
  22. $finallat = $lat;
  23. $finallong = $long;
  24. $failtowater = false;
  25. if (!$database->has('terrain', ['AND' => [
  26. 'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees(),
  27. 'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees(),
  28. 'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees(),
  29. 'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees()]
  30. ])) {
  31. $failtowater = true;
  32. }
  33. $terrainstrings = [
  34. 0 => "Water",
  35. 1 => "Evergreen Needleleaf Forest",
  36. 2 => "Evergreen Broadleaf Forest",
  37. 3 => "Deciduous Needleleaf Forest",
  38. 4 => "Deciduous Broadleaf Forest",
  39. 5 => "Mixed Forest",
  40. 6 => "Woodland",
  41. 7 => "Wooded Grassland",
  42. 8 => "Closed Shrubland",
  43. 9 => "Open Shrubland",
  44. 10 => "Grassland",
  45. 11 => "Cropland",
  46. 12 => "Bare Ground",
  47. 13 => "Urban and Built"
  48. ];
  49. if ($failtowater) {
  50. $terrainid = 0;
  51. } else {
  52. $terrainid = (int) $database->select('terrain', 'type', ['AND' => [
  53. 'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees(),
  54. 'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees(),
  55. 'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees(),
  56. 'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees()]
  57. ])[0];
  58. if ($terrainid == 14) {
  59. $terrainid = 13;
  60. }
  61. }
  62. $out = ["status" => "OK", "type" => $terrainid, "latitude" => $finallat, "longitude" => $finallong, "name" => $terrainstrings[$terrainid]];
  63. echo json_encode($out);