-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Geo: Makes coordinate validator in libs/geo plugable #43657
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
libs/geo/src/main/java/org/elasticsearch/geo/geometry/GeometryUtils.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
libs/geo/src/main/java/org/elasticsearch/geo/utils/GeographyValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
libs/geo/src/main/java/org/elasticsearch/geo/utils/GeometryValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran into this in #40869 Because we don't include lucene dependencies in our library plugins I had to copy much of Lucene's
BitUtils
andGeoHashUtils
implementation. Here we're having to copy some of Lucene'sGeoUtils
functionality. Starting to get a little concerned w/ the code duplication and am curious as to whether or not it makes sense to start allowing lucene dependencies in our Libraries? Will this break the build configuration or just our library philosophy? /cc @rjernst @atorokThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to reuse lucene pieces we would have to jump through a lot of hoops and probably move validation to server since we need libs/geo in SQL client and we really don't want SQL client to depend on Lucene. I don't think it is worth bringing lucene here because of 2 constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I'm not so concerned about the two constants here as I am about the growing duplication of code, like
IOUtils
,BitUtils
,GeoHashUtils
, and nowGeoUtils
. Perhaps the concern is premature, but I'm starting to see this become a bit of a trend. I just don't want to wake up one day and see that it got out of hand and future maintenance becomes a nightmare. It doesn't have to hold up this PR but I think it does warrant an async discussion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worst case, in a distant future if it gets out of hand, we could have a compile time shading/copying of relevant lucene classes, if we want to include these in our lib without adding an external jar dep. I agree with Igor we should not add deps to SQL client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome... thx @rjernst
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be conceivable to have something like
lucene-utils
produced upstream on which we could depend on ? That could be a long term solution too, would be the same as @rjernst suggested, but would allow us to add a clean dependency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this would be likely, since from Lucene's perspective there is no reason for this extra dependency. Lucene doesn't want to be a general purpose utility library.