Skip to content

Commit 447f5d0

Browse files
authored
modify integer conversion for postgresql implementation (#701)
1 parent 740b35a commit 447f5d0

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

plpgsql/README.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
# Open Location Code PL/SQL
2+
23
This is the pl/sql implementation of the Open Location Code.
34

45
The library file is in `pluscode_functions.sql`.
56

67
All functions are installed in the public Schema.
78

8-
# Tests
9+
## Tests
10+
11+
Unit tests require [Docker](https://www.docker.com/) to be installed.
12+
13+
Download the `pluscode_functions.sql` and `tests_script_l.sql` files.
914

10-
Unit tests require docker,
11-
[DockerDesktop](https://www.docker.com/)
15+
Start a [PostgreSQL Docker](https://hub.docker.com/_/postgres) image and copy the Open Location Code files to it:
1216

13-
Download the pluscode_functions.sql file.
17+
1. Download and run a PostgreSQL image. Call it `pgtest` and run on port 5433:
1418

15-
Download the tests_script_l.sql file.
19+
```shell
20+
docker run --name pgtest -e POSTGRES_PASSWORD=postgres -d -p 5433:5432 postgres
21+
```
1622

17-
Before run the tests :
23+
1. Copy the Open Location Code files to the container and change the permissions to allow the `postgres` user to read them:
1824

19-
A - Upload and run postgresql image name : pgtest port 5433
20-
`docker run --name pgtest -e POSTGRES_PASSWORD=postgres -d -p 5433:5432 postgres`
25+
```shell
26+
docker cp pluscode_functions.sql pgtest:/pluscode_functions.sql
27+
docker cp tests_script_l.sql pgtest:/tests_script_l.sql
28+
sudo docker exec pgtest chmod a+r *.sql
29+
```
2130

22-
B - COPY file with olc functions in the container
23-
`docker cp c:/path/to/file/pluscode_functions.sql pgtest:/pluscode_functions.sql`
31+
1. Execute the SQL that defines the functions in the db:
2432

25-
C - COPY file with Tests in the container
26-
`docker cp c:/path/to/file/tests_script_l.sql pgtest:/tests_script_l.sql`
33+
```shell
34+
docker exec -u postgres pgtest psql postgres postgres -f ./pluscode_functions.sql
35+
```
2736

28-
D - Execute openlocation.sql in db
29-
`docker exec -u postgres pgtest psql postgres postgres -f ./pluscode_functions.sql`
37+
1. Execute tests script
3038

31-
Then Execute tests script
32-
`docker exec -u postgres pgtest psql postgres postgres -f ./tests_script_l.sql`
39+
```shell
40+
docker exec -u postgres pgtest psql postgres postgres -f ./tests_script_l.sql
41+
```
3342

43+
## Functions
3444

35-
## pluscode_encode()
45+
### pluscode_encode()
3646

3747
```sql
3848
pluscode_encode(latitude, longitude, codeLength) → {string}
@@ -48,7 +58,7 @@ Encode a location into an Open Location Code.
4858
| `longitude` | `number` |
4959
| `codeLength` | `number` |
5060

51-
## pluscode_decode()
61+
### pluscode_decode()
5262

5363
```sql
5464
pluscode_decode(code) → {codearea record}
@@ -66,8 +76,7 @@ Decodes an Open Location Code into its location coordinates.
6676

6777
The `CodeArea` record.
6878

69-
70-
## pluscode_shorten()
79+
### pluscode_shorten()
7180

7281
```sql
7382
pluscode_shorten(code, latitude, longitude) → {string}
@@ -88,8 +97,7 @@ Remove characters from the start of an OLC code.
8897
The code, shortened as much as possible that it is still the closest matching
8998
code to the reference location.
9099

91-
92-
## pluscode_recoverNearest()
100+
### pluscode_recoverNearest()
93101

94102
```sql
95103
pluscode_recoverNearest(shortCode, referenceLatitude, referenceLongitude) → {string}

plpgsql/pluscode_functions.sql

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,24 @@ BEGIN
351351

352352
codeLength := LEAST(codeLength, MAX_DIGIT_COUNT_);
353353

354-
latitude := pluscode_clipLatitude(latitude);
355-
longitude := pluscode_normalizeLongitude(longitude);
354+
latVal := floor(round((latitude + LATITUDE_MAX_) * FINAL_LAT_PRECISION_, 6));
355+
lngVal := floor(round((longitude + LONGITUDE_MAX_) * FINAL_LNG_PRECISION_, 6));
356356

357-
IF (latitude = 90) THEN
358-
latitude := latitude - pluscode_computeLatitudePrecision(codeLength);
357+
latVal := round(latitude * FINAL_LAT_PRECISION_);
358+
latVal := latVal + LATITUDE_MAX_ * FINAL_LAT_PRECISION_;
359+
IF (latVal < 0) THEN
360+
latVal := 0;
361+
ELSIF (latVal > 2 * LATITUDE_MAX_ * FINAL_LAT_PRECISION_) THEN
362+
latVal := 2 * LATITUDE_MAX_ * FINAL_LAT_PRECISION_ - 1;
359363
END IF;
360364

361-
latVal := floor(round((latitude + LATITUDE_MAX_) * FINAL_LAT_PRECISION_, 6));
362-
lngVal := floor(round((longitude + LONGITUDE_MAX_) * FINAL_LNG_PRECISION_, 6));
365+
lngVal := round(longitude * FINAL_LNG_PRECISION_);
366+
lngVal := lngVal + LONGITUDE_MAX_ * FINAL_LNG_PRECISION_;
367+
IF (lngVal < 0) THEN
368+
lngVal := lngVal % (2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_) + 2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_;
369+
ELSIF (lngVal > 2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_) THEN
370+
lngVal := lngVal % (2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_);
371+
END IF;
363372

364373
IF (codeLength > PAIR_CODE_LENGTH_) THEN
365374
i_ := 0;

0 commit comments

Comments
 (0)