In the H3-JS example I have, it is implemented in mapbox. But I would like to integrate the H3-JS binning as part of Openlayers map instead. This is the example I want to implement in OpenLayers. https://github.com/agrc/gis.utah.gov/tree/master/developer/applications/suitability
I had asked this question at the Mapbox Github repo, and was told it can be done. https://github.com/uber/h3-js/issues/160#issuecomment-1251248799
function renderHexes(map, hexagons) {
console.log("renderHexes");
// Transform the current hexagon map into a GeoJSON object
// const geojson = geojson2h3.h3SetToFeatureCollection(
binnedGeoJSON = geojson2h3.h3SetToFeatureCollection(
Object.keys(hexagons),
(hex) => ({ value: hexagons[hex] })
);
const sourceId = "h3-hexes";
const layerId = `${sourceId}-layer`;
let source = map.getSource(sourceId);
// Add the source and layer if we haven't created them yet
if (!source) {
map.addSource(sourceId, {
type: "geojson",
data: binnedGeoJSON, //geojson,
});
map.addLayer({
id: layerId,
source: sourceId,
type: "fill",
interactive: false,
paint: {
"fill-outline-color": "rgba(0,0,0,0)",
},
});
source = map.getSource(sourceId);
}
// Update the geojson data
source.setData(binnedGeoJSON); //geojson);
//Call my new function to carry out H3-JS in Openlayers
newMap(binnedGeoJSON);
console.log('end tomap2')
// Update the layer paint properties, using the current config values
map.setPaintProperty(layerId, "fill-color", {
property: "value",
stops: [
[0, config.colorScale[0]],
[0.5, config.colorScale[1]],
[1, config.colorScale[2]],
],
});
map.setPaintProperty(layerId, "fill-opacity", config.fillOpacity);
}
And my new function is
function newMap(geoJSON)
{
console.log('Begin NewMap Function: ');
var coord = [-76.95266723632812, 39.07974903895123];
var features = new ol.format.GeoJSON().readFeatures(geoJSON, { //geojsonObject
featureProjection: 'EPSG:3857'
});
var vectorSource = new ol.source.Vector({ features: features });
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: function (feature, resolution) {
console.log(feature.getProperties(), feature.get('marker-color'));
return [new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({ color: feature.get('marker-color') })
})
})];
}
});
var map3 = new ol.Map({
target: 'map3',
layers: [
new ol.layer.Tile({ source: new ol.source.OSM() }),
vectorLayer
],
view: new ol.View({
center: ol.proj.fromLonLat(coord),
zoom: 5
})
});
// Add the created layer to the Map
//map3.addLayer(vectorLayer);
console.log('End NewMap Function: ');
}
But still the H3-binning results are now shown in the OpenLayers map. Maybe I have to find the Openlayers equivalent for the following function.
So my main question is how to convert the below code block in Mapbox into Openlayers?
// Update the layer paint properties, using the current config values
map.setPaintProperty(layerId, "fill-color", {
property: "value",
stops: [
[0, config.colorScale[0]],
[0.5, config.colorScale[1]],
[1, config.colorScale[2]],
],
});
Upon commenting out the style function, as suggested by Mike, I only get the frames pf the hexagons. They are not shaded and also binning needs to be carried out.
Please advice.
[![enter image description here][2]][2]
dataProjection: 'EPSG:4326',
in addition tofeatureProjection: 'EPSG:3857'
. How doesmarker-color
relate to your original code? To style a polygon with an image with you would to set the style geometry to its interior point. Forfill-color
your style function could set different fill colors depending on a feature property value.