Point inside the Polygon using Latitude, Longitude ASP.Net

I'm taking the polygon points from database and also the points i fetch from database that need to check inside or outside the polygon (lat,lon), With this we cna  determine a polygon and implement an algorithm which would check if a point is inside or outside the polygon.Its very simple code i ASP.net C#, you can also use this login in ASP or PHP based code.

 

 public static void CheckPloygonPoint()
    {
        string sqlQuery = "select  latitude, longitude from tablename";
        SqlDataAdapter adp1 = new SqlDataAdapter(sqlQuery, DbConn);
        DataSet ds = new DataSet();
        adp1.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int LIntCtr = 0; LIntCtr <= ds.Tables[0].Rows.Count - 1; LIntCtr++)
            {
                string geoCounty = "";
                string latitude="";
                string longitude="";
                if (geoCounty.Length == 0)
                    geoCounty = ds.Tables[0].Rows[LIntCtr]["geofence"].ToString();
                else
                {
                    if ((ds.Tables[0].Rows[LIntCtr]["geofence"].ToString()).Length > 0)
                    {
                        geoCounty = ds.Tables[0].Rows[LIntCtr]["geofence"].ToString();
                    }

                }
                latitude = ds.Tables[0].Rows[LIntCtr]["latitude"].ToString();
                longitude = ds.Tables[0].Rows[LIntCtr]["longitude"].ToString();
             
                bool PointInOutGeofence = true;
                if (geoCounty.Length > 0)
                {
                    //DataView dv = dsgeo.Tables[0].DefaultView;
                    //dv.RowFilter = "CountyName='" + geoCounty + "'";
                    //if (geoArr.Length <= 0)
                    //    geoArr = dv[0]["latlng"].ToString();
                    //else
                    //    geoArr = geoArr + "#" + dv[0]["latlng"].ToString();

                    geoCounty = "";
                    PointInOutGeofence = checkPointExistsInGeofencePolygon(ds.Tables[0].Rows[LIntCtr]["Latlng"].ToString(), latitude, longitude);
                }
            }
        }
    }
    private static bool checkPointExistsInGeofencePolygon(string latlnglist, string lat, string lng)
    {
        List<Loc> objList = new List<Loc>();
        // sample string should be like this strlatlng = "39.11495,-76.873259|39.114588,-76.872808|39.112921,-76.870373|";
        string[] arr = latlnglist.Split('|');
        for (int i = 0; i <= arr.Length - 1; i++)
        {
            string latlng = arr[i];
            string[] arrlatlng = latlng.Split(',');

            Loc er = new Loc(Convert.ToDouble(arrlatlng[0]), Convert.ToDouble(arrlatlng[1]));
            objList.Add(er);
        }
        Loc pt = new Loc(Convert.ToDouble(lat), Convert.ToDouble(lng));

        if (IsPointInPolygon(objList, pt) == true)
        {
              return true;
        }
        else
        {
               return false;
        }
    }
    private static bool IsPointInPolygon(List<Loc> poly, Loc point)
    {
        int i, j;
        bool c = false;
        for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
        {
            if ((((poly[i].Lt <= point.Lt) && (point.Lt < poly[j].Lt)) |
                ((poly[j].Lt <= point.Lt) && (point.Lt < poly[i].Lt))) &&
                (point.Lg < (poly[j].Lg - poly[i].Lg) * (point.Lt - poly[i].Lt) / (poly[j].Lt - poly[i].Lt) + poly[i].Lg))
                c = !c;
        }
        return c;
    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Loc
/// </summary>
public class Loc
{
    private double lt;
    private double lg;

    public double Lg
    {
        get { return lg; }
        set { lg = value; }
    }

    public double Lt
    {
        get { return lt; }
        set { lt = value; }
    }

    public Loc(double lt, double lg)
    {
        this.lt = lt;
        this.lg = lg;
    }
}

Related Alrticles

FREE!!! Registration