Last active
December 18, 2023 12:41
-
-
Save mrmascott10/0c2e8e972a0ad0d53447e1c22a297991 to your computer and use it in GitHub Desktop.
Validate a waypoint whether it's a lat lng or a postcode. postcodeToLatLng() refers to another gist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// **************************************** | |
// * Validate waypoint whether it's latlng or postcode | |
// **************************************** | |
function validateWaypoint($waypoint) { | |
if (strpos($waypoint, ",")) { | |
// ? Probably lat lng | |
$latLng = explode(",", $waypoint); | |
if (count($latLng) == 2) { | |
if ($latLng[0] >= -90 && $latLng[0] <= 90) { | |
if ($latLng[1] >= -180 && $latLng[1] <= 180) { | |
$getNearPostcode = json_decode(@file_get_contents("https://api.postcodes.io/postcodes?lon={$latLng[1]}&lat={$latLng[0]}"), true); | |
if ($getNearPostcode && $getNearPostcode['status'] == 200 && $getNearPostcode['result'] != null) { | |
$postcode = $getNearPostcode['result'][0]['postcode']; | |
} else $postcode = ''; | |
return [$postcode, $latLng]; | |
} else return false; | |
} else return false; | |
} else return false; | |
} else { | |
// ? Probably postcode | |
$validatePostcode = postcodeToLatLng($waypoint); | |
if ($validatePostcode[0] != "FAIL") { | |
return [$waypoint, $validatePostcode]; | |
} else return false; | |
} | |
} | |
// **************************************** | |
// **************************************** | |
// * Geocode one single postcode. | |
// **************************************** | |
function postcodeToLatLng($postcode) { | |
include '../../../GLOBAL_HANDLERS/DatabaseHandler.php'; | |
$po = rtrim(ltrim(strtoupper(str_replace(' ', '', $postcode)))); // Pre-validate postcodes. | |
// Check if the postcode is within our database. | |
$getLatLng = $GLOBAL->prepare("SELECT lat, lng FROM `uk_postcodes` USE INDEX(`postcodes`) WHERE `postcode` LIKE ? LIMIT 1"); | |
$getLatLng->bind_param("s", $po); | |
$getLatLng->execute(); | |
$result = $getLatLng->get_result(); | |
// If the SQL query returned a record. | |
if ($result->num_rows > 0) { | |
$location = $result->fetch_assoc(); | |
return [round($location['lat'], 6), round($location['lng'], 6)]; | |
} else { | |
// If the postcode isn't in our database then check the api. | |
// The api will also check the validity with the composer package. | |
$response = json_decode(@file_get_contents('https://api.postcodes.io/postcodes/' . urlencode($po)), true); | |
if ($response != false && $response['status'] == 200) { | |
return [round($response['result']['latitude'], 5), round($response['result']['longitude'], 5)]; | |
} else { | |
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($postcode) . "&key=AIzaSyBZr-Aaq5G2cUYe2qxW8xznEgSBnJsQIWM"; | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
$response = curl_exec($curl); | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
curl_close($curl); | |
if ($status == 200) { | |
$googleResponse = json_decode($response, true); | |
if ($googleResponse['status'] == 'OK') { | |
return [round($googleResponse['results'][0]['geometry']['location']['lat'], 5), round($googleResponse['results'][0]['geometry']['location']['lng'], 5)]; | |
} else return['FAIL', 'FAIL']; | |
} else return['FAIL', 'FAIL']; | |
} | |
} | |
return ['FAIL', 'FAIL']; | |
} | |
// **************************************** | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment