Skip to content

Commit

Permalink
Geo: Change order of parameter in Geometries to lon, lat 7.x (#45618)
Browse files Browse the repository at this point in the history
Changes the order of parameters in Geometries from lat, lon to lon, lat
and moves all Geometry classes are moved to the
org.elasticsearch.geomtery package.

Backport of #45332

Closes #45048
  • Loading branch information
imotov committed Aug 16, 2019
1 parent 742213d commit 98c850c
Show file tree
Hide file tree
Showing 99 changed files with 1,159 additions and 1,053 deletions.
8 changes: 8 additions & 0 deletions docs/reference/migration/migrate_7_0/java.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ The client method `termVector`, deprecated in 2.0, has been removed. The method

The constructor `AbstractLifecycleComponent(Settings settings)`, deprecated in 6.7
has been removed. The parameterless constructor should be used instead.

[float]
==== Changes to Geometry classes

Geometry classes used to represent geo values in SQL have been moved from the
`org.elasticsearch.geo.geometry` package to the `org.elasticsearch.geometry`
package and the order of the constructor parameters has changed from `lat`, `lon`
to `lon`, `lat`.
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@
* under the License.
*/

package org.elasticsearch.geo.geometry;
package org.elasticsearch.geometry;

import org.elasticsearch.geometry.utils.WellKnownText;

/**
* Circle geometry (not part of WKT standard, but used in elasticsearch) defined by lat/lon coordinates of the center in degrees
* and optional altitude in meters.
*/
public class Circle implements Geometry {
public static final Circle EMPTY = new Circle();
private final double lat;
private final double lon;
private final double alt;
private final double y;
private final double x;
private final double z;
private final double radiusMeters;

private Circle() {
lat = 0;
lon = 0;
alt = Double.NaN;
y = 0;
x = 0;
z = Double.NaN;
radiusMeters = -1;
}

public Circle(final double lat, final double lon, final double radiusMeters) {
this(lat, lon, Double.NaN, radiusMeters);
public Circle(final double x, final double y, final double radiusMeters) {
this(x, y, Double.NaN, radiusMeters);
}

public Circle(final double lat, final double lon, final double alt, final double radiusMeters) {
this.lat = lat;
this.lon = lon;
public Circle(final double x, final double y, final double z, final double radiusMeters) {
this.y = y;
this.x = x;
this.radiusMeters = radiusMeters;
this.alt = alt;
this.z = z;
if (radiusMeters < 0 ) {
throw new IllegalArgumentException("Circle radius [" + radiusMeters + "] cannot be negative");
}
Expand All @@ -56,20 +58,32 @@ public ShapeType type() {
return ShapeType.CIRCLE;
}

public double getLat() {
return lat;
public double getY() {
return y;
}

public double getLon() {
return lon;
public double getX() {
return x;
}

public double getRadiusMeters() {
return radiusMeters;
}

public double getZ() {
return z;
}

public double getLat() {
return y;
}

public double getLon() {
return x;
}

public double getAlt() {
return alt;
return z;
}

@Override
Expand All @@ -78,23 +92,23 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;

Circle circle = (Circle) o;
if (Double.compare(circle.lat, lat) != 0) return false;
if (Double.compare(circle.lon, lon) != 0) return false;
if (Double.compare(circle.y, y) != 0) return false;
if (Double.compare(circle.x, x) != 0) return false;
if (Double.compare(circle.radiusMeters, radiusMeters) != 0) return false;
return (Double.compare(circle.alt, alt) == 0);
return (Double.compare(circle.z, z) == 0);
}

@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(lat);
temp = Double.doubleToLongBits(y);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(lon);
temp = Double.doubleToLongBits(x);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(radiusMeters);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(alt);
temp = Double.doubleToLongBits(z);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
Expand All @@ -111,11 +125,11 @@ public boolean isEmpty() {

@Override
public String toString() {
return "lat=" + lat + ", lon=" + lon + ", radius=" + radiusMeters + (Double.isNaN(alt) ? ", alt=" + alt : "");
return WellKnownText.INSTANCE.toWKT(this);
}

@Override
public boolean hasAlt() {
return Double.isNaN(alt) == false;
public boolean hasZ() {
return Double.isNaN(z) == false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.geo.geometry;
package org.elasticsearch.geometry;

/**
* Base class for all Geometry objects supported by elasticsearch
Expand All @@ -30,7 +30,11 @@ public interface Geometry {

boolean isEmpty();

default boolean hasAlt() {
default boolean hasZ() {
return false;
}

default boolean hasAlt() {
return hasZ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.geo.geometry;
package org.elasticsearch.geometry;

import org.elasticsearch.geometry.utils.WellKnownText;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
Expand All @@ -42,9 +43,9 @@ public GeometryCollection(List<G> shapes) {
if (shapes == null || shapes.isEmpty()) {
throw new IllegalArgumentException("the list of shapes cannot be null or empty");
}
hasAlt = shapes.get(0).hasAlt();
hasAlt = shapes.get(0).hasZ();
for (G shape : shapes) {
if (shape.hasAlt() != hasAlt) {
if (shape.hasZ() != hasAlt) {
throw new IllegalArgumentException("all elements of the collection should have the same number of dimension");
}
}
Expand Down Expand Up @@ -93,16 +94,12 @@ public Iterator<G> iterator() {
}

@Override
public boolean hasAlt() {
public boolean hasZ() {
return hasAlt;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type().name().toLowerCase(Locale.ROOT)).append("(shapes=");
sb.append(shapes);
sb.append(")");
return sb.toString();
return WellKnownText.INSTANCE.toWKT(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* under the License.
*/

package org.elasticsearch.geo.geometry;
package org.elasticsearch.geometry;

import org.elasticsearch.geometry.utils.WellKnownText;

/**
* Support class for creating Geometry Visitors.
Expand All @@ -40,7 +42,7 @@
* The Visitor Pattern replaces this structure with Interface inheritance making it easier to identify all places that are using this
* structure, and making a shape a compile-time failure instead of runtime.
* <p>
* See {@link org.elasticsearch.geo.utils.WellKnownText#toWKT(Geometry, StringBuilder)} for an example of how this interface is used.
* See {@link WellKnownText#toWKT(Geometry, StringBuilder)} for an example of how this interface is used.
*
* @see <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor Pattern</a>
*/
Expand Down
Loading

0 comments on commit 98c850c

Please sign in to comment.