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); } } $terrainstrings = [ 0 => "Water", 1 => "Evergreen Needleleaf Forest", 2 => "Evergreen Broadleaf Forest", 3 => "Deciduous Needleleaf Forest", 4 => "Deciduous Broadleaf Forest", 5 => "Mixed Forest", 6 => "Woodland", 7 => "Wooded Grassland", 8 => "Closed Shrubland", 9 => "Open Shrubland", 10 => "Grassland", 11 => "Cropland", 12 => "Bare Ground", 13 => "Urban and Built" ]; $terrainid = $database->select('terrain', 'type', ["AND" => ["latitude" => $finallat, "longitude" => $finallong]])[0]; $out = ["status" => "OK", "type" => $terrainid, "latitude" => $finallat, "longitude" => $finallong, "name" => $terrainstrings[$terrainid]]; echo json_encode($out);