I am writing a Openlayers map app, and wondering whether there's a better practice to query which features of which vector layers intersect with a given bounds..

It's certainly not a good idea to iterate features...

thanks for any replies!


In this scenario, vector layers are already added to map, so I assume I don't need to request features from remote GeoServer again... then'll just do intersect check on the fly

  • I have exactly the same problem...
    – user13402
    Commented Dec 7, 2012 at 13:40

2 Answers 2


If fetching features from Geoserver WFS you can issue a WFS request with a CQL geometric filter like those described here:


You should issue one request per layer. I'm pretty sure Mapserver also has something like that, but I can't recall the correct syntax right now.

CQL/ECQL operators are described here.

  • thanks, good suggestion! In my app, I already added a WFS vector layer into map, so I suppose I don't need to request features from GeoServer again per intersection calculation. do you know whether there's better way to deal with feature intersection process for existing layers other than iterate each feature? thank you a lot!
    – Simon
    Commented Dec 6, 2011 at 15:41

Your best approach is probably to set up a unit test with the maximum number of expected features and run it multiple times in a few different browsers to get an accurate idea of how long exactly it will take. You can base it on an OpenLayers unit test (see here for more details on setting one up) or an example such as this one.

JavaScript is getting surprisingly fast, and intersections even on a large number of features may be unnoticeable to a user.

A few factors will affect this:

  • the number of features in the map at anyone time, although you'll probably notice slow loading / panning times before you even get to querying the features
  • the type of geometry you are querying. If it is a point OpenLayers simply checks if the other point has the same coordinates. If it is a polygon then it loops through each vertex to see if it falls in the other polygon.

There are a few other options before you start looping through geometries. You could get the bounds of each geometry, and seeing which of these intersect to filter out many of the features, before comparing the remaining geometries.

I've also found the distanceTo function to be useful, though this could be slower than intersects.

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.