test_geo_code - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of test_geo_code.php

  1. <?php
  2. class pointLocation {
  3.     var $pointOnVertex = true; // Check if the point sits exactly on one of the vertices?
  4.  
  5.     function pointLocation() {
  6.     }
  7.  
  8.     function pointInPolygon($point, $polygon, $pointOnVertex = true) {
  9.         $this->pointOnVertex = $pointOnVertex;
  10.  
  11.         // Transform string coordinates into arrays with x and y values
  12.         $point = $this->pointStringToCoordinates($point);
  13.         $vertices = array(); 
  14.         foreach ($polygon as $vertex) {
  15.             $vertices[] = $this->pointStringToCoordinates($vertex); 
  16.         }
  17.  
  18.         // Check if the point sits exactly on a vertex
  19.         if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
  20.             return "vertex";
  21.         }
  22.  
  23.         // Check if the point is inside the polygon or on the boundary
  24.         $intersections = 0; 
  25.         $vertices_count = count($vertices);
  26.  
  27.         for ($i=1; $i < $vertices_count; $i++) {
  28.             $vertex1 = $vertices[$i-1]; 
  29.             $vertex2 = $vertices[$i];
  30.             if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Check if point is on an horizontal polygon boundary
  31.                 return "boundary";
  32.             }
  33.             if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) { 
  34.                 $xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x']; 
  35.                 if ($xinters == $point['x']) { // Check if point is on the polygon boundary (other than horizontal)
  36.                     return "boundary";
  37.                 }
  38.                 if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
  39.                     $intersections++; 
  40.                 }
  41.             } 
  42.         } 
  43.         // If the number of edges we passed through is odd, then it's in the polygon. 
  44.         if ($intersections % 2 != 0) {
  45.             return "inside";
  46.         } else {
  47.             return "outside";
  48.         }
  49.     }
  50.  
  51.     function pointOnVertex($point, $vertices) {
  52.         foreach($vertices as $vertex) {
  53.             if ($point == $vertex) {
  54.                 return true;
  55.             }
  56.         }
  57.  
  58.     }
  59.  
  60.     function pointStringToCoordinates($pointString) {
  61.         $coordinates = explode(" ", $pointString);
  62.         return array("x" => $coordinates[0], "y" => $coordinates[1]);
  63.     }
  64.  
  65. }
  66.  
  67. $pointLocation = new pointLocation();
  68. $a = [
  69.                             [
  70.                                 27.654320492097895,
  71.                                 49.804205674553145
  72.                             ],
  73.                             [
  74.                                 27.654370612214848,
  75.                                 49.80412674015787
  76.                             ],
  77.                             [
  78.                                 27.65426372239375,
  79.                                 49.80413705088512
  80.                             ],
  81.                             [
  82.                                 27.654131751380078,
  83.                                 49.8041487699507
  84.                             ],
  85.                             [
  86.                                 27.65417557714204,
  87.                                 49.80421843323501
  88.                             ],
  89.                             [
  90.                                 27.654320492097895,
  91.                                 49.804205674553145
  92.                             ]
  93.                         ];
  94. $polygon = array("-50 30","50 70","100 50","80 10","110 -10","110 -30","-20 -50","-30 -40","10 -10","-10 10","-30 -20","-50 30");
  95. // The last point's coordinates must be the same as the first one's, to "close the loop"
  96. echo $pointLocation->pointInPolygon('27.002909 49.412186', array_map(function($item){
  97.     return join(' ',$item);
  98. },$a)) ;
File Description
  • test_geo_code
  • PHP Code
  • 24 Jan-2020
  • 3.89 Kb
You can Share it: