Determine if point intersects 35km radius around another point? Possible in Linq?

StackOverflow https://stackoverflow.com/questions/2954820

  •  23-10-2019
  •  | 
  •  

سؤال

Assume I have a point at the following location:

Latitude: 47°36′N Longitude: 122°19′W

Around the above point, I draw a 35Km radius. I have another point now or several and I want to see if they fall within the 35Km radius? How can I do this? Is it possible with Linq given the coordinates (lat, long) of both points?

هل كانت مفيدة؟

المحلول

Sure. Assume you have a function that computes the Haversine distance between two Positions (consisting of a latitude and longitude coordinate). If you don't you can find one here. Then simply use the function as the selector in a Where clause. If using LINQ to SQL, you'll need to materialize them to your Position objects so that the you can use the Haversine function on them as LINQ to objects; there isn't a translation to SQL, though you could probably create a table-valued function that does the same thing if you really don't want to return all the points first.

var origin = new Position( 47.6, 122.3 );
var close = positions.Where( p => Haversine.Distance( origin, p, DistanceType.Km ) <= 35 );
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top