Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] query source features
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Mar 9, 2017
1 parent 103fbd0 commit 42b76cd
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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 {

/**
Expand Down Expand Up @@ -159,6 +167,18 @@ public void setUrl(String url) {
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
Expand All @@ -173,6 +193,8 @@ protected void setRawJson(String geoJson) {

private native void nativeSetGeoJson(Object geoJson);

private native Feature[] querySourceFeatures(Object[] filter);

@Override
protected native void finalize() throws Throwable;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

/**
Expand Down Expand Up @@ -48,8 +60,28 @@ public VectorSource(String id, TileSet tileSet) {
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion platform/android/src/conversion/collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 1 addition & 16 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions platform/android/src/style/conversion/filter.hpp
Original file line number Diff line number Diff line change
@@ -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
21 changes: 20 additions & 1 deletion platform/android/src/style/sources/geojson_source.cpp
Original file line number Diff line number Diff line change
@@ -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>

Expand Down Expand Up @@ -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) {
Expand All @@ -63,7 +81,8 @@ namespace android {
"initialize",
"finalize",
METHOD(&GeoJSONSource::setGeoJSON, "nativeSetGeoJson"),
METHOD(&GeoJSONSource::setURL, "nativeSetUrl")
METHOD(&GeoJSONSource::setURL, "nativeSetUrl"),
METHOD(&GeoJSONSource::querySourceFeatures, "querySourceFeatures")
);
}

Expand Down
Loading

0 comments on commit 42b76cd

Please sign in to comment.