CALCULATE LINE OF SIGHT FROM POINT TO POINT USING GDAL ON LEAFLET | |
This blog illustrates the methods and procedures involved in the calculation of Line of Sight (LOS). Line of sight helps in the calculation of intervisibility between two points and is defined as the path followed by a signal from an emitter at position A to reach an observer at position B which are a certain distance apart. If this line is blocked by the geographical relief/terrain, then intervisibility is lost. The following graphs show line of sight with both possibilities. | |
![]() | ![]() |
For this calculation, we need DEM files which are available online for the whole world. This files have the AMSL values for every x meters depending on its spatial resolution. How to retrieve these values on a web page has already been explained in the previous blog on displaying altitude on map. In this blog, I will be using Java language as scripting language and GDAL java library for determining the status of LOS between two given points. If you do not know Java language, you can go through Guru99 for an overview of Java flavour. Following pre-requisites are to be fulfilled before proceeding to the coding part.
| |
LINE OF SIGHT CALCULATION METHODOLOGY | |
Consider two points on earth as A (emitter) and B (observer) which are x meter apart. We need to find the intervibility of these two points. We would draw a line connecting these two points as shown below. This line is called the Line of Sight. | |
![]() | |
This Line of Sight is split into n equidistant number of points depending on the spatial resolution of DEM or user requirement (using GeographicLib-Java-1.47.jar) and at each point the value of elevation is determined using the same process explain in the previous blog on displaying altitude on map. Simulteneously, the height of the line of sight is calculated using the simple mathematics. Both these elevation and height of LOS is compared and at any point if the value of elevation is more than the height of LOS, then A and B are said to be invisible or else they achieve intervisibility. The right side diagrams explains it. |
![]() |
Let us try to achieve this concept through GDAL and Geographiclib in Java. | |
You need to add gdal.jar and GeographicLib-Java-1.47.jar files in the JAVA BUILD PATH Library location.
| |
Create a java class called Coordinate.java and create class variables like latitude, longitude and altitude along with constructors and setters/getters as follows:
package xyz;
public class Coordinate {
private double lat;
private double lon;
private double height = 0; //initialize to 0;
public Coordinate(){
}
public Coordinate(double height){
this.height = height;
}
public Coordinate(int height){
this.height = height;
}
public Coordinate(double lat, double lon){
this.lat = lat;
this.lon = lon;
}
public Coordinate(double lat, double lon, double height){
this.lat = lat;
this.lon = lon;
this.height = height;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
Copy the following Code in your GDALLOS.java file and create setters/getters as shown below: package xyz; import java.util.ArrayList; import java.util.Iterator; import net.sf.geographiclib.*; import org.gdal.gdal.*; import org.gdal.gdalconst.gdalconstConstants; public class GDALLOS { private Coordinate coordTransmitter; // Transmitter Location private Coordinate coordObs; // Observer Location private int obsHeight = 8000; // Default altitude private double transOffset = 0.0d; // Transmitter height above ground private int accuracyDem = 30; // DEM resolution protected final static String GDEM_location = "D:\\GDEMv2\\TIF\\GDemV2.vrt"; // DEM VRT location private static Dataset hDataset = null; // DEM GDAL dataset initialised to null private final static Geodesic geod = Geodesic.WGS84;//Default datum private static void regDataset(){ //method to initialise static dataset only once. if(hDataset==null){ gdal.AllRegister(); //Registers GDAL hDataset = gdal.Open(GDEM_location, gdalconstConstants.GA_ReadOnly); } } public Viewshed(){ // Default constructor regDataset(); } public Coordinate getCoordTransmitter() { return coordTransmitter; } public void setCoordTransmitter(Coordinate coordTransmitter) { this.coordTransmitter = coordTransmitter; } public Coordinate getCoordObs() { return coordObs; } public void setCoordObs(Coordinate coordObs) { this.coordObs = coordObs; } public int getObsHeight() { return obsHeight; } public void setObsHeight(int obsHeight) { this.obsHeight = obsHeight; } public double getTransOffset() { return transOffset; } public void setTransOffset(double transOffset) { this.transOffset = transOffset; } public int getAccuracyDem() { return accuracyDem; } public void setAccuracyDem(int accuracyDem) { this.accuracyDem = accuracyDem; } } The following methods are added to the above GDALLOS.java files.
| |
You call pass parameters through Ajax calls using EventListener either click or mousemove on your GIS web clients. Remember to check for cross-origin CORS if web client and the above codes run on different servers. Remember that while working with java code along with GDAL on web aplications, you need to restart the server every time you make some changes with Java codes else it would show UnsatisfiedLinkError from GDAL. The above mentioned procedure for calculation of LOS will give good accuracy for upto 20-30 km only. If the requirement exists for LOS calculation beyond 30 Km then the effect of earth curvature and atmospheric refrection need to be accounted for. |