google / open-location-code

Open Location Code is a library to generate short codes, called "plus codes", that can be used as digital addresses where street addresses don't exist.
https://plus.codes
Apache License 2.0
4.06k stars 471 forks source link

plpgsql implementation is buggy #467

Closed nvx closed 2 years ago

nvx commented 2 years ago

Encoding values with the plpgsql functions do not match other implementations, eg Golang:

postgres=# select pluscode_encode(31.820654,-81.636994,13);
 pluscode_encode
-----------------
 863WR9C7+7622X
(1 row)

Time: 0.319 ms

While the Golang implementation at https://play.golang.org/p/IcIVqEHu2RE produces 863WR9C7+7662X for the same input.

Passing both the plpgsql and Golang encoded codes through pluscode_decode results in both returning the same output which does not cover the input latitude. Decoding both codes on plus.codes shows that the plpgsql code is incorrect.

postgres=# select pluscode_decode('863WR9C7+7622X');
NOTICE:  digits char_length13
                      pluscode_decode
------------------------------------------------------------
 (31.820625,-81.637,31.820625,-81.637,13,31.820625,-81.637)
(1 row)

Time: 7.282 ms
postgres=# select pluscode_decode('863WR9C7+7662X');
NOTICE:  digits char_length13
                      pluscode_decode
------------------------------------------------------------
 (31.820625,-81.637,31.820625,-81.637,13,31.820625,-81.637)
(1 row)

Time: 0.583 ms
postgres=#

Furthermore some locations give a differing output length when calling pluscode_encode with the same codelength parameter:

postgres=# select pluscode_encode(-37.801809,175.305396,13);
 pluscode_encode
-----------------
 4VJQ58X4+75P8
(1 row)

Time: 0.376 ms
postgres=# select pluscode_encode(-42.902090,-71.300046,13);
 pluscode_encode
-----------------
 479C3MXX+5XG2
(1 row)

Time: 0.316 ms
postgres=#

Notice how both of these locations produce codes with only 4 characters after the + when the code length is set to 13 (inconsistent with other implementations), while the first example in this issue produced a code with 5 characters after the + with the same code length (consistent with other implementations). Note these examples also don't match other implementations output when truncated to 4 characters after the + either (same as the first example).

nvx commented 2 years ago

Tagging @benno-p (author of the plpgsql implementation)