Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"./src/polygon.cc",
"./src/polyline.cc",
"./src/region_coverer.cc",
"./src/region_term_indexer.cc",
"./src/cell_union.cc",

"./third_party/s2geometry/src/s2/base/stringprintf.cc",
Expand Down
88 changes: 88 additions & 0 deletions src/region_term_indexer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "region_term_indexer.h"

using absl::make_unique;
using absl::string_view;
using std::vector;

Napi::FunctionReference RegionTermIndexer::constructor;

Napi::Object RegionTermIndexer::Init(Napi::Env env, Napi::Object exports)
{
Napi::HandleScope scope(env);

Napi::Function func = DefineClass(env, "RegionTermIndexer", {
StaticMethod("getIndexTerms", &RegionTermIndexer::GetIndexTerms),
StaticMethod("getQueryTerms", &RegionTermIndexer::GetQueryTerms),
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();

exports.Set("RegionTermIndexer", func);
return exports;
}

RegionTermIndexer::RegionTermIndexer(const Napi::CallbackInfo &info) : Napi::ObjectWrap<RegionTermIndexer>(info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
}

Napi::Value RegionTermIndexer::GetIndexTerms(const Napi::CallbackInfo &info)
{
// TODO implement properly.
Napi::Env env = info.Env();

uint32_t size = 5;
Napi::Array returnedIds = Napi::Array::New(env, size);

for (uint32_t i = 0; i < size; i++)
{
returnedIds[i] = i;
}

return returnedIds;

// Napi::Env env = info.Env();

// S2RegionTermIndexer termIndexer;
// S2Point point(0.1, -0.4, 0.3);
// vector<string> result = termIndexer.GetIndexTerms(point, string_view("foo"));

// uint32_t size = 5;
// Napi::Array returnedIds = Napi::Array::New(env, size);

// for (uint32_t i = 0; i < size; i++)
// {
// returnedIds[i] = result[i];
// }

// return returnedIds;
}

Napi::Value RegionTermIndexer::GetQueryTerms(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();

S2RegionTermIndexer::Options options;
S2RegionTermIndexer termIndexer(options);

S2Point point(0.1, -0.4, 0.3);
absl::string_view str("foo");

// This line causes it to die at runtime with the error:
// dyld[15800]: missing symbol called
// Abort trap: 6
std::vector<std::string> result = termIndexer.GetQueryTerms(point, str);

// TODO this is just so we're returning the correct type for now. WIP.
uint32_t size = 5;
Napi::Array returnedIds = Napi::Array::New(env, size);

for (uint32_t i = 0; i < size; i++)
{
returnedIds[i] = i;
}

return returnedIds;
}
32 changes: 32 additions & 0 deletions src/region_term_indexer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef RADAR_REGION_TERM_INDEXER
#define RADAR_REGION_TERM_INDEXER

#include <napi.h>
#include "polygon.h"
#include "cell_id.h"
#include "cell_union.h"
#include "s2/s1angle.h"
#include "s2/s2builder.h"
#include "s2/s2builderutil_s2polygon_layer.h"
#include "s2/s2cap.h"
#include "s2/s2cell_union.h"
#include "s2/s2earth.h"
#include "s2/s2polygon.h"
#include "s2/s2region_term_indexer.h"
#include "s2/third_party/absl/memory/memory.h"

class RegionTermIndexer : public Napi::ObjectWrap<RegionTermIndexer>
{

public:
static Napi::FunctionReference constructor;
static Napi::Object Init(Napi::Env env, Napi::Object exports);

RegionTermIndexer(const Napi::CallbackInfo &info);

private:
static Napi::Value GetIndexTerms(const Napi::CallbackInfo &info);
static Napi::Value GetQueryTerms(const Napi::CallbackInfo &info);
};

#endif
5 changes: 4 additions & 1 deletion src/s2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#include "polygon.h"
#include "polyline.h"
#include "region_coverer.h"
#include "region_term_indexer.h"

Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
Napi::Object InitAll(Napi::Env env, Napi::Object exports)
{
Builder::Init(env, exports);
Cell::Init(env, exports);
CellId::Init(env, exports);
Expand All @@ -21,6 +23,7 @@ Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
Polygon::Init(env, exports);
Polyline::Init(env, exports);
CellUnion::Init(env, exports);
RegionTermIndexer::Init(env, exports);
return RegionCoverer::Init(env, exports);
}

Expand Down
19 changes: 19 additions & 0 deletions test/RegionTermIndexer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// magic incantation from step 3 @ https://github.com/mapbox/node-pre-gyp#readme
const binary = require("@mapbox/node-pre-gyp");
const path = require("path");
var binding_path = binary.find(path.resolve("./package.json"));
const s2 = require(binding_path);

test("RegionTermIndexer#getIndexTerms works", () => {
// const dumbo = [40.7033, -73.9881];
// const ll = new s2.LatLng(...dumbo);
const tokens = s2.RegionTermIndexer.getIndexTerms({}, "asd");
expect(tokens).toBe(["89c25a3", "89c25a5", "89c25bb", "89c25bd"]);
});

test("RegionTermIndexer#getQueryTerms works", () => {
// const dumbo = [40.7033, -73.9881];
// const ll = new s2.LatLng(...dumbo);
const tokens = s2.RegionTermIndexer.getQueryTerms({}, "asd");
expect(tokens).toBe(["89c25a3", "89c25a5", "89c25bb", "89c25bd"]);
});