-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Geo: Adds a set of no dependency geo classes for JDBC driver (#36477)
Adds a set of geo classes to represent geo data in the JDBC driver and to be used as an intermediate format to pass geo shapes for indexing and query generation in #35320. Relates to #35767 and #35320
- Loading branch information
Showing
29 changed files
with
2,435 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
apply plugin: 'elasticsearch.build' | ||
apply plugin: 'nebula.maven-base-publish' | ||
apply plugin: 'nebula.maven-scm' | ||
|
||
dependencies { | ||
if (isEclipse == false || project.path == ":libs:geo-tests") { | ||
testCompile("org.elasticsearch.test:framework:${version}") { | ||
exclude group: 'org.elasticsearch', module: 'elasticsearch-geo' | ||
} | ||
} | ||
} | ||
|
||
forbiddenApisMain { | ||
// geo does not depend on server | ||
// TODO: Need to decide how we want to handle for forbidden signatures with the changes to core | ||
replaceSignatureFiles 'jdk-signatures' | ||
} | ||
|
||
if (isEclipse) { | ||
// in eclipse the project is under a fake root, we need to change around the source sets | ||
sourceSets { | ||
if (project.path == ":libs:geo") { | ||
main.java.srcDirs = ['java'] | ||
main.resources.srcDirs = ['resources'] | ||
} else { | ||
test.java.srcDirs = ['java'] | ||
test.resources.srcDirs = ['resources'] | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
libs/geo/src/main/java/org/elasticsearch/geo/geometry/Circle.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,104 @@ | ||
/* | ||
* 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.geometry; | ||
|
||
/** | ||
* Circle geometry (not part of WKT standard, but used in elasticsearch) | ||
*/ | ||
public class Circle implements Geometry { | ||
public static final Circle EMPTY = new Circle(); | ||
private final double lat; | ||
private final double lon; | ||
private final double radiusMeters; | ||
|
||
private Circle() { | ||
lat = 0; | ||
lon = 0; | ||
radiusMeters = -1; | ||
} | ||
|
||
public Circle(final double lat, final double lon, final double radiusMeters) { | ||
this.lat = lat; | ||
this.lon = lon; | ||
this.radiusMeters = radiusMeters; | ||
if (radiusMeters < 0 ) { | ||
throw new IllegalArgumentException("Circle radius [" + radiusMeters + "] cannot be negative"); | ||
} | ||
GeometryUtils.checkLatitude(lat); | ||
GeometryUtils.checkLongitude(lon); | ||
} | ||
|
||
@Override | ||
public ShapeType type() { | ||
return ShapeType.CIRCLE; | ||
} | ||
|
||
public double getLat() { | ||
return lat; | ||
} | ||
|
||
public double getLon() { | ||
return lon; | ||
} | ||
|
||
public double getRadiusMeters() { | ||
return radiusMeters; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
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; | ||
return (Double.compare(circle.radiusMeters, radiusMeters) == 0); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result; | ||
long temp; | ||
temp = Double.doubleToLongBits(lat); | ||
result = (int) (temp ^ (temp >>> 32)); | ||
temp = Double.doubleToLongBits(lon); | ||
result = 31 * result + (int) (temp ^ (temp >>> 32)); | ||
temp = Double.doubleToLongBits(radiusMeters); | ||
result = 31 * result + (int) (temp ^ (temp >>> 32)); | ||
return result; | ||
} | ||
|
||
@Override | ||
public <T> T visit(GeometryVisitor<T> visitor) { | ||
return visitor.visit(this); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return radiusMeters < 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "lat=" + lat + ", lon=" + lon + ", radius=" + radiusMeters; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
libs/geo/src/main/java/org/elasticsearch/geo/geometry/Geometry.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,32 @@ | ||
/* | ||
* 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.geometry; | ||
|
||
/** | ||
* Base class for all Geometry objects supported by elasticsearch | ||
*/ | ||
public interface Geometry { | ||
|
||
ShapeType type(); | ||
|
||
<T> T visit(GeometryVisitor<T> visitor); | ||
|
||
boolean isEmpty(); | ||
} |
85 changes: 85 additions & 0 deletions
85
libs/geo/src/main/java/org/elasticsearch/geo/geometry/GeometryCollection.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,85 @@ | ||
/* | ||
* 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.geometry; | ||
|
||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Collection of arbitrary geometry classes | ||
*/ | ||
public class GeometryCollection<G extends Geometry> implements Geometry, Iterable<G> { | ||
public static final GeometryCollection<Geometry> EMPTY = new GeometryCollection<>(); | ||
|
||
private final List<G> shapes; | ||
|
||
public GeometryCollection() { | ||
shapes = Collections.emptyList(); | ||
} | ||
|
||
public GeometryCollection(List<G> shapes) { | ||
if (shapes == null || shapes.isEmpty()) { | ||
throw new IllegalArgumentException("the list of shapes cannot be null or empty"); | ||
} | ||
this.shapes = shapes; | ||
} | ||
|
||
@Override | ||
public ShapeType type() { | ||
return ShapeType.GEOMETRYCOLLECTION; | ||
} | ||
|
||
@Override | ||
public <T> T visit(GeometryVisitor<T> visitor) { | ||
return visitor.visit(this); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return shapes.isEmpty(); | ||
} | ||
|
||
public int size() { | ||
return shapes.size(); | ||
} | ||
|
||
public G get(int i) { | ||
return shapes.get(i); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GeometryCollection<?> that = (GeometryCollection<?>) o; | ||
return Objects.equals(shapes, that.shapes); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(shapes); | ||
} | ||
|
||
@Override | ||
public Iterator<G> iterator() { | ||
return shapes.iterator(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
libs/geo/src/main/java/org/elasticsearch/geo/geometry/GeometryUtils.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,70 @@ | ||
/* | ||
* 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.geometry; | ||
|
||
/** | ||
* Geometry-related utility methods | ||
*/ | ||
final class GeometryUtils { | ||
/** | ||
* Minimum longitude value. | ||
*/ | ||
static final double MIN_LON_INCL = -180.0D; | ||
|
||
/** | ||
* Maximum longitude value. | ||
*/ | ||
static final double MAX_LON_INCL = 180.0D; | ||
|
||
/** | ||
* Minimum latitude value. | ||
*/ | ||
static final double MIN_LAT_INCL = -90.0D; | ||
|
||
/** | ||
* Maximum latitude value. | ||
*/ | ||
static final double MAX_LAT_INCL = 90.0D; | ||
|
||
// No instance: | ||
private GeometryUtils() { | ||
} | ||
|
||
/** | ||
* validates latitude value is within standard +/-90 coordinate bounds | ||
*/ | ||
static 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 | ||
*/ | ||
static 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); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.