[OpenLayers-Users] Utilize DWITHIN spatial filter in OpenLayers

Phil Scadden p.scadden at gns.cri.nz
Mon Jan 6 15:44:54 PST 2014


 >But about the solution you mentioned convert point to meters. Not sure 
how to do that, could you give me some hint?

If you are doing this within context of event (likely), the point you 
want to search around will be in the event
(lets assume you assigned event to e)
  var geom = e.feature.geometry.clone();

Create a suitable projection for your part of the world that uses meters.
   var myproj = new OpenLayers.Projection ("a suitable EPSG");

you also need the projection code of whatever the native layer 
projection is:
   var lyProj = new OpenLayers.Projection("native EPSG");

geom.transform(lyProj,myproj); // convert to meters
geom.x = geom.x + myDis;
geom.transform(myproj,lyProj) // convert back to latlong
mydisD = 
Math.sqrt((e.feature.geometry.x-geom.x)*(e.feature.geometry.x-geom.x) + 
(e.feature.geometry.y-geom.y)*(e.feature.geometry.y-geom.y) )

Doing this off top of my head so may be some errors but general idea. If 
you want this to work anywhere in world, then more problematic (no 
universally accurate projection).  You could use haversine formula for 
get a second point x meters from first. viz:
        convert lat/lon into radians  (lat1,lon1), R is radius of earth

var  lat2=  Math.asin(  Math.sin(lat1)*Math.cos(d/R)  +  Math.cos(lat1)*Math.sin(myDis/R));
var lon2 = lon1;

Spherical approx, but good for short distances (<10km).


     if (l.imageProjection != proj.getCode()) {
         pproj = new OpenLayers.Projection(l.imageProjection);
         geom.transform(proj, pproj);
     }


Notice: This email and any attachments are confidential.
If received in error please destroy and immediately notify us.
Do not copy or disclose the contents.



More information about the Users mailing list