Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a DbGeography describing a rectangle

I have a two points describing a rectangle:
a Northeast Lat/Long and a Southwest Lat/Long.

What will be the correct and efficient way to generate a simple rectangle entity that can be stored in a DbGeography field?

Note that later on I would like to use that field to determine if a POINT is inside this Rectangle.

I realize I should use DbGeography.FromText(...) methods, but I'm not sure how.

like image 786
Liel Avatar asked Nov 21 '25 11:11

Liel


1 Answers

The FromText method expects a WKT representation, in this case for a polygon:

Something like this should do the trick:

DbGeography box = DbGeography.FromText(
    string.Format("POLYGON(({0} {1}, {0} {2}, {3} {2}, {3} {1}, {0} {1}))",
                         nwLongitude, 
                         nwLatitude,
                         seLatitude,
                         seLongitude), 4326);
like image 167
psousa Avatar answered Nov 24 '25 01:11

psousa



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!