Alex Petropavlovsky
Alex Petropavlovsky

Reputation: 590

.NET DbGeography.Intersects works incorrectly for a polygon which intersects with two opposite hemispheres

Say, we have the polygon which covers a Russia territory (by google maps geocoding):

POLYGON ((19.64053 41.18535, -169.0465 41.18535, -169.0465 81.85812, 19.64053 81.85812, 19.64053 41.18535))

This polygon is intersected with the Northern hemisphere and two opposite hemispheres: the Eastern and the Western.

Let's take a point somewhere in Siberia:

POINT (93.3252301 56.1171069)

And check what Siberia still stays in Russia:

var russia = DbGeography.FromText("POLYGON ((19.64053 41.18535, -169.0465 41.18535, -169.0465 81.85812, 19.64053 81.85812, 19.64053 41.18535))", 4326);
var point = DbGeography.FromText("POINT (93.3252301 56.1171069)", 4326);
var isSiberiaInRussia = russia.Intersects(point); 
// isSiberiaInRussia equals false, and it's wrong.

Let's take a polygon which is intersected only with the Northern and the Eastern hemispheres. Let it be Moscow:

POLYGON ((37.31933 55.48993, 37.94566 55.48993, 37.94566 56.00966, 37.31933 56.00966, 37.31933 55.48993))

And take someone who lays in there:

POINT (37.622504899999967 55.753602)

Let's check it:

var moscow = DbGeography.FromText("POLYGON ((37.31933 55.48993, 37.94566 55.48993, 37.94566 56.00966, 37.31933 56.00966, 37.31933 55.48993))", 4326);
var lenin = DbGeography.FromText("POINT (37.622504899999967 55.753602)", 4326);
var isLeninLaysInMoscow = moscow.Intersects(lenin);
// He's still there...

So, is there any way to make geographical hit test which satisfy following criterias:

  1. Be LINQ compatible
  2. Be EF Code First compatible

Upvotes: 1

Views: 1015

Answers (1)

root
root

Reputation: 2357

The problem you're having is because of the curvature of the Earth. Polygon you have for Russia is a very big rectangle with only 4 points. The line representing the southern edge is curved north so much that it goes above (to the north from) "Siberia".

You will need to add more points to the "Russia" polygon for this to work or use Geometry instead of Geography. With Geometry though you will have to split the polygon into 2 so that it doesn't cross the -180 meridian.

Upvotes: 2

Related Questions