From 9c74aa618f7c0feb8d3bc9193207f88d2cb7aa68 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Wed, 4 Apr 2018 16:12:09 +0300 Subject: [android] test activity for geojson bindings --- .../src/main/AndroidManifest.xml | 12 + .../QueryRenderedFeaturesLimitsActivity.java | 271 +++++++++++++++++++++ .../src/main/res/layout/activity_query_limits.xml | 16 ++ .../src/main/res/values/descriptions.xml | 2 + 4 files changed, 301 insertions(+) create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesLimitsActivity.java create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_query_limits.xml diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 442635f909..70a6264d22 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -598,6 +598,18 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".activity.FeatureOverviewActivity" /> + + + + { + QueryRenderedFeaturesLimitsActivity.this.mapboxMap = mapboxMap; + int multiplier = 2; + mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(9 - multiplier)); + + // Start a background test task + new TestTask(mapboxMap, multiplier).execute(); + + // Query + mapView.addOnMapChangedListener(change -> { + if (change == DID_FINISH_RENDERING_MAP_FULLY_RENDERED) { + GeoJsonSource source = mapboxMap.getSourceAs(GEOMETRY_SOURCE); + if (source != null) { + List result = source.querySourceFeatures(null); + dumpQueryResult(result); + } + } + }); + + mapboxMap.addOnMapClickListener(point -> { + GeoJsonSource source = mapboxMap.getSourceAs(GEOMETRY_SOURCE); + if (source != null) { + int top = mapView.getTop(); + int left = mapView.getLeft(); + RectF box = new RectF(left, top, left + mapView.getWidth(), top + mapView.getHeight()); + List result = mapboxMap.queryRenderedFeatures(box); + dumpQueryResult(result); + } + }); + }); + } + + private void dumpQueryResult(List result) { + Timber.i("Query resulted in %s features", result.size()); + Toast.makeText(this, String.format("Query resulted in %s features", result.size()), Toast.LENGTH_SHORT).show(); + int points = 0; + int properties = 0; + for (Feature f : result) { + if (f.geometry() instanceof Point) { + points++; + } else if (f.geometry() instanceof LineString) { + points += ((LineString)f.geometry()).coordinates().size(); + } else if (f.geometry() instanceof Polygon) { + points += ((Polygon)f.geometry()).coordinates().get(0).size(); + } + properties += f.properties().size(); + } + Timber.i("Converted points: %s, properties: %s", points, properties); + } + + private static class TestTask extends AsyncTask { + private MapboxMap map; + private int multiplier; + + TestTask(MapboxMap map, int multiplier) { + this.map = map; + this.multiplier = multiplier; + } + + @Override + protected void onPreExecute() { + } + + @Override + protected FeatureCollection doInBackground(Void... voids) { + List features = new ArrayList<>(); + + // Create a bunch of points + JsonObject properties = properties(100); + properties.addProperty("type", "point"); + int maxX = 10 * multiplier; + for (int x = 1; x < maxX; x++) { + int maxY = 10 * multiplier; + for (int y = 1; y < maxY; y++) { + features.add( + Feature.fromGeometry( + Point.fromLngLat(((x - maxX / 2) / (maxX * 1.0)), ((y - maxY / 2) / (maxY * 1.0))), + properties, + String.format("point-%s-%s", x, y) + ) + ); + } + } + + // Create a bunch of lineStrings + properties = properties(100); + properties.addProperty("type", "line"); + maxX = 10 * multiplier; + for (int x = 1; x < maxX; x++) { + int maxY = 10 * multiplier; + List points = new ArrayList<>(); + for (int y = 1; y < maxY; y++) { + points.add(Point.fromLngLat(((x - maxX / 2) / (maxX * 1.0)), ((y - maxY / 2) / (maxY * 1.0)))); + } + features.add(Feature.fromGeometry( + LineString.fromLngLats(points), + properties)); + } + + // Create a bunch of fills + properties = properties(100); + properties.addProperty("type", "fill"); + maxX = 10 * multiplier; + for (int x = 1; x < maxX; x++) { + int maxY = 10 * multiplier; + for (int y = 1; y < maxY; y++) { + List points = new ArrayList<>(); + double center_x = ((x - maxX / 2) / (maxX * 1.0)); + double center_y = ((y - maxY / 2) / (maxY * 1.0)); + double offset = 0.02; + points.add(Point.fromLngLat(center_x - offset, center_y - offset)); + points.add(Point.fromLngLat(center_x + offset, center_y - offset)); + points.add(Point.fromLngLat(center_x + offset, center_y + offset)); + points.add(Point.fromLngLat(center_x - offset, center_y + offset)); + points.add(Point.fromLngLat(center_x - offset, center_y - offset)); + + features.add(Feature.fromGeometry( + Polygon.fromLngLats(Collections.singletonList(points)), + properties)); + } + } + + // Add one long line string + properties = properties(100000); + properties.addProperty("type", "line"); + maxX = 200 * multiplier; + List points = new ArrayList<>(); + for (int x = 1; x < maxX; x++) { + int maxY = 200 * multiplier; + for (int y = 1; y < maxY; y++) { + points.add(Point.fromLngLat(((x - maxX / 2) / (maxX * 1.0)), ((y - maxY / 2) / (maxY * 1.0)))); + } + } + features.add(Feature.fromGeometry( + LineString.fromLngLats(points), + properties)); + + return FeatureCollection.fromFeatures(features); + } + + private JsonObject properties(int num) { + JsonObject properties = new JsonObject(); + + for (int i = 0; i < num / 2; i++) { + properties.addProperty("double-prop-" + i, 1.828282828d); + } + for (int i = 0; i < num / 2; i++) { + properties.addProperty("string-prop-" + i, "sdfsdf sdfsd fff fasdasd" + i); + } + + return properties; + } + + @Override + protected void onPostExecute(FeatureCollection features) { + // Add stuff + GeoJsonSource source = new GeoJsonSource(GEOMETRY_SOURCE, features); + map.addSource(source); + map.addLayer(new CircleLayer("circles", source.getId()) + .withFilter(eq("type", "point")) + .withProperties(circleColor(Color.RED))); + map.addLayer(new LineLayer("lines", source.getId()) + .withFilter(eq("type", "line")) + .withProperties(lineColor(Color.GREEN))); + map.addLayerBelow(new FillLayer("fill", source.getId()) + .withFilter(eq("type", "fill")) + .withProperties(fillColor(Color.BLUE), fillOpacity(.5f)), + "circles"); + } + } + + 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_limits.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_query_limits.xml new file mode 100644 index 0000000000..1c439a5ecc --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_query_limits.xml @@ -0,0 +1,16 @@ + + + + + + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml index 5238176ce8..4ed6278e1e 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml @@ -37,8 +37,10 @@ Shows how to print a map Query rendered feature properties on click Count all rendered features in box + Test limits on geojson conversions Count all rendered symbols in box Highlight buildings in box + Test geojson conversion limits Query source for features Shows a simple map Logs map change events to Logcat -- cgit v1.2.1