Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 Spatial Data

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 ?!

like image 650
delmalki Avatar asked Nov 29 '25 01:11

delmalki


1 Answers

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 !!

like image 75
delmalki Avatar answered Dec 01 '25 10:12

delmalki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!