summaryrefslogtreecommitdiff
path: root/platform/android/src/geojson/conversion/feature.hpp
blob: 8fc62a27893e3cfbc9e81bfe5c525fd83ab06623 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma once

#include "../../conversion/constant.hpp"
#include "../../conversion/conversion.hpp"
#include "geometry.hpp"
#include "../../gson/json_object.hpp"

#include <mbgl/util/feature.hpp>
#include <mapbox/variant.hpp>
#include <mapbox/geometry.hpp>

#include <jni/jni.hpp>
#include "../../jni/local_object.hpp"
#include "../feature.hpp"

#include <string>
#include <array>
#include <vector>
#include <sstream>

#include <mbgl/util/logging.hpp>

namespace mbgl {
namespace android {
namespace conversion {

/**
 * Turn feature identifier into std::string
 */
class FeatureIdVisitor {
public:

    template<class T>
    std::string operator()(const T& i) const {
        return std::to_string(i);
    }

    std::string operator()(const std::string& i) const {
        return i;
    }

    std::string operator()(const std::nullptr_t&) const {
        return "";
    }

};

/**
 * Turn properties into Java GSON JsonObject's
 */
class PropertyValueEvaluator {
public:
    jni::JNIEnv& env;

    /**
     * null
     */
    jni::jobject* operator()(const mapbox::geometry::null_value_t &) const {
        return (jni::jobject*) nullptr;
    }

    /**
     * Boolean primitive
     */
    jni::jobject* operator()(const bool& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(Ljava/lang/Boolean;)V");

        // Create JsonPrimitive
        jni::LocalObject<jni::jobject> converted = jni::NewLocalObject(env, *convert<jni::jobject*, bool>(env, value));
        jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, *converted);

        return object;
    }

    /**
     * String primitive
     */
    jni::jobject* operator()(const std::string& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(Ljava/lang/String;)V");

        // Create JsonPrimitive
        jni::LocalObject<jni::jobject> converted = jni::NewLocalObject(env, *convert<jni::jobject*, std::string>(env, value));
        jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get());

        return object;
    }

    /**
     * Number primitives
     */
    template <class Number>
    jni::jobject* operator()(const Number& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(Ljava/lang/Number;)V");

        // Create JsonPrimitive
        jni::LocalObject<jni::jobject> converted = jni::NewLocalObject(env, *convert<jni::jobject*, Number>(env, value));
        jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get());

        return object;
    }


    /**
     * Json Array
     */
    jni::jobject* operator()(const std::vector<mbgl::Value> &values) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonArray")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "()V");;
        static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Lcom/google/gson/JsonElement;)V");

        // Create json array
        jni::jobject* jarray = &jni::NewObject(env, *javaClass, *constructor);

        // Add values
        for (const auto &v : values) {
            jni::LocalObject<jni::jobject> converted = jni::NewLocalObject(env, mbgl::Value::visit(v, *this));
            jni::CallMethod<void>(env, jarray, *add, converted.get());
        }

        return jarray;
    }

    /**
     * Json Object
     */
    jni::jobject* operator()(const std::unordered_map<std::string, mbgl::Value> &value) const {
        // TODO: clean up duplication here
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonObject")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "()V");;
        static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Ljava/lang/String;Lcom/google/gson/JsonElement;)V");

        // Create json object
        jni::jobject* jsonObject = &jni::NewObject(env, *javaClass, *constructor);

        // Add items
        for (auto &item : value) {
            jni::LocalObject<jni::jobject> converted = jni::NewLocalObject(env, mbgl::Value::visit(item.second, *this));
            jni::LocalObject<jni::jobject> key = jni::NewLocalObject(env, *convert<jni::jobject*, std::string>(env, item.first));
            jni::CallMethod<void>(env, jsonObject, *add, key.get(), converted.get());
        }

        return jsonObject;
    }
};

template <>
struct Converter<jni::jobject*, std::unordered_map<std::string, mbgl::Value>> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::unordered_map<std::string, mbgl::Value>& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonObject")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "()V");;
        static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Ljava/lang/String;Lcom/google/gson/JsonElement;)V");

        // Create json object
        jni::jobject* jsonObject = &jni::NewObject(env, *javaClass, *constructor);

        // Add items
        PropertyValueEvaluator evaluator {env};
        for (auto &item : value) {
            jni::LocalObject<jni::jobject> converted = jni::NewLocalObject(env, mbgl::Value::visit(item.second, evaluator));
            jni::LocalObject<jni::jobject> key = jni::NewLocalObject(env, *convert<jni::jobject*, std::string>(env, item.first));
            jni::CallMethod<void>(env, jsonObject, *add, key.get(), converted.get());
        }

        return {jsonObject};
    }
};


template <>
struct Converter<jni::Object<android::geojson::Feature>, mbgl::Feature> {
    Result<jni::Object<android::geojson::Feature>> operator()(jni::JNIEnv& env, const mbgl::Feature& value) const {

        // Convert Id
        FeatureIdVisitor idEvaluator;
        std::string id = (value.id) ? mapbox::geometry::identifier::visit(value.id.value(), idEvaluator) : "";
        auto jid = jni::Make<jni::String>(env, id);

        // Convert properties
        auto properties = jni::Object<gson::JsonObject>(*convert<jni::jobject*>(env, value.properties));

        // Convert geometry
        auto geometry = *convert<jni::Object<android::geojson::Geometry>>(env, value.geometry);

        // Create feature
        auto feature = android::geojson::Feature::fromGeometry(env, geometry, properties, jid);

        //Cleanup
        jni::DeleteLocalRef(env, jid);
        jni::DeleteLocalRef(env, geometry);
        jni::DeleteLocalRef(env, properties);

        return feature;
    }
};

template <>
struct Converter<jni::Array<jni::Object<android::geojson::Feature>>, std::vector<mbgl::Feature>> {
    Result<jni::Array<jni::Object<android::geojson::Feature>>> operator()(jni::JNIEnv& env, const std::vector<mbgl::Feature>& value) const {
        using namespace mbgl::android::geojson;
        auto features = jni::Array<jni::Object<Feature>>::New(env, value.size(), Feature::javaClass);

        for(size_t i = 0; i < value.size(); i = i + 1) {
            auto converted = *convert<jni::Object<android::geojson::Feature>, mbgl::Feature>(env, value.at(i));
            features.Set(env, i, converted);
            jni::DeleteLocalRef(env, converted);
        }

        return {features};
    }
};

} // namespace conversion
} // namespace android
} // namespace mbgl