Within - A little confused

Topics: General Topics
Dec 2, 2006 at 4:49 PM
SarpMap and MaSqlSpatial are just what I have been looking for to complete some work for an client. But, I am missing the po int with the folowing code. I create a rough square to describe london, to be replaced with a better polygon in the future. The point is definitely within the square but the within method returns false. does anyone know why?


SharpMap.Geometries.Polygon london = new SharpMap.Geometries.Polygon(new SharpMap.Geometries.LinearRing(London()));

SharpMap.Geometries.Point test = new SharpMap.Geometries.Point(0, 0);

if (test.Within(london))
{
AnswerText.Text = "Yes";
}
else
{
AnswerText.Text = "No";
}

private List<SharpMap.Geometries.Point> London()
{
List<SharpMap.Geometries.Point> london = new List<SharpMap.Geometries.Point>();

london.Add(new SharpMap.Geometries.Point(-0.5, 51.6));
london.Add(new SharpMap.Geometries.Point(0.1, 51.6));
london.Add(new SharpMap.Geometries.Point(0.1, 51.3));
london.Add(new SharpMap.Geometries.Point(-0.5, 51.3));
london.Add(new SharpMap.Geometries.Point(-0.5, 51.6));

return london;
Dec 7, 2006 at 10:32 AM
I have a similar problem. I've tried both Within and IsPointWithin using the following code:

SharpMap.Layers.VectorLayer layer = new VectorLayer("GeometryLayer");
System.Collections.Generic.List<SharpMap.Geometries.Point> PointColl = new System.Collections.Generic.List<SharpMap.Geometries.Point>();
PointColl.Add(new SharpMap.Geometries.Point(12.582457066, 55.668711662));
PointColl.Add(new SharpMap.Geometries.Point(12.582457066, 55.672295094));
PointColl.Add(new SharpMap.Geometries.Point(12.575805187, 55.672295094));
PointColl.Add(new SharpMap.Geometries.Point(12.575805187, 55.668711662));
PointColl.Add(new SharpMap.Geometries.Point(12.582457066, 55.668711662));

SharpMap.Geometries.LinearRing lr = new SharpMap.Geometries.LinearRing(PointColl);
SharpMap.Geometries.Geometry polygon = new SharpMap.Geometries.Polygon(lr);

SharpMap.Geometries.Point p = new SharpMap.Geometries.Point(12.58200, 55.668911);

bool b1 = p.Within(lr);
bool b2 = p.Within(polygon);
bool b3 = lr.IsPointWithin(p);

bool b4 = lr.Contains(p);
bool b5 = polygon.Contains(p);

layer.DataSource = new SharpMap.Data.Providers.GeometryProvider(polygon);
map.Layers.Add(layer);


All of the above tests (b1-b5) returns false. Maybe I'm missing an important point? :)


Dec 8, 2006 at 11:06 PM
The problem is that you fall into an infinite loop between SpatialRelation.Contains() and Geometry.Within().

Don't know if this solution fits you, but that can be done ExecuteIntersectionQuery(), and get accurate results with the aid of NetTopologySuite, as explained in
http://www.codeplex.com/SharpMap/Wiki/View.aspx?title=NetTopologySuite%20Data%20Provider



Oct 22, 2007 at 6:21 PM
Edited Oct 22, 2007 at 6:22 PM
There is a bug in the IsPointWithin() method in the LinearRing class. Replacing the following code seems to fix the problem.

Original:

for (int i = 0; i < this.Vertices.Count; i++)