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

Commit

Permalink
[android] - add heatmap color property
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Feb 16, 2018
1 parent 63eb511 commit 366051e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ public void setHeatmapIntensityTransition(TransitionOptions options) {
nativeSetHeatmapIntensityTransition(options.getDuration(), options.getDelay());
}

/**
* Get the HeatmapColor property
*
* @return property wrapper value around String
*/
@SuppressWarnings("unchecked")
public PropertyValue<String> getHeatmapColor() {
return (PropertyValue<String>) new PropertyValue("heatmap-color", nativeGetHeatmapColor());
}

/**
* Defines the color of each pixel based on its density value in a heatmap. Should be an expression that uses `["heatmap-density"]` as input.
*
* @return int representation of a rgba string color
* @throws RuntimeException thrown if property isn't a value
*/
@ColorInt
public int getHeatmapColorAsInt() {
PropertyValue<String> value = getHeatmapColor();
if (value.isValue()) {
return rgbaToColor(value.getValue());
} else {
throw new RuntimeException("heatmap-color was set as a Function");
}
}

/**
* Get the HeatmapOpacity property
*
Expand Down Expand Up @@ -209,6 +235,8 @@ public void setHeatmapOpacityTransition(TransitionOptions options) {

private native void nativeSetHeatmapIntensityTransition(long duration, long delay);

private native Object nativeGetHeatmapColor();

private native Object nativeGetHeatmapOpacity();

private native TransitionOptions nativeGetHeatmapOpacityTransition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,36 @@ public static <Z extends Number> PropertyValue<CameraFunction<Z, Float>> heatmap
return new PaintPropertyValue<>("heatmap-intensity", function);
}

/**
* Defines the color of each pixel based on its density value in a heatmap. Should be an expression that uses `["heatmap-density"]` as input.
*
* @param value a int color value
* @return property wrapper around String color
*/
public static PropertyValue<String> heatmapColor(@ColorInt int value) {
return new PaintPropertyValue<>("heatmap-color", colorToRgbaString(value));
}

/**
* Defines the color of each pixel based on its density value in a heatmap. Should be an expression that uses `["heatmap-density"]` as input.
*
* @param value a String value
* @return property wrapper around String
*/
public static PropertyValue<String> heatmapColor(String value) {
return new PaintPropertyValue<>("heatmap-color", value);
}

/**
* Defines the color of each pixel based on its density value in a heatmap. Should be an expression that uses `["heatmap-density"]` as input.
*
* @param expression an expression statement
* @return property wrapper around an expression statement
*/
public static PropertyValue<Expression> heatmapColor(Expression expression) {
return new PaintPropertyValue<>("heatmap-color", expression);
}

/**
* The global opacity at which the heatmap layer will be drawn.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public class <%- camelize(type) %>LayerTest extends BaseActivityTest {
}
<% } -%>
<% for (const property of properties) { -%>
<% if (property.name != 'heatmap-color') { -%>
<% if (property.transition) { -%>
@Test
Expand Down Expand Up @@ -456,5 +457,6 @@ public class <%- camelize(type) %>LayerTest extends BaseActivityTest {
}
<% } -%>
<% } -%>
<% } -%>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.expressions.Expression.heatmapDensity;
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;
Expand All @@ -28,6 +29,7 @@
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.heatmapColor;
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;
Expand All @@ -51,7 +53,6 @@ public class HeatmapLayerActivity extends AppCompatActivity {
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 -> {
Expand All @@ -76,11 +77,20 @@ private void addHeatmapLayer() {
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(),
heatmapColor(
interpolate(
linear(), heatmapDensity(),
literal(0), rgba(33, 102, 172, 0),
literal(0.2), rgb(103, 169, 207),
literal(0.4), rgb(209, 229, 240),
literal(0.6), rgb(253, 219, 199),
literal(0.8), rgb(239, 138, 98),
literal(1), rgb(178, 24, 43)
)
),

// Increase the heatmap weight based on frequency and property magnitude
heatmapWeight(
Expand Down
3 changes: 0 additions & 3 deletions platform/android/scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ var layers = Object.keys(spec.layer.type.values).map((type) => {
}, []);

const paintProperties = Object.keys(spec[`paint_${type}`]).reduce((memo, name) => {
// disabled for now, see https://github.com/mapbox/mapbox-gl-native/issues/11172
if (name === 'heatmap-color') return memo;

spec[`paint_${type}`][name].name = name;
memo.push(spec[`paint_${type}`][name]);
return memo;
Expand Down
13 changes: 13 additions & 0 deletions platform/android/src/style/conversion/property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <mbgl/style/property_value.hpp>
#include <mbgl/style/data_driven_property_value.hpp>
#include <mbgl/style/heatmap_color_property_value.hpp>
#include "../../conversion/conversion.hpp"
#include "../../conversion/constant.hpp"
#include "types.hpp"
Expand Down Expand Up @@ -70,6 +71,18 @@ struct Converter<jni::jobject*, mbgl::style::DataDrivenPropertyValue<T>> {
}
};

/**
* Convert core heat map color property value to java
*/
template <>
struct Converter<jni::jobject*, mbgl::style::HeatmapColorPropertyValue> {

Result<jni::jobject*> operator()(jni::JNIEnv& env, const mbgl::style::HeatmapColorPropertyValue value) const {
PropertyValueEvaluator<mbgl::style::HeatmapColorPropertyValue> evaluator(env);
return *convert<jni::jobject*>(env, value.evaluate(evaluator));
}
};

} // namespace conversion
} // namespace android
} // namespace mbgl
7 changes: 7 additions & 0 deletions platform/android/src/style/layers/heatmap_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ namespace android {
layer.as<mbgl::style::HeatmapLayer>()->HeatmapLayer::setHeatmapIntensityTransition(options);
}

jni::Object<jni::ObjectTag> HeatmapLayer::getHeatmapColor(jni::JNIEnv& env) {
using namespace mbgl::android::conversion;
Result<jni::jobject*> converted = convert<jni::jobject*>(env, layer.as<mbgl::style::HeatmapLayer>()->HeatmapLayer::getHeatmapColor());
return jni::Object<jni::ObjectTag>(*converted);
}

jni::Object<jni::ObjectTag> HeatmapLayer::getHeatmapOpacity(jni::JNIEnv& env) {
using namespace mbgl::android::conversion;
Result<jni::jobject*> converted = convert<jni::jobject*>(env, layer.as<mbgl::style::HeatmapLayer>()->HeatmapLayer::getHeatmapOpacity());
Expand Down Expand Up @@ -125,6 +131,7 @@ namespace android {
METHOD(&HeatmapLayer::getHeatmapIntensityTransition, "nativeGetHeatmapIntensityTransition"),
METHOD(&HeatmapLayer::setHeatmapIntensityTransition, "nativeSetHeatmapIntensityTransition"),
METHOD(&HeatmapLayer::getHeatmapIntensity, "nativeGetHeatmapIntensity"),
METHOD(&HeatmapLayer::getHeatmapColor, "nativeGetHeatmapColor"),
METHOD(&HeatmapLayer::getHeatmapOpacityTransition, "nativeGetHeatmapOpacityTransition"),
METHOD(&HeatmapLayer::setHeatmapOpacityTransition, "nativeSetHeatmapOpacityTransition"),
METHOD(&HeatmapLayer::getHeatmapOpacity, "nativeGetHeatmapOpacity"));
Expand Down
2 changes: 2 additions & 0 deletions platform/android/src/style/layers/heatmap_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class HeatmapLayer : public Layer {
void setHeatmapIntensityTransition(jni::JNIEnv&, jlong duration, jlong delay);
jni::Object<TransitionOptions> getHeatmapIntensityTransition(jni::JNIEnv&);

jni::Object<jni::ObjectTag> getHeatmapColor(jni::JNIEnv&);

jni::Object<jni::ObjectTag> getHeatmapOpacity(jni::JNIEnv&);
void setHeatmapOpacityTransition(jni::JNIEnv&, jlong duration, jlong delay);
jni::Object<TransitionOptions> getHeatmapOpacityTransition(jni::JNIEnv&);
Expand Down

0 comments on commit 366051e

Please sign in to comment.