YolandaMDavis commented on a change in pull request #5518:
URL: https://github.com/apache/nifi/pull/5518#discussion_r771763988



##########
File path: 
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GeohashDecodeBaseEvaluator.java
##########
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.nifi.attribute.expression.language.evaluation.functions;
+
+import org.apache.commons.lang3.EnumUtils;
+import org.apache.nifi.attribute.expression.language.EvaluationContext;
+import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
+import 
org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator;
+import 
org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult;
+import org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
+import 
org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException;
+
+import ch.hsr.geohash.GeoHash;
+import ch.hsr.geohash.WGS84Point;
+
+public abstract class GeohashDecodeBaseEvaluator extends NumberEvaluator {
+
+    public enum GeohashFormat {
+        BASE32, BINARY, LONG
+    }
+
+    public enum GeoCoord {
+        LATITUDE, LONGITUDE
+    }
+
+    private final Evaluator<String> subject;
+    private final Evaluator<String> format;
+
+    public GeohashDecodeBaseEvaluator(final Evaluator<String> subject, final 
Evaluator<String> format) {
+        this.subject = subject;
+        this.format = format;
+    }
+
+    protected QueryResult<Number> geohashDecodeEvaluate(final 
EvaluationContext evaluationContext, final GeoCoord geoCoord) {
+        //Optional argument. If not specified, defaults to BASE_32_STRING.
+        final GeohashFormat geohashFormatValue;
+        if (format != null) {
+            if(!EnumUtils.isValidEnum(GeohashFormat.class, 
format.evaluate(evaluationContext).getValue())) {
+                throw new AttributeExpressionLanguageException("Format values 
must be 'BASE32', 'BINARY' or 'LONG'");
+            }
+            geohashFormatValue = 
GeohashFormat.valueOf(format.evaluate(evaluationContext).getValue());
+        } else {
+            geohashFormatValue = GeohashFormat.BASE32;
+
+        }
+
+        final Long geohashLongValue = geohashFormatValue == GeohashFormat.LONG 
? Long.valueOf(subject.evaluate(evaluationContext).getValue()) : null;
+        final String geohashStringValue = (geohashFormatValue == 
GeohashFormat.BASE32
+                || geohashFormatValue == GeohashFormat.BINARY) ? 
subject.evaluate(evaluationContext).getValue() : null;
+
+        if (geohashLongValue == null && geohashStringValue == null) {
+            return new NumberQueryResult(null);
+        }
+
+        try {
+            WGS84Point boundingBoxCenter;
+            switch (geohashFormatValue) {
+                case LONG:
+                    String binaryString = 
Long.toBinaryString(geohashLongValue);
+                    boundingBoxCenter = 
GeoHash.fromBinaryString(binaryString).getBoundingBoxCenter();
+                    break;
+                case BINARY:
+                    boundingBoxCenter = 
GeoHash.fromBinaryString(geohashStringValue).getBoundingBoxCenter();
+                    break;
+                default:
+                    boundingBoxCenter = 
GeoHash.fromGeohashString(geohashStringValue).getBoundingBoxCenter();

Review comment:
       In testing with invalid data (e.g. "cat") this will throw a null pointer 
exception. We should do a null check before calling the getBoundingBoxCenter 
methods.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to