San
San

Reputation: 666

Point in polygon using geoPHP

I want to check a point exists in a polygon or not using GEOPHP in Drupal. I have created the polygon and got the coordinates from http://www.birdtheme.org/useful/v3tool.html.

$userLocation = geoPHP::load("POINT(77.600555 12.965416)","wkt");
$storeServingArea=geoPHP::load("POLYGON((77.569656 12.986159, 77.596779 12.993518, 77.622185 12.973780, 77.623215 12.954710, 77.608109 12.945676, 77.577896 12.945341, 77.565880 12.957386, 77.565193 12.976122, 77.568970 12.985824))","wkt");
var_dump($storeServingArea->contains($userLocation));

This code gives the result as NULL.

Then I tried to exchange the values of latitude and longitude and tested like this:

$userLocation = geoPHP::load("POINT(12.965416 77.600555)","wkt");
$storeServingArea=geoPHP::load("POLYGON((12.986159 77.569656, 12.993518 77.596779, 12.973780 77.622185, 12.954710 77.623215, 12.945676 77.608109, 12.945341 77.577896, 12.957386 77.565880, 12.976122 77.565193, 12.985824 77.568970))","wkt");

var_dump($storeServingArea->contains($userLocation));

This also give the result as NULL.

What could be the possible mistake I'm doing here? how can I fix it?

SOLUTION:

Helpful link: Check if Google Map Point is in polygon from PHP

I fixed my issue with the following link: http://assemblysys.com/php-point-in-polygon-algorithm/

Upvotes: 1

Views: 1805

Answers (1)

mihkelo
mihkelo

Reputation: 111

I have forked geoPHP and added Polygon->pointInPolygon and MultiPolygon->pointInPolygon methods for that purpose. Take a look: https://github.com/sookoll/geoPHP

$point = \geoPHP::load('POINT (x y)','wkt');
$polygon = \geoPHP::load('POLYGON ((x y...))','wkt');
$point_is_in_polygon = $polygon->pointInPolygon($point);

Upvotes: 4

Related Questions