Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework and Spatial Search POINT inside POLYGON

I have an entity called Shop which has a DBGeorgpraphy column called Position

A sample shop in the database has a Position value of POINT (145.034242 -37.825519)

I am trying to retrieve all shops that fall within a polygon.

var polygon = DbGeography.PolygonFromText(@"POLYGON((145.2898592378906 -37.66376896413059,
                                                     145.2898592378906 -37.93504877166811, 
                                                     144.7075838472656 -37.93504877166811, 
                                                     144.7075838472656 -37.66376896413059, 
                                                     145.2898592378906 -37.66376896413059))", 
                                                     4326);

var shops = db.Shops.Where(p => p.Position.Intersects(polygon));

I would expect the sample shop to be included in the results but it doesn't. Can anyone enlighten me?

like image 205
Gordon Hickley Avatar asked Feb 06 '26 01:02

Gordon Hickley


1 Answers

The answer is to construct the polygon in the opposite direction, i.e. anti-clockwise.

var polygon = DbGeography.PolygonFromText(@"POLYGON((145.2898592378906 -37.66376896413059,
                                                     144.7075838472656 -37.66376896413059,
                                                     144.7075838472656 -37.93504877166811,
                                                     145.2898592378906 -37.93504877166811, 
                                                     145.2898592378906 -37.66376896413059))", 
                                                     4326);
like image 110
Gordon Hickley Avatar answered Feb 09 '26 12:02

Gordon Hickley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!