People's Rectified Coordinates

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
    ) where

Points

A 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)

Constants

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

e2=2f−f2=0.00669342162296594323. e^2 = 2f - f^2 = 0.00669342162296594323.

gcjA, gcjEe, bdDLat, bdDLon, prcEps :: Double
gcjA = 6378245.0
gcjEe = 0.00669342162296594323

BD-09 adds the hard-coded Baidu offsets 0.0060 degrees north and 0.0065 degrees east.

bdDLat = 0.0060
bdDLon = 0.0065
prcEps = 1e-5

The distortion sums are multiples of π\pi, 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 / 0

The China check

GCJ-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 = p

WGS-84 to GCJ-02

GCJ-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)
  where

The 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))

GCJ-02 back to WGS-84

The rough inverse reuses the forward map:

𝚠𝚐𝚜=𝚐𝚌𝚓−(wgsGcj⁡(𝚐𝚌𝚓)−𝚐𝚌𝚓). \mathtt{wgs} = \mathtt{gcj} - (\operatorname{wgsGcj}(\mathtt{gcj}) - \mathtt{gcj}).

One such step is accurate to roughly 2×10−62 \times 10^{-6} 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)

GCJ-02 and BD-09

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 . bdGcjNC

The “bored” fixed point

The 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

𝚐𝚞𝚎𝚜𝚜←𝚐𝚞𝚎𝚜𝚜−(fwd⁡(𝚐𝚞𝚎𝚜𝚜)−𝚋𝚊𝚍). \mathtt{guess} \leftarrow \mathtt{guess} - (\operatorname{fwd}(\mathtt{guess}) - \mathtt{bad}).

It stops after ten rounds or once the correction is below 10−510^{-5} 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 bdWgs

Usually 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))

Notes