People’s Rectified Coordinates (PRCoords) is a cross-language implementation of the “public secret” Chinese coordinate obfuscations GCJ-02 and BD-09, together with the fixed-point inverses.
You are reading the literate Haskell port. The other ports, the test vectors and the reference Python implementation live in the main repository.
The code is dedicated to the Public Domain under CC0.
{-# LANGUAGE Strict #-}
module PRCoords
( PCoords(..)
, inChina
, wgsGcj, wgsGcjNC
, gcjWgs, gcjWgsNC
, gcjBd, gcjBdNC, bdGcj, bdGcjNC
, wgsBd, wgsBdNC
, bdWgs, bdWgsNC
, gcjWgsBored, bdGcjBored, bdWgsBored
, bored3
) whereA point is a pair of degrees. Latitude is first, matching the rest of the project.
data PCoords = PCoords { lat :: Double
, lon :: Double } deriving (Eq, Ord, Show)
subtractPCoords :: PCoords -> PCoords -> PCoords
subtractPCoords a b = PCoords (lat a - lat b) (lon a - lon b)GCJ-02 uses the Krasovsky 1940 ellipsoid (the SK-42 ellipsoid) for calculating the shift. The semimajor axis ‘’a’’ is 6378245 m; the flattening ‘’f’’ is 1/298.3, hence
gcjA, gcjEe, bdDLat, bdDLon, prcEps :: Double
gcjA = 6378245.0
gcjEe = 0.00669342162296594323BD-09 adds the hard-coded Baidu offsets 0.0060 degrees north and 0.0065 degrees east.
bdDLat = 0.0060
bdDLon = 0.0065
prcEps = 1e-5The distortion sums are multiples of , and the helpers are named after the C23 functions. We do not actually need to be this accurate.
degToRad :: Double -> Double
degToRad d = d * (pi / 180)
sinpi :: Double -> Double
sinpi x = sin (pi * x)
cospi :: Double -> Double
cospi x = cos (pi * x)
inf :: Double
inf = 1 / 0GCJ-02 and BD-09 are only defined inside China. The check below is a coarse rectangle. The caller should have its own check if it handles coordiantes from both inside and outside of china.
The port therefore also exposes the NC variants for
callers that have their own definition of “inside”.
inChina :: PCoords -> Bool
inChina (PCoords lat lon) =
0.8293 <= lat && lat <= 55.8271
&& 72.004 <= lon && lon <= 137.8347
withChinaCheck :: (PCoords -> PCoords) -> PCoords -> PCoords
withChinaCheck f p
| inChina p = f p
| otherwise = pGCJ-02 works by finding out the desired distortion of arc length, then converting to degrees.
wgsGcj :: PCoords -> PCoords
wgsGcj = withChinaCheck wgsGcjNC
wgsGcjNC :: PCoords -> PCoords
wgsGcjNC (PCoords lat lon) =
PCoords (lat + dLat / arclenLat) (lon + dLon / arclenLon)
whereThe two arc-length factors are the standard lengths of a degree of latitude and longitude on the SK-42 ellipsoid. Mixing WGS-84 and SK-42 is, well, what the original obfuscator did and what we must continue doing.
rad = degToRad lat
s = sin rad
magic = 1 - gcjEe * s * s
arclenLat = degToRad (gcjA * (1 - gcjEe) / (magic ** 1.5))
arclenLon = degToRad (gcjA * cos rad / sqrt magic)The distortion functions are centred at 105E, 35N. The constant terms set a baseline error of (300, -100).
x = lon - 105
y = lat - 35
dLat = -100 + 2 * x + 3 * y + 0.2 * y * y + 0.1 * x * y
+ 0.2 * sqrt (abs x)
+ (20 / 3) * ( 2 * sinpi (x * 6) + 2 * sinpi (x * 2)
+ 2 * sinpi y + 4 * sinpi (y / 3)
+ 16 * sinpi (y / 12) + 32 * sinpi (y / 30))
dLon = 300 + x + 2 * y + 0.1 * x * x + 0.1 * x * y
+ 0.1 * sqrt (abs x)
+ (20 / 3) * ( 2 * sinpi (x * 6) + 2 * sinpi (x * 2)
+ 2 * sinpi x + 4 * sinpi (x / 3)
+ 15 * sinpi (x / 12) + 30 * sinpi (x / 30))The rough inverse reuses the forward map:
One such step is accurate to roughly
degrees for the usual Chinese data. It is the same gcj_wgs
used by the other ports.
gcjWgs :: PCoords -> PCoords
gcjWgs = withChinaCheck gcjWgsNC
gcjWgsNC :: PCoords -> PCoords
gcjWgsNC gcj = subtractPCoords gcj (subtractPCoords (wgsGcjNC gcj) gcj)BD-09 is defined on top of GCJ-02. Treat the GCJ longitude/latitude pair as a plane, rotate-and-stretch it slightly, and add the Baidu offsets.
The inverse subtracts the offsets first and uses the opposite signs.
These constants are hard-coded by Baidu. pycoordtrans/coordtrans.cpp
may contain some information on how they are determined.
gcjBd :: PCoords -> PCoords
gcjBd = withChinaCheck gcjBdNC
gcjBdNC :: PCoords -> PCoords
gcjBdNC (PCoords lat lon) =
PCoords (r * sin theta + bdDLat) (r * cos theta + bdDLon)
where
x = lon
y = lat
r = sqrt (x * x + y * y) + 0.00002 * sinpi (y * 3000 / 180)
theta = atan2 y x + 0.000003 * cospi (x * 3000 / 180)
bdGcj :: PCoords -> PCoords
bdGcj = withChinaCheck bdGcjNC
bdGcjNC :: PCoords -> PCoords
bdGcjNC (PCoords lat lon) = PCoords (r * sin theta) (r * cos theta)
where
x = lon - bdDLon
y = lat - bdDLat
r = sqrt (x * x + y * y) - 0.00002 * sinpi (y * 3000 / 180)
theta = atan2 y x - 0.000003 * cospi (x * 3000 / 180)Each transform has a checked form that returns its input unchanged
outside China, and an NC form that always applies the map.
The composite checked forms test the input once and use the
NC maps internally.
wgsBd :: PCoords -> PCoords
wgsBd = withChinaCheck wgsBdNC
wgsBdNC :: PCoords -> PCoords
wgsBdNC = gcjBdNC . wgsGcjNC
bdWgs :: PCoords -> PCoords
bdWgs = withChinaCheck bdWgsNC
bdWgsNC :: PCoords -> PCoords
bdWgsNC = gcjWgsNC . bdGcjNCThe single-step inverse is close, but not close enough for archival work. The Caijun 2014 method takes the rough inverse as a guess and repeatedly applies
It stops after ten rounds or once the correction is below degrees. The name “bored” is the project’s joke: the result usually exceeds the accuracy of the source data. The original GCJ implementation contains noise from a linear-congruential pseudo-random generator, and Baidu’s API appears to do something similar, so there is no point chasing the last bit.
bored :: (PCoords -> PCoords) -> (PCoords -> PCoords) -> PCoords -> PCoords
bored fwd rev bad = go (rev bad) (PCoords inf inf) (0 :: Int)
where
go wgs diff i
| i >= 10 = wgs
| magnitude diff <= prcEps = wgs
| otherwise = go (subtractPCoords wgs d) d (i + 1)
where
d = subtractPCoords (fwd wgs) bad
magnitude (PCoords la lo) = sqrt (la * la + lo * lo)
gcjWgsBored :: PCoords -> PCoords
gcjWgsBored = bored wgsGcjNC gcjWgs
bdGcjBored :: PCoords -> PCoords
bdGcjBored = bored gcjBdNC bdGcj
bdWgsBored :: PCoords -> PCoords
bdWgsBored = bored wgsBdNC bdWgsUsually three rounds are enough, so a fixed-count variant skips the convergence test:
bored3 :: (PCoords -> PCoords) -> (PCoords -> PCoords) -> PCoords -> PCoords
bored3 fwd rev bad = go (3 :: Int) (rev bad)
where
go 0 p = p
go n p = go (n - 1) (subtractPCoords p (subtractPCoords (fwd p) bad))NC functions perform no China test; they are what
the fixed-point wrappers use internally.operator<.sqrt(abs x) has its own
test set.