3

I am having extreme difficulty making this doctrine2 extension works. It is https://github.com/djlambert/doctrine2-spatial and theres not a lot of doc on how to create a polygon. I got the config file working and all but I am struggling with creating the actual polygon.

array:564 [
   0 => array:2 [
    0 => -73.698313
    1 => 45.546876
   ]
   1 => array:2 [
     0 => -73.69813
     1 => 45.546916
   ]
   2 => array:2 [
     0 => -73.697656
     1 => 45.546899
   ]
    3 => array:2 [
      0 => -73.697413
      1 => 45.546899
   ]

 $poly = new Polygon($array);

[CrEOF\Spatial\Exception\InvalidValueException]  
  Invalid Polygon Point value of type "double"  

This is the actual error Im getting. I tried creating points instead because apparently it doesn't like doubles.

$p = new Point($coord);
$temp[] = $p;
$poly = new Polygon($temp);


[CrEOF\Spatial\Exception\InvalidValueException]                                    
  Invalid Polygon LineString value of type      "CrEOF\Spatial\PHP\Types\Geometry\Point" 

After that, I was like ok, lets create a line string object and pass it.

$line = new LineString($points);
$poly - new Polygon($line);

 [Symfony\Component\Debug\Exception\ContextErrorException]                                                                                                           
  Catchable Fatal Error: Argument 1 passed to    CrEOF\Spatial\PHP\Types\AbstractPolygon::__construct() must be of the type array,   object given, called in /Library/Web        Server/Documents/mg/src/Momoa/ImmobilierBundle/Entity/geography/Quartier.php on line 131 and defined

Im just lost right now, the only thing I wanted was to store polygons in the database and call spatial functions such as CONTAINS. Do you have any recommendation or such other things to make all of this work.

After digging through the source code I found this validate function which seems to be the problem

case (is_array($point) && count($point) == 2 && is_numeric($point[0]) &&    is_numeric($point[1])):
            return array_values($point);
            break;
        default:
            throw InvalidValueException::invalidType($this, GeometryInterface::POINT, $point);
    }

The way I'm understanding this is that the extension do not accept points that have decimal values ?! Huh, Does that mean I need to convert my coordinates to 2 integers ?!

delmalki
  • 1,326
  • 1
  • 13
  • 31

2 Answers2

6

I will post the solution I found. Basically you need to create your polygon like this

$line = new LineString($coords);
$poly = new Polygon(array($line));
//Or you can do it like this
$coords[0] = $coords;
$poly = new Polygon($coords);
//Following if you wanna use MBRContains or Contains
$dql = "SELECT p FROM polygon p WHERE MBRContains(p.geometry, GeomFromText('Point($lat $lng)'))=1";
//Dont use GeomFromText(:point), and then $point = new Point(array($lat,$lng));

Basically good luck guys, that library is useful but the documentation is BAD ! Spent the whole day on that yesterday !!

delmalki
  • 1,326
  • 1
  • 13
  • 31
  • You're right about the documentation. I want to use it for unserializion from JSON file and can't find anything about it. – Sela Yair Jun 20 '18 at 14:01
1

You can create it by passing data in constructor. But the problem is that you should have a valid polygon data:

$p = new Polygon([[[lat1, lng1], [lat2, lng2], [lat3, lng3], [lat1, lng2]]]);

Be sure that in the Polygon

  1. there are three arrays — array of lines, where line is an array of points, where every point is an array.
  2. the path is closed — the last point equals to the first one.

The same truth for MultiPolygon but + one more array. F.e. [[[["32.699005219026645","-117.18222600929262"],["32.694563070816095","-117.18437177650453"],["32.697687043641835","-117.17149717323305"],["32.699005219026645","-117.18222600929262"]]]]