summaryrefslogtreecommitdiff
path: root/platform/node/src/node_feature.cpp
blob: 1a5e31fe97103c099be087bee4eae0b9128c25dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "node_feature.hpp"

namespace node_mbgl {

using namespace mapbox::geometry;

using Value = mbgl::Value;
using Feature = mbgl::Feature;
using Geometry = mbgl::Feature::geometry_type;
using GeometryCollection = mapbox::geometry::geometry_collection<double>;
using Properties = mbgl::Feature::property_map;

template <class T>
struct ToType {
public:
    v8::Local<v8::String> operator()(const point<T>&) {
        return type("Point");
    }

    v8::Local<v8::String> operator()(const line_string<T>&) {
        return type("LineString");
    }

    v8::Local<v8::String> operator()(const polygon<T>&) {
        return type("Polygon");
    }

    v8::Local<v8::String> operator()(const multi_point<T>&) {
        return type("MultiPoint");
    }

    v8::Local<v8::String> operator()(const multi_line_string<T>&) {
        return type("MultiLineString");
    }

    v8::Local<v8::String> operator()(const multi_polygon<T>&) {
        return type("MultiPolygon");
    }

    v8::Local<v8::String> operator()(const geometry_collection<T>&) {
        return type("GeometryCollection");
    }

private:
    v8::Local<v8::String> type(const char* type) {
        Nan::EscapableHandleScope scope;
        return scope.Escape(Nan::New(type).ToLocalChecked());
    }
};

template <class T>
struct ToCoordinatesOrGeometries {
public:
    // Handles line_string, polygon, multi_point, multi_line_string, multi_polygon, and geometry_collection.
    template <class E>
    v8::Local<v8::Object> operator()(const std::vector<E>& vector) {
        Nan::EscapableHandleScope scope;
        v8::Local<v8::Array> result = Nan::New<v8::Array>(vector.size());
        for (std::size_t i = 0; i < vector.size(); ++i) {
            Nan::Set(result, i, operator()(vector[i]));
        }
        return scope.Escape(result);
    }

    v8::Local<v8::Object> operator()(const point<T>& point) {
        Nan::EscapableHandleScope scope;
        v8::Local<v8::Array> result = Nan::New<v8::Array>(2);
        Nan::Set(result, 0, Nan::New(point.x));
        Nan::Set(result, 1, Nan::New(point.y));
        return scope.Escape(result);
    }

    v8::Local<v8::Object> operator()(const geometry<T>& geometry) {
        return toJS(geometry);
    }
};

struct ToValue {
    v8::Local<v8::Value> operator()(std::nullptr_t) {
        Nan::EscapableHandleScope scope;
        return scope.Escape(Nan::Null());
    }

    v8::Local<v8::Value> operator()(bool t) {
        Nan::EscapableHandleScope scope;
        return scope.Escape(Nan::New(t));
    }

    v8::Local<v8::Value> operator()(int64_t t) {
        return operator()(double(t));
    }

    v8::Local<v8::Value> operator()(uint64_t t) {
        return operator()(double(t));
    }

    v8::Local<v8::Value> operator()(double t) {
        Nan::EscapableHandleScope scope;
        return scope.Escape(Nan::New(t));
    }

    v8::Local<v8::Value> operator()(const std::string& t) {
        Nan::EscapableHandleScope scope;
        return scope.Escape(Nan::New(t).ToLocalChecked());
    }

    v8::Local<v8::Value> operator()(const std::vector<mbgl::Value>& array) {
        Nan::EscapableHandleScope scope;
        v8::Local<v8::Array> result = Nan::New<v8::Array>();
        for (unsigned int i = 0; i < array.size(); i++) {
            result->Set(i, toJS(array[i]));
        }
        return scope.Escape(result);
    }

    v8::Local<v8::Value> operator()(const std::unordered_map<std::string, mbgl::Value>& map) {
        return toJS(map);
    }
};

v8::Local<v8::Object> toJS(const Geometry& geometry) {
    Nan::EscapableHandleScope scope;

    v8::Local<v8::Object> result = Nan::New<v8::Object>();

    Nan::Set(result,
        Nan::New("type").ToLocalChecked(),
        Geometry::visit(geometry, ToType<double>()));

    Nan::Set(result,
        Nan::New(geometry.is<GeometryCollection>() ? "geometries" : "coordinates").ToLocalChecked(),
        Geometry::visit(geometry, ToCoordinatesOrGeometries<double>()));

    return scope.Escape(result);
}

v8::Local<v8::Value> toJS(const Value& value) {
    return Value::visit(value, ToValue());
}

v8::Local<v8::Object> toJS(const Properties& properties) {
    Nan::EscapableHandleScope scope;

    v8::Local<v8::Object> result = Nan::New<v8::Object>();
    for (const auto& property : properties) {
        Nan::Set(result, Nan::New(property.first).ToLocalChecked(), toJS(property.second));
    }

    return scope.Escape(result);
}

v8::Local<v8::Object> toJS(const Feature& feature) {
    Nan::EscapableHandleScope scope;

    v8::Local<v8::Object> result = Nan::New<v8::Object>();

    Nan::Set(result, Nan::New("type").ToLocalChecked(), Nan::New("Feature").ToLocalChecked());
    Nan::Set(result, Nan::New("geometry").ToLocalChecked(), toJS(feature.geometry));
    Nan::Set(result, Nan::New("properties").ToLocalChecked(), toJS(feature.properties));

    if (feature.id) {
        Nan::Set(result, Nan::New("id").ToLocalChecked(), Nan::New(double(*feature.id)));
    }

    return scope.Escape(result);
}

}