Skip to content

Commit 2d2ab09

Browse files
authored
Merge pull request #64 from zongweil/master
Add C++ implementation of OLC.
2 parents 63102ad + 64ee6b5 commit 2d2ab09

File tree

10 files changed

+1030
-0
lines changed

10 files changed

+1030
-0
lines changed

cpp/BUILD

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
cc_library(
2+
name = "openlocationcode",
3+
srcs = [
4+
"openlocationcode.cc",
5+
],
6+
hdrs = [
7+
"codearea.h",
8+
"openlocationcode.h",
9+
],
10+
deps = [
11+
":codearea",
12+
],
13+
copts = ["-pthread"],
14+
linkopts = ["-pthread"],
15+
visibility = ["//visibility:public"],
16+
)
17+
18+
cc_library(
19+
name = "codearea",
20+
srcs = [
21+
"codearea.cc",
22+
],
23+
hdrs = [
24+
"codearea.h",
25+
],
26+
visibility = ["//visibility:private"],
27+
)
28+
29+
filegroup(
30+
name = "testdata",
31+
srcs = glob([
32+
"test_data/*",
33+
]),
34+
)
35+
36+
cc_test(
37+
name = "openlocationcode_test",
38+
size = "small",
39+
srcs = ["openlocationcode_test.cc"],
40+
data = [
41+
"testdata",
42+
],
43+
deps = [
44+
"@gtest//:main",
45+
":openlocationcode",
46+
],
47+
copts = ["-pthread", "-Iexternal/gtest/include"],
48+
linkopts = ["-pthread"],
49+
)
50+
51+
cc_binary(
52+
name = "openlocationcode_example",
53+
srcs = [
54+
"openlocationcode_example.cc",
55+
],
56+
deps = [
57+
":openlocationcode",
58+
],
59+
)

cpp/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Open Location Code C++ API
2+
This is the C++ implementation of the Open Location Code API.
3+
4+
# Usage
5+
6+
See openlocationcode_example.cc for how to use the library. To run the example, use:
7+
8+
```
9+
bazel run openlocationcode_example
10+
```
11+
12+
# Development
13+
14+
The library is built/tested using [Bazel](https://bazel.build). To build the library, use:
15+
16+
```
17+
bazel build openlocationcode
18+
```
19+
20+
To run the tests, use:
21+
22+
```
23+
bazel test openlocationcode_test
24+
```
25+
26+
The tests use the CSV files in the test_data folder. Make sure you copy this folder to the
27+
root of your local workspace.

cpp/WORKSPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
workspace(name = "openlocationcode")
2+
3+
new_http_archive(
4+
name = "gtest",
5+
url = "https://github.com/google/googletest/archive/release-1.8.0.zip",
6+
sha256 = "f3ed3b58511efd272eb074a3a6d6fb79d7c2e6a0e374323d1e6bcbcc1ef141bf",
7+
build_file = "gtest.BUILD",
8+
strip_prefix = "googletest-release-1.8.0/googletest",
9+
)

cpp/codearea.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "codearea.h"
2+
3+
#include <algorithm>
4+
5+
namespace openlocationcode {
6+
7+
const double kLatitudeMaxDegrees = 90;
8+
const double kLongitudeMaxDegrees = 180;
9+
10+
CodeArea::CodeArea(double latitude_lo, double longitude_lo, double latitude_hi,
11+
double longitude_hi, size_t code_length) {
12+
latitude_lo_ = latitude_lo;
13+
longitude_lo_ = longitude_lo;
14+
latitude_hi_ = latitude_hi;
15+
longitude_hi_ = longitude_hi;
16+
code_length_ = code_length;
17+
}
18+
19+
double CodeArea::GetLatitudeLo() const{
20+
return latitude_lo_;
21+
}
22+
23+
double CodeArea::GetLongitudeLo() const {
24+
return longitude_lo_;
25+
}
26+
27+
double CodeArea::GetLatitudeHi() const {
28+
return latitude_hi_;
29+
}
30+
31+
double CodeArea::GetLongitudeHi() const {
32+
return longitude_hi_;
33+
}
34+
35+
size_t CodeArea::GetCodeLength() const { return code_length_; }
36+
37+
LatLng CodeArea::GetCenter() const {
38+
double latitude_center = std::min(latitude_lo_ + (latitude_hi_ - latitude_lo_) / 2, kLatitudeMaxDegrees);
39+
double longitude_center = std::min(longitude_lo_ + (longitude_hi_ - longitude_lo_) / 2, kLongitudeMaxDegrees);
40+
LatLng center = {latitude: latitude_center, longitude: longitude_center};
41+
return center;
42+
}
43+
44+
} // namespace openlocationcode

cpp/codearea.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef LOCATION_OPENLOCATIONCODE_CODEAREA_H_
2+
#define LOCATION_OPENLOCATIONCODE_CODEAREA_H_
3+
4+
#include <cstdlib>
5+
6+
namespace openlocationcode {
7+
8+
struct LatLng {
9+
double latitude;
10+
double longitude;
11+
};
12+
13+
class CodeArea {
14+
public:
15+
CodeArea(double latitude_lo, double longitude_lo, double latitude_hi,
16+
double longitude_hi, size_t code_length);
17+
double GetLatitudeLo() const;
18+
double GetLongitudeLo() const;
19+
double GetLatitudeHi() const;
20+
double GetLongitudeHi() const;
21+
size_t GetCodeLength() const;
22+
LatLng GetCenter() const;
23+
24+
private:
25+
double latitude_lo_;
26+
double longitude_lo_;
27+
double latitude_hi_;
28+
double longitude_hi_;
29+
size_t code_length_;
30+
};
31+
32+
} // namespace openlocationcode
33+
34+
#endif // LOCATION_OPENLOCATIONCODE_CODEAREA_H_

cpp/gtest.BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cc_library(
2+
name = "main",
3+
srcs = glob(
4+
["src/*.cc"],
5+
exclude = ["src/gtest-all.cc"]
6+
),
7+
hdrs = glob([
8+
"include/**/*.h",
9+
"src/*.h"
10+
]),
11+
copts = ["-Iexternal/gtest/include"],
12+
linkopts = ["-pthread"],
13+
visibility = ["//visibility:public"],
14+
)

0 commit comments

Comments
 (0)