Is there a way to check if there is a line of sight between two points by getting the elevation profile data via the google API?
E.g. I can get an elevation profile via this link: https://maps.googleapis.com/maps/api/elevation/json?path=36.578581,-118.291994|36.23998,-116.83171&samples=10&key=YOUR_API_KEY
The result is a json with 10 point situated between two points with it ground elevation, for ex:
{
"results" : [
{
"elevation" : 4411.94189453125,
"location" : {
"lat" : 36.578581,
"lng" : -118.291994
},
"resolution" : 19.08790397644043
},
{
"elevation" : 1658.561645507812,
"location" : {
"lat" : 36.54183904164596,
"lng" : -118.1291141726249
},
"resolution" : 9.543951988220215
},
{
"elevation" : 1085.295776367188,
"location" : {
"lat" : 36.50487579522382,
"lng" : -117.966389550823
},
"resolution" : 9.543951988220215
},
{
"elevation" : 1491.4912109375,
"location" : {
"lat" : 36.46769177008888,
"lng" : -117.8038207060907
},
"resolution" : 9.543951988220215
},
{
"elevation" : 1583.762817382812,
"location" : {
"lat" : 36.43028747728768,
"lng" : -117.6414082023003
},
"resolution" : 9.543951988220215
},
{
"elevation" : 488.8834228515625,
"location" : {
"lat" : 36.39266342953181,
"lng" : -117.4791525957044
},
"resolution" : 9.543951988220215
},
{
"elevation" : 933.0382080078125,
"location" : {
"lat" : 36.35482014117158,
"lng" : -117.3170544349424
},
"resolution" : 9.543951988220215
},
{
"elevation" : 1657.123779296875,
"location" : {
"lat" : 36.31675812816988,
"lng" : -117.1551142610472
},
"resolution" : 9.543951988220215
},
{
"elevation" : 1074.835205078125,
"location" : {
"lat" : 36.27847790807609,
"lng" : -116.9933326074523
},
"resolution" : 9.543951988220215
},
{
"elevation" : -84.51690673828125,
"location" : {
"lat" : 36.23998,
"lng" : -116.83171
},
"resolution" : 9.543951988220215
}
],
"status" : "OK"
}
Is there a mathematical formula to check if point A and point B can be seen?
I need to write the code in PHP.
Thanks for all.
Given that you are working with 2 points along a straight line I think the easiest way to deal with this would be to look at it from 2D point of view, where the x-axis refers to your sample number and the y-axis refers to your elevation.
Then we make the assumption that if the elevation at result n is higher than the straight line then the line of sight is broken.
Where:
(x1, y1) = (1, 4411.94)
(x2, y2) = (10, -84.51)
Here is a picture of the results graphed out and the line from one point to the other drawn over it:
We can see that for sample 8 and 9 the line of sight is broken.
Below is the code to find the same. It uses the basic equation of the line: y = mx + c
$results_json = json_decode('{"results":[{"elevation":4411.94189453125,"location":{"lat":36.578581,"lng":-118.291994},"resolution":19.08790397644043},{"elevation":1658.561645507812,"location":{"lat":36.54183904164596,"lng":-118.1291141726249},"resolution":9.543951988220215},{"elevation":1085.295776367188,"location":{"lat":36.50487579522382,"lng":-117.966389550823},"resolution":9.543951988220215},{"elevation":1491.4912109375,"location":{"lat":36.46769177008888,"lng":-117.8038207060907},"resolution":9.543951988220215},{"elevation":1583.762817382812,"location":{"lat":36.43028747728768,"lng":-117.6414082023003},"resolution":9.543951988220215},{"elevation":488.8834228515625,"location":{"lat":36.39266342953181,"lng":-117.4791525957044},"resolution":9.543951988220215},{"elevation":933.0382080078125,"location":{"lat":36.35482014117158,"lng":-117.3170544349424},"resolution":9.543951988220215},{"elevation":1657.123779296875,"location":{"lat":36.31675812816988,"lng":-117.1551142610472},"resolution":9.543951988220215},{"elevation":1074.835205078125,"location":{"lat":36.27847790807609,"lng``":-116.9933326074523},"resolution":9.543951988220215},{"elevation":-84.51690673828125,"location":{"lat":36.23998,"lng":-116.83171},"resolution":9.543951988220215}],"status":"OK"}');
$x1 = 0;
$x2 = count($results_json->results);
$y1 = $results_json->results[0]->elevation;
$y2 = $results_json->results[$x2 - 1]->elevation; // last result in results array
$m = ($y2 - $y1) / ($x2 - $x1); // slope of the line
$c = $y2 - ($m * $x2);
$i = 0;
foreach ($results_json->results as $result){
$elevation_of_straight_line = ($m * $i) + $c;
$elevation_of_result = $result->elevation;
if ($elevation_of_result > $elevation_of_straight_line){
echo 'line of sight broken';
echo 'Elevation of sample index '.$i.' = '.$elevation_of_result;
echo 'Elevation of line of sight = '.$elevation_of_straight_line;
}
$i++;
}
This takes out the complications brought about by looking at this problem from a 3D point of view. This has its short-comings though for situations where the curvature of the earth comes into play. This should also be scalable to as many samples as you want.