Repository: nationalsecurityagency/datawave
Issue: Geo to GeoWave function conversion results in inaccurate bounds

When converting geo functions to geowave functions, the precision of the coordinates is not being preserved.  

Changed files:
warehouse/query-core/src/test/java/datawave/query/jexl/functions/GeoFunctionsDescriptorTest.java
warehouse/query-core/src/main/java/datawave/query/jexl/functions/GeoFunctionsDescriptor.java

diff --git a/warehouse/query-core/src/main/java/datawave/query/jexl/functions/GeoFunctionsDescriptor.java b/warehouse/query-core/src/main/java/datawave/query/jexl/functions/GeoFunctionsDescriptor.java
index ee81a503f4..d67c2e34f1 100644
--- a/warehouse/query-core/src/main/java/datawave/query/jexl/functions/GeoFunctionsDescriptor.java
+++ b/warehouse/query-core/src/main/java/datawave/query/jexl/functions/GeoFunctionsDescriptor.java
@@ -28,12 +28,13 @@ import org.apache.commons.jexl2.parser.JexlNode;
 import org.apache.commons.jexl2.parser.ParserTreeConstants;
 import org.apache.log4j.Logger;
 import org.locationtech.jts.geom.Coordinate;
-import org.locationtech.jts.geom.Envelope;
+import org.locationtech.jts.geom.CoordinateXY;
 import org.locationtech.jts.geom.GeometryFactory;
 import org.locationtech.jts.geom.MultiPolygon;
 import org.locationtech.jts.geom.Polygon;
 import org.locationtech.jts.util.GeometricShapeFactory;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
@@ -388,10 +389,14 @@ public class GeoFunctionsDescriptor implements JexlFunctionArgumentDescriptorFac
         }
         
         private Polygon createRectangle(double minLon, double maxLon, double minLat, double maxLat) {
-            GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
-            shapeFactory.setEnvelope(new Envelope(minLon, maxLon, minLat, maxLat));
-            shapeFactory.setNumPoints(4);
-            return shapeFactory.createRectangle();
+            GeometryFactory geomFactory = new GeometryFactory();
+            List<Coordinate> coordinates = new ArrayList<>();
+            coordinates.add(new CoordinateXY(minLon, minLat));
+            coordinates.add(new CoordinateXY(maxLon, minLat));
+            coordinates.add(new CoordinateXY(maxLon, maxLat));
+            coordinates.add(new CoordinateXY(minLon, maxLat));
+            coordinates.add(new CoordinateXY(minLon, minLat));
+            return geomFactory.createPolygon(coordinates.toArray(new Coordinate[0]));
         }
         
         private MultiPolygon createMultiPolygon(Polygon poly1, Polygon poly2) {
diff --git a/warehouse/query-core/src/test/java/datawave/query/jexl/functions/GeoFunctionsDescriptorTest.java b/warehouse/query-core/src/test/java/datawave/query/jexl/functions/GeoFunctionsDescriptorTest.java
index 61445668e2..991dbb8c06 100644
--- a/warehouse/query-core/src/test/java/datawave/query/jexl/functions/GeoFunctionsDescriptorTest.java
+++ b/warehouse/query-core/src/test/java/datawave/query/jexl/functions/GeoFunctionsDescriptorTest.java
@@ -1,5 +1,6 @@
 package datawave.query.jexl.functions;
 
+import com.google.common.collect.Sets;
 import datawave.query.jexl.JexlASTHelper;
 import datawave.query.jexl.functions.arguments.JexlArgumentDescriptor;
 import datawave.query.jexl.visitors.JexlStringBuildingVisitor;
@@ -10,6 +11,17 @@ import org.junit.Test;
 
 public class GeoFunctionsDescriptorTest {
     
+    @Test
+    public void testGeoToGeoWaveFunction() throws Exception {
+        String query = "geo:within_bounding_box(GEO_FIELD, \\"-12.74,16.30\\", \\"-3.31,26.16\\")";
+        JexlNode node = JexlASTHelper.parseJexlQuery(query);
+        GeoFunctionsDescriptor.GeoJexlArgumentDescriptor argDesc = (GeoFunctionsDescriptor.GeoJexlArgumentDescriptor) new GeoFunctionsDescriptor()
+                        .getArgumentDescriptor((ASTFunctionNode) node.jjtGetChild(0).jjtGetChild(0));
+        JexlNode queryNode = argDesc.toGeoWaveFunction(Sets.newHashSet("GEO_FIELD"));
+        Assert.assertEquals("geowave:intersects(GEO_FIELD, 'POLYGON ((16.3 -12.74, 26.16 -12.74, 26.16 -3.31, 16.3 -3.31, 16.3 -12.74))')",
+                        JexlStringBuildingVisitor.buildQuery(queryNode));
+    }
+    
     @Test
     public void antiMeridianTest1() throws Exception {
         String query = "geo:within_bounding_box(GEO_FIELD, '40_170', '50_-170')";