[OpenLayers-Users] GetFeature: select best feature using centroid

Duncan Sayers duncansayers at threefivesouth.net
Fri Oct 9 06:33:21 EDT 2009


Here's the scenario:

I am mapping a number of sites where I want to be able to click on or  
near a site (sites displayed via mapserver wms - data in Postgis), and  
do a WFS GetFeature to bring up a polygon associated with that site  
and draw it in a Vector layer.

I've simplified this for testing using simple 5km buffers for each  
site in a separate Layer (also postgis -> mapserver wms/wfs etc). Many  
of these polygons overlap so it's not a simple matter of grabbing the  
polygon that contains the click position. The buffers will obviously  
have their centroid at the site, and the final shapes will be line-of- 
sight plots, where the centroid will be closeish. But it seems that  
the selectBestFeature function in GetFeature.js grabs the polygon  
whose edge is nearest the click position via the distanceTo function -  
not the behaviour I'm after but I can't seem to find anything to  
change it.

So I've modified the GetFeature control slightly replacing the line in  
the selectBestFeature function:

	...
	dist = point.distanceTo(feature.geometry, {edge: false});
	...

With...

	...
	var centroid = feature.geometry.getCentroid();
         dist = point.distanceTo(centroid, {edge: false});
	...

It works brilliantly, but I was wondering if anyone knows of a  
function/option that I haven't found that enables me to achieve the  
same without needing my dodgy hack, or alternatively suggest  a  
completely different approach?

Thanks,

Duncan Sayers



More information about the Users mailing list