Click here to Skip to main content
Click here to Skip to main content

A Simple Geo Fencing Using Polygon Method

By , 7 Mar 2010
 

Introduction

One of the important feature of GPS Tracking software using GPS Tracking devices is geo-fencing and its ability to help keep track of assets. Geo-fencing allows users of a GPS Tracking Solution to draw zones (i.e., a Geo Fence) around places of work, customer’s sites and secure areas. These geo fences when crossed by a GPS equipped vehicle or person can trigger a warning to the user or operator via SMS or Email.

Geo Fence

A Geo fence is a virtual perimeter on a geographic area using a location-based service, so that when the geo fencing device enters or exits the area, a notification is generated. The notification can contain information about the location of the device and might be sent to a mobile telephone or an email account. Reference: http://en.wikipedia.org/wiki/Geofence.

Background

For geo-fencing, I used Polygonal geo-fencing method where a polygon is drawn around the route or area. Using this method, GPS Tracking devices can be tracked either inside or outside of the polygon.

Determining a Point

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. Thanks for the article “Determining Whether A Point Is Inside A Complex Polygon”.

public bool FindPoint(double X, double Y)
{
            int sides = this.Count() - 1;
            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;
}

Creating a Polygon

On the map, draw a polygon to the area which is to be geo-fenced and capture the corner points of the polygon and store into XML file (see: PolygonPoints.XML). loadData() function will create a polygon using defined corner points in the XML file.

private void loadData()
{
    DataSet ds = new DataSet();
    ds.ReadXml("PolygonPoints.XML");

    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);              
    }
} 

Sample Code

When you run and enter latitude and longitude outside the polygon, then a message shows point not found in the route and otherwise it shows point found in the route.

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";
}
else
    lblResult.Text = "Point not found in the route";

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

RajuBhupathi
Chief Technology Officer Vajra Infratech Pvt. Ltd.,
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Geofencing? Polygon?memberbulleh20 Jul '11 - 0:22 
GeneralRe: Geofencing? Polygon?memberborno5 Aug '11 - 8:40 
GeneralRe: Geofencing? Polygon?memberbulleh5 Aug '11 - 23:38 
GeneralRe: Geofencing? Polygon?memberzinobu24 Aug '11 - 7:30 
GeneralGetting decimal data to work.memberdomanet16 Jun '11 - 0:23 
GeneralRe: Getting decimal data to work.memberRajuBhupathi19 Jun '11 - 22:44 
GeneralRe: Getting decimal data to work.memberdomanet20 Jun '11 - 7:04 
QuestionHow to convert coordinates to XML file?memberTadysas26 Mar '11 - 6:12 
AnswerRe: How to convert coordinates to XML file?memberTadysas26 Mar '11 - 6:35 
Generalformula errormemberNoman Aftab29 Dec '10 - 1:48 
Questionask about coordinat in the xml file?memberkanvista8 Jul '10 - 23:17 
AnswerRe: ask about coordinat in the xml file?memberoldsap13 Jul '10 - 16:28 
AnswerRe: ask about coordinat in the xml file?memberRajuBhupathi13 Jul '10 - 23:22 
GeneralRe: ask about coordinat in the xml file?memberoldsap3 Aug '10 - 22:03 
GeneralRe: ask about coordinat in the xml file?memberRajuBhupathi4 Aug '10 - 20:01 
GeneralRe: ask about coordinat in the xml file?memberoldsap13 Aug '10 - 14:39 
GeneralRe: ask about coordinat in the xml file?memberRajuBhupathi16 Aug '10 - 1:09 
GeneralRe: ask about coordinat in the xml file?memberRajuBhupathi16 Aug '10 - 1:10 
GeneralRe: ask about coordinat in the xml file?memberoldsap16 Aug '10 - 13:23 
GeneralRe: ask about coordinat in the xml file?memberoldsap17 Aug '10 - 20:48 
GeneralRe: ask about coordinat in the xml file?memberRajuBhupathi17 Aug '10 - 21:56 
GeneralRe: ask about coordinat in the xml file?memberoldsap17 Aug '10 - 22:07 
GeneralRe: ask about coordinat in the xml file?memberRajuBhupathi17 Aug '10 - 22:22 
GeneralRe: ask about coordinat in the xml file?memberoldsap17 Aug '10 - 22:23 
GeneralRe: ask about coordinat in the xml file?memberoldsap17 Aug '10 - 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 7 Mar 2010
Article Copyright 2010 by RajuBhupathi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid