0

Currently in OpenLayers, I am creating features from a GeoJSON FeatureCollection as follows:

const points = JSON.parse(document.getElementById('points-data').textContent);
const features = new ol.format.GeoJSON().readFeatures(
    points,
    {featureProjection: "EPSG:3857",}
);

This all works fine normally, but breaks an OpenLayers extension I'm trying to use called GridBin at a step that tries to get the geometry of the feature sources passed to it. This is what a GridBin layer constructor looks like:

    var countingGrid = new ol.layer.Vector({
        style: gridCount,
        source: new ol.source.GridBin({
            source: source,
        }),
    });

From what I can tell in the documentation, objects created via the GeoJSON().readFeatures() method is slightly different to standard ol.geom.Point objects, in that the coordinates are not retrievable from a getter function for geojson features unlike geom Point features. I'm sorry if this is confusing, I'm still trying to work out the OpenLayers API. The solution I think would be to somehow add a geometry to, or fetch the geometry from, the features created from GeoJSON, how do I achieve this? Below is the console error trace for reference.


ol.js:8 Uncaught TypeError: Cannot read property 'getCode' of null
    at Zr (ol.js:8)
    at Kr (ol.js:8)
    at Object.Hr [as transform] (ol.js:8)
    at ol.source.GridBin.getGridGeomAt (ol-ext.js:19900)
    at ol.source.GridBin.ol.source.BinBase.getBinAt (ol-ext.js:18390)
    at ol.source.GridBin.ol.source.BinBase._onAddFeature (ol-ext.js:18311)
    at ol.source.GridBin.ol.source.BinBase.reset (ol-ext.js:18430)
    at ol.source.GridBin.ol.source.BinBase [as constructor] (ol-ext.js:18294)
    at new ol.source.GridBin (ol-ext.js:19875)
    at displayDateRange (main.js:431)

My suspicion is that it breaks where it tries getGridGeomAt, since the features I'm using are not geom objects, it returns null.

3
  • transform and getCode suggest a projection is expected but not correctly defined.
    – Mike
    Commented Jul 21, 2021 at 15:13
  • Also if the geometry may not be Point you will need a geometry function to return a Point geometry from LineStrings, Polygons, etc. viglino.github.io/ol-ext/doc/doc-pages/ol.source.GridBin.html
    – Mike
    Commented Jul 21, 2021 at 15:21
  • @Mike Thank you for responding! Do you have any idea where the projection might be expected? I've looked at the doc page, and I've tried creating a new set of ol.geom.Point objects using the coordinates from the geosjon collection but it still returns the same error. Commented Jul 21, 2021 at 15:36

1 Answer 1

1

I've gotten it working, I had to create an empty source object, set it as the source of the grid layer, and then populate the source with features afterwards. I have no idea why this fixes it, if anyone has any idea please explain, it would be very appreciated!

var source = new ol.source.Vector();
var featuresList = [] //populated in whatever way
var grid = new ol.layer.Vector({
    style: styleFn,
    source: new ol.source.GridBin({
        source: source;
    }),
});
source.addFeatures(featuresList);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.