forked from radarlabs/s2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Earth#getInitalBearingDegree method for initialBearing.add polyli…
…ne#getCentroid and polyline#interpolate and polyline#project method
- Loading branch information
王亮杰
committed
May 19, 2020
1 parent
6beb5bb
commit 1b1d846
Showing
8 changed files
with
379 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
#include "earth.h" | ||
|
||
Napi::FunctionReference Earth::constructor; | ||
|
||
Napi::Object Earth::Init(Napi::Env env, Napi::Object exports) { | ||
Napi::HandleScope scope(env); | ||
|
||
Napi::Function func = DefineClass(env, "Earth", { | ||
StaticMethod("toMeters", &Earth::ToMeters), | ||
StaticMethod("toKm", &Earth::ToKm), | ||
StaticMethod("getRadian", &Earth::GetRadian), | ||
StaticMethod("getDegree", &Earth::GetDegree), | ||
StaticMethod("getDistanceKm", &Earth::GetDistanceKm), | ||
StaticMethod("getDistanceMeters", &Earth::GetDistanceMeters), | ||
StaticMethod("getInitalBearingDegree", &Earth::GetInitalBearingDegree), | ||
}); | ||
|
||
constructor = Napi::Persistent(func); | ||
constructor.SuppressDestruct(); | ||
|
||
exports.Set("Earth", func); | ||
return exports; | ||
} | ||
|
||
Earth::Earth(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Earth>(info) { | ||
Napi::Env env = info.Env(); | ||
Napi::HandleScope scope(env); | ||
} | ||
|
||
bool Earth::PreCheck(const Napi::CallbackInfo &info, S2LatLng &latlng1, S2LatLng &latlng2){ | ||
int length = info.Length(); | ||
|
||
if(length < 2 || !info[0].IsObject() || !info[1].IsObject()){ | ||
return false; | ||
} | ||
|
||
Napi::Object objectA = info[0].As<Napi::Object>(); | ||
bool isLatlng1 = objectA.InstanceOf(LatLng::constructor.Value()); | ||
if (!isLatlng1) { | ||
return false; | ||
} | ||
|
||
LatLng* ll1 = LatLng::Unwrap(objectA); | ||
latlng1 = ll1->Get(); | ||
|
||
Napi::Object objectB = info[1].As<Napi::Object>(); | ||
bool isLatlng2 = objectB.InstanceOf(LatLng::constructor.Value()); | ||
if (!isLatlng2) { | ||
return false; | ||
} | ||
|
||
LatLng* ll2 = LatLng::Unwrap(objectB); | ||
latlng2 = ll2->Get(); | ||
|
||
return true; | ||
} | ||
|
||
Napi::Value Earth::ToMeters(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
|
||
S2LatLng s2LatLngA; | ||
S2LatLng s2LatLngB; | ||
|
||
bool isValid = Earth::PreCheck(info, s2LatLngA, s2LatLngB); | ||
if(!isValid){ | ||
Napi::TypeError::New(env, "2 Latlng Object expected.").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
S1Angle s1Angle = S1Angle(s2LatLngA, s2LatLngB); | ||
double distance = S2Earth::ToMeters(s1Angle); | ||
|
||
return Napi::Number::New(env, distance); | ||
} | ||
|
||
Napi::Value Earth::ToKm(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
|
||
S2LatLng s2LatLngA; | ||
S2LatLng s2LatLngB; | ||
|
||
bool isValid = Earth::PreCheck(info, s2LatLngA, s2LatLngB); | ||
if(!isValid){ | ||
Napi::TypeError::New(env, "2 Latlng Object expected.").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
S1Angle s1Angle = S1Angle(s2LatLngA, s2LatLngB); | ||
|
||
double distance = S2Earth::ToKm(s1Angle); | ||
|
||
return Napi::Number::New(env, distance); | ||
} | ||
|
||
Napi::Value Earth::GetRadian(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
|
||
S2LatLng s2LatLngA; | ||
S2LatLng s2LatLngB; | ||
|
||
bool isValid = Earth::PreCheck(info, s2LatLngA, s2LatLngB); | ||
if(!isValid){ | ||
Napi::TypeError::New(env, "2 Latlng Object expected.").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
S1Angle s1Angle = S1Angle(s2LatLngA, s2LatLngB); | ||
|
||
double radians = s1Angle.radians(); | ||
return Napi::Number::New(env, radians); | ||
} | ||
|
||
Napi::Value Earth::GetDegree(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
|
||
S2LatLng s2LatLngA; | ||
S2LatLng s2LatLngB; | ||
|
||
bool isValid = Earth::PreCheck(info, s2LatLngA, s2LatLngB); | ||
if(!isValid){ | ||
Napi::TypeError::New(env, "2 Latlng Object expected.").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
S1Angle s1Angle = S1Angle(s2LatLngA, s2LatLngB); | ||
|
||
double degrees = s1Angle.degrees(); | ||
return Napi::Number::New(env, degrees); | ||
} | ||
|
||
Napi::Value Earth::GetDistanceKm(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
|
||
S2LatLng s2LatLngA; | ||
S2LatLng s2LatLngB; | ||
|
||
bool isValid = Earth::PreCheck(info, s2LatLngA, s2LatLngB); | ||
if(!isValid){ | ||
Napi::TypeError::New(env, "2 Latlng Object expected.").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
double distance = S2Earth::GetDistanceKm(s2LatLngA, s2LatLngB); | ||
|
||
return Napi::Number::New(env, distance); | ||
} | ||
|
||
Napi::Value Earth::GetDistanceMeters(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
|
||
S2LatLng s2LatLngA; | ||
S2LatLng s2LatLngB; | ||
|
||
bool isValid = Earth::PreCheck(info, s2LatLngA, s2LatLngB); | ||
if(!isValid){ | ||
Napi::TypeError::New(env, "2 Latlng Object expected.").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
double distance = S2Earth::GetDistanceMeters(s2LatLngA, s2LatLngB); | ||
|
||
return Napi::Number::New(env, distance); | ||
} | ||
|
||
Napi::Value Earth::GetInitalBearingDegree(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
|
||
S2LatLng s2LatLngA; | ||
S2LatLng s2LatLngB; | ||
|
||
bool isValid = Earth::PreCheck(info, s2LatLngA, s2LatLngB); | ||
if(!isValid){ | ||
Napi::TypeError::New(env, "2 Latlng Object expected.").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
S1Angle s1Angle = S2Earth::GetInitialBearing(s2LatLngA, s2LatLngB); | ||
|
||
double degrees = s1Angle.degrees(); | ||
return Napi::Number::New(env, degrees); | ||
} | ||
|
||
S2Earth Earth::Get() { | ||
return this->s2earth; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef RADAR_EARTH | ||
#define RADAR_EARTH | ||
|
||
#include <napi.h> | ||
#include "latlng.h" | ||
#include "s2/s1angle.h" | ||
#include "s2/s2latlng.h" | ||
#include "s2/s2earth.h" | ||
|
||
class Earth : public Napi::ObjectWrap<Earth> { | ||
public: | ||
Earth(const Napi::CallbackInfo& info); | ||
S2Earth Get(); | ||
|
||
static Napi::FunctionReference constructor; | ||
static Napi::Object Init(Napi::Env env, Napi::Object exports); | ||
|
||
private: | ||
static Napi::Value ToMeters(const Napi::CallbackInfo &info); | ||
static Napi::Value ToKm(const Napi::CallbackInfo &info); | ||
static Napi::Value GetRadian(const Napi::CallbackInfo &info); | ||
static Napi::Value GetDegree(const Napi::CallbackInfo &info); | ||
static Napi::Value GetDistanceKm(const Napi::CallbackInfo &info); | ||
static Napi::Value GetDistanceMeters(const Napi::CallbackInfo &info); | ||
static Napi::Value GetInitalBearingDegree(const Napi::CallbackInfo &info); | ||
|
||
static bool PreCheck(const Napi::CallbackInfo &info, S2LatLng &latlng1, S2LatLng &latlng2); | ||
|
||
S2Earth s2earth; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.