Skip to content

Commit

Permalink
optimize const EARTH_R
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Jun 11, 2016
1 parent 784520c commit e05c227
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
10 changes: 5 additions & 5 deletions c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ INLINE static int outOfChina(double lat, double lng) {
return 0;
}

#define EARTH_R 6378245.0

void transform(double x, double y, double *lat, double *lng) {
double xy = x * y;
double absX = sqrt(fabs(x));
Expand Down Expand Up @@ -51,15 +53,14 @@ static void delta(double lat, double lng, double *dLat, double *dLng) {
if ((dLat == NULL) || (dLng == NULL)) {
return;
}
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
transform(lng-105.0, lat-35.0, dLat, dLng);
double radLat = lat / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - ee*magic*magic;
double sqrtMagic = sqrt(magic);
*dLat = (*dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
*dLng = (*dLng * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
*dLat = (*dLat * 180.0) / ((EARTH_R * (1 - ee)) / (magic * sqrtMagic) * M_PI);
*dLng = (*dLng * 180.0) / (EARTH_R / sqrtMagic * cos(radLat) * M_PI);
}

void wgs2gcj(double wgsLat, double wgsLng, double *gcjLat, double *gcjLng) {
Expand Down Expand Up @@ -126,7 +127,6 @@ void gcj2wgs_exact(double gcjLat, double gcjLng, double *wgsLat, double *wgsLng)
}

double distance(double latA, double lngA, double latB, double lngB) {
const double earthR = 6378137;
double arcLatA = latA * M_PI/180;
double arcLatB = latB * M_PI/180;
double x = cos(arcLatA) * cos(arcLatB) * cos((lngA-lngB)*M_PI/180);
Expand All @@ -139,5 +139,5 @@ double distance(double latA, double lngA, double latB, double lngB) {
s = -1;
}
double alpha = acos(s);
return alpha * earthR;
return alpha * EARTH_R;
}
2 changes: 1 addition & 1 deletion javascript/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var transform = require('./transform.js');
var tests = [
// wgsLat, wgsLng, gcjLat, gcjLng
[31.1774276, 121.5272106, 31.17530398364597, 121.531541859215], // shanghai
[22.543847, 113.912316, 22.540796131694766, 113.9171764808363], // shenzhen
[22.543847, 113.912316, 22.540796131694766, 113.9171774808363], // shenzhen
[39.911954, 116.377817, 39.91334545536069, 116.38404722455657] // beijing
];

Expand Down
8 changes: 4 additions & 4 deletions javascript/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if (typeof module === "object" && exports) {
exports = window["eviltransform"] = {};
}

var earthR = 6378137.0;

function outOfChina(lat, lng) {
if ((lng < 72.004) || (lng > 137.8347)) {
return true;
Expand Down Expand Up @@ -43,15 +45,14 @@ function transform(x, y) {
}

function delta(lat, lng) {
var a = 6378137.0;
var ee = 0.00669342162296594323;
var d = transform(lng-105.0, lat-35.0);
var radLat = lat / 180.0 * Math.PI;
var magic = Math.sin(radLat);
magic = 1 - ee*magic*magic;
var sqrtMagic = Math.sqrt(magic);
d.lat = (d.lat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
d.lng = (d.lng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);
d.lat = (d.lat * 180.0) / ((earthR * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
d.lng = (d.lng * 180.0) / (earthR / sqrtMagic * Math.cos(radLat) * Math.PI);
return d;
}

Expand Down Expand Up @@ -105,7 +106,6 @@ function gcj2wgs_exact(gcjLat, gcjLng) {
exports.gcj2wgs_exact = gcj2wgs_exact;

function distance(latA, lngA, latB, lngB) {
var earthR = 6371000;
var pi180 = Math.PI / 180;
var arcLatA = latA * pi180;
var arcLatB = latB * pi180;
Expand Down
7 changes: 3 additions & 4 deletions python/eviltransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
__all__ = ['wgs2gcj', 'gcj2wgs', 'gcj2wgs_exact',
'distance', 'gcj2bd', 'bd2gcj', 'wgs2bd', 'bd2wgs']

earthR = 6378137.0

def outOfChina(lat, lng):
return not (72.004 <= lng <= 137.8347 and 0.8293 <= lat <= 55.8271)
Expand Down Expand Up @@ -38,15 +39,14 @@ def transform(x, y):


def delta(lat, lng):
a = 6378137.0
ee = 0.00669342162296594323
dLat, dLng = transform(lng-105.0, lat-35.0)
radLat = lat / 180.0 * math.pi
magic = math.sin(radLat)
magic = 1 - ee * magic * magic
sqrtMagic = math.sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * math.pi)
dLng = (dLng * 180.0) / (a / sqrtMagic * math.cos(radLat) * math.pi)
dLat = (dLat * 180.0) / ((earthR * (1 - ee)) / (magic * sqrtMagic) * math.pi)
dLng = (dLng * 180.0) / (earthR / sqrtMagic * math.cos(radLat) * math.pi)
return dLat, dLng


Expand Down Expand Up @@ -94,7 +94,6 @@ def gcj2wgs_exact(gcjLat, gcjLng):


def distance(latA, lngA, latB, lngB):
earthR = 6371000
pi180 = math.pi / 180
arcLatA = latA * pi180
arcLatB = latB * pi180
Expand Down
10 changes: 5 additions & 5 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::f64::consts::PI;

const EARTH_R: f64 = 6378137.0;

fn out_of_china(lat: f64, lng: f64) -> bool {
if lng < 72.004 || lng > 137.8347 {
return true
Expand Down Expand Up @@ -36,7 +38,6 @@ fn transform(x: f64, y: f64) -> (f64, f64) {
}

fn delta(lat: f64, lng: f64) -> (f64, f64) {
let a = 6378137.0;
let ee = 0.00669342162296594323;
let (d_lat, d_lng) = transform(lng-105.0, lat-35.0);
let mut d_lat = d_lat;
Expand All @@ -45,8 +46,8 @@ fn delta(lat: f64, lng: f64) -> (f64, f64) {
let mut magic = (rad_lat).sin();
magic = 1.0 - ee*magic*magic;
let sqrt_magic = (magic).sqrt();
d_lat = (d_lat * 180.0) / ((a * (1.0 - ee)) / (magic * sqrt_magic) * PI);
d_lng = (d_lng * 180.0) / (a / sqrt_magic * (rad_lat).cos() * PI);
d_lat = (d_lat * 180.0) / ((EARTH_R * (1.0 - ee)) / (magic * sqrt_magic) * PI);
d_lng = (d_lng * 180.0) / (EARTH_R / sqrt_magic * (rad_lat).cos() * PI);
(d_lat, d_lng)
}

Expand Down Expand Up @@ -105,7 +106,6 @@ pub fn gcj2wgs_exact(gcj_lat: f64, gcj_lng: f64) -> (f64, f64) {

// distance calculate the distance between point(lat_a, lng_a) and point(lat_b, lng_b), unit in meter.
pub fn distance(lat_a: f64, lng_a: f64, lat_b: f64, lng_b: f64) -> f64 {
const EARTH_R: f64 = 6371000.0;
let arc_lat_a = lat_a * PI / 180.0;
let arc_lat_b = lat_b * PI / 180.0;
let x = (arc_lat_a).cos() * (arc_lat_b).cos() * ((lng_a-lng_b)*PI/180.0).cos();
Expand Down Expand Up @@ -133,7 +133,7 @@ mod tests {
distance: f64,
}
const TESTS: [Test; 1] = [
Test{a_lat:31.17530398364597, a_lng:121.531541859215, b_lat:39.91334545536069, b_lng:116.38404722455657, distance:1076958.0}, // shanghai to beijing
Test{a_lat:31.17530398364597, a_lng:121.531541859215, b_lat:39.91334545536069, b_lng:116.38404722455657, distance:1078164.0}, // shanghai to beijing
];
for test in TESTS.iter() {
let d = super::distance(test.a_lat, test.a_lng, test.b_lat, test.b_lng);
Expand Down

0 comments on commit e05c227

Please sign in to comment.