Skip to content

Commit

Permalink
Geo: Adds a set of no dependency geo classes for JDBC driver (#36477)
Browse files Browse the repository at this point in the history
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
imotov authored Jan 15, 2019
1 parent 0b396a0 commit 6f91f06
Show file tree
Hide file tree
Showing 29 changed files with 2,435 additions and 1 deletion.
49 changes: 49 additions & 0 deletions libs/geo/build.gradle
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 libs/geo/src/main/java/org/elasticsearch/geo/geometry/Circle.java
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;
}

}
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();
}
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();
}
}
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);
}
}

}
Loading

0 comments on commit 6f91f06

Please sign in to comment.