Skip to content

Commit

Permalink
Geo: Makes coordinate validator in libs/geo plugable (#43657)
Browse files Browse the repository at this point in the history
Moves coordinate validation from Geometry constructors into
parser.

Relates #43644
  • Loading branch information
imotov authored Jun 27, 2019
1 parent 25792d3 commit 8029b47
Show file tree
Hide file tree
Showing 31 changed files with 472 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public Circle(final double lat, final double lon, final double alt, final double
if (radiusMeters < 0 ) {
throw new IllegalArgumentException("Circle radius [" + radiusMeters + "] cannot be negative");
}
GeometryUtils.checkLatitude(lat);
GeometryUtils.checkLongitude(lon);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ public Line(double[] lats, double[] lons, double[] alts) {
if (alts != null && alts.length != lats.length) {
throw new IllegalArgumentException("alts and lats must be equal length");
}
for (int i = 0; i < lats.length; i++) {
GeometryUtils.checkLatitude(lats[i]);
GeometryUtils.checkLongitude(lons[i]);
}
}

public int length() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public Point(double lat, double lon) {
}

public Point(double lat, double lon, double alt) {
GeometryUtils.checkLatitude(lat);
GeometryUtils.checkLongitude(lon);
this.lat = lat;
this.lon = lon;
this.alt = alt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public Rectangle(double minLat, double maxLat, double minLon, double maxLon) {
* Constructs a bounding box by first validating the provided latitude and longitude coordinates
*/
public Rectangle(double minLat, double maxLat, double minLon, double maxLon, double minAlt, double maxAlt) {
GeometryUtils.checkLatitude(minLat);
GeometryUtils.checkLatitude(maxLat);
GeometryUtils.checkLongitude(minLon);
GeometryUtils.checkLongitude(maxLon);
this.minLon = minLon;
this.maxLon = maxLon;
this.minLat = minLat;
Expand All @@ -90,17 +86,6 @@ public Rectangle(double minLat, double maxLat, double minLon, double maxLon, dou
}
}

public double getWidth() {
if (crossesDateline()) {
return GeometryUtils.MAX_LON_INCL - minLon + maxLon - GeometryUtils.MIN_LON_INCL;
}
return maxLon - minLon;
}

public double getHeight() {
return maxLat - minLat;
}

public double getMinLat() {
return minLat;
}
Expand Down Expand Up @@ -156,21 +141,6 @@ public String toString() {
return b.toString();
}

/**
* Returns true if this bounding box crosses the dateline
*/
public boolean crossesDateline() {
return maxLon < minLon;
}

/** returns true if rectangle (defined by minLat, maxLat, minLon, maxLon) contains the lat lon point */
public boolean containsPoint(final double lat, final double lon) {
if (lat >= minLat && lat <= maxLat) {
return crossesDateline() ? lon >= minLon || lon <= maxLon : lon >= minLon && lon <= maxLon;
}
return false;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.geo.utils;

import org.elasticsearch.geo.geometry.Circle;
import org.elasticsearch.geo.geometry.Geometry;
import org.elasticsearch.geo.geometry.GeometryCollection;
import org.elasticsearch.geo.geometry.GeometryVisitor;
import org.elasticsearch.geo.geometry.Line;
import org.elasticsearch.geo.geometry.LinearRing;
import org.elasticsearch.geo.geometry.MultiLine;
import org.elasticsearch.geo.geometry.MultiPoint;
import org.elasticsearch.geo.geometry.MultiPolygon;
import org.elasticsearch.geo.geometry.Point;
import org.elasticsearch.geo.geometry.Polygon;
import org.elasticsearch.geo.geometry.Rectangle;

/**
* Validator that checks that lats are between -90 and +90 and lons are between -180 and +180 and altitude is present only if
* ignoreZValue is set to true
*/
public class GeographyValidator implements GeometryValidator {

/**
* Minimum longitude value.
*/
private static final double MIN_LON_INCL = -180.0D;

/**
* Maximum longitude value.
*/
private static final double MAX_LON_INCL = 180.0D;

/**
* Minimum latitude value.
*/
private static final double MIN_LAT_INCL = -90.0D;

/**
* Maximum latitude value.
*/
private static final double MAX_LAT_INCL = 90.0D;

private final boolean ignoreZValue;

public GeographyValidator(boolean ignoreZValue) {
this.ignoreZValue = ignoreZValue;
}

/**
* validates latitude value is within standard +/-90 coordinate bounds
*/
protected void checkLatitude(double latitude) {
if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) {
throw new IllegalArgumentException(
"invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL);
}
}

/**
* validates longitude value is within standard +/-180 coordinate bounds
*/
protected void checkLongitude(double longitude) {
if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) {
throw new IllegalArgumentException(
"invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL);
}
}

protected void checkAltitude(double zValue) {
if (ignoreZValue == false && Double.isNaN(zValue) == false) {
throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] "
+ "parameter is [" + ignoreZValue + "]");
}
}

@Override
public void validate(Geometry geometry) {
geometry.visit(new GeometryVisitor<Void, RuntimeException>() {

@Override
public Void visit(Circle circle) throws RuntimeException {
checkLatitude(circle.getLat());
checkLongitude(circle.getLon());
checkAltitude(circle.getAlt());
return null;
}

@Override
public Void visit(GeometryCollection<?> collection) throws RuntimeException {
for (Geometry g : collection) {
g.visit(this);
}
return null;
}

@Override
public Void visit(Line line) throws RuntimeException {
for (int i = 0; i < line.length(); i++) {
checkLatitude(line.getLat(i));
checkLongitude(line.getLon(i));
checkAltitude(line.getAlt(i));
}
return null;
}

@Override
public Void visit(LinearRing ring) throws RuntimeException {
for (int i = 0; i < ring.length(); i++) {
checkLatitude(ring.getLat(i));
checkLongitude(ring.getLon(i));
checkAltitude(ring.getAlt(i));
}
return null;
}

@Override
public Void visit(MultiLine multiLine) throws RuntimeException {
return visit((GeometryCollection<?>) multiLine);
}

@Override
public Void visit(MultiPoint multiPoint) throws RuntimeException {
return visit((GeometryCollection<?>) multiPoint);
}

@Override
public Void visit(MultiPolygon multiPolygon) throws RuntimeException {
return visit((GeometryCollection<?>) multiPolygon);
}

@Override
public Void visit(Point point) throws RuntimeException {
checkLatitude(point.getLat());
checkLongitude(point.getLon());
checkAltitude(point.getAlt());
return null;
}

@Override
public Void visit(Polygon polygon) throws RuntimeException {
polygon.getPolygon().visit(this);
for (int i = 0; i < polygon.getNumberOfHoles(); i++) {
polygon.getHole(i).visit(this);
}
return null;
}

@Override
public Void visit(Rectangle rectangle) throws RuntimeException {
checkLatitude(rectangle.getMinLat());
checkLatitude(rectangle.getMaxLat());
checkLongitude(rectangle.getMinLon());
checkLongitude(rectangle.getMaxLon());
checkAltitude(rectangle.getMinAlt());
checkAltitude(rectangle.getMaxAlt());
return null;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.geo.utils;

import org.elasticsearch.geo.geometry.Geometry;

/**
* Generic geometry validator that can be used by the parser to verify the validity of the parsed geometry
*/
public interface GeometryValidator {

/**
* Validates the geometry and throws IllegalArgumentException if the geometry is not valid
*/
void validate(Geometry geometry);

}
Loading

0 comments on commit 8029b47

Please sign in to comment.