[android] query source features
This commit is contained in:
committed by
Ivo van Dongen
parent
12568f21a3
commit
b2b5f9e571
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -551,6 +551,17 @@
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
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"
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
31
platform/android/src/style/conversion/filter.hpp
Normal file
31
platform/android/src/style/conversion/filter.hpp
Normal file
@@ -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
|
||||
@@ -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")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user