[core] Add toDouble Conversion method for correctly parsing Lat Lng coordinates
This commit is contained in:
@@ -46,6 +46,7 @@ namespace conversion {
|
||||
|
||||
* `toBool(v)` -- returns `optional<bool>`, absence indicating `v` is not a JSON boolean
|
||||
* `toNumber(v)` -- returns `optional<float>`, absence indicating `v` is not a JSON number
|
||||
* `toDouble(v)` -- returns `optional<double>`, absence indicating `v` is not a JSON number
|
||||
* `toString(v)` -- returns `optional<std::string>`, absence indicating `v` is not a JSON string
|
||||
* `toValue(v)` -- returns `optional<mbgl::Value>`, a variant type, for generic conversion,
|
||||
absence indicating `v` is not a boolean, number, or string. Numbers should be converted to
|
||||
|
||||
@@ -8,17 +8,17 @@ namespace style {
|
||||
namespace conversion {
|
||||
|
||||
template<>
|
||||
struct Converter<std::unique_ptr<LatLng>> {
|
||||
struct Converter<LatLng> {
|
||||
public:
|
||||
template <class V>
|
||||
optional<std::unique_ptr<LatLng>> operator() (const V& value, Error& error) const {
|
||||
optional<LatLng> operator() (const V& value, Error& error) const {
|
||||
if (!isArray(value) || arrayLength(value) < 2 ) {
|
||||
error = { "coordinate array must contain numeric longtitude and latitude values" };
|
||||
return {};
|
||||
}
|
||||
//Style spec uses GeoJSON convention for specifying coordinates
|
||||
optional<float> latitude = toNumber(arrayMember(value, 1));
|
||||
optional<float> longitude = toNumber(arrayMember(value, 0));
|
||||
optional<double> latitude = toDouble(arrayMember(value, 1));
|
||||
optional<double> longitude = toDouble(arrayMember(value, 0));
|
||||
|
||||
if (!latitude || !longitude) {
|
||||
error = { "coordinate array must contain numeric longtitude and latitude values" };
|
||||
@@ -28,7 +28,7 @@ public:
|
||||
error = { "coordinate latitude must be between -90 and 90" };
|
||||
return {};
|
||||
}
|
||||
return { std::make_unique<LatLng>(*latitude, *longitude) };
|
||||
return LatLng(*latitude, *longitude);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -66,6 +66,15 @@ inline optional<float> toNumber(const mbgl::android::Value& value) {
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<double> toDouble(const mbgl::android::Value& value) {
|
||||
if (value.isNumber()) {
|
||||
auto num = value.toDouble();
|
||||
return num;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(const mbgl::android::Value& value) {
|
||||
if (value.isString()) {
|
||||
return value.toString();
|
||||
|
||||
@@ -104,6 +104,14 @@ inline optional<float> toNumber(const id value) {
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<double> toDouble(const id value) {
|
||||
if (_isNumber(value)) {
|
||||
return ((NSNumber *)value).doubleValue;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(const id value) {
|
||||
if (_isString(value)) {
|
||||
return std::string(static_cast<const char *>([value UTF8String]));
|
||||
|
||||
@@ -82,6 +82,14 @@ inline optional<float> toNumber(v8::Local<v8::Value> value) {
|
||||
return value->NumberValue();
|
||||
}
|
||||
|
||||
inline optional<double> toDouble(v8::Local<v8::Value> value) {
|
||||
Nan::HandleScope scope;
|
||||
if (!value->IsNumber()) {
|
||||
return {};
|
||||
}
|
||||
return value->NumberValue();
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(v8::Local<v8::Value> value) {
|
||||
Nan::HandleScope scope;
|
||||
if (!value->IsString()) {
|
||||
|
||||
@@ -83,6 +83,13 @@ inline optional<float> toNumber(const QVariant& value) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
inline optional<double> toDouble(const QVariant& value) {
|
||||
if (value.type() == QVariant::Int || value.type() == QVariant::Double) {
|
||||
return value.toDouble();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(const QVariant& value) {
|
||||
if (value.type() == QVariant::String) {
|
||||
|
||||
@@ -62,6 +62,13 @@ inline optional<float> toNumber(const JSValue& value) {
|
||||
return value.GetDouble();
|
||||
}
|
||||
|
||||
inline optional<double> toDouble(const JSValue& value) {
|
||||
if (!value.IsNumber()) {
|
||||
return {};
|
||||
}
|
||||
return value.GetDouble();
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(const JSValue& value) {
|
||||
if (!value.IsString()) {
|
||||
return {};
|
||||
|
||||
@@ -17,6 +17,7 @@ using ValueMap = std::unordered_map<std::string, Value>;
|
||||
using ValueVector = std::vector<Value>;
|
||||
class Value : public mbgl::variant<std::string,
|
||||
float,
|
||||
double,
|
||||
bool,
|
||||
mapbox::util::recursive_wrapper<ValueMap>,
|
||||
mapbox::util::recursive_wrapper<ValueVector>> {
|
||||
@@ -90,6 +91,14 @@ inline optional<float> toNumber(const Value& value) {
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
inline optional<double> toDouble(const Value& value) {
|
||||
if (value.is<double>()) {
|
||||
return value.get<double>();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(const Value& value) {
|
||||
if (value.is<std::string>()) {
|
||||
return value.get<std::string>();
|
||||
|
||||
Reference in New Issue
Block a user