summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-03-05 16:55:58 -0800
committerJesse Bounds <jesse@rebounds.net>2017-03-10 11:08:32 -0800
commita3121fc7a3228b7f1c66ab7388bee16cadfd70e0 (patch)
treedd9e3be9c54374b56f5a09ada0945b19783e8605
parenta7fd788b5d5d7734666758320235f14be8187179 (diff)
downloadqtlocation-mapboxgl-a3121fc7a3228b7f1c66ab7388bee16cadfd70e0.tar.gz
[android] query source features
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java22
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/VectorSource.java32
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml11
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QuerySourceFeaturesActivity.java116
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_query_source_features.xml16
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml2
-rw-r--r--platform/android/src/conversion/collection.hpp3
-rwxr-xr-xplatform/android/src/native_map_view.cpp17
-rw-r--r--platform/android/src/style/conversion/filter.hpp31
-rw-r--r--platform/android/src/style/sources/geojson_source.cpp21
-rw-r--r--platform/android/src/style/sources/geojson_source.hpp3
-rw-r--r--platform/android/src/style/sources/vector_source.cpp24
-rw-r--r--platform/android/src/style/sources/vector_source.hpp4
13 files changed, 282 insertions, 20 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java
index f9875c7242..9f957e7727 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java
@@ -1,17 +1,25 @@
package com.mapbox.mapboxsdk.style.sources;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.annotation.UiThread;
+
+import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
+import java.util.List;
/**
* A GeoJson source. Exposes a {@link FeatureCollection} from Json.
*
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">the style specification</a>
*/
+@UiThread
public class GeoJsonSource extends Source {
/**
@@ -159,6 +167,18 @@ public class GeoJsonSource extends Source {
nativeSetUrl(url);
}
+ /**
+ * Queries the source for features.
+ *
+ * @param filter an optional filter statement to filter the returned Features
+ * @return the features
+ */
+ @NonNull
+ public List<Feature> querySourceFeatures(@Nullable Filter.Statement filter) {
+ Feature[] features = querySourceFeatures(filter != null ? filter.toArray() : null);
+ return features != null ? Arrays.asList(features) : new ArrayList<Feature>();
+ }
+
protected void setRawJson(String geoJson) {
// Wrap the String in a map as an Object is expected by the
// style conversion template
@@ -173,6 +193,8 @@ public class GeoJsonSource extends Source {
private native void nativeSetGeoJson(Object geoJson);
+ private native Feature[] querySourceFeatures(Object[] filter);
+
@Override
protected native void finalize() throws Throwable;
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/VectorSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/VectorSource.java
index feaca839d3..68eb81b7f4 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/VectorSource.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/VectorSource.java
@@ -1,12 +1,24 @@
package com.mapbox.mapboxsdk.style.sources;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.annotation.Size;
+import android.support.annotation.UiThread;
+
+import com.mapbox.mapboxsdk.style.layers.Filter;
+import com.mapbox.services.commons.geojson.Feature;
+
import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
/**
* Vector source enables the use of vector tiles.
*
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-vector">the style specification</a>
*/
+@UiThread
public class VectorSource extends Source {
/**
@@ -48,8 +60,28 @@ public class VectorSource extends Source {
initialize(id, tileSet.toValueObject());
}
+ /**
+ * Queries the source for features.
+ *
+ * @param sourceLayerIds the source layer identifiers. At least one must be specified.
+ * @param filter an optional filter statement to filter the returned Features
+ * @return the features
+ */
+ @NonNull
+ public List<Feature> querySourceFeatures(@Size(min = 1) String[] sourceLayerIds,
+ @Nullable Filter.Statement filter) {
+ Feature[] features = querySourceFeatures(
+ sourceLayerIds,
+ filter != null ? filter.toArray() : null);
+ return features != null ? Arrays.asList(features) : new ArrayList<Feature>();
+ }
+
protected native void initialize(String layerId, Object payload);
@Override
protected native void finalize() throws Throwable;
+
+ private native Feature[] querySourceFeatures(String[] sourceLayerId,
+ Object[] filter);
+
}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml
index 0c76cb8151..e6d118692e 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml
@@ -549,6 +549,17 @@
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
+ android:name=".activity.feature.QuerySourceFeaturesActivity"
+ android:description="@string/description_query_source_features"
+ android:label="@string/activity_query_source_features">
+ <meta-data
+ android:name="@string/category"
+ android:value="@string/category_features"/>
+ <meta-data
+ android:name="android.support.PARENT_ACTIVITY"
+ android:value=".activity.FeatureOverviewActivity"/>
+ </activity>
+ <activity
android:name=".activity.annotation.AddRemoveMarkerActivity"
android:description="@string/description_add_remove_markers"
android:label="@string/activity_add_remove_markers">
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QuerySourceFeaturesActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QuerySourceFeaturesActivity.java
new file mode 100644
index 0000000000..861f2fef34
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QuerySourceFeaturesActivity.java
@@ -0,0 +1,116 @@
+package com.mapbox.mapboxsdk.testapp.activity.feature;
+
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.v7.app.AppCompatActivity;
+import android.widget.Toast;
+
+import com.google.gson.JsonObject;
+import com.mapbox.mapboxsdk.geometry.LatLng;
+import com.mapbox.mapboxsdk.maps.MapView;
+import com.mapbox.mapboxsdk.maps.MapboxMap;
+import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
+import com.mapbox.mapboxsdk.style.layers.CircleLayer;
+import com.mapbox.mapboxsdk.style.layers.Filter;
+import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
+import com.mapbox.mapboxsdk.testapp.R;
+import com.mapbox.services.commons.geojson.Feature;
+import com.mapbox.services.commons.geojson.FeatureCollection;
+import com.mapbox.services.commons.geojson.Point;
+
+import java.util.List;
+
+/**
+ * Test activity showcasing using the query source features API to query feature counts
+ */
+public class QuerySourceFeaturesActivity extends AppCompatActivity {
+
+ public MapView mapView;
+ private MapboxMap mapboxMap;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_query_source_features);
+
+ final float density = getResources().getDisplayMetrics().density;
+
+ // Initialize map as normal
+ mapView = (MapView) findViewById(R.id.mapView);
+ mapView.onCreate(savedInstanceState);
+ mapView.getMapAsync(new OnMapReadyCallback() {
+ @Override
+ public void onMapReady(final MapboxMap mapboxMap) {
+ QuerySourceFeaturesActivity.this.mapboxMap = mapboxMap;
+
+ JsonObject properties = new JsonObject();
+ properties.addProperty("key1", "value1");
+ final GeoJsonSource source = new GeoJsonSource("test-source",
+ FeatureCollection.fromFeatures(new Feature[] {
+ Feature.fromGeometry(Point.fromCoordinates(new double[] {0, 0}), properties)
+ }));
+ mapboxMap.addSource(source);
+
+ mapboxMap.addLayer(new CircleLayer("test-layer", source.getId()).withFilter(Filter.neq("key1", "value1")));
+
+ // Add a click listener
+ mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
+ @Override
+ public void onMapClick(@NonNull LatLng point) {
+ // Query
+ List<Feature> features = source.querySourceFeatures(Filter.eq("key1", "value1"));
+ Toast.makeText(QuerySourceFeaturesActivity.this, String.format("Found %s features",
+ features.size()), Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+ });
+
+ }
+
+ public MapboxMap getMapboxMap() {
+ return mapboxMap;
+ }
+
+ @Override
+ protected void onStart() {
+ super.onStart();
+ mapView.onStart();
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ mapView.onResume();
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ mapView.onPause();
+ }
+
+ @Override
+ protected void onStop() {
+ super.onStop();
+ mapView.onStop();
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ mapView.onSaveInstanceState(outState);
+ }
+
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+ mapView.onDestroy();
+ }
+
+ @Override
+ public void onLowMemory() {
+ super.onLowMemory();
+ mapView.onLowMemory();
+ }
+}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_query_source_features.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_query_source_features.xml
new file mode 100644
index 0000000000..1c439a5ecc
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_query_source_features.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <com.mapbox.mapboxsdk.maps.MapView
+ android:id="@+id/mapView"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ app:mapbox_cameraTargetLat="0"
+ app:mapbox_cameraTargetLng="0"
+ app:mapbox_cameraZoom="16"/>
+
+</RelativeLayout>
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml
index 52b65af2da..f15ee20be6 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml
@@ -47,6 +47,7 @@
<string name="activity_query_rendered_features_box_count">Count features in box</string>
<string name="activity_query_rendered_features_box_symbol_count">Count symbols in box</string>
<string name="activity_query_rendered_features_box_highlight">Highlight features in box</string>
+ <string name="activity_query_source_features">Query source features</string>
<string name="activity_symbol_layer">Symbols</string>
<string name="activity_add_sprite">Add Custom Sprite</string>
<string name="activity_navigation_drawer">Android SDK View integration</string>
@@ -99,6 +100,7 @@
<string name="description_query_rendered_features_box_count">Count all rendered features in box</string>
<string name="description_query_rendered_features_box_symbol_count">Count all rendered symbols in box</string>
<string name="description_query_rendered_features_box_highlight">Hightligh buildings in box</string>
+ <string name="description_query_source_features">Query source for features</string>
<string name="description_simple_map">Shows a simple map</string>
<string name="description_add_remove_markers">Based on zoom level</string>
<string name="description_style_file">Use a local file as the style</string>
diff --git a/platform/android/src/conversion/collection.hpp b/platform/android/src/conversion/collection.hpp
index da5eed64d2..549121c7ef 100644
--- a/platform/android/src/conversion/collection.hpp
+++ b/platform/android/src/conversion/collection.hpp
@@ -39,8 +39,9 @@ inline std::vector<std::string> toVector(JNIEnv& env, jni::jarray<jni::jobject>&
}
inline std::vector<std::string> toVector(JNIEnv& env, jni::Array<jni::String> array) {
- std::vector<std::string> vector;
std::size_t len = array.Length(env);
+ std::vector<std::string> vector;
+ vector.reserve(len);
for (std::size_t i = 0; i < len; i++) {
jni::String jstr = array.Get(env, i);
diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp
index d49f4f2a59..696849b11b 100755
--- a/platform/android/src/native_map_view.cpp
+++ b/platform/android/src/native_map_view.cpp
@@ -36,6 +36,7 @@
// C++ -> Java conversion
#include "conversion/conversion.hpp"
#include "conversion/collection.hpp"
+#include "style/conversion/filter.hpp"
#include "geometry/conversion/feature.hpp"
#include "jni.hpp"
@@ -721,22 +722,6 @@ jni::Array<jlong> NativeMapView::queryPointAnnotations(JNIEnv& env, jni::Object<
return result;
}
-static inline optional<mbgl::style::Filter> toFilter(jni::JNIEnv& env, jni::Array<jni::Object<>> jfilter) {
- using namespace mbgl::style;
- using namespace mbgl::style::conversion;
-
- mbgl::optional<Filter> filter;
- if (jfilter) {
- Value filterValue(env, jfilter);
- auto converted = convert<Filter>(filterValue);
- if (!converted) {
- mbgl::Log::Error(mbgl::Event::JNI, "Error setting filter: " + converted.error().message);
- }
- filter = std::move(*converted);
- }
- return filter;
-}
-
jni::Array<jni::Object<Feature>> NativeMapView::queryRenderedFeaturesForPoint(JNIEnv& env, jni::jfloat x, jni::jfloat y,
jni::Array<jni::String> layerIds,
jni::Array<jni::Object<>> jfilter) {
diff --git a/platform/android/src/style/conversion/filter.hpp b/platform/android/src/style/conversion/filter.hpp
new file mode 100644
index 0000000000..fc36d3a044
--- /dev/null
+++ b/platform/android/src/style/conversion/filter.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+#include "../android_conversion.hpp"
+#include <mbgl/style/conversion.hpp>
+#include <mbgl/style/conversion/filter.hpp>
+
+#include <jni/jni.hpp>
+
+#include <tuple>
+#include <map>
+
+namespace mbgl {
+namespace android {
+namespace conversion {
+
+inline optional<mbgl::style::Filter> toFilter(jni::JNIEnv& env, jni::Array<jni::Object<>> jfilter) {
+ mbgl::optional<mbgl::style::Filter> filter;
+ if (jfilter) {
+ Value filterValue(env, jfilter);
+ auto converted = mbgl::style::conversion::convert<mbgl::style::Filter>(filterValue);
+ if (!converted) {
+ mbgl::Log::Error(mbgl::Event::JNI, "Error converting filter: " + converted.error().message);
+ }
+ filter = std::move(*converted);
+ }
+ return filter;
+}
+
+} // namespace conversion
+} // namespace android
+} // namespace mbgl \ No newline at end of file
diff --git a/platform/android/src/style/sources/geojson_source.cpp b/platform/android/src/style/sources/geojson_source.cpp
index 37ce0644c1..7ab98e47c0 100644
--- a/platform/android/src/style/sources/geojson_source.cpp
+++ b/platform/android/src/style/sources/geojson_source.cpp
@@ -1,7 +1,15 @@
#include "geojson_source.hpp"
+// Java -> C++ conversion
#include "../android_conversion.hpp"
+#include "../conversion/filter.hpp"
#include "../conversion/geojson.hpp"
+
+// C++ -> Java conversion
+#include "../../conversion/conversion.hpp"
+#include "../../conversion/collection.hpp"
+#include "../../geometry/conversion/feature.hpp"
+#include "../conversion/url_or_tileset.hpp"
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson_options.hpp>
@@ -43,6 +51,16 @@ namespace android {
source.as<mbgl::style::GeoJSONSource>()->GeoJSONSource::setURL(jni::Make<std::string>(env, url));
}
+ jni::Array<jni::Object<Feature>> GeoJSONSource::querySourceFeatures(jni::JNIEnv& env,
+ jni::Array<jni::Object<>> jfilter) {
+ using namespace mbgl::android::conversion;
+ using namespace mapbox::geometry;
+
+ auto filter = toFilter(env, jfilter);
+ auto features = source.querySourceFeatures({ {}, filter });
+ return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(env, features);
+ }
+
jni::Class<GeoJSONSource> GeoJSONSource::javaClass;
jni::jobject* GeoJSONSource::createJavaPeer(jni::JNIEnv& env) {
@@ -63,7 +81,8 @@ namespace android {
"initialize",
"finalize",
METHOD(&GeoJSONSource::setGeoJSON, "nativeSetGeoJson"),
- METHOD(&GeoJSONSource::setURL, "nativeSetUrl")
+ METHOD(&GeoJSONSource::setURL, "nativeSetUrl"),
+ METHOD(&GeoJSONSource::querySourceFeatures, "querySourceFeatures")
);
}
diff --git a/platform/android/src/style/sources/geojson_source.hpp b/platform/android/src/style/sources/geojson_source.hpp
index 10c51e81b2..5b529fc52a 100644
--- a/platform/android/src/style/sources/geojson_source.hpp
+++ b/platform/android/src/style/sources/geojson_source.hpp
@@ -2,6 +2,7 @@
#include "source.hpp"
#include <mbgl/style/sources/geojson_source.hpp>
+#include "../../geometry/feature.hpp"
#include <jni/jni.hpp>
namespace mbgl {
@@ -26,6 +27,8 @@ public:
void setURL(jni::JNIEnv&, jni::String);
+ jni::Array<jni::Object<Feature>> querySourceFeatures(jni::JNIEnv&, jni::Array<jni::Object<>> jfilter);
+
jni::jobject* createJavaPeer(jni::JNIEnv&);
}; // class GeoJSONSource
diff --git a/platform/android/src/style/sources/vector_source.cpp b/platform/android/src/style/sources/vector_source.cpp
index e60d8d4641..67777f50d4 100644
--- a/platform/android/src/style/sources/vector_source.cpp
+++ b/platform/android/src/style/sources/vector_source.cpp
@@ -1,12 +1,19 @@
#include "vector_source.hpp"
+// Java -> C++ conversion
#include "../android_conversion.hpp"
-#include "../value.hpp"
+#include "../conversion/filter.hpp"
+
+// C++ -> Java conversion
+#include "../../conversion/conversion.hpp"
+#include "../../conversion/collection.hpp"
+#include "../../geometry/conversion/feature.hpp"
#include "../conversion/url_or_tileset.hpp"
#include <mbgl/util/variant.hpp>
#include <string>
+#include <vector>
namespace mbgl {
namespace android {
@@ -27,6 +34,18 @@ namespace android {
VectorSource::~VectorSource() = default;
+ jni::Array<jni::Object<Feature>> VectorSource::querySourceFeatures(jni::JNIEnv& env,
+ jni::Array<jni::String> jSourceLayerIds,
+ jni::Array<jni::Object<>> jfilter) {
+ using namespace mbgl::android::conversion;
+ using namespace mapbox::geometry;
+
+ mbgl::optional<std::vector<std::string>> sourceLayerIds = { toVector(env, jSourceLayerIds) };
+ auto filter = toFilter(env, jfilter);
+ auto features = source.querySourceFeatures({ sourceLayerIds, filter });
+ return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(env, features);
+ }
+
jni::Class<VectorSource> VectorSource::javaClass;
jni::jobject* VectorSource::createJavaPeer(jni::JNIEnv& env) {
@@ -45,7 +64,8 @@ namespace android {
env, VectorSource::javaClass, "nativePtr",
std::make_unique<VectorSource, JNIEnv&, jni::String, jni::Object<>>,
"initialize",
- "finalize"
+ "finalize",
+ METHOD(&VectorSource::querySourceFeatures, "querySourceFeatures")
);
}
diff --git a/platform/android/src/style/sources/vector_source.hpp b/platform/android/src/style/sources/vector_source.hpp
index 95d22ef7b7..f7e7645c5b 100644
--- a/platform/android/src/style/sources/vector_source.hpp
+++ b/platform/android/src/style/sources/vector_source.hpp
@@ -2,6 +2,7 @@
#include "source.hpp"
#include <mbgl/style/sources/vector_source.hpp>
+#include "../../geometry/feature.hpp"
#include <jni/jni.hpp>
namespace mbgl {
@@ -22,6 +23,9 @@ public:
~VectorSource();
+ jni::Array<jni::Object<Feature>> querySourceFeatures(jni::JNIEnv&, jni::Array<jni::String>,
+ jni::Array<jni::Object<>> jfilter);
+
jni::jobject* createJavaPeer(jni::JNIEnv&);
}; // class VectorSource