# Point inside the Polygon(Square or Rectangle) using Latitude, Longitude RSS

## 4 replies

Last post Jan 04, 2011 12:03 PM by meersajidali

0 Points

4 Posts

### Point inside the Polygon(Square or Rectangle) using Latitude, Longitude

Jan 02, 2011 05:39 PM|LINK

How to find point(lat,lon) whether its inside the Rectangle or Outside the Rectangle using the latitude and longitudes ?

I'm taking the 4 points(lat,lon) from databse, and I want to make it as Rectangle, With this , I want to comapare the point whether its inside or outside ?

Ex: Points :

Square Polygon Points
"A1" lat=26.4005468423, lon=49.876067469
"A2" lat=26.4012043391, lon=49.7196231051
"A3" lat=26.5417861847, lon=49.8799462857
"A4" lat=26.5402702627, lon=49.7205040114

Test Points
"test1" lat=26.4729446305, lon=49.8260514451
"test2" lat=26.4477477915, lon=49.7669868375
"test3" lat=26.4789993695, lon=49.7934318402
"test4" lat=26.3736638963, lon=49.7831075436
"test5" lat=26.4649043892, lon=49.8922252455
"test6" lat=26.4971811973, lon=49.6881746616
"test7" lat=26.5581720741, lon=49.7931671621
"test8" lat=26.5182964967, lon=49.9076046365

Please Advice ....

Sajid

vb 2005 vb.net 2008

• Moved by mbanavige on Jan 02, 2011 09:29 PM
• ### SGWellens

All-Star

126029 Points

10309 Posts

Moderator

### Re: Point inside the Polygon(Square or Rectangle) using Latitude, Longitude

Jan 02, 2011 10:24 PM|LINK

The Rectangle object has a Contains method that does exactly what you need.

Steve Wellens

My blog

0 Points

4 Posts

### Re: Point inside the Polygon(Square or Rectangle) using Latitude, Longitude

Jan 03, 2011 06:42 AM|LINK

SGWallens,

I'm using following methods. I'm checking the point with the method called FindPoint()..............as follows.

private void loadData()
{
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select Latitude, Longitude from tb_polygon", cn);
sda.Fill(ds);

foreach (DataRow dr in ds.Tables[0].Rows)
{
Point p = new Point();

//Convert Latitude into degrees
String Lat = dr[0].ToString();
double LatSec = Double.Parse(Lat.Substring(4, 4)) / 6000;
double LatMin = (Double.Parse(Lat.Substring(2, 2)) + LatSec) / 60;
p.X = Double.Parse(Lat.Substring(0, 2)) + LatMin;

//Convert Longitude into degrees
String Long = dr[1].ToString();
double LongSec = Double.Parse(Long.Substring(5, 4)) / 6000;
double LongMin = (Double.Parse(Long.Substring(3, 2)) + LongSec) / 60;
p.Y = Double.Parse(Long.Substring(0, 3)) + LongMin;
points.Add(p);

}

}

---------------------------------------------------------------------

private void cmdSearch_Click(object sender, EventArgs e)
{
loadData();
PolyGon myRoute = new PolyGon(points);
bool stat = myRoute.FindPoint(Double.Parse(txtLat.Text.ToString()), Double.Parse(txtLang.Text.ToString()));
if (stat)
{
lblResult.Text = "Point found in the route";
points.Clear();
}
else
{
lblResult.Text = "Point not found in the route";
points.Clear();
}
}

-----------------------------------------------------------------------------

namespace myGeoFence
{
class Point
{
public double X;
public double Y;
}
class PolyGon
{
public List<Point> myPts = new List<Point>();
public PolyGon()
{
}
public PolyGon(List<Point> points)
{
foreach (Point p in points)
{
this.myPts.Add(p);
}
}
public void Add(Point p)
{
this.myPts.Add(p);
}
public int Count()
{
return myPts.Count;
}

//  The function will return true if the point x,y is inside the polygon, or
//  false if it is not.  If the point is exactly on the edge of the polygon,
//  then the function may return true or false.

public bool FindPoint(double X, double Y)
{
int sides = this.Count();

int j = sides - 1;
bool pointStatus = false;

for (int i = 0; i < sides; i++)
{
if (myPts[i].Y > Y && myPts[j].Y >= Y || myPts[j].Y > Y && myPts[i].Y >= Y)
{
if (myPts[i].X + (Y - myPts[i].Y) / (myPts[j].Y - myPts[i].Y) * (myPts[j].X - myPts[i].X) < X)
{
pointStatus = !pointStatus;
}
}
j = i;
}
return pointStatus;
}

}
}

• ### SGWellens

All-Star

126029 Points

10309 Posts

Moderator

### Re: Point inside the Polygon(Square or Rectangle) using Latitude, Longitude

Jan 03, 2011 01:09 PM|LINK

Your initial post was misleading.  You don't have a square or rectangle, it looks like you have a trapezoid.

Here's a wiki article on algorithms.

http://en.wikipedia.org/wiki/Point_in_polygon

Here's some poorly formatted C# code, note the points in the polygon array must be in the correct order:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/95055cdc-60f8-4c22-8270-ab5f9870270a/

Steve Wellens

My blog

0 Points

4 Posts

### Re: Point inside the Polygon(Square or Rectangle) using Latitude, Longitude

Jan 04, 2011 12:03 PM|LINK

not helpful