Permalink
Browse files

C version: make gcj2wgs_exact() much faster, by using fixed-point ite…

…ration.
  • Loading branch information...
bewantbe committed Mar 1, 2015
1 parent 6a6fb60 commit 86ae326d7f463f68aeef2b5bc287b53ad7425fa7
Showing with 15 additions and 25 deletions.
  1. +15 −25 c/transform.c
@@ -77,32 +77,22 @@ void gcj2wgs(double gcjLat, double gcjLng, double *wgsLat, double *wgsLng) {
}
void gcj2wgs_exact(double gcjLat, double gcjLng, double *wgsLat, double *wgsLng) {
const double initDelta = 0.01;
const double threshold = 0.000001;
double dLat = initDelta, dLng = initDelta;
double mLat = gcjLat-dLat, mLng = gcjLng-dLng;
double pLat = gcjLat+dLat, pLng = gcjLng+dLng;
double dLat, dLng;
// n_iter=2: centimeter precision, n_iter=5: double precision
const int n_iter = 2;
int i;
for (i = 0; i < 30; i++) {
*wgsLat = (mLat+pLat)/2;
*wgsLng = (mLng+pLng)/2;
double tmpLat, tmpLng;
wgs2gcj(*wgsLat, *wgsLng, &tmpLat, &tmpLng);
dLat = tmpLat - gcjLat;
dLng = tmpLng - gcjLng;
if ((fabs(dLat) < threshold) && (fabs(dLng) < threshold)) {
return;
}
if (dLat > 0) {
pLat = *wgsLat;
} else {
mLat = *wgsLat;
}
if (dLng > 0) {
pLng = *wgsLng;
} else {
mLng = *wgsLng;
}
if ((wgsLat == NULL) || (wgsLng == NULL)) {
return;
}
*wgsLat = gcjLat;
*wgsLng = gcjLng;
if (outOfChina(gcjLat, gcjLng)) {
return;
}
for (i = 0; i < n_iter; i++) {
delta(*wgsLat, *wgsLng, &dLat, &dLng);
*wgsLat = gcjLat - dLat;
*wgsLng = gcjLng - dLng;
}
}

0 comments on commit 86ae326

Please sign in to comment.