Skip to content

SQL: Adds a set of geo objects for JDBC driver #36415

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions libs/geo/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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'

archivesBaseName = 'elasticsearch-geo'

publishing {
publications {
nebula {
artifactId = archivesBaseName
}
}
}

dependencies {
compile "org.elasticsearch:elasticsearch-core:${version}"


testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
testCompile "junit:junit:${versions.junit}"
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"

if (isEclipse == false || project.path == ":libs:x-content-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']
}
}
}

jarHell.enabled = false
70 changes: 70 additions & 0 deletions libs/geo/src/main/java/org/elasticsearch/geo/GeoUtils.java
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;

/**
* Basic reusable geo-spatial utility methods
*/
public final class GeoUtils {
/**
* Minimum longitude value.
*/
public static final double MIN_LON_INCL = -180.0D;

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

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

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

// No instance:
private GeoUtils() {
}

/**
* validates latitude value is within standard +/-90 coordinate bounds
*/
public 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
*/
public 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);
}
}

}
106 changes: 106 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,106 @@
/*
* 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 org.elasticsearch.geo.GeoUtils;

/**
* 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");
}
GeoUtils.checkLatitude(lat);
GeoUtils.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();
}
}
Loading