Jump to content

Recommended Posts

Hello, I am looking for a solution to a point in polygon problem.

 

My goal is to find a way for me to determine if a latitude/longitude marker falls within a polygon. I found a couple of PiP solutions, neither were completely what I was looking for but they were close.

 

One is based on JavaScript and it takes a Virtual Earth map, displays the polygon using my co-ordinates and if I click inside the polygon, an alert box returns true, if I click outside the polygon, it returns false.

 

I don't know enough about JavaScript to determine if this can be adapted into PHP where it just runs a loop and returns if an array of markers fall within a polygon.

 

I apologize if it's confusing (it is to me). If anyone can help I would appreciate it. Here is the javascript code and thanks for everything:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript" src="http://example.com/mapcontrol/mapcontrol.ashx?v=6.1"></script>

<script type="text/javascript">

var map = null;

var polygon = null;

 

function GetMap()

{

map = new VEMap('myMap');

map.LoadMap();

 

//Plot Polygon

var points = new Array(new VELatLong(41.7972300222736, -87.7281369157155),

new VELatLong(41.7983290222745, -87.7281489157156),

new VELatLong(41.798857022275, -87.7267849157143),

new VELatLong(41.7989060222752, -87.7266109157142),

new VELatLong(41.7990980222752, -87.7260359157136),

new VELatLong(41.7991840222754, -87.7258169157134),

new VELatLong(41.8001330222763, -87.7233909157112));

 

polygon = new VEShape(VEShapeType.Polygon, points);

polygon.HideIcon();

map.AddShape(polygon);

map.SetMapView(points);

 

//Add onclick handler

map.AttachEvent("onclick", map_click);

}

 

function map_click(eventArgs)

{

var latlong = map.PixelToLatLong(new VEPixel(eventArgs.mapX, eventArgs.mapY));

alert("Is Point Within Polyline:\n" + GeoHelper.IsInPolygon(polygon.GetPoints(), latlong));

}

 

if (GeoHelper == undefined)

var GeoHelper = {};

 

GeoHelper.IsInPolygon=function(points,latlong)

{

 

var i;

var j=points.length-1;

var inPoly=false;

var lat = latlong.Latitude;

var lon = latlong.Longitude;

 

for (i=0; i<points.length; i++)

{

if (points.Longitude<lon && points[j].Longitude>=lon ¦¦ points[j].Longitude<lon && points.Longitude>=lon)

{

if (points.Latitude+(lon-points.Longitude)/(points[j].Longitude-points.Longitude)*(points[j].Latitude-points.Latitude)<lat)

{

inPoly=!inPoly;

}

}

j=i;

}

 

return inPoly;

};

</script>

</head>

<body onload="GetMap();">

<div id='myMap' style="position: relative; width: 600px; height: 400px;"></div>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/137635-can-this-be-done-in-php/
Share on other sites

Hi,

I checked around and saw this thread:

http://www.phpfreaks.com/forums/index.php/topic,108445.0.html

The final suggestion was to draw lines from my point to see how many times it intersects with the polygon, if it intersects an odd number of times, it's inside, an even number, it's outside.

 

However I have no idea how to do this. I have a working example in Javascript and I don't know enough about Javascript and not having a good enough understanding in math to figure out how to do this and am looking for a new direction. I thought with my code and trying to go from javascript to php I would post it under a new topic.

What I would like this to do is to just loop through the points of the polygon and check it against a longitude/latitude point and tell me if my point falls inside or outside the polygon.

 

Like I said, a JavaScript master I am not, but what really confuses me is in the IsInPolygon function. I have no idea what j=points.length-1 means or what points.Longitude<lon && points[j].Longitude>=lon means. Because after that, it should just be a simple task of looping through the array, separating the latitude and longitude values and running through this math:

 

for (i=0; i<points.length; i++) {

    if (points.Longitude<lon && points[j].Longitude>=lon ¦¦ points[j].Longitude<lon && points.Longitude>=lon) {

        if (points.Latitude+(lon-points.Longitude)/(points[j].Longitude-points.Longitude)*(points[j].Latitude-points.Latitude)<lat) {

    inPoly=!inPoly;

}

    }

    j=i;

}

 

Right? They set the inPoly variable to false so if it passes through both those if statements, then the point I gave it must fall within the polygon?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.