3

我在使这个教义2扩展工作时遇到了极大的困难。它是https://github.com/djlambert/doctrine2-spatial,关于如何创建多边形的文档并不多。我得到了配置文件,但我正在努力创建实际的多边形。

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"  

这是我得到的实际错误。我尝试创建积分,因为显然它不喜欢双打。

$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" 

之后,我就好了,让我们创建一个线串对象并传递它。

$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

我现在只是迷路了,我唯一想要的就是将多边形存储在数据库中并调用空间函数,例如CONTAINS. 您是否有任何建议或其他类似的事情来完成所有这些工作。

挖掘源代码后,我发现这个验证函数似乎是问题所在

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);
    }

我理解这一点的方式是扩展不接受具有十进制值的点?!嗯,这是否意味着我需要将坐标转换为 2 个整数?!

4

2 回答 2

6

我将发布我找到的解决方案。基本上你需要像这样创建你的多边形

$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));

基本上祝你好运,那个库很有用,但文档很糟糕!昨天花了一整天的时间!!

于 2015-04-06T17:51:29.597 回答
1

您可以通过在构造函数中传递数据来创建它。但问题是你应该有一个有效的多边形数据:

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

确保在多边形

  1. 共有三个数组 - 线数组,其中线是点数组,其中每个点都是数组。
  2. 路径是封闭的——最后一个点等于第一个点。

MultiPolygon 的情况相同,但多了一个数组。铁 [[[["32.699005219026645","-117.18222600929262"],["32.694563070816095","-117.18437177650453"],["32.697687043641835","-117.17149717323305"],["32.699005219026645","-117.18222600929262"]]]]

于 2021-06-04T16:33:10.353 回答