package com.venk.hive.udf;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;

@Description (
		name = "geoDistance",
		value = "_FUNC_(str) - Calculates a distance between two points when Latitude and Longitude are given",
		extended = "Example:\n" +
		"  > SELECT geoDistance(store1.lat1, store1.lon1, store2.lat2, store2.lon2) FROM STORES stores1;\n" +
		"  CROSS JOIN STORES stores2 WHERE store1.id = 'ABC' and store2.id ='DEF'"

		)
public class UDFGeoDistance extends UDF {
	
	public double evaluate(double lat1, double lon1, double lat2, double lon2){
			double retVal =0.0;
			
			if (new Double(lat1) == null || new Double(lat2) == null || new Double(lon1) == null || new Double(lon2) == null ) {
				return 0.0;
			}
			try {
				retVal = distanceFormula4(lat1,lat2,lon1,lon2, 1);
			} catch(Exception e) {
				retVal = 0.0;
			}
		return  Math.round(retVal * 100.0)/100.0;
	}
	
	public double evaluate(double lat1, double lon1, double lat2, double lon2, int milesOrKms) {
		return distanceFormula4(lat1, lat2, lon1, lon2, milesOrKms);
	}
	/**
	 * @param lat1 - Latitude of Point 1
	 * @param lat2 - Latitude of Point 2
	 * @param lon1 - Longitude of Point 1
	 * @param lon2 - Longitude of Point 2
	 * @param milesOrKms - 1 for miles and 0 for kilometers
	 * @return  distance in Miles
	 */
	private double distanceFormula4(double lat1, double lat2, double lon1, double lon2, int milesOrKms) {
		
		double R = 6371.0; // Earth's radius in Kms
		double milesToKms = 1.60934;
		double deltaLat = Math.toRadians((lat2 - lat1));
		double deltaLon = Math.toRadians((lon2 - lon1));
		
		double intermediate = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
		* Math.sin(deltaLon/2) * Math.sin(deltaLon/2);

		double distance = R * 2 * Math.atan2(Math.sqrt(intermediate), Math.sqrt(1 - intermediate));
		
		return milesOrKms == 1 ? (distance /milesToKms) : distance;
	}
	
}

@@ -218,13 +218,13 @@
 two 
-point
+store
 s wh
@@ -535,33 +535,33 @@
 double evaluate(
-d
+D
 ouble lat1, doub
@@ -548,33 +548,33 @@
 te(Double lat1, 
-d
+D
 ouble lon1, doub
@@ -561,33 +561,33 @@
 1, Double lon1, 
-d
+D
 ouble lat2, doub
@@ -574,33 +574,33 @@
 1, Double lat2, 
-d
+D
 ouble lon2)%7B%0A%09%09%09
@@ -627,149 +627,8 @@
 %09%09%09%0A
-%09%09%09if (new Double(lat1) == null %7C%7C new Double(lat2) == null %7C%7C new Double(lon1) == null %7C%7C new Double(lon2) == null ) %7B%0A%09%09%09%09return 0.0;%0A%09%09%09%7D%0A
 %09%09%09t
@@ -809,17 +809,17 @@
 valuate(
-d
+D
 ouble la
@@ -818,25 +818,25 @@
 ouble lat1, 
-d
+D
 ouble lon1, 
@@ -831,25 +831,25 @@
 ouble lon1, 
-d
+D
 ouble lat2, 
@@ -840,33 +840,33 @@
 1, Double lat2, 
-d
+D
 ouble lon2, int 
@@ -1230,17 +1230,17 @@
 ormula4(
-d
+D
 ouble la
@@ -1243,17 +1243,17 @@
 e lat1, 
-d
+D
 ouble la
@@ -1256,17 +1256,17 @@
 e lat2, 
-d
+D
 ouble lo
@@ -1269,17 +1269,17 @@
 e lon1, 
-d
+D
 ouble lo
@@ -1379,16 +1379,53 @@
 1.60934;
+ //Miles to Kilometers conversion %0A%09%09
 %0A%09%09doubl

Update UDFGeoDistance.java