【问题标题】:DQL query for getting geo spatial points st_within a rectangle用于获取矩形内地理空间点 st_within 的 DQL 查询
【发布时间】:2016-03-20 14:16:46
【问题描述】:

我想编写 DQL 查询来选择地图中矩形内的所有点。矩形由左上点和右下点定义。有关详细信息,请参阅此link

为此,我编写了以下 DQL 查询:

$qb = $this->em->createQueryBuilder()
    ->select("m")
    ->from($this->getEntityClassName(), "m")
    ->where("ST_Within(m.geom, envelope(linestring(:topleft, :bottomright)))")
    ->setParameter(":topleft", $topleft)
    ->setParameter(":bottomright", $bottomright)
    ->orderBy("m.date", "DESC");

我得到的错误是:

[Syntax Error] line 0, col 110: Error: Expected =, <, <=, <>, >, >=, !=, got 'ORDER'

供您参考,我正在查询的实体具有如下定义的 geom 属性:

/**
 * @var point $geom
 * @ORM\Column(type="point", nullable=true)
 */
protected $geom;  

请注意,SQL 查询运行良好。如下:

SELECT *FROM MotorsAds WHERE
st_within(point(lng, lat),         
envelope(linestring(point(10.090792984008772,36.83717099338201 ), 
point(10.310519546508772,36.749467295867646 ))))

我用于 DQL 数值函数的库(例如 st_within)是 creof/doctrine2-spatial

准备好接受任何建议来解决这个问题。

谢谢,


问题更新

我尝试了您提出的解决方案如下:

    $qb = $this->em->createQueryBuilder()
        ->select("m")
        ->from($this->getEntityClassName(), "m")
        ->where(
            $this->em->createQueryBuilder()->expr()->eq(
                    "ST_Within(m.geom, envelope(linestring(:topleft, :bottomright)))",
                    $this->em->createQueryBuilder()->expr()->literal(true)
                )
        )
        ->setParameter(":topleft", $topleft)
        ->setParameter(":bottomright", $bottomright)
        ->orderBy("m.date", "DESC");

所以,我得到的错误是:

执行 'SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id_0 FROM (SELECT m0_.id AS id_0, m0_.price AS price_1, m0_.year AS year_2, m0_.km AS km_3) 时发生异常, m0_.slug AS slug_4, m0_.title AS title_5, m0_.description AS description_6, m0_.address AS address_7, m0_.isPublished AS isPublished_8, m0_.delegation ASdelegation_9, m0_.lat AS lat_10, m0_.lng AS lng_11, m0_ .date AS date_12, m0_.count AS count_13, AsBinary(m0_.geom) AS geom_14 FROM MotorsAds m0_ WHERE ST_Within(m0_.geom, Envelope(LineString(?, ?))) = 1 ORDER BY m0_.date DESC) dctrn_result) dctrn_table' 带参数 [{}, {}]:

SQLSTATE[22007]:无效的日期时间格式:1367 在解析过程中发现非法非几何“10.090792984009 36.837170993382”值

【问题讨论】:

    标签: mysql symfony geospatial dql


    【解决方案1】:

    试试这个:

    $qb = $this->em->createQueryBuilder()
        ->select("m")
        ->from($this->getEntityClassName(), "m")
        ->where(
            $queryBuilder->expr()->eq(
                    "ST_Within(m.geom, envelope(linestring(:topleft, :bottomright)))",
                    $queryBuilder->expr()->literal(true)
                )
        )
        ->setParameter(":topleft", $topleft)
        ->setParameter(":bottomright", $bottomright)
        ->orderBy("m.date", "DESC");
    

    我从文档中获取它:https://github.com/creof/doctrine2-spatial/blob/master/doc/index.md

    【讨论】:

    • 谢谢@Miro 的回答。我更新我的问题。我可以请你评论吗?
    • 你提出的解决方案的问题是积分的转换......下面是我的解决方案。再次感谢@Miro
    【解决方案2】:

    下面的解决方案将防止您将 PHP 点转换为 DQL 查询点的问题。

        $qb = $this->em->createQueryBuilder()
            ->select("m")
            ->from($this->getEntityClassName(), "m")
            ->where(
                $this->em->createQueryBuilder()->expr()->eq(
                        sprintf("ST_Within(m.geom, envelope(linestring(point(:topleftX,:topleftY), point(:bottomrightX,:bottomrightY ))))"),
                        $this->em->createQueryBuilder()->expr()->literal(true)
                    )
            )
            ->setParameter(":topleftX", $topleftX)
            ->setParameter(":topleftY", $topleftY)
            ->setParameter(":bottomrightX", $bottomrightX)
            ->setParameter(":bottomrightY", $bottomrightY)
            ->orderBy("m.date", "DESC");
    

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 2012-07-10
      • 2014-06-01
      • 1970-01-01
      • 2012-06-19
      • 2012-04-08
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      相关资源
      最近更新 更多