Skip to content

Commit

Permalink
Merge pull request #30 from grumlimited/rg/0.6
Browse files Browse the repository at this point in the history
0.6
  • Loading branch information
gr211 authored Oct 3, 2020
2 parents 1132b21 + 4de8d7c commit d36ed7d
Show file tree
Hide file tree
Showing 14 changed files with 445 additions and 553 deletions.
50 changes: 9 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@ This library is being used on [rentbarometer.com](https://www.rentbarometer.com)

This library implements in Java lots of ideas from [Movable-Type](http://www.movable-type.co.uk/scripts/latlong.html). Many thanks.

## Changelog

### 0.5.8
* Renamed in `EarthCalc.EARTH_DIAMETER` to `EarthCalc.EARTH_RADIUS`. Thanks [pradeepmurugesan](https://github.com/pradeepmurugesan)


### 0.5.7
* Bugfix in `EarthCalc.gcdDictance` [24](https://github.com/grumlimited/geocalc/issues/24). Thanks [dusiema](https://github.com/dusiema)

### 0.5.6
* Replaced `new BoundingArea(...)` with `BoundingArea.at`
* Bugfix in `EarthCalc.pointAt`. Normalising `λ2` so that bearing starts clockwise from north

### 0.5.5
* Renamed `getVincentyFinalBearing` to `vincentyFinalBearing`
* Updated copyright

### 0.5.4
* added `EarthCalc.midPoint(p1, p2)`
* renamed `EarthCalc.pointRadialDistance()` to `EarthCalc.pointAt(...)`
* renamed `BoundingArea.boundingArea()` to `BoundingArea.around(...)`
* removed `get...()` out of `Point` and `BoundingArea`
* added maven javadoc plugin

### 0.5.3
* changed constructors to default and private visibility
* removed `get...()` keyword out of `EarthCalc` methods
* `EarthCalc.getDistance()` is now `EarthCalc.gcdDistance()`
* renamed `BoundingArea.isContainedWithin(...)` to `BoundingArea.contains(...)`

### Embed

<repositories>
Expand Down Expand Up @@ -115,7 +85,7 @@ back and forth
lng = Coordinate.fromDegrees(-0.3035466);
Point richmond = Point.at(lat, lng);

double distance = EarthCalc.gcdDistance(richmond, kew); //in meters
double distance = EarthCalc.gcd.distance(richmond, kew); //in meters

#### Harvesine formula

Expand All @@ -129,7 +99,7 @@ back and forth
lng = Coordinate.fromDegrees(-0.3035466);
Point richmond = Point.at(lat, lng);

double distance = EarthCalc.harvesineDistance(richmond, kew); //in meters
double distance = EarthCalc.harvesine.distance(richmond, kew); //in meters

#### Vincenty formula

Expand All @@ -143,7 +113,7 @@ back and forth
lng = Coordinate.fromDegrees(-0.3035466);
Point richmond = Point.at(lat, lng);

double distance = EarthCalc.vincentyDistance(richmond, kew); //in meters
double distance = EarthCalc.vincenty.distance(richmond, kew); //in meters


### Finding a point at 'distance in meters away' from a standpoint, given a bearing
Expand All @@ -156,7 +126,7 @@ back and forth
Point kew = Point.at(lat, lng);

//Distance away point, bearing is 45deg
Point otherPoint = EarthCalc.pointAt(kew, 45, 1000);
Point otherPoint = EarthCalc.gcd.pointAt(kew, 45, 1000);

### BoundingArea

Expand All @@ -168,7 +138,7 @@ other points, you need to figure out which ones are at most, say, 3000 meters aw
While this only gives an approximation, it is several order of magnitude faster
than calculating the distances from each point in the set to the reference point.

BoundingArea area = EarthCalc.boundingArea(kew, 3000);
BoundingArea area = EarthCalc.gcd.boundingArea(kew, 3000);
Point nw = area.northWest;
Point se = area.southEast;
Expand Down Expand Up @@ -205,7 +175,7 @@ you can determine whether a point is contained within that area using:
lng = Coordinate.fromDegrees(-0.3035466);
Point richmond = Point.at(lat, lng);

double bearing = EarthCalc.bearing(kew, richmond); //in decimal degrees
double bearing = EarthCalc.gcd.bearing(kew, richmond); //in decimal degrees

#### Azimuth bearing - Vincenty formula

Expand All @@ -219,7 +189,7 @@ you can determine whether a point is contained within that area using:
lng = Coordinate.fromDegrees(-0.3035466);
Point richmond = Point.at(lat, lng);

double bearing = EarthCalc.vincentyBearing(kew, richmond); //in decimal degrees
double bearing = EarthCalc.vincenty.bearing(kew, richmond); //in decimal degrees

#### Final bearing - Vincenty formula

Expand All @@ -233,7 +203,7 @@ you can determine whether a point is contained within that area using:
lng = Coordinate.fromDegrees(-0.3035466);
Point richmond = Point.at(lat, lng);

double bearing = EarthCalc.vincentyFinalBearing(kew, richmond); //in decimal degrees
double bearing = EarthCalc.vincenty.finalBearing(kew, richmond); //in decimal degrees

#### Mid point - This is the half-way point along a great circle path between the two points.

Expand All @@ -243,6 +213,4 @@ you can determine whether a point is contained within that area using:
//Richmond, London
Point richmond = Point.at(Coordinate.fromDegrees(51.4613418), Coordinate.fromDegrees(-0.3035466));

Point midPoint = EarthCalc.midPoint(richmond, kew) // Point{latitude=51.47285976194266, longitude=-0.2973770580524634}


Point midPoint = EarthCalc.gcd.midPoint(richmond, kew) // Point{latitude=51.47285976194266, longitude=-0.2973770580524634}
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation=true
19 changes: 13 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<groupId>com.grum</groupId>
<artifactId>geocalc</artifactId>
<packaging>jar</packaging>
<version>0.5.8</version>
<version>0.6</version>
<name>${project.groupId}.${project.artifactId}</name>
<url>http://www.grumlimited.co.uk</url>
<url>https://www.grumlimited.co.uk</url>

<prerequisites>
<maven>3.1.0</maven>
Expand All @@ -24,14 +24,14 @@
</licenses>
<organization>
<name>Grum Ltd</name>
<url>http://www.grumlimited.co.uk</url>
<url>https://www.grumlimited.co.uk</url>
</organization>
<developers>
<developer>
<name>GR211</name>
<email>romain.gallet__at__gmail.com</email>
<email>rgallet_at_grumlimited_co_uk</email>
<organization>Grum Limited</organization>
<organizationUrl>http://www.grumlimited.co.uk</organizationUrl>
<organizationUrl>https://www.grumlimited.co.uk</organizationUrl>
</developer>
<developer>
<name>Blackleg</name>
Expand Down Expand Up @@ -80,7 +80,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.3</version>
<version>2.8.1</version>
</plugin>
</plugins>
</build>
Expand All @@ -93,6 +93,13 @@
<version>1.7.30</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
Expand Down
42 changes: 10 additions & 32 deletions src/main/java/com/grum/geocalc/BoundingArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@

package com.grum.geocalc;

import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.val;

/**
* Represents an area (viewed rectangular shaped projected onto Earth), defined by its top left and bottom right
* coordinates
*
* @author rgallet
* Represents an area (rectangular shape projected onto Earth), defined by its top left and bottom right
* coordinates.
* <p>
* Allows for fast geo-spatial search of all points within this rectangular area.
*/
@EqualsAndHashCode
public class BoundingArea {
public final Point northEast, southWest;
public final Point southEast, northWest;
Expand Down Expand Up @@ -75,7 +77,7 @@ public boolean isContainedWithin(Point point) {
* @return true if Point point is contained withing this bounding area
*/
public boolean contains(Point point) {
boolean predicate1 = point.latitude >= this.southWest.latitude && point.latitude <= this.northEast.latitude;
val predicate1 = point.latitude >= this.southWest.latitude && point.latitude <= this.northEast.latitude;

if (!predicate1) {
return false;
Expand All @@ -85,8 +87,8 @@ public boolean contains(Point point) {

if (southWest.longitude > northEast.longitude) { //area is going across the max/min longitude boundaries (ie. sort of back of the Earth)
//we "split" the area in 2, longitude-wise, point only needs to be in one or the other.
boolean predicate3 = point.longitude <= northEast.longitude && point.longitude >= -180;
boolean predicate4 = point.longitude >= southWest.longitude && point.longitude <= 180;
val predicate3 = point.longitude <= northEast.longitude && point.longitude >= -180;
val predicate4 = point.longitude >= southWest.longitude && point.longitude <= 180;

predicate2 = predicate3 || predicate4;
} else {
Expand All @@ -95,28 +97,4 @@ public boolean contains(Point point) {

return predicate2;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (getClass() != obj.getClass()) {
return false;
}

BoundingArea other = (BoundingArea) obj;

return Objects.equals(this.northEast, other.northEast) &&
Objects.equals(this.southWest, other.southWest);
}

@Override
public int hashCode() {
int hash = 7;
hash = 13 * hash + (this.northEast != null ? this.northEast.hashCode() : 0);
hash = 13 * hash + (this.southWest != null ? this.southWest.hashCode() : 0);
return hash;
}
}
33 changes: 12 additions & 21 deletions src/main/java/com/grum/geocalc/Coordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/

package com.grum.geocalc;
import lombok.val;
import lombok.var;

import java.io.Serializable;
import java.math.BigDecimal;
Expand All @@ -40,22 +42,11 @@

/**
* Abstraction of coordinate systems (degrees, radians, dms and gps)
*
* @author rgallet
*/
abstract public class Coordinate implements Serializable {

abstract double degrees();

/**
* @return degree value
* @deprecated use degrees()
*/
@Deprecated
public double getValue() {
return degrees();
}

public static DegreeCoordinate fromDegrees(double decimalDegrees) {
return new DegreeCoordinate(decimalDegrees);
}
Expand All @@ -73,25 +64,25 @@ public static RadianCoordinate fromRadians(double radians) {
}

DMSCoordinate toDMSCoordinate() {
double _wholeDegrees = (int) degrees();
double remaining = abs(degrees() - _wholeDegrees);
double _minutes = (int) (remaining * 60);
remaining = remaining * 60 - _minutes;
double _seconds = new BigDecimal(remaining * 60).setScale(4, RoundingMode.HALF_UP).doubleValue();
val wholeDegrees = (int) degrees();
var remaining = abs(degrees() - wholeDegrees);
val minutes = (int) (remaining * 60);
remaining = remaining * 60 - minutes;
val seconds = new BigDecimal(remaining * 60).setScale(4, RoundingMode.HALF_UP).doubleValue();

return new DMSCoordinate(_wholeDegrees, _minutes, _seconds);
return new DMSCoordinate(wholeDegrees, minutes, seconds);
}

DegreeCoordinate toDegreeCoordinate() {
return new DegreeCoordinate(degrees());
}

GPSCoordinate toGPSCoordinate() {
double _wholeDegrees = floor(degrees());
double remaining = degrees() - _wholeDegrees;
double _minutes = floor(remaining * 60);
val wholeDegrees = floor(degrees());
val remaining = degrees() - wholeDegrees;
val minutes = floor(remaining * 60);

return new GPSCoordinate(_wholeDegrees, _minutes);
return new GPSCoordinate(wholeDegrees, minutes);
}

RadianCoordinate toRadianCoordinate() {
Expand Down
42 changes: 6 additions & 36 deletions src/main/java/com/grum/geocalc/DMSCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,24 @@

package com.grum.geocalc;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.var;

import static java.lang.Math.abs;

/**
* Represents coordinates given in
* Degrees Minutes decimal-seconds (D M s) format
*
* @author rgallet
*/
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class DMSCoordinate extends Coordinate {

public final double wholeDegrees, minutes, seconds;

DMSCoordinate(double wholeDegrees, double minutes, double seconds) {
this.wholeDegrees = wholeDegrees;
this.minutes = minutes;
this.seconds = seconds;
}

@Override
double degrees() {
double decimalDegrees = abs(wholeDegrees) + minutes / 60 + seconds / 3600;
var decimalDegrees = abs(wholeDegrees) + minutes / 60 + seconds / 3600;

if (wholeDegrees < 0) {
decimalDegrees = -decimalDegrees;
Expand All @@ -61,33 +58,6 @@ public class DMSCoordinate extends Coordinate {
return decimalDegrees;
}

/**
* @return minutes
* @deprecated use minutes
*/
@Deprecated
public double getMinutes() {
return minutes;
}

/**
* @return wholeDegrees
* @deprecated use wholeDegrees
*/
@Deprecated
public double getWholeDegrees() {
return wholeDegrees;
}

/**
* @return seconds
* @deprecated use seconds
*/
@Deprecated
public double getSeconds() {
return seconds;
}

@Override
public String toString() {
return "DMSCoordinate{" +
Expand Down
Loading

0 comments on commit d36ed7d

Please sign in to comment.