Symfony Postgresql Postgis查找周围的元素

i7uaboj4  于 11个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(131)

我试图用postgis来找到一个以米为单位的点周围的东西。
我在Symfony 6.3.7,教义包2.10,postresql 13和postgis 3.0我还安装了jsor bundle用于教义扩展,这是我的conf:

orm:
        dql:
            string_functions:
                ST_DWithin: Jsor\Doctrine\PostGIS\Functions\ST_DWithin
                ST_SetSRID: Jsor\Doctrine\PostGIS\Functions\ST_SetSRID
                ST_POINT: Jsor\Doctrine\PostGIS\Functions\ST_Point
                ST_GeomFromText: Jsor\Doctrine\PostGIS\Functions\ST_GeomFromText
                ST_Transform: Jsor\Doctrine\PostGIS\Functions\ST_Transform
                geometry: Jsor\Doctrine\PostGIS\Functions\Geometry
                geography: Jsor\Doctrine\PostGIS\Functions\Geography

字符串
我的数据库中有两个带有经度和纬度的参数。我创建了一个DoctrineMigration,并使用以下请求来创建一个点列。我选择SRID 4326,因为它似乎是一个常见的参数,但它可能不适合我的情况

$this->addSql('CREATE EXTENSION IF NOT EXISTS postgis;');
$this->addSql('ALTER TABLE myTable ADD point geometry(GEOMETRY, 4326)');
$this->addSql('UPDATE myTable SET point = ST_SetSRID(ST_Point(longitude, latitude), 4326)');


对于raw sql,像SELECT * FROM myTable WHERE ST_DWithin(ST_SetSRID(point, 4326)::geography, ST_GeomFromText('POINT(4.137325 46.691456)', 4326)::geography, 20000)这样的东西似乎可以工作,但它可能没有优化。
我正在努力使用querybuilder来搜索一个给定区域内的元素。我想按接近度排序。
我该怎么做?

toiithl6

toiithl61#

它最终创建了一个地理列而不是几何,以避免转换

'ALTER TABLE Creche ADD point geography(point, 4326)'

字符串
对于查询,使用$qb->expr()->eq似乎可以让它工作。

$exp = "n.point, ST_GeomFromText('" . $entity->getPoint() . "', 4326)";
$qb = $this->createQueryBuilder('n');    
$queryBuilder = $qb
            ->addSelect('st_distance('.$exp.') as distance')
            ->where($qb->expr()->eq("ST_DWithin($exp, " . $rayon * 1000 . ")","true"))
            ->orderBy('distance', 'asc')
            ->setMaxResults(10);

相关问题