Reputation: 1997
I'm trying to make a query where I can find all locations in my SQL database within (lets say 50 miles) of a given Longitude and Latitude points.
I populated some posts, and in the Post table I have two columns where I store Longitude and Latitude when creating a post. Here is how the table looks:
I have browsed many examples of how to try to implement this, but it seems like they just don't work. I know from researching that ASP.NET Core doesn't support DbGeography
, and other articles are just outdated.
Found this article, but it uses DbGeography
, so that doesn't work.
Found this on GitHub: here I just don't know if that would work though.
Just for reference, this is how my Post model looks:
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public double Lat { get; set; }
public double Lng { get; set; }
public DateTime Created { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; }
// Other navigation properties...
}
And this is how I do my query as of right now to display points on my Google Maps in the frontend:
public class HomeController : Controller
{
private readonly ApplicationDbContext _context;
public HomeController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet]
public JsonResult GetGalleryLocations()
{
var data = _context.Posts
.Include(c => c.Category)
.ToList();
return Json(data);
}
}
Does anyone have any insight on how to implement a query like this?
Upvotes: 2
Views: 3633
Reputation: 1916
They introduced spatial data support with version 2.2 of Entity Framework Core.
You can take a look at this StackOverflow answer.
Upvotes: 1
Reputation: 26917
How about if you extract the data from the database that fits in the square around your circle of desired distance, and then fine tune on the client side to just the posts in the circle?
Given your initial data
var myLat = 25.05;
var myLon = -80.3;
var radiusInMile = 50;
You can compute the bounds of the square containing that circle
var minMilePerLat = 68.703;
var milePerLon = Math.Cos(myLat) * 69.172;
var minLat = myLat - radiusInMile / minMilePerLat;
var maxLat = myLat + radiusInMile / minMilePerLat;
var minLon = myLon - radiusInMile / milePerLon;
var maxLon = myLon + radiusInMile / milePerLon;
Then you can query the database for those posts inside the square, bring them to the client and keep just the ones in the circle
var data = _context.Posts
.Where(p => (minLat <= p.Lat && p.Lat <= maxLat) && (minLon <= p.Lng && p.Lng <= maxLon))
.AsEnumerable()
.Select(p => new { p, Dist = distanceInMiles(myLon, myLat, p.Lng, p.Lat) })
.Where(p => p.Dist <= radiusInMile);
Where the distanceInMiles
function is defined as
public double ToRadians(double degrees) => degrees * Math.PI / 180.0;
public double distanceInMiles(double lon1d, double lat1d, double lon2d, double lat2d) {
var lon1 = ToRadians(lon1d);
var lat1 = ToRadians(lat1d);
var lon2 = ToRadians(lon2d);
var lat2 = ToRadians(lat2d);
var deltaLon = lon2 - lon1;
var c = Math.Acos(Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(deltaLon));
var earthRadius = 3958.76;
var distInMiles = earthRadius * c;
return distInMiles;
}
I am using the spherical law of cosines to compute the distance, which I assume is enough for this problem. If you need more accuracy, you could upgrade the formula to something more complicated like haversine or Vincenty.
Upvotes: 5