Skip to main content

PRCoords

简体中文 || GitHub Main / Gitee Mirror

People's Rectified Coordinates (PRCoords) is a cross-language implementation of "public secret" Chinese coordinate obfuscation methods including GCJ-02 and BD-09, along with general deobfuscation methods previously established in ChinaMapShift, eviltransform, and geoChina. (Referring to the process of replacing straight lines with wavy ones as a "transform" is euphemism overdone.)

For a background on China's geographic obfuscation, see Restrictions on geographic data in China and 中华人民共和国测绘限制 on Wikipedia.

Languages

  • JavaScript (npm install prcoords)
  • Python (pip install prcoords)
  • (Obj-)C/C++ (C ABI) - simple Makefile with install
  • Ruby
  • C#
  • Haskell (haskell/PRCoords.hs)
  • Java 8+ (io.github.artoria2e5.prcoords, with an optional JNI bridge to the C++ core)
  • Matlab/Octave
  • R (r/, package prcoords, vectorized)
  • Julia (julia/, package PRCoords)
  • PGSQL
  • Typed Racket (racket/, package prcoords, Flonum types)
  • Go (go/, module github.com/Artoria2e5/PRCoords, package path .../go)
  • Lua 5.1+ (lua/PRCoords.lua, a plain PRCoords table; lua/mediawiki.lua is a Scribunto shim)
  • Swift 5+ (swift/, SwiftPM package prcoords, product PRCoords, d4 sinpi approximation)

Tests

TestVecs/ holds reference vectors generated by the Python implementation, which is the canonical one: CPython does its floating-point arithmetic the dumb, faithful way, so its numbers travel well.

Implementations are expected to ship a dumper that reads input coordinates and writes the same 8-column TSV layout as the reference, one line per point. The Python tool py/checkvecs.py then compares the dump with the vectors and reports the worst deviation per transform:

$ cpp/vecs TestVecs/inputs.tsv | python py/checkvecs.py - --profile badmath
$ python py/checkvecs.py --run "node js/vecs.js" --profile exact

The --profile picks the tolerance that fits the build (exact for plain double math, badmath for the sinpi approximation, float and float-badmath for the 32-bit build). The 105E 35N origin of the deviation polynomial sits on a cusp of the field, so it ships separately, with a sweep of neighbours, as TestVecs/cusp.tsv, and is not graded. See TestVecs/README.md for the format and the numbers behind each profile.

API

PRCoord's APIs operate on, and returns, dedicated structures for coordinates. In API names, we generally refer to WGS-84 as wgs, GCJ-02 as gcj, and BD-09 (lat-lon) as bd.

Inverse functions

The obfuscations generally have these properties to maintain basic usefulness:

  1. obfs(coord) is sort of close to coord.
  2. obfs(a) - obfs(b) is usually close to a - b. (The closer a and b are to each other, the better it works.)

In general two approaches of inverting the "forward" obfuscations, or working from obfs(coord) to coord, are implemented:

  • Run it backwards: obfs(coord) is never too far from coord, so just use  obfs(obfs(coord)) - obfs(coord) to estimate obfs(coord) - coord.
  • Iterate a bit: Get a rough guess somehow, and just use property 2 to estimate the remaining error as obfs(guess) - obfs(coord) and correct the guess.

You can read on the demo page about how well these methods work from the ΔRoundtrip entry. Unless you are doing archival work, you generally don't have to iterate.

The "in China" sanity check

Typically PRCoords is only supposed to be ran on obfuscated input data, which are primarily Chinese coordinates. For this reason, initial implementations include this very very rough sanity check that spans a rectangular region on a mercator-projected map. This check can be overridden by passing a boolean value, or may be not at all implemented in certain languages if I am not in the right mood for doing silly things.

There is an "insane" sanity check intended to approximate the range of Google and Baidu's distortion, intended for use by IITC: js/insane_is_in_china.js. It is basically a ray-casting polygon check with 70 vertices. You, as the caller, should still be responsible for telling whether a point is part of the gov-screwed Chinese data.

FAQ

Why another wheel?

  • Correctness: everyone else is either wrong or deleted
  • Public Domain: does not make sense to even try to copyright a public secret
  • Clean API based on pairs of coordinates
  • Need to find a place for this sarcastic name

Can the systems be described as WKT or proj-strings?

Not directly as a datum, because in both representations a datum is either "sane" (no non-linearity in 3D, Helmert possible) or a big table of grids.

However, the PROJECTION entry of a PROJCS can be used to describe the obfuscation as a black box. Since a PROJCS cannot be nested in another, we would not be able to describe the sequential nature of BD but only specify it as an apparantly unrelated projection.

PROJCS["Chinese BSM 2002, Pseudo-Ellipsoidal",
    GEOGCS["WGS 84", AUTHORITY["EPSG","4326"]],
    PROJECTION["CN_Obfs_GCJ_2002_Ellipsoidal"],
    AXIS["longitude",east],
    AXIS["latitude",north],
    UNIT["degree",0.0174532925199433,
      AUTHORITY["EPSG","9122"]],
    EXTENSION["PROJ4","+proj=gcjlonglat +units=deg +nadgrids=@null +wktext +no_defs"],
    AUTHORITY["EPSG","888000"]]

PROJCS["Baidu 2009, Pseudo-Ellipsoidal",
    GEOGCS["WGS 84", AUTHORITY["EPSG","4326"]],
    PROJECTION["CN_Obfs_Baidu_2009_Ellipsoidal"],
    AXIS["longitude",east],
    AXIS["latitude",north],
    UNIT["degree",0.0174532925199433,
      AUTHORITY["EPSG","9122"]],
    EXTENSION["PROJ4","+proj=bd09 +units=deg +nadgrids=@null +wktext +no_defs"],
    AUTHORITY["EPSG","888001"]]

Also encountered on the web is a "Baidu Meractor", fortunately only used in proximity to their tile server. It works by pretending the BD-09 lat-lon are on a Clark 1866 ellipsoid.

PROJCS["Baidu 2009, Pseudo-Mercator",
    GEOGCS["WGS 84", AUTHORITY["EPSG","4326"]],
    PROJECTION["CN_Obfs_Baidu_2009_Mercator"],
    AXIS["x",east],
    AXIS["y",north],
    UNIT["metre",1,
      AUTHORITY["EPSG","9001"]],
    EXTENSION["PROJ4","+proj=baidumerc +units=m +nadgrids=@null +wktext +no_defs"],
    AUTHORITY["EPSG","888002"]]

In all cases we use GEOGCS["WGS 84", AUTHORITY["EPSG","4326"]] and +natgrids=@null to prevent any datum transformation. The EPSG:88800x codes are private-use extensions chosen for the positive connotations of 888 in Chinese culture; think of it like the EPSG:900913 code of old.

proj4 itself of course won't understand these projections, but the good people at proj4js has made their stuff very easy to extend. Here is an example (backed up in docs/proj4_plugin.js in case RunKit goes down) of how to add PRCoords support to proj4js.

Should I use fast fp math?

Yes. The original GCJ-02 (in "EMQ") has a pseudo-random component that adds up to 1 meter of error. You just can't do worse than that.

A few corner-cutting options may be enabled in the C++ implementation. These include:

  • PRCOORDS_NUM may be redefined from double to float for a 32-bit float implementation.
  • PRCOORDS_APPROX may be defined to enable a fast approximation of the sin(x*pi) function due to devmaster user Nick. See cpp/badmath.hh and cpp/bench_out.

Physical PRCoords

You can print out a minimal copy of PRCoords with this PDF file. I am working on some better options in issue #2. A fairly simple tote bag with an older version of the PDF is available from Teespring.

Feel free to print and sell t-shirts with the PDF file! It is put in the Public Domain, so you don't have to pay me for that. You can always fund my subversive activities on Patreon though.

License

Unless otherwise mentioned, all files in this package, including this README file, are dual-licensed under:

GPL is only included for fun here. Dual-licensing works under an "or" condition, so don't worry.

Sources

  • Algorithm.Coords.Converter from EMQ (GitHub mirror) is probably the GCJ leak. It is a JSP project "for demonstrating GIS systems", probably done by some government contractor.
    • There is some randomness in the GCJ deltas on both axes: one sin invocation and one LCG. Each add a maximum of 1 meter of error.
  • on4wp7 (2013) is the earliest rationalized GCJ (forward) implementation.
  • ChinaMapShift (2014) figured out the quick iterative inverse for GCJ. I learned about it via geoChina first and generalized it here.
  • BD-09 is not very well sourced, but pycoordtrans (2014) seems to have it.

See also

  • eviltransform is among the most popular cross-language soltions to the problem. It borrows its name directly from EvilTransform.cs, an early refactored version of a raw-flesh Java implementation found in "emq", some sort of government contractor GIS demo project.
    • Since June 2016, eviltransform contains numerous parameter errors that compromise its output, especially for BD-09. See googollee/eviltransform#43, #53 and #44 for corrections. As of June 2019 these problems are not fixed.
  • geoChina by caijun is a clear, concise implementation written in R. It features the iterative method from ChinaMapShift.
  • I am planning on moving some of the comments on the algorithm found in my initial JavaScript implementation to the GitHub Wiki. I don't think anyone is going to be interested in copying comments on these idiocy when translating my implementation to other languages.
    • If you are doing a translation, consider only using the comments from the PDF.
  • Ishisashi's writeup on this subject. They wrote a super enhanced version of the demo too.

Oh, and finally, here is an official news report on that particular [bleep] who came up with GCJ-02.

Metadata

Release files for prcoords 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prcoords 1.2.1
File Size Uploaded Provenance Actions
prcoords-1.2.1.tar.gz 11.5 kB Without provenance Details for prcoords-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for prcoords 1.2.1
File Interpreter ABI Platform Actions
prcoords-1.2.1-py3-none-any.whl Python 3 none any Details for prcoords-1.2.1-py3-none-any.whl

Total release size: 22.3 kB

Release files / prcoords-1.2.1.tar.gz

Download URL prcoords-1.2.1.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
814966005583bd30c4178fc5957d7ec9aecfd054f3666dd9015bd784c002effa
BLAKE2b-256 checksum
How to use checksums
b5657d1edbfb7507c9fbe2b8a90cbf149ec17925fd28c8d32be511d3954e68dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / prcoords-1.2.1-py3-none-any.whl

Download URL prcoords-1.2.1-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e2d2f25abc290e78fcbdf72c73ddaba66cb13e91dcf3799d17e2e16cf0cd01b
BLAKE2b-256 checksum
How to use checksums
f680ac2d6051025bd2d3c3101851949d06738a7e57f4393d852949023a95156a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for prcoords 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page