2

I expected a WFS call to be made when a Vector Layer was added to the map, but it doesn't happen until the layer is marked as visible.

Here is how I'm currently creating my vector layers. The visible param is initially marked as false via layer.checked

function createVectorLayer (layer, zIndex) {
  const source = new VectorSource({
    format: new GeoJSON(),
    url: function (extent) {
      return `${process.env.GEOSERVER_URL}/wfs?service=WFS&version=${process.env.WFS_VERSION}&request=GetFeature&typename=${layer.name}&outputFormat=application/json`
    }
  })
  return new VectorLayer({
    source,
    style: VectorStyle,
    opacity: layer.opacity,
    visible: layer.checked,
    zIndex
  })
}

I then add them to the map via the standard method: map.addLayer(vectorLayer)

The problem is that until I update the layer's visibility via vectorLayer.setVisible(true) the wfs call made to the url defined in VectorSource won't be made and therefore the layer has no features which is causing problems.

Again, I assumed would be that the wfs call would be made when the Layer was added to the map and not on the condition of its visibility.

I haven't been able to find anything in the docs that explains this, so I could use some help/information.

How can I get a vector features (via wfs call) before the layer is marked visible?

app-specific information

  • I'm creating a feature search that sets the map's center to that feature's coordinates
  • but, if the layer is not 'visible' yet (the user just opened the map and everything is at default settings), the layer has no features to search from
    • I can then set the layer as visible on search, but then the wfs call is made in the background and I have no way to listen to it's return. I could listen to a layer's change event but that's irregardless of the main question of:
    • how (if possible) can I get a layer's features via wfs before the layer is visible
1
  • Just an aside, I see you have version number as a variable, but then use 'typename' as as if it's independent of version number, it's not. For version 2 it should be typenames
    – nmtoken
    Commented Sep 18, 2019 at 19:47

1 Answer 1

1

The easiest way is probably by using opacity instead of visibility

new VectorLayer({
    source,
    style: VectorStyle,
    opacity: layer.checked * layer.opacity,
    visible: true,
    zIndex
  })

Alternatively if you have a fully opaque base layer you could hide other layers behind it by reducing their zIndex, or you you have a visible layer but keep the features hidden by setting an empty style.

1
  • yes, that is technically a way to do it so I'll mark it as correct, but as far as my app goes that would mean creating an inconsistency between the user's visibility/order/opacity settings :(
    – orpheus
    Commented Sep 19, 2019 at 16:22

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.