函数 st_distancespheroid(几何,几何) 不存在

function st_distancespheroid(geometry, geometry) does not exist

我已经安装了postgis,但还是有问题:

function st_distancespheroid(geometry, geometry) does not exist

    WITH data AS (
    SELECT a.*,
    CAST(ST_DistanceSpheroid(geometry(location), st_geomfromtext('POINT(' || ::decimal || ' ' || ::decimal || ')', 4326))as numeric) / 1000 AS distance
    FROM agent AS a
    WHERE (a.agent_code ILIKE 
    OR a.name ILIKE 
    OR a.phone LIKE )
    AND
    a.sub_district_name LIKE ANY(string_to_array(, ','))
    AND
    a.agent_status_id LIKE ANY(string_to_array(, ','))
    AND CASE WHEN  = 'FAVORITE' THEN
    a.is_subscription = true
    WHEN  = 'REGULER' THEN
    a.is_subscription != true
    ELSE
    a.location LIKE '%%'
    END
    ORDER BY distance ASC
),
data_counter AS (
    SELECT COUNT(agent_id) AS __total__
    FROM data
)
SELECT * 
FROM data, data_counter
LIMIT 
OFFSET 
`

当我 运行 我的 go repo

如何安装 PostGIS

安装后您必须在数据库中创建扩展。例如,要在 Debian 发行版中安装 PostGIS 3.0 运行 PostgreSQL 13:

apt-get install postgresql-13-postgis-3

之后在数据库中创建扩展

CREATE EXTENSION postgis;

现在您可以使用空间函数了。

如何计算椭球体/球体上的距离

函数ST_DistanceSpheroid需要三个参数,即两个几何图形和球状体。在您的代码中,您仅使用两个几何图形调用该函数,因此出现错误 function st_distancespheroid(geometry, geometry) does not exist。这是它应该如何调用(使其适应您选择的球体):

SELECT
  ST_DistanceSpheroid(geom1,geom2,'SPHEROID["WGS 84",6378137,298.257223563]')
FROM t;

如果将 geometry 参数转换为 geography:

,您还可以告诉 ST_Distance 使用椭球体计算距离
SELECT ST_Distance(geom1::geography,geom2::geography,true) FROM t;

另一个不太准确的选项是使用 ST_DistanceSphere:

 SELECT ST_DistanceSphere(geom1,geom2) FROM t;