summaryrefslogtreecommitdiff
path: root/platform/android/src/geojson/feature.cpp
blob: d8a4e829e28b6833f697d71b51c0eb1a2f7e81d5 (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
#include "feature.hpp"

#include "geometry.hpp"

namespace mbgl {
namespace android {
namespace geojson {

mbgl::Feature Feature::convert(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
    // Convert
    auto jGeometry = geometry(env, jFeature);
    auto jProperties = Feature::properties(env, jFeature);

    std::experimental::optional<mapbox::geometry::identifier> id;
    auto jId = Feature::id(env, jFeature);
    if (jId) {
        id = {  jni::Make<std::string>(env, jId) };
    }

    auto feature = mbgl::Feature {
            Geometry::convert(env, jGeometry),
            gson::JsonObject::convert(env, jProperties),
            id
    };

    // Cleanup
    jni::DeleteLocalRef(env, jGeometry);
    jni::DeleteLocalRef(env, jProperties);
    jni::DeleteLocalRef(env, jId);

    return feature;
}

jni::Object<Geometry> Feature::geometry(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
    static auto method = Feature::javaClass.GetMethod<jni::Object<Geometry> ()>(env, "geometry");
    return jFeature.Call(env, method);
}

jni::Object<gson::JsonObject> Feature::properties(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
    static auto method = Feature::javaClass.GetMethod<jni::Object<gson::JsonObject> ()>(env, "properties");
    return jFeature.Call(env, method);
}

jni::String Feature::id(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
    static auto method = Feature::javaClass.GetMethod<jni::String ()>(env, "id");
    return jFeature.Call(env, method);
}

jni::Object<Feature> Feature::fromGeometry(jni::JNIEnv& env, jni::Object<Geometry> geometry, jni::Object<gson::JsonObject> properties, jni::String id) {
    static auto method = Feature::javaClass.GetStaticMethod<jni::Object<Feature> (jni::Object<Geometry>, jni::Object<gson::JsonObject>, jni::String)>(env, "fromGeometry");
    return Feature::javaClass.Call(env, method, geometry, properties, id);
}

void Feature::registerNative(jni::JNIEnv& env) {
    // Lookup the class
    Feature::javaClass = *jni::Class<Feature>::Find(env).NewGlobalRef(env).release();
}

jni::Class<Feature> Feature::javaClass;

} // namespace geojson
} // namespace android
} // namespace mbgl