TypeORM - PostGIS 在查询中使用自定义功能

TypeORM - PostGIS use custom feature in query

我有一个项目,我需要检查某条道路在哪些城市中。为此,我使用 PostGIS 提供的 ST_Intersects 函数进行检查。

我面临的问题是,在 typeorm 的查询生成器中使用道路时,我不断出错。

我的第一次尝试给了我以下错误 QueryFailedError: ST_Intersects: Operation on mixed SRID geometries (MultiPolygon, 4326) != (LineString, 0) at new QueryFailedError:

          const municipalities = await getConnection()
            .getRepository(Municipality)
            .createQueryBuilder('municipality')
            .where('ST_Intersects(municipality.polygon, :lineString)', { lineString: feature.geometry })
            .getQueryAndParameters()

然后我尝试通过使用 PostGIS 也提供的 ST_SetSRID 函数设置线串的 CRS 来解决这个问题。但这给了我以下错误 QueryFailedError: function st_setsrid(unknown, integer) is not unique:

          const municipalities = await getConnection()
            .getRepository(Municipality)
            .createQueryBuilder('municipality')
            .where('ST_Intersects(municipality.polygon, ST_SetSRID(:lineString, 4326))', { lineString: feature.geometry })
            .getMany()

我还尝试使用 PostGIS 中的 ST_GeomFromGeoJSON 函数传输 Geojson 对象。但这给了我以下错误:QueryFailedError: quoted object property name expected (at offset 1):

          const municipalities = await getConnection()
            .getRepository(Municipality)
            .createQueryBuilder('municipality')
            .where('ST_Intersects(municipality.polygon, ST_GeomFromGeoJSON(:lineString))', { lineString: feature.geometry.coordinates })
            .getMany()

我试图查看问题是否出在将线串插入查询时。我通过使用 typeorm 的 getQueryAndParameters return 函数来做到这一点:

[
  'SELECT "municipality"."uuid" AS "municipality_uuid", "municipality"."name" AS "municipality_name", ST_AsGeoJSON("municipality"."polygon")::json AS "municipality_polygon" FROM "municipality" "municipality" WHERE ST_Intersects("municipality"."polygon", ST_SetSRID(, 4326))',
  [ { type: 'LineString', coordinates: [Array] } ]

中的一个 Linestring 对象如下所示:

{
  type: 'LineString',
  coordinates: [
    [ 3.2188848, 51.1980164 ],
    [ 3.2190236, 51.1981144 ],
    [ 3.2190737, 51.1981991 ],
    [ 3.2191065, 51.1982793 ],
    [ 3.2191314, 51.1983772 ],
    [ 3.2191128, 51.1984885 ],
    [ 3.2192215, 51.1985207 ],
    [ 3.2192447, 51.1985056 ],
    [ 3.219269, 51.1985136 ],
    [ 3.2193766, 51.1985571 ],
    [ 3.2194258, 51.1985769 ],
    [ 3.2194638, 51.198697 ],
    [ 3.2195437, 51.1987221 ],
    [ 3.2196618, 51.1987591 ],
    [ 3.219529, 51.1989397 ],
    [ 3.2195909, 51.1990766 ],
    [ 3.2196759, 51.1992964 ],
    [ 3.2197817, 51.1993408 ],
    [ 3.2199103, 51.1992987 ],
    [ 3.2204127, 51.1991677 ],
    [ 3.2208458, 51.199056 ],
    [ 3.2211454, 51.1989993 ],
    [ 3.2217751, 51.1988675 ],
    [ 3.2219908, 51.1988136 ],
    [ 3.2223186, 51.1987693 ],
    [ 3.2223696, 51.1987867 ],
    [ 3.2223974, 51.1987845 ],
    [ 3.2225339, 51.1987696 ],
    [ 3.2230863, 51.1987062 ],
    [ 3.2233638, 51.198683 ],
    [ 3.2234618, 51.1986505 ],
    [ 3.223517, 51.1986535 ],
    [ 3.2235298, 51.1986528 ],
    [ 3.2236432, 51.198651 ],
    [ 3.2243337, 51.1986565 ],
    [ 3.2244463, 51.198654 ],
    [ 3.2245644, 51.1986568 ],
    [ 3.2249334, 51.1986506 ],
    [ 3.2249647, 51.1986493 ],
    [ 3.2251107, 51.19867 ],
    [ 3.225378, 51.1986779 ],
    [ 3.2257131, 51.1987153 ],
    [ 3.225936, 51.1987341 ]
  ]
}

我找到了答案。 ST_GeomFromGeoJSON 确实是用于将 GeoJSON 对象注入查询的 wright 函数。但是我不小心使用了坐标而不是完整的 GeoJSON 对象。

所以解决方案是:

    const municipalities = await getConnection()
    .getRepository(Municipality)
    .createQueryBuilder('municipality')
    .where('ST_Intersects(municipality.polygon, ST_GeomFromGeoJSON(:lineString))', { lineString: feature.geometry })
    .getMany()