Skip to content

Commit

Permalink
Add support for heatmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
TDesjardins committed May 23, 2021
1 parent 15a2927 commit 944ca92
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 1 deletion.
69 changes: 69 additions & 0 deletions gwt-ol3-client/src/main/java/ol/layer/Heatmap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright 2014, 2021 gwt-ol
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package ol.layer;

import jsinterop.annotations.JsType;

/**
* Layer for rendering vector data as a heatmap. Note that any property set in the
* options is set as a {@link ol.Object} property on the layer object;
* for example, setting title: 'My Title' in the options means that title is
* observable, and has get/set accessors.
*
* @author Tino Desjardins
*
*/
@JsType(isNative = true)
public class Heatmap extends Vector {

public Heatmap() {}

public Heatmap(HeatmapOptions vectorLayerOptions) {}

/**
* @return blur size in pixels.
*/
public native double getBlur();

/**
* @param blur blur size in pixels.
*/
public native void setBlur(double blur);

/**
* @return gradient colors.
*/
public native String[] getGradient();

/**
* Default gradient: {"#00f", "#0ff", "#0f0", "#ff0", "#f00"}
*
* @param gradient gradient colors.
*/
public native void setGradient(String[] gradient);

/**
* @return radius size in pixels.
*/
public native double getRadius();


/**
* @param radius radius size in pixels.
*/
public native void setRadius(double radius);

}
62 changes: 62 additions & 0 deletions gwt-ol3-client/src/main/java/ol/layer/HeatmapOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright 2014, 2021 gwt-ol
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package ol.layer;

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsProperty;
import jsinterop.annotations.JsType;

/**
* Heatmap options.
*
*/
@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object")
public class HeatmapOptions extends VectorLayerOptions {

/**
* @param gradient The color gradient of the heatmap, specified as
* an array of CSS color strings.
*/
@JsProperty
public native void setGradient(String[] gradient);

/**
* @param radius radius size in pixels.
*/
@JsProperty
public native void setRadius(double radius);

/**
* @param blur blur size in pixels.
*/
@JsProperty
public native void setBlur(double blur);

/**
* @param weightAttribute The feature attribute to use for the weight
*/
@JsProperty
public native void setWeight(String weightAttribute);

/**
* @param heatmapWeightFunction function that calculates a weight
* from a feature. Weight values should range from 0 to 1 (and values
* outside will be clamped to that range).
*/
@JsProperty
public native void setWeight(HeatmapWeightFunction heatmapWeightFunction);

}
35 changes: 35 additions & 0 deletions gwt-ol3-client/src/main/java/ol/layer/HeatmapWeightFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright 2014, 2021 gwt-ol
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package ol.layer;

import jsinterop.annotations.JsFunction;
import ol.Feature;

/**
* A function that returns a weight from a feature. Weight values should range
* from 0 to 1 (and values outside will be clamped to that range)
*/
@FunctionalInterface
@JsFunction
public interface HeatmapWeightFunction {

/**
* @param feature feature for calculating the weight
* @return weight
*/
double getWeight(Feature feature);

}
1 change: 1 addition & 0 deletions gwt-ol3-client/src/test/java/ol/GwtOLTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public static TestSuite suite() {
suite.addTestSuite(TranslateTest.class);

// layers
suite.addTestSuite(ol.layer.HeatmapTest.class);
suite.addTestSuite(ol.layer.ImageTest.class);
suite.addTestSuite(LayerGroupTest.class);
suite.addTestSuite(ol.layer.TileTest.class);
Expand Down
66 changes: 66 additions & 0 deletions gwt-ol3-client/src/test/java/ol/layer/HeatmapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright 2014, 2021 gwt-ol
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package ol.layer;

import ol.Feature;
import ol.GwtOLBaseTestCase;
import ol.format.Kml;
import ol.source.VectorOptions;

/**
* A test case for {@link ol.layer.Heatmap}.
*
* @author Tino Desjardins
*
*/
public class HeatmapTest extends GwtOLBaseTestCase {

public void testVectorLayer() {

injectUrlAndTest(() -> {

HeatmapOptions heatmapOptions = new HeatmapOptions();
assertNotNull(heatmapOptions);

VectorOptions vectorOptions = new VectorOptions();
vectorOptions.setUrl("https://openlayers.org/en/latest/examples/data/kml/2012_Earthquakes_Mag5.kml");
vectorOptions.setFormat(new Kml());
ol.source.Vector vector = new ol.source.Vector(vectorOptions);

heatmapOptions.setSource(vector);
heatmapOptions.setBlur(2);
heatmapOptions.setRadius(15);
heatmapOptions.setWeight((Feature feature) -> {

String magnitudeValue = feature.get("name");
String parsedMagnitude = magnitudeValue.substring(2);
parsedMagnitude = parsedMagnitude.substring(0, parsedMagnitude.indexOf(" "));

return Double.parseDouble(parsedMagnitude) - 5;

});

/*Heatmap heatmap = new Heatmap(heatmapOptions);
assertNotNull(heatmap);
assertTrue(heatmap instanceof Base);
assertTrue(heatmap instanceof Layer);
assertTrue(heatmap instanceof Vector);*/

});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*******************************************************************************
* Copyright 2014, 2021 gwt-ol
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.github.tdesjardins.ol3.demo.client.example;

import ol.Coordinate;
import ol.Feature;
import ol.source.Stamen;
import ol.source.StamenOptions;
import ol.source.VectorOptions;
import ol.Map;
import ol.MapOptions;
import ol.OLFactory;
import ol.View;
import ol.control.MousePosition;
import ol.format.Kml;
import ol.format.KmlOptions;
import ol.layer.Heatmap;
import ol.layer.HeatmapOptions;
import ol.layer.LayerOptions;
import ol.layer.Tile;

/**
* Example with heatmap.
*
* @author Tino Desjardins
*
*/
public class HeatmapExample implements Example {

/* (non-Javadoc)
* @see de.desjardins.ol3.demo.client.example.Example#show()
*/
@Override
public void show(String exampleId) {

StamenOptions stamenOptions = new StamenOptions();
stamenOptions.setLayer("toner");

Stamen stamen = new Stamen(stamenOptions);

LayerOptions rasterLayerOptions = OLFactory.createOptions();
rasterLayerOptions.setSource(stamen);

VectorOptions vectorOptions = new VectorOptions();
vectorOptions.setUrl("https://openlayers.org/en/latest/examples/data/kml/2012_Earthquakes_Mag5.kml");
KmlOptions kmlOptions = new KmlOptions();
kmlOptions.setExtractStyles(false);
Kml kml = new Kml(kmlOptions);
vectorOptions.setFormat(kml);

ol.source.Vector vectorSource = new ol.source.Vector(vectorOptions);

Tile stamenLayer = new Tile(rasterLayerOptions);

HeatmapOptions heatmapOptions = new HeatmapOptions();
heatmapOptions.setSource(vectorSource);
heatmapOptions.setBlur(15);
heatmapOptions.setRadius(8);
heatmapOptions.setWeight((Feature feature) -> {

String magnitudeValue = feature.get("name");
String parsedMagnitude = magnitudeValue.substring(2);
parsedMagnitude = parsedMagnitude.substring(0, parsedMagnitude.indexOf(" "));

return Double.parseDouble(parsedMagnitude) - 5;

});

Heatmap heatmapLayer = new Heatmap(heatmapOptions);

// create a view
View view = new View();

Coordinate centerCoordinate = new Coordinate(0, 0);
view.setCenter(centerCoordinate);
view.setZoom(2);

// create the map
MapOptions mapOptions = OLFactory.createOptions();
mapOptions.setTarget(exampleId);
mapOptions.setView(view);

Map map = new Map(mapOptions);

ol.control.Attribution attributionControl = new ol.control.Attribution();
attributionControl.setCollapsed(false);

map.addControl(attributionControl);
MousePosition mousePosition = new MousePosition();
mousePosition.setCoordinateFormat(Coordinate.createStringXY(2));
map.addControl(mousePosition);
map.addLayer(stamenLayer);
map.addLayer(heatmapLayer);

}

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2014, 2019 gwt-ol
* Copyright 2014, 2021 gwt-ol
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@ public enum OLExampleType {
GeolocationExample(new GeolocationExample()),
GpxExample(new GpxExample()),
GraticuleExample(new GraticuleExample()),
HeatmapExample(new HeatmapExample()),
ImageExample(new StaticImageExample()),
MapBoxExample(new MapboxExample()),
MapEventsExample(new MapEventsExample()),
Expand Down

0 comments on commit 944ca92

Please sign in to comment.