Skip to content

Commit

Permalink
add getLength function for polyline
Browse files Browse the repository at this point in the history
  • Loading branch information
王亮杰 committed May 9, 2020
1 parent dd5424c commit 6beb5bb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/polyline.cc
Expand Up @@ -8,6 +8,7 @@ Napi::Object Polyline::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "Polyline", {
InstanceMethod("contains", &Polyline::Contains),
InstanceMethod("nearlyCovers", &Polyline::NearlyCovers),
InstanceMethod("getLength", &Polyline::GetLength),
});

constructor = Napi::Persistent(func);
Expand Down Expand Up @@ -111,4 +112,11 @@ S2Polyline* Polyline::Get() {
return this->s2polyline.Clone();
}

Napi::Value Polyline::GetLength(const Napi::CallbackInfo& info){
double length = 1;
Napi::Env env = info.Env();
S1Angle s1Angle = this->s2polyline.GetLength();
length = S2Earth::ToMeters(s1Angle);

return Napi::Number::New(env, length);
}
2 changes: 2 additions & 0 deletions src/polyline.h
Expand Up @@ -6,6 +6,7 @@
#include "latlng.h"
#include "cell.h"
#include "s2/s1angle.h"
#include "s2/s2earth.h"
#include "s2/s2pointutil.h"
#include "s2/s2polyline.h"
#include "s2/base/stringprintf.h"
Expand All @@ -21,6 +22,7 @@ class Polyline : public Napi::ObjectWrap<Polyline> {
private:
Napi::Value Contains(const Napi::CallbackInfo &info);
Napi::Value NearlyCovers(const Napi::CallbackInfo &info);
Napi::Value GetLength(const Napi::CallbackInfo &info);

S2Polyline s2polyline;
};
Expand Down
15 changes: 13 additions & 2 deletions test/Polyline.test.js
Expand Up @@ -44,6 +44,7 @@ test("Polyline#NearlyCovers true", () => {
});
const polyline = new s2.Polyline(lls);


const newBaiCauseway = [[30.261254, 120.147342],[30.260202, 120.146395],[30.258967, 120.145267],[30.257545, 120.144089], [30.256662, 120.143349],[30.256027, 120.142843],[30.255445, 120.142336],[30.254703, 120.141366]];
const newLls = newBaiCauseway.map((latlng) => {
const [lat, lng] = latlng;
Expand All @@ -69,7 +70,17 @@ test("Polyline#NearlyCovers false", () => {
const [lat, lng] = latlng;
return new s2.LatLng(lat, lng);
});
const newPolyline = new s2.Polyline(newLls);
const newPolyline = new s2.Polyline(newLls);

expect(polyline.nearlyCovers(newPolyline, 1e-3)).toBe(false);
});
});

test("Polyline#getLength", () => {
const lls = baiCauseway.map((latlng) => {
const [lat, lng] = latlng;
return new s2.LatLng(lat, lng);
});
const polyline = new s2.Polyline(lls);

expect(polyline.getLength()).toBe(805.4416481053961);
});

0 comments on commit 6beb5bb

Please sign in to comment.