Search code examples
javascriptopenlayersopenlayers-5

Using a vector layer as an input to a raster source in openlayers


I'm trying to use vector features as a mask for a raster source in Openlayers 5. (I want to do calculations on raster data only within specific jurisdictions, which are dynamically selectable by the user.) To do this, I tried to use the vector layer with my features as an input to the raster source, and then test each pixel to see if the mask is present.

The documentation indicates that this should be possible if the vector layer is configured with the option renderMode: 'image'. However, when the raster layer attempts to run I get a TypeError: h.getImage is not a function.

My code is as follows:

// The base raster data
var arc = new TileArcGISRest({
    params: {
        'LAYERS': "view:0"
    },
    url: myDataURL,
    crossOrigin: 'anonymous',
});

// The vector mask in GeoJSON format
var town_vector = new ol.source.Vector({
    url: 'towns.json',
    format: new ol.format.GeoJSON(),
});

// The layer based on that vector
var town_layer = new ol.layer.Vector({
    title: 'added Layer',
    source: town_vector,
    renderMode: 'image',
    style: interest_style,
});

// The raster source that attempts the analysis
var summary_raster = new RasterSource({
    sources: [arc, town_layer],
    operation: function (pixels, data) {
        var pixel = pixels[0];
        var mask_pixel = pixels[1];
        if (mask_pixel[0] == mask_value) {
            run_calculation(pixel)
        }
    },
    lib: {
        run_calculation: run_calculation,
    }
});

It works perfectly when I remove the "town_layer" from the source, and eliminate the masking logic. However, when I add the "town_layer" to the raster source I get the aforementioned TypeError: h.getImage is not a function. How can I "rasterize" this vector layer and use it as in input to the raster source?


Solution

  • please check a this link which refers to a link

    It states that ol.source.ImageVector has been deprecated from v4.5.0 and suggested to use ol.layer.Vector. And it worked for me.(I used a openlayers v6.0) Please try with v6.0 it may work for you too.