You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
<?php
|
|
|
|
require 'required.php';
|
|
|
|
// Validate input
|
|
if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
|
|
sendError("Missing information.", true);
|
|
}
|
|
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) {
|
|
sendError("Latitude (lat) is in the wrong format.", true);
|
|
}
|
|
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) {
|
|
sendError("Longitude (long) is in the wrong format.", true);
|
|
}
|
|
|
|
// Round to 2 digits (approx. 1.1km)
|
|
$lat = (float) number_format((float) $VARS['lat'], 2, '.', '');
|
|
$long = (float) number_format((float) $VARS['long'], 2, '.', '');
|
|
|
|
if (abs($lat) > 90.00 || abs($long) > 180.00) {
|
|
sendError("Coordinates out-of-bounds. Are you sure you're on Earth?", true);
|
|
}
|
|
|
|
$finallat = $lat;
|
|
$finallong = $long;
|
|
|
|
if ($database->has('terrain', ["AND" => ["latitude" => $lat, "longitude" => $long]])) {
|
|
// We're good
|
|
} else if ($database->has('terrain', ["AND" => ["latitude" => $lat + .01, "longitude" => $long]])) {
|
|
$finallat = $lat + 0.01;
|
|
} else if ($database->has('terrain', ["AND" => ["latitude" => $lat, "longitude" => $long + .01]])) {
|
|
$finallong = $long + 0.01;
|
|
} else {
|
|
// last resort
|
|
$tries = 0;
|
|
while (!$database->has('terrain', ["AND" => ["latitude" => $finallat, "longitude" => $finallong]]) && $tries < 5) {
|
|
$finallat = $finallat + 0.01;
|
|
$finallong = $finallong + 0.01;
|
|
$tries++;
|
|
}
|
|
if ($tries >= 5) {
|
|
sendError("Terrain data not found for the given coordinates or anything nearby.", true);
|
|
}
|
|
}
|
|
|
|
$terrainid = $database->select('terrain', 'type', ["AND" => ["latitude" => $finallat, "longitude" => $finallong]])[0];
|
|
$out = ["status" => "OK", "type" => $terrainid, "latitude" => $finallat, "longitude" => $finallong];
|
|
echo json_encode($out);
|