Skip to content

Commit

Permalink
Resolves #5
Browse files Browse the repository at this point in the history
- Modified Tile to have a function to get height by double lat/lon.
- Renamed Xyz to Vector3D and added some useful functions to it.
- Implemented the main calculation for CanSeeCalculator.
- Added examples to readme.
  • Loading branch information
simonadamprog committed Oct 16, 2021
1 parent b7ec5f0 commit 27246e8
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 42 deletions.
18 changes: 18 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,21 @@ java -jar target/hgt-map-renderer-0.0.1-SNAPSHOT.jar "contour" "<path-to-hgt-dat
```

![Contour lines](https://github.com/dodie/hgt-map-renderer/blob/master/docs/contour.png "Contour lines")

## Can-See Calculation

The "cansee" command tells you if a point in the 3D space is visible from the viewpoint.
The inputs are the two coordinates with height data: Viewpoint(lat, lon, height), WatchedPoint(lat, lon, height) in this order.

In the following example we try to see a point at 500m above Pásztó from 500m above Verpelét unsuccessfully because Kékes blocks the view:
```
java -jar target/hgt-map-renderer-0.0.1-SNAPSHOT.jar "cansee" "<path-to-hgt-data>" "44" "16" "48" "22" "47.84907" "20.19078" "500" "47.91870" "19.70804" "500"
```

Int the next example the viewpoint is on the lookout tower in Kékes, and the target is the reservoir in Nagyréde.
The point is visible.
```
java -jar target/hgt-map-renderer-0.0.1-SNAPSHOT.jar "cansee" "<path-to-hgt-data>" "44" "16" "48" "22" "47.87231" "20.00913" "1050" "47.78333" "19.83700" "200"
```

Warning! This method does not yet calculate with the Earth's curve.
14 changes: 14 additions & 0 deletions src/main/java/hu/awm/srtm/data/hgt/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public double getMaxElevation() {
return maxElevation;
}

public double elevationByExactCoordinates(final double lat, final double lon) {
if (lat < this.lat || lat >= this.lat + 1 || lon < this.lon || lon >= this.lon + 1) {
throw new IllegalArgumentException("{lat: " + lat + " lon: " + lon + "} is not in tile of {lat: "
+ this.lat + " lon: " + this.lon + "}");
}
double latDelta = lat - this.lat;
double lonDelta = lon - this.lon;

int row = (int)(RESOLUTION * latDelta);
int col = (int)(RESOLUTION * lonDelta);

return elevation(row, col);
}

private static String createExceptionMessage(int row, int col) {
return "Query: " + row + "/" + col;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ else if (eyeHeight < targetVisibleHeight) {
}

//Based on https://github.com/dizzib/earthcalc.
static double calculateTargetHiddenHeight(double eyeHeight, int targetDistance) {
public static double calculateTargetHiddenHeight(double eyeHeight, double targetDistance) {
final int EARTH_RADIUS_KM = 6371;
final double eyeHeightKm = eyeHeight * 0.001;
final double targetDistanceKm = targetDistance * 0.001;
Expand Down
38 changes: 32 additions & 6 deletions src/main/java/hu/awm/srtm/tools/cansee/CanSeeCalculator.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
package hu.awm.srtm.tools.cansee;

import hu.awm.srtm.data.hgt.Tile;
import hu.awm.srtm.data.hgt.TileMap;
import hu.awm.srtm.map.contour.ContourCalculator;

public class CanSeeCalculator {

private Xyz from;
private Xyz to;
private Vector3D from;
private Vector3D to;
private TileMap tileMap;

public CanSeeCalculator() {

}

public void setCoordinates(Double fromLat, Double fromLon, Double fromHeight, Double toLat, Double toLon, Double toHeight) {
this.from = new Xyz(fromLat, fromLon, fromHeight);
this.to = new Xyz(toLat, toLon, toHeight);
this.from = new Vector3D(fromLat, fromLon, fromHeight);
this.to = new Vector3D(toLat, toLon, toHeight);
}

public void setTileMap(TileMap tileMap) {
this.tileMap = tileMap;
}

public Sight calculateSight() {
// TODO: Replace this with the correct math.
return new Sight(null, null, null);
Vector3D loseSightCoordinate = null;
Vector3D directionVector = to.subtract(from);
double stepCount = Math.max(Math.abs(to.getLat() - from.getLat()) * Tile.RESOLUTION,
Math.abs(to.getLon() - from.getLon()) * Tile.RESOLUTION) * 2;
Vector3D stepVector = directionVector.getInstanceResizedToLength(directionVector.length() / stepCount);

for (Vector3D i = from.newInstance(); i.lengthTo(to) > stepVector.length(); i = i.add(stepVector)) {
if (!isPointVisible(i)) {
loseSightCoordinate = i.newInstance();
break;
}
}
if (loseSightCoordinate == null) {
if (!isPointVisible(to)) {
loseSightCoordinate = to.newInstance();
}
}
return new Sight(from, to, loseSightCoordinate);
}

public boolean calculateBoolean() {
Sight sight = calculateSight();
return !sight.hasBlocker();
}

private boolean isPointVisible(Vector3D pointToCheck) {
int tileLat = pointToCheck.getLat().intValue();
int tileLon = pointToCheck.getLon().intValue();
Tile tile = tileMap.getByLatLon(tileLat, tileLon);
double height = tile.elevationByExactCoordinates(pointToCheck.getLat(), pointToCheck.getLon());
return height <= pointToCheck.getHeight();
}
}
20 changes: 10 additions & 10 deletions src/main/java/hu/awm/srtm/tools/cansee/Sight.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package hu.awm.srtm.tools.cansee;

public class Sight {
private Xyz startCoordinate;
private Xyz endCoordinate;
private Xyz looseSightCoordinate;
private Vector3D startCoordinate;
private Vector3D endCoordinate;
private Vector3D looseSightCoordinate;

public Sight() {
}

public Sight(Xyz startCoordinate, Xyz endCoordinate, Xyz looseSightCoordinate) {
public Sight(Vector3D startCoordinate, Vector3D endCoordinate, Vector3D looseSightCoordinate) {
this.startCoordinate = startCoordinate;
this.endCoordinate = endCoordinate;
this.looseSightCoordinate = looseSightCoordinate;
}

public Xyz getStartCoordinate() {
public Vector3D getStartCoordinate() {
return startCoordinate;
}

public void setStartCoordinate(Xyz startCoordinate) {
public void setStartCoordinate(Vector3D startCoordinate) {
this.startCoordinate = startCoordinate;
}

public Xyz getEndCoordinate() {
public Vector3D getEndCoordinate() {
return endCoordinate;
}

public void setEndCoordinate(Xyz endCoordinate) {
public void setEndCoordinate(Vector3D endCoordinate) {
this.endCoordinate = endCoordinate;
}

public Xyz getLooseSightCoordinate() {
public Vector3D getLooseSightCoordinate() {
return looseSightCoordinate;
}

public void setLooseSightCoordinate(Xyz looseSightCoordinate) {
public void setLooseSightCoordinate(Vector3D looseSightCoordinate) {
this.looseSightCoordinate = looseSightCoordinate;
}

Expand Down
64 changes: 64 additions & 0 deletions src/main/java/hu/awm/srtm/tools/cansee/Vector3D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package hu.awm.srtm.tools.cansee;

public class Vector3D {
private Double lat;
private Double lon;
private Double height;

public Vector3D(Double lat, Double lon, Double height) {
this.lat = lat;
this.lon = lon;
this.height = height;
}

public Vector3D newInstance() {
return new Vector3D(this.lat, this.lon, this.height);
}

public Double getLat() {
return lat;
}

public Double getLon() {
return lon;
}

public Double getHeight() {
return height;
}

public Vector3D getInstanceResizedToLength(Double newLength) {
Double ratio = newLength / length();
return new Vector3D(lat * ratio, lon * ratio, height * ratio);
}

public Double length() {
return Math.sqrt(Math.pow(lat, 2.0)
+ Math.pow(lon, 2.0)
+ Math.pow(height, 2.0));
}

public static Vector3D subtract(Vector3D from, Vector3D what) {
return from.subtract(what);
}

public Vector3D subtract(Vector3D what) {
return new Vector3D(this.lat - what.getLat(),
this.lon - what.getLon(),
this.height - what.getHeight());
}

public static Vector3D add(Vector3D to, Vector3D what) {
return to.add(what);
}

public Vector3D add(Vector3D what) {
return new Vector3D(this.lat + what.getLat(),
this.lon + what.getLon(),
this.height + what.getHeight());
}

public double lengthTo(Vector3D what) {
return what.subtract(this).length();
}
}
25 changes: 0 additions & 25 deletions src/main/java/hu/awm/srtm/tools/cansee/Xyz.java

This file was deleted.

0 comments on commit 27246e8

Please sign in to comment.