[android] Simplify Feature conversion
This commit is contained in:
@@ -55,8 +55,6 @@ platform/android/src/conversion/color.hpp
|
||||
platform/android/src/conversion/constant.cpp
|
||||
platform/android/src/conversion/constant.hpp
|
||||
platform/android/src/conversion/conversion.hpp
|
||||
platform/android/src/geojson/conversion/feature.cpp
|
||||
platform/android/src/geojson/conversion/feature.hpp
|
||||
platform/android/src/style/conversion/filter.cpp
|
||||
platform/android/src/style/conversion/filter.hpp
|
||||
platform/android/src/style/conversion/position.cpp
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
#include "feature.hpp"
|
||||
|
||||
#include "../../gson/json_object.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
using namespace gson;
|
||||
|
||||
/**
|
||||
* 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 "";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
jni::Object<android::geojson::Feature> convertFeature(jni::JNIEnv& env, const mbgl::Feature& value) {
|
||||
using namespace mbgl::android::geojson;
|
||||
|
||||
return Feature::fromGeometry(env,
|
||||
*jni::SeizeLocal(env, Geometry::New(env, value.geometry)),
|
||||
*jni::SeizeLocal(env, JsonObject::New(env, value.properties)),
|
||||
*jni::SeizeLocal(env, jni::Make<jni::String>(env, value.id ? value.id.value().match(FeatureIdVisitor()) : "")));
|
||||
}
|
||||
|
||||
Result<jni::Array<jni::Object<android::geojson::Feature>>> Converter<jni::Array<jni::Object<android::geojson::Feature>>, std::vector<mbgl::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());
|
||||
|
||||
for (size_t i = 0; i < value.size(); i = i + 1) {
|
||||
features.Set(env, i, *jni::SeizeLocal(env, convertFeature(env, value.at(i))));
|
||||
}
|
||||
|
||||
return {features};
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace android
|
||||
} // namespace mbgl
|
||||
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../conversion/conversion.hpp"
|
||||
#include "../feature.hpp"
|
||||
|
||||
#include <mbgl/util/feature.hpp>
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace android
|
||||
} // namespace mbgl
|
||||
@@ -1,50 +1,66 @@
|
||||
#include "feature.hpp"
|
||||
|
||||
#include "geometry.hpp"
|
||||
#include "../gson/json_object.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace geojson {
|
||||
|
||||
mbgl::Feature Feature::convert(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
|
||||
auto jGeometry = jni::SeizeLocal(env, geometry(env, jFeature));
|
||||
auto jProperties = jni::SeizeLocal(env, Feature::properties(env, jFeature));
|
||||
using namespace gson;
|
||||
|
||||
std::experimental::optional<mapbox::geometry::identifier> id;
|
||||
auto jId = jni::SeizeLocal(env, Feature::id(env, jFeature));
|
||||
if (jId) {
|
||||
id = { jni::Make<std::string>(env, *jId) };
|
||||
}
|
||||
mbgl::Feature Feature::convert(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
|
||||
static auto javaClass = jni::Class<Feature>::Singleton(env);
|
||||
static auto id = javaClass.GetMethod<jni::String ()>(env, "id");
|
||||
static auto geometry = javaClass.GetMethod<jni::Object<Geometry> ()>(env, "geometry");
|
||||
static auto properties = javaClass.GetMethod<jni::Object<gson::JsonObject> ()>(env, "properties");
|
||||
|
||||
auto jId = jni::SeizeLocal(env, jFeature.Call(env, id));
|
||||
|
||||
return mbgl::Feature {
|
||||
Geometry::convert(env, *jGeometry),
|
||||
gson::JsonObject::convert(env, *jProperties),
|
||||
id
|
||||
Geometry::convert(env, *jni::SeizeLocal(env, jFeature.Call(env, geometry))),
|
||||
JsonObject::convert(env, *jni::SeizeLocal(env, jFeature.Call(env, properties))),
|
||||
jId ? std::experimental::optional<mapbox::geometry::identifier>(jni::Make<std::string>(env, *jId))
|
||||
: std::experimental::nullopt
|
||||
};
|
||||
}
|
||||
|
||||
jni::Object<Geometry> Feature::geometry(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
|
||||
/**
|
||||
* 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 "";
|
||||
}
|
||||
};
|
||||
|
||||
jni::Object<Feature> convertFeature(jni::JNIEnv& env, const mbgl::Feature& value) {
|
||||
static auto javaClass = jni::Class<Feature>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::Object<Geometry> ()>(env, "geometry");
|
||||
return jFeature.Call(env, method);
|
||||
static auto method = javaClass.GetStaticMethod<jni::Object<Feature> (jni::Object<Geometry>, jni::Object<JsonObject>, jni::String)>(env, "fromGeometry");
|
||||
|
||||
return javaClass.Call(env, method,
|
||||
*jni::SeizeLocal(env, Geometry::New(env, value.geometry)),
|
||||
*jni::SeizeLocal(env, JsonObject::New(env, value.properties)),
|
||||
*jni::SeizeLocal(env, jni::Make<jni::String>(env, value.id ? value.id.value().match(FeatureIdVisitor()) : "")));
|
||||
}
|
||||
|
||||
jni::Object<gson::JsonObject> Feature::properties(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
|
||||
static auto javaClass = jni::Class<Feature>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::Object<gson::JsonObject> ()>(env, "properties");
|
||||
return jFeature.Call(env, method);
|
||||
}
|
||||
jni::Array<jni::Object<Feature>> Feature::convert(jni::JNIEnv& env, const std::vector<mbgl::Feature>& value) {
|
||||
auto features = jni::Array<jni::Object<Feature>>::New(env, value.size());
|
||||
|
||||
jni::String Feature::id(jni::JNIEnv& env, jni::Object<Feature> jFeature) {
|
||||
static auto javaClass = jni::Class<Feature>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::String ()>(env, "id");
|
||||
return jFeature.Call(env, method);
|
||||
}
|
||||
for (size_t i = 0; i < value.size(); i = i + 1) {
|
||||
features.Set(env, i, *jni::SeizeLocal(env, convertFeature(env, value.at(i))));
|
||||
}
|
||||
|
||||
jni::Object<Feature> Feature::fromGeometry(jni::JNIEnv& env, jni::Object<Geometry> geometry, jni::Object<gson::JsonObject> properties, jni::String id) {
|
||||
static auto javaClass = jni::Class<Feature>::Singleton(env);
|
||||
static auto method = javaClass.GetStaticMethod<jni::Object<Feature> (jni::Object<Geometry>, jni::Object<gson::JsonObject>, jni::String)>(env, "fromGeometry");
|
||||
return javaClass.Call(env, method, geometry, properties, id);
|
||||
return features;
|
||||
}
|
||||
|
||||
void Feature::registerNative(jni::JNIEnv& env) {
|
||||
|
||||
@@ -1,32 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/feature.hpp>
|
||||
#include <mbgl/util/geometry.hpp>
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include "geometry.hpp"
|
||||
#include "../gson/json_object.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace geojson {
|
||||
|
||||
class Feature : private mbgl::util::noncopyable {
|
||||
class Feature : public jni::ObjectTag {
|
||||
public:
|
||||
|
||||
static constexpr auto Name() { return "com/mapbox/geojson/Feature"; };
|
||||
|
||||
static jni::Object<Feature> fromGeometry(jni::JNIEnv&, jni::Object<Geometry>, jni::Object<gson::JsonObject>, jni::String);
|
||||
|
||||
static mbgl::Feature convert(jni::JNIEnv&, jni::Object<Feature>);
|
||||
|
||||
static jni::Object<Geometry> geometry(jni::JNIEnv&, jni::Object<Feature>);
|
||||
|
||||
static jni::String id(jni::JNIEnv&, jni::Object<Feature>);
|
||||
|
||||
static jni::Object<gson::JsonObject> properties(jni::JNIEnv&, jni::Object<Feature>);
|
||||
static jni::Array<jni::Object<Feature>> convert(jni::JNIEnv&, const std::vector<mbgl::Feature>&);
|
||||
|
||||
static void registerNative(jni::JNIEnv&);
|
||||
};
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
#include "conversion/conversion.hpp"
|
||||
#include "conversion/collection.hpp"
|
||||
#include "style/conversion/filter.hpp"
|
||||
#include "geojson/conversion/feature.hpp"
|
||||
#include "geojson/feature.hpp"
|
||||
|
||||
#include "jni.hpp"
|
||||
#include "attach_env.hpp"
|
||||
@@ -658,7 +658,7 @@ jni::Array<jni::Object<geojson::Feature>> NativeMapView::queryRenderedFeaturesFo
|
||||
}
|
||||
mapbox::geometry::point<double> point = {x, y};
|
||||
|
||||
return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(
|
||||
return Feature::convert(
|
||||
env,
|
||||
rendererFrontend->queryRenderedFeatures(point, { layers, toFilter(env, jni::SeizeLocal(env, std::move(jfilter))) }));
|
||||
}
|
||||
@@ -678,7 +678,7 @@ jni::Array<jni::Object<geojson::Feature>> NativeMapView::queryRenderedFeaturesFo
|
||||
mapbox::geometry::point<double>{ right, bottom }
|
||||
};
|
||||
|
||||
return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(
|
||||
return Feature::convert(
|
||||
env,
|
||||
rendererFrontend->queryRenderedFeatures(box, { layers, toFilter(env, jni::SeizeLocal(env, std::move(jfilter))) }));
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
// C++ -> Java conversion
|
||||
#include "../../conversion/conversion.hpp"
|
||||
#include "../../conversion/collection.hpp"
|
||||
#include "../../geojson/conversion/feature.hpp"
|
||||
#include "../../geojson/feature.hpp"
|
||||
#include <mbgl/style/conversion/custom_geometry_source_options.hpp>
|
||||
|
||||
#include <string>
|
||||
@@ -155,7 +155,7 @@ namespace android {
|
||||
features = rendererFrontend->querySourceFeatures(source.getID(),
|
||||
{ {}, toFilter(env, jni::SeizeLocal(env, std::move(jfilter))) });
|
||||
}
|
||||
return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(env, features);
|
||||
return Feature::convert(env, features);
|
||||
}
|
||||
|
||||
jni::Object<Source> CustomGeometrySource::createJavaPeer(jni::JNIEnv& env) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// C++ -> Java conversion
|
||||
#include "../../conversion/conversion.hpp"
|
||||
#include "../../conversion/collection.hpp"
|
||||
#include "../../geojson/conversion/feature.hpp"
|
||||
#include "../../geojson/feature.hpp"
|
||||
#include "../conversion/url_or_tileset.hpp"
|
||||
|
||||
#include <string>
|
||||
@@ -104,7 +104,7 @@ namespace android {
|
||||
features = rendererFrontend->querySourceFeatures(source.getID(),
|
||||
{ {}, toFilter(env, jni::SeizeLocal(env, std::move(jfilter))) });
|
||||
}
|
||||
return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(env, features);
|
||||
return Feature::convert(env, features);
|
||||
}
|
||||
|
||||
jni::Object<Source> GeoJSONSource::createJavaPeer(jni::JNIEnv& env) {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
// C++ -> Java conversion
|
||||
#include "../../conversion/conversion.hpp"
|
||||
#include "../../conversion/collection.hpp"
|
||||
#include "../../geojson/conversion/feature.hpp"
|
||||
#include "../../geojson/feature.hpp"
|
||||
#include "../conversion/url_or_tileset.hpp"
|
||||
|
||||
#include <mbgl/util/variant.hpp>
|
||||
@@ -54,7 +54,7 @@ namespace android {
|
||||
features = rendererFrontend->querySourceFeatures(source.getID(),
|
||||
{ toVector(env, jSourceLayerIds), toFilter(env, jni::SeizeLocal(env, std::move(jfilter))) });
|
||||
}
|
||||
return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(env, features);
|
||||
return Feature::convert(env, features);
|
||||
}
|
||||
|
||||
jni::Object<Source> VectorSource::createJavaPeer(jni::JNIEnv& env) {
|
||||
|
||||
Reference in New Issue
Block a user