OpenLayers Add-on: How to select features?

Hi guys.

I have a map:

map = new OpenLayersMap();

I have a wfs layer:

wfs = new WebFeatureServiceLayer();
      Style style = new Style();
      StyleMap styleMap = new StyleMap(style);

Now i want zoomToExtent of some features of this layer. And i have some questions.

1 How to get layer in map?
2 How to get layer’s features?
3 How to programly select features?


I have used WFS quite a little and I guess there might be someone else who could instruct you better. I’ll try my best.

Please elaborate.

In WFS, browsers connect directly to the WFS server. The UI server don’t necessary know anything about features what’s displayed on browser. You can though make an http request directly from the server side code to you WFS server and read features from the response. Then you don’t necessary need to use WFS layer on map at all, but use the raw VectorLayer. Then editing, selections and stuff like that becomes much easier.

If you attach FeatureSelectedListener to your layer, you’ll get notified of features selected by users. Selection event contains identifier, attributes and coordinates in WKT about the selected feature. Don’t know if this would be enough for you.

The same problem here: as with WFS the server side don’t know about features displayed on the client, it is hard to select something from the server side code. Don’t know WFS well enough to tell if there is a method to “preselect” features with some kind of query. If you know the ids of features to select, I think it should be rather easy to build API to select them by their ids.


Hi Matti

Im new in vaadin can you tell me more how to



That would require you to get into world of GWT, browser environment and develop OpenLayers Wrapper further.

If you don’t want to get into that territory, I think you should consider accessing your WFS server directly form your UI code and use plain VectorLayer. Protocol is rather simple to use with java and then selections, editing etc. becomes much simpler. This way you don’t even need a separate proxy to bypass same domain policy as browsers only communicate to your UI server.


After investigating WFS I have actually switched to vector layers in my application, like Matti suggested.

The good points are:

  • gives you total control of what is displayed
  • it’s easy to create any shapes you need, so also how the features are displayed
  • no proxy needed

The cons

  • having large number of features, you have to manually keep the number of ones displayed on the screen in the limits
  • no support for clustering, also have to implement it myself

So, WFS i harder to set up initially and makes some use cases harder, but lacks some of the power features of WFS.

I believe at least controlling number of the features on the client-side could be done by lazy-loading them from the GWT side.
Also, would be good to make context menu (the “actions”) aware of the vector features and have double-click events on them. Right now I emulate both in the code.

How did you go about implementing clustering?