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

#include "multi_line_string.hpp"

#include "util.hpp"

namespace mbgl {
namespace android {
namespace geojson {

jni::Object<Polygon> Polygon::New(jni::JNIEnv& env, const mbgl::Polygon<double>& polygon) {
    auto jList = asPointsListsList(env, polygon);

    static auto method = javaClass.GetStaticMethod<jni::Object<Polygon> (jni::Object<java::util::List>)>(env, "fromLngLats");
    auto jPolygon = javaClass.Call(env, method, jList);

    jni::DeleteLocalRef(env, jList);
    return jPolygon;
}

mapbox::geojson::polygon Polygon::convert(jni::JNIEnv &env, jni::Object<Polygon> jPolygon) {
    mapbox::geojson::polygon polygon;

    if (jPolygon) {
        auto jPositionListsList = Polygon::coordinates(env, jPolygon);
        polygon = Polygon::convert(env, jPositionListsList);
        jni::DeleteLocalRef(env, jPositionListsList);
    }

    return polygon;
}

mapbox::geojson::polygon Polygon::convert(jni::JNIEnv &env, jni::Object<java::util::List/*<java::util::List<Point>>*/> jPointListsList) {
    mapbox::geojson::polygon polygon;

    if (jPointListsList) {
        auto multiLine = MultiLineString::convert(env, jPointListsList);
        polygon.reserve(multiLine.size());
        for (auto&& line : multiLine) {
            polygon.emplace_back(convertExplicit<mapbox::geojson::linear_ring>(std::move(line)));
        }
    }

    return polygon;
}


jni::Object<java::util::List> Polygon::coordinates(jni::JNIEnv &env, jni::Object<Polygon> jPolygon) {
    static auto method = Polygon::javaClass.GetMethod<jni::Object<java::util::List> ()>(env, "coordinates");
    return jPolygon.Call(env, method);
}

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

jni::Class<Polygon> Polygon::javaClass;

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