Je kunt niet meer dan 25 onderwerpen selecteren Onderwerpen moeten beginnen met een letter of nummer, kunnen streepjes bevatten ('-') en kunnen maximaal 35 tekens lang zijn.

75 regels
2.4 KiB
PHP

<?php
require 'required.php';
use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
// 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);
}
$userlocation = GeoLocation::fromDegrees($lat, $long);
$searchbounds = $userlocation->boundingCoordinates(1.2, 'km');
$finallat = $lat;
$finallong = $long;
$failtowater = false;
if (!$database->has('terrain', ['AND' => [
'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees(),
'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees(),
'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees(),
'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees()]
])) {
$failtowater = 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"
];
if ($failtowater) {
$terrainid = 0;
} else {
$terrainid = (int) $database->select('terrain', 'type', ['AND' => [
'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees(),
'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees(),
'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees(),
'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees()]
])[0];
if ($terrainid == 14) {
$terrainid = 13;
}
}
$out = ["status" => "OK", "type" => $terrainid, "latitude" => $finallat, "longitude" => $finallong, "name" => $terrainstrings[$terrainid]];
echo json_encode($out);