[core, ios, macos, android, node] Heatmap layer (#11046)

Co-Authored-By: Konstantin Käfer <mail@kkaefer.com>
Co-Authored-By: Anand Thakker <anandthakker@users.noreply.github.com>
Co-Authored-By: Minh Nguyễn <1ec5@users.noreply.github.com>
This commit is contained in:
Vladimir Agafonkin
2018-02-15 17:38:23 +02:00
committed by GitHub
parent 1fdec7a5c1
commit 82de856c94
90 changed files with 4570 additions and 61 deletions

View File

@@ -0,0 +1,626 @@
// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`.
package com.mapbox.mapboxsdk.testapp.style;
import android.graphics.Color;
import android.support.test.espresso.UiController;
import android.support.test.runner.AndroidJUnit4;
import timber.log.Timber;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.style.functions.CompositeFunction;
import com.mapbox.mapboxsdk.style.functions.CameraFunction;
import com.mapbox.mapboxsdk.style.functions.SourceFunction;
import com.mapbox.mapboxsdk.style.functions.stops.CategoricalStops;
import com.mapbox.mapboxsdk.style.functions.stops.ExponentialStops;
import com.mapbox.mapboxsdk.style.functions.stops.IdentityStops;
import com.mapbox.mapboxsdk.style.functions.stops.IntervalStops;
import com.mapbox.mapboxsdk.style.functions.stops.Stop;
import com.mapbox.mapboxsdk.style.functions.stops.Stops;
import com.mapbox.mapboxsdk.style.layers.HeatmapLayer;
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction;
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import static com.mapbox.mapboxsdk.style.functions.Function.*;
import static com.mapbox.mapboxsdk.style.functions.stops.Stop.stop;
import static com.mapbox.mapboxsdk.style.functions.stops.Stops.*;
import static com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke;
import static org.junit.Assert.*;
import static com.mapbox.mapboxsdk.style.layers.Property.*;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.*;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity;
/**
* Basic smoke tests for HeatmapLayer
*/
@RunWith(AndroidJUnit4.class)
public class HeatmapLayerTest extends BaseActivityTest {
private HeatmapLayer layer;
@Override
protected Class getActivityClass() {
return EspressoTestActivity.class;
}
private void setupLayer() {
Timber.i("Retrieving layer");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
if ((layer = mapboxMap.getLayerAs("my-layer")) == null) {
Timber.i("Adding layer");
layer = new HeatmapLayer("my-layer", "composite");
layer.setSourceLayer("composite");
mapboxMap.addLayer(layer);
// Layer reference is now stale, get new reference
layer = mapboxMap.getLayerAs("my-layer");
}
}
});
}
@Test
public void testSetVisibility() {
validateTestSetup();
setupLayer();
Timber.i("Visibility");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Get initial
assertEquals(layer.getVisibility().getValue(), VISIBLE);
// Set
layer.setProperties(visibility(NONE));
assertEquals(layer.getVisibility().getValue(), NONE);
}
});
}
@Test
public void testSourceLayer() {
validateTestSetup();
setupLayer();
Timber.i("SourceLayer");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Get initial
assertEquals(layer.getSourceLayer(), "composite");
// Set
final String sourceLayer = "test";
layer.setSourceLayer(sourceLayer);
assertEquals(layer.getSourceLayer(), sourceLayer);
}
});
}
@Test
public void testHeatmapRadiusTransition() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-radiusTransitionOptions");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set and Get
TransitionOptions options = new TransitionOptions(300, 100);
layer.setHeatmapRadiusTransition(options);
assertEquals(layer.getHeatmapRadiusTransition(), options);
}
});
}
@Test
public void testHeatmapRadiusAsConstant() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-radius");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set and Get
layer.setProperties(heatmapRadius(0.3f));
assertEquals((Float) layer.getHeatmapRadius().getValue(), (Float) 0.3f);
}
});
}
@Test
public void testHeatmapRadiusAsCameraFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-radius");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapRadius(
zoom(
exponential(
stop(2, heatmapRadius(0.3f))
).withBase(0.5f)
)
)
);
// Verify
assertNotNull(layer.getHeatmapRadius());
assertNotNull(layer.getHeatmapRadius().getFunction());
assertEquals(CameraFunction.class, layer.getHeatmapRadius().getFunction().getClass());
assertEquals(ExponentialStops.class, layer.getHeatmapRadius().getFunction().getStops().getClass());
assertEquals(0.5f, ((ExponentialStops) layer.getHeatmapRadius().getFunction().getStops()).getBase(), 0.001);
assertEquals(1, ((ExponentialStops) layer.getHeatmapRadius().getFunction().getStops()).size());
}
});
}
@Test
public void testHeatmapRadiusAsIdentitySourceFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-radius");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapRadius(property("FeaturePropertyA", Stops.<Float>identity()))
);
// Verify
assertNotNull(layer.getHeatmapRadius());
assertNotNull(layer.getHeatmapRadius().getFunction());
assertEquals(SourceFunction.class, layer.getHeatmapRadius().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getHeatmapRadius().getFunction()).getProperty());
assertEquals(IdentityStops.class, layer.getHeatmapRadius().getFunction().getStops().getClass());
}
});
}
@Test
public void testHeatmapRadiusAsExponentialSourceFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-radius");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapRadius(
property(
"FeaturePropertyA",
exponential(
stop(0.3f, heatmapRadius(0.3f))
).withBase(0.5f)
)
)
);
// Verify
assertNotNull(layer.getHeatmapRadius());
assertNotNull(layer.getHeatmapRadius().getFunction());
assertEquals(SourceFunction.class, layer.getHeatmapRadius().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getHeatmapRadius().getFunction()).getProperty());
assertEquals(ExponentialStops.class, layer.getHeatmapRadius().getFunction().getStops().getClass());
}
});
}
@Test
public void testHeatmapRadiusAsCategoricalSourceFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-radius");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapRadius(
property(
"FeaturePropertyA",
categorical(
stop(1.0f, heatmapRadius(0.3f))
)
).withDefaultValue(heatmapRadius(0.3f))
)
);
// Verify
assertNotNull(layer.getHeatmapRadius());
assertNotNull(layer.getHeatmapRadius().getFunction());
assertEquals(SourceFunction.class, layer.getHeatmapRadius().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getHeatmapRadius().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getHeatmapRadius().getFunction().getStops().getClass());
assertNotNull(((SourceFunction) layer.getHeatmapRadius().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getHeatmapRadius().getFunction()).getDefaultValue().getValue());
assertEquals(0.3f, ((SourceFunction) layer.getHeatmapRadius().getFunction()).getDefaultValue().getValue());
}
});
}
@Test
public void testHeatmapRadiusAsCompositeFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-radius");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapRadius(
composite(
"FeaturePropertyA",
exponential(
stop(0, 0.3f, heatmapRadius(0.9f))
).withBase(0.5f)
).withDefaultValue(heatmapRadius(0.3f))
)
);
// Verify
assertNotNull(layer.getHeatmapRadius());
assertNotNull(layer.getHeatmapRadius().getFunction());
assertEquals(CompositeFunction.class, layer.getHeatmapRadius().getFunction().getClass());
assertEquals("FeaturePropertyA", ((CompositeFunction) layer.getHeatmapRadius().getFunction()).getProperty());
assertEquals(ExponentialStops.class, layer.getHeatmapRadius().getFunction().getStops().getClass());
assertEquals(1, ((ExponentialStops) layer.getHeatmapRadius().getFunction().getStops()).size());
ExponentialStops<Stop.CompositeValue<Float, Float>, Float> stops =
(ExponentialStops<Stop.CompositeValue<Float, Float>, Float>) layer.getHeatmapRadius().getFunction().getStops();
Stop<Stop.CompositeValue<Float, Float>, Float> stop = stops.iterator().next();
assertEquals(0f, stop.in.zoom, 0.001);
assertEquals(0.3f, stop.in.value, 0.001f);
assertEquals(0.9f, stop.out, 0.001f);
}
});
}
@Test
public void testHeatmapWeightAsConstant() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-weight");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set and Get
layer.setProperties(heatmapWeight(0.3f));
assertEquals((Float) layer.getHeatmapWeight().getValue(), (Float) 0.3f);
}
});
}
@Test
public void testHeatmapWeightAsCameraFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-weight");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapWeight(
zoom(
exponential(
stop(2, heatmapWeight(0.3f))
).withBase(0.5f)
)
)
);
// Verify
assertNotNull(layer.getHeatmapWeight());
assertNotNull(layer.getHeatmapWeight().getFunction());
assertEquals(CameraFunction.class, layer.getHeatmapWeight().getFunction().getClass());
assertEquals(ExponentialStops.class, layer.getHeatmapWeight().getFunction().getStops().getClass());
assertEquals(0.5f, ((ExponentialStops) layer.getHeatmapWeight().getFunction().getStops()).getBase(), 0.001);
assertEquals(1, ((ExponentialStops) layer.getHeatmapWeight().getFunction().getStops()).size());
}
});
}
@Test
public void testHeatmapWeightAsIdentitySourceFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-weight");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapWeight(property("FeaturePropertyA", Stops.<Float>identity()))
);
// Verify
assertNotNull(layer.getHeatmapWeight());
assertNotNull(layer.getHeatmapWeight().getFunction());
assertEquals(SourceFunction.class, layer.getHeatmapWeight().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getHeatmapWeight().getFunction()).getProperty());
assertEquals(IdentityStops.class, layer.getHeatmapWeight().getFunction().getStops().getClass());
}
});
}
@Test
public void testHeatmapWeightAsExponentialSourceFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-weight");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapWeight(
property(
"FeaturePropertyA",
exponential(
stop(0.3f, heatmapWeight(0.3f))
).withBase(0.5f)
)
)
);
// Verify
assertNotNull(layer.getHeatmapWeight());
assertNotNull(layer.getHeatmapWeight().getFunction());
assertEquals(SourceFunction.class, layer.getHeatmapWeight().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getHeatmapWeight().getFunction()).getProperty());
assertEquals(ExponentialStops.class, layer.getHeatmapWeight().getFunction().getStops().getClass());
}
});
}
@Test
public void testHeatmapWeightAsCategoricalSourceFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-weight");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapWeight(
property(
"FeaturePropertyA",
categorical(
stop(1.0f, heatmapWeight(0.3f))
)
).withDefaultValue(heatmapWeight(0.3f))
)
);
// Verify
assertNotNull(layer.getHeatmapWeight());
assertNotNull(layer.getHeatmapWeight().getFunction());
assertEquals(SourceFunction.class, layer.getHeatmapWeight().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getHeatmapWeight().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getHeatmapWeight().getFunction().getStops().getClass());
assertNotNull(((SourceFunction) layer.getHeatmapWeight().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getHeatmapWeight().getFunction()).getDefaultValue().getValue());
assertEquals(0.3f, ((SourceFunction) layer.getHeatmapWeight().getFunction()).getDefaultValue().getValue());
}
});
}
@Test
public void testHeatmapWeightAsCompositeFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-weight");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapWeight(
composite(
"FeaturePropertyA",
exponential(
stop(0, 0.3f, heatmapWeight(0.9f))
).withBase(0.5f)
).withDefaultValue(heatmapWeight(0.3f))
)
);
// Verify
assertNotNull(layer.getHeatmapWeight());
assertNotNull(layer.getHeatmapWeight().getFunction());
assertEquals(CompositeFunction.class, layer.getHeatmapWeight().getFunction().getClass());
assertEquals("FeaturePropertyA", ((CompositeFunction) layer.getHeatmapWeight().getFunction()).getProperty());
assertEquals(ExponentialStops.class, layer.getHeatmapWeight().getFunction().getStops().getClass());
assertEquals(1, ((ExponentialStops) layer.getHeatmapWeight().getFunction().getStops()).size());
ExponentialStops<Stop.CompositeValue<Float, Float>, Float> stops =
(ExponentialStops<Stop.CompositeValue<Float, Float>, Float>) layer.getHeatmapWeight().getFunction().getStops();
Stop<Stop.CompositeValue<Float, Float>, Float> stop = stops.iterator().next();
assertEquals(0f, stop.in.zoom, 0.001);
assertEquals(0.3f, stop.in.value, 0.001f);
assertEquals(0.9f, stop.out, 0.001f);
}
});
}
@Test
public void testHeatmapIntensityTransition() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-intensityTransitionOptions");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set and Get
TransitionOptions options = new TransitionOptions(300, 100);
layer.setHeatmapIntensityTransition(options);
assertEquals(layer.getHeatmapIntensityTransition(), options);
}
});
}
@Test
public void testHeatmapIntensityAsConstant() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-intensity");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set and Get
layer.setProperties(heatmapIntensity(0.3f));
assertEquals((Float) layer.getHeatmapIntensity().getValue(), (Float) 0.3f);
}
});
}
@Test
public void testHeatmapIntensityAsCameraFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-intensity");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapIntensity(
zoom(
exponential(
stop(2, heatmapIntensity(0.3f))
).withBase(0.5f)
)
)
);
// Verify
assertNotNull(layer.getHeatmapIntensity());
assertNotNull(layer.getHeatmapIntensity().getFunction());
assertEquals(CameraFunction.class, layer.getHeatmapIntensity().getFunction().getClass());
assertEquals(ExponentialStops.class, layer.getHeatmapIntensity().getFunction().getStops().getClass());
assertEquals(0.5f, ((ExponentialStops) layer.getHeatmapIntensity().getFunction().getStops()).getBase(), 0.001);
assertEquals(1, ((ExponentialStops) layer.getHeatmapIntensity().getFunction().getStops()).size());
}
});
}
@Test
public void testHeatmapOpacityTransition() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-opacityTransitionOptions");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set and Get
TransitionOptions options = new TransitionOptions(300, 100);
layer.setHeatmapOpacityTransition(options);
assertEquals(layer.getHeatmapOpacityTransition(), options);
}
});
}
@Test
public void testHeatmapOpacityAsConstant() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-opacity");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set and Get
layer.setProperties(heatmapOpacity(0.3f));
assertEquals((Float) layer.getHeatmapOpacity().getValue(), (Float) 0.3f);
}
});
}
@Test
public void testHeatmapOpacityAsCameraFunction() {
validateTestSetup();
setupLayer();
Timber.i("heatmap-opacity");
invoke(mapboxMap, new MapboxMapAction.OnInvokeActionListener() {
@Override
public void onInvokeAction(UiController uiController, MapboxMap mapboxMap) {
assertNotNull(layer);
// Set
layer.setProperties(
heatmapOpacity(
zoom(
exponential(
stop(2, heatmapOpacity(0.3f))
).withBase(0.5f)
)
)
);
// Verify
assertNotNull(layer.getHeatmapOpacity());
assertNotNull(layer.getHeatmapOpacity().getFunction());
assertEquals(CameraFunction.class, layer.getHeatmapOpacity().getFunction().getClass());
assertEquals(ExponentialStops.class, layer.getHeatmapOpacity().getFunction().getStops().getClass());
assertEquals(0.5f, ((ExponentialStops) layer.getHeatmapOpacity().getFunction().getStops()).getBase(), 0.001);
assertEquals(1, ((ExponentialStops) layer.getHeatmapOpacity().getFunction().getStops()).size());
}
});
}
}

View File

@@ -789,6 +789,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.style.HeatmapLayerActivity"
android:description="@string/description_heatmaplayer"
android:label="@string/activity_heatmaplayer">
<meta-data
android:name="@string/category"
android:value="@string/category_style"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<!-- For Instrumentation tests -->
<activity

View File

@@ -0,0 +1,216 @@
package com.mapbox.mapboxsdk.testapp.activity.style;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.layers.HeatmapLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;
import java.net.MalformedURLException;
import java.net.URL;
import timber.log.Timber;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import static com.mapbox.mapboxsdk.style.expressions.Expression.linear;
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb;
import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba;
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleStrokeColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleStrokeWidth;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapIntensity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapRadius;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapWeight;
/**
* Test activity showcasing the heatmap layer api.
*/
public class HeatmapLayerActivity extends AppCompatActivity {
private static final String EARTHQUAKE_SOURCE_URL = "https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson";
private static final String EARTHQUAKE_SOURCE_ID = "earthquakes";
private static final String HEATMAP_LAYER_ID = "earthquakes-heat";
private static final String HEATMAP_LAYER_SOURCE = "earthquakes";
private static final String CIRCLE_LAYER_ID = "earthquakes-circle";
private MapView mapView;
private MapboxMap mapboxMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_heatmaplayer);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(map -> {
mapboxMap = map;
addEarthquakeSource();
addHeatmapLayer();
addCircleLayer();
});
}
private void addEarthquakeSource() {
try {
mapboxMap.addSource(new GeoJsonSource(EARTHQUAKE_SOURCE_ID, new URL(EARTHQUAKE_SOURCE_URL)));
} catch (MalformedURLException malformedUrlException) {
Timber.e(malformedUrlException, "That's not an url... ");
}
}
private void addHeatmapLayer() {
HeatmapLayer layer = new HeatmapLayer(HEATMAP_LAYER_ID, EARTHQUAKE_SOURCE_ID);
layer.setMaxZoom(9);
layer.setSourceLayer(HEATMAP_LAYER_SOURCE);
layer.setProperties(
// TODO add heatmap color https://github.com/mapbox/mapbox-gl-native/issues/11172
// Color ramp for heatmap. Domain is 0 (low) to 1 (high).
// Begin color ramp at 0-stop with a 0-transparancy color
// to create a blur-like effect.
//heatmapColor(),
// Increase the heatmap weight based on frequency and property magnitude
heatmapWeight(
interpolate(
linear(), get("mag"),
stop(0, 0),
stop(6, 1)
)
),
// Increase the heatmap color weight weight by zoom level
// heatmap-intensity is a multiplier on top of heatmap-weight
heatmapIntensity(
interpolate(
linear(), zoom(),
stop(0, 1),
stop(9, 3)
)
),
// Adjust the heatmap radius by zoom level
heatmapRadius(
interpolate(
linear(), zoom(),
stop(0, 2),
stop(9, 20)
)
),
// Transition from heatmap to circle layer by zoom level
heatmapOpacity(
interpolate(
linear(), zoom(),
stop(7, 1),
stop(9, 0)
)
)
);
mapboxMap.addLayerAbove(layer, "waterway-label");
}
private void addCircleLayer() {
CircleLayer circleLayer = new CircleLayer(CIRCLE_LAYER_ID, EARTHQUAKE_SOURCE_ID);
circleLayer.setProperties(
// Size circle radius by earthquake magnitude and zoom level
circleRadius(
interpolate(
linear(), zoom(),
literal(7), interpolate(
linear(), get("mag"),
stop(1, 1),
stop(6, 4)
),
literal(16), interpolate(
linear(), get("mag"),
stop(1, 5),
stop(6, 50)
)
)
),
// Color circle by earthquake magnitude
circleColor(
interpolate(
linear(), get("mag"),
literal(1), rgba(33, 102, 172, 0),
literal(2), rgb(103, 169, 207),
literal(3), rgb(209, 229, 240),
literal(4), rgb(253, 219, 199),
literal(5), rgb(239, 138, 98),
literal(6), rgb(178, 24, 43)
)
),
// Transition from heatmap to circle layer by zoom level
circleOpacity(
interpolate(
linear(), zoom(),
stop(7, 0),
stop(8, 1)
)
),
circleStrokeColor("white"),
circleStrokeWidth(1.0f)
);
mapboxMap.addLayerBelow(circleLayer, HEATMAP_LAYER_ID);
}
@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
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}

View File

@@ -0,0 +1,14 @@
<?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"
android:orientation="vertical">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_styleUrl="@string/mapbox_style_dark"/>
</RelativeLayout>

View File

@@ -70,4 +70,5 @@
<string name="description_grid_source">Example Custom Geometry Source</string>
<string name="description_local_glyph">Suzhou using Droid Sans for Chinese glyphs</string>
<string name="description_hillshade">Example raster-dem source and hillshade layer</string>
<string name="description_heatmaplayer">Use HeatmapLayer to visualise earthquakes</string>
</resources>

View File

@@ -70,4 +70,5 @@
<string name="activity_grid_source">Grid Source</string>
<string name="activity_local_glyph">Local CJK glyph generation</string>
<string name="activity_hillshade">Hillshade</string>
<string name="activity_heatmaplayer">Heatmap layer</string>
</resources>