//
//   Copyright 2016  Cityzen Data
//
//   Licensed 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 io.warp10.script.functions;

import io.warp10.script.NamedWarpScriptFunction;
import io.warp10.script.WarpScriptStackFunction;
import io.warp10.script.WarpScriptException;
import io.warp10.script.WarpScriptStack;

/**
 * Compute distance between two lat/lon using the Haversine formula
 */
public class HAVERSINE extends NamedWarpScriptFunction implements WarpScriptStackFunction {
  
  private static final double EARTH_RADIUS = 6378137.0D;
  
  public HAVERSINE(String name) {
    super(name);
  }
  
  @Override
  public Object apply(WarpScriptStack stack) throws WarpScriptException {
    
    Object lon2 = stack.pop();
    Object lat2 = stack.pop();
    Object lon1 = stack.pop();
    Object lat1 = stack.pop();
    
    if (!(lat1 instanceof Number) || !(lat2 instanceof Number) || !(lon1 instanceof Number) || !(lon2 instanceof Number)) {
      throw new WarpScriptException(getName() + " expects two sets of lat/lon coordinates.");
    }
    
    double rlat1 = Math.toRadians(((Number) lat1).doubleValue());
    double rlon1 = Math.toRadians(((Number) lon1).doubleValue());
    double rlat2 = Math.toRadians(((Number) lat2).doubleValue());
    double rlon2 = Math.toRadians(((Number) lon2).doubleValue());
    
    double sinphi2 = Math.sin((rlon2 - rlon1)/2.0D);
    sinphi2 *= sinphi2;

    double sinlambda2 = Math.sin((rlat2 - rlat1)/2.0D);
    sinlambda2 *= sinlambda2;

    double d = 2.0D * EARTH_RADIUS * Math.asin(Math.sqrt(sinphi2 + Math.cos(rlon1) * Math.cos(rlon2) * sinlambda2));
    
    stack.push(d);
    
    return stack;
  }
}

@@ -998,18 +998,111 @@
 ction %7B%0A
+%0A
   
+// Mean radius in meters see https://en.wikipedia.org/wiki/Earth_radius#Global_average_radii
 %0A  priva
@@ -1146,19 +1146,17 @@
  637
-8137
+1000
 .0D;%0A
-  
 %0A  p
@@ -1207,18 +1207,16 @@
 e);%0A  %7D%0A
-  
 %0A  @Over
@@ -1294,20 +1294,16 @@
 ption %7B%0A
-    
 %0A    Obj
@@ -1419,20 +1419,16 @@
 .pop();%0A
-    
 %0A    if 
@@ -1644,20 +1644,16 @@
 ;%0A    %7D%0A
-    
 %0A    dou
@@ -1905,28 +1905,24 @@
 leValue());%0A
-    
 %0A    double 
@@ -1947,21 +1947,23 @@
 ((rl
-on
+at
 2 - rl
-on1)/
+at1) / 
 2.0D
@@ -2030,21 +2030,23 @@
 ((rl
-at
+on
 2 - rl
-at1)/
+on1) / 
 2.0D
@@ -2157,18 +2157,18 @@
 h.cos(rl
-on
+at
 1) * Mat
@@ -2175,18 +2175,18 @@
 h.cos(rl
-on
+at
 2) * sin
@@ -2196,20 +2196,16 @@
 bda2));%0A
-    
 %0A    sta
@@ -2216,20 +2216,16 @@
 ush(d);%0A
-    
 %0A    ret

Fix formula by switching lat and lon.