Skip to content

Commit

Permalink
GEODE-5998 geospatial support
Browse files Browse the repository at this point in the history
Fixing LGTM issues
  • Loading branch information
bschuchardt committed Nov 12, 2018
1 parent 9a5eda9 commit b43e571
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ public static ByteBuf getBulkStringArrayResponse(ByteBufAllocator alloc, Collect
response.writeBytes(tmp);
}
} finally {
tmp.release();
if (tmp != null) {
tmp.release();
}
}
}

Expand Down Expand Up @@ -480,7 +482,7 @@ public static long bytesToLong(byte[] bytes) {
* @return Parsed value
* @throws NumberFormatException if bytes to string does not yield a convertible double
*/
public static Double bytesToDouble(byte[] bytes) {
public static double bytesToDouble(byte[] bytes) {
return stringToDouble(bytesToString(bytes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.github.davidmoten.geo.GeoHash;
Expand Down Expand Up @@ -89,16 +90,18 @@ public static ByteBuf geoRadiusResponse(ByteBufAllocator alloc,
hash = element.getHash().get();
}

if (distStr != "" || !coord.isEmpty() || hash != "") {
if (!Objects.equals(distStr, "") || !coord.isEmpty() || !Objects.equals(hash, "")) {
List<Object> elementData = new ArrayList<>();
elementData.add(name);
if (distStr != "")
if (!Objects.equals(distStr, "")) {
elementData.add(distStr);
if (!coord.isEmpty())
}
if (!coord.isEmpty()) {
elementData.add(coord);
if (hash != "")
}
if (!Objects.equals(hash, "")) {
elementData.add(hash);

}
responseElements.add(elementData);
} else {
responseElements.add(name);
Expand All @@ -125,14 +128,14 @@ public static LatLong geoPos(String hash) {
* @param hash2 geohash of second point
* @return distance in meters
*/
public static Double geoDist(String hash1, String hash2) {
public static double geoDist(String hash1, String hash2) {
LatLong coord1 = geoPos(hash1);
LatLong coord2 = geoPos(hash2);

Double lat1 = Math.toRadians(coord1.getLat());
Double long1 = Math.toRadians(coord1.getLon());
Double lat2 = Math.toRadians(coord2.getLat());
Double long2 = Math.toRadians(coord2.getLon());
double lat1 = Math.toRadians(coord1.getLat());
double long1 = Math.toRadians(coord1.getLon());
double lat2 = Math.toRadians(coord2.getLat());
double long2 = Math.toRadians(coord2.getLon());

return dist(long1, lat1, long2, lat2);
}
Expand All @@ -145,8 +148,8 @@ public static Double geoDist(String hash1, String hash2) {
* @return geohash as base32
*/
public static String geohash(byte[] lon, byte[] lat) throws IllegalArgumentException {
Double longitude = Coder.bytesToDouble(lon);
Double latitude = Coder.bytesToDouble(lat);
double longitude = Coder.bytesToDouble(lon);
double latitude = Coder.bytesToDouble(lat);
return GeoHash.encodeHash(latitude, longitude);
}

Expand Down Expand Up @@ -184,15 +187,15 @@ public static HashArea boundingBox(double longitude, double latitude,
return new HashArea(minlon, maxlon, minlat, maxlat);
}

public static Double dist(Double long1, Double lat1, Double long2, Double lat2) {
Double hav =
public static double dist(double long1, double lat1, double long2, double lat2) {
double hav =
haversine(lat2 - lat1) + (Math.cos(lat1) * Math.cos(lat2) * haversine(long2 - long1));
Double distAngle = Math.acos(1 - (2 * hav));
double distAngle = Math.acos(1 - (2 * hav));

return EARTH_RADIUS_IN_METERS * distAngle;
}

public static double haversine(Double rad) {
public static double haversine(double rad) {
return 0.5 * (1 - Math.cos(rad));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
String name = point.get("key").toString();
String hash = point.get("value").toString();

Double dist = GeoCoder.geoDist(params.centerHashPrecise, hash) * params.distScale;
double dist = GeoCoder.geoDist(params.centerHashPrecise, hash) * params.distScale;

// Post-filter for accuracy
if (dist > (params.radius * params.distScale))
if (dist > (params.radius * params.distScale)) {
continue;
}

Optional<LatLong> coord =
params.withCoord ? Optional.of(GeoCoder.geoPos(hash)) : Optional.empty();
Expand All @@ -100,8 +101,9 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {

// Because of the way hashing works, sometimes you can get the same requested member back
// in the results
if (!name.equals(params.member))
if (!name.equals(params.member)) {
results.add(new GeoRadiusResponseElement(name, coord, dist, params.withDist, hashOpt));
}
}
} catch (Exception e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
String name = point.get("key").toString();
String hash = point.get("value").toString();

Double dist = GeoCoder.geoDist(params.centerHashPrecise, hash) * params.distScale;
double dist = GeoCoder.geoDist(params.centerHashPrecise, hash) * params.distScale;

// Post-filter for accuracy
if (dist > (params.radius * params.distScale))
Expand Down

0 comments on commit b43e571

Please sign in to comment.