postgres - 错误:运算符不存在

时间:2010-04-19 07:28:16

标签: postgresql postgis

同样,我有一个在本地运行良好的函数,但是在线移动会产生一个很大的错误...从一个有人指出我传递的参数数量的响应中得到一个提示并不准确,我仔细检查了这种情况,以确定我将5个参数传递给函数本身......

Query failed: ERROR: operator does not exist: point <@> point HINT: No operator matches the given name and argument type(s). You may need to add explicit type casts.

查询是这样的:

BEGIN; SELECT zip_proximity_sum('zc',                                                                                                                                                                  
    (SELECT g.lat FROM geocoded g                                                                                                                                                                              
    LEFT JOIN masterfile m ON g.recordid = m.id                                                                                                                                                                
    WHERE m.zip = '10050' ORDER BY m.id LIMIT 1),                                                                                                                                                             
    (SELECT g.lon FROM geocoded g                                                                                                                                                                              
    LEFT JOIN masterfile m ON g.recordid = m.id                                                                                                                                                                
    WHERE m.zip = '10050' ORDER BY m.id LIMIT 1),                                                                                                                                                             
    (SELECT m.zip FROM geocoded g                                                                                                                                                                              
    LEFT JOIN masterfile m ON g.recordid = m.id                                                                                                                                                                
    WHERE m.zip = '10050' ORDER BY m.id LIMIT 1)                                                                                                                                                              
    ,10);

PG功能如下:

CREATE OR REPLACE FUNCTION zip_proximity_sum(refcursor, numeric, numeric, character, numeric)
  RETURNS refcursor AS
$BODY$ 
    BEGIN 

        OPEN $1 FOR 
            SELECT r.zip, point($2,$3) <@> point(g.lat, g.lon) AS distance
            FROM
            geocoded g LEFT JOIN masterfile r ON g.recordid = r.id 
            WHERE (geo_distance( point($2,$3),point(g.lat,g.lon)) < $5)
            ORDER BY r.zip, distance;
        RETURN $1; 
    END; 
    $BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;

6 个答案:

答案 0 :(得分:3)

以下是确切的命令:

create extension cube;
create extension earthdistance;
select (point(-0.1277,51.5073) <@> point(-74.006,40.7144)) as distance;

     distance     
------------------
 3461.10547602474
(1 row)

请注意points是使用 LONGITUDE FIRST 创建的。根据{{​​3}}:

  

点数被视为(经度,纬度)而不是相反,因为经度更接近于x轴的直观概念和y轴的纬度。

这是一个糟糕的设计......但事实就是如此。

答案 1 :(得分:2)

<@>运算符由earthdistance扩展名提供。 您需要在生产数据库上调用create extension earthdistance;

答案 2 :(得分:1)

您确定在线服务器上正确安装了postgis吗?

答案 3 :(得分:0)

错误点是存储过程中的select语句:

SELECT r.zip, point($2,$3) <@> point(g.lat, g.lon) AS distance
                            ^

在标记位置,需要操作员,但您的操作员未定义或具有其他参数类型。 Postgres本身只知道<@@>包含运算符。查看here的解释。

请你详细说明你想要达到的目标。

答案 4 :(得分:0)

您需要像这样限定<@>运算符:

OPERATOR(schema.<@>)

如果在搜索路径中未列出创建扩展的架构:

SHOW search_path;

https://www.postgresql.org/docs/current/ddl-schemas.html

答案 5 :(得分:0)

以下是解决此问题的步骤

  1. 运行以下命令两次,检查扩展名(cubeearthdistance
SELECT * FROM pg_available_extensions ORDER BY "name"
  1. 如果列表中仍显示两个扩展名(cubeearthdistance),请运行以下命令:
CREATE EXTENSION cube;
CREATE EXTENSION earthdistance;
  1. 创建表并插入值
  • 创建位置表
CREATE TABLE location (
    id serial PRIMARY KEY,
    name varchar(50) NOT NULL,
    longitude double precision NOT NULL,
    latitude double precision NOT NULL
);
  • 向我们插入一些数据(#CHA附近的站点)
INSERT INTO location(name, latitude, longitude)
VALUES ('SupplyHog HQ', 35.0472780, -85.3071590)
,('Chickamauga Dam', 35.0975557,-85.2197027)
,('Five Points Mtn Biking', 34.851249, -85.423983)
,('Harrison Bay State Park', 35.179631, -85.114359)
,('Mojo Burrito', 35.0094040,-85.3275640)
,('Rock Creek', 35.0556150,-85.2803290);
  1. 运行选择查询,您将获得结果
  • 创建点时,它是point(long, lat)
SELECT *, point(-85.3078294, 35.0609500) <@> point(longitude, latitude)::point as distance 
FROM location
WHERE (point(-85.3078294, 35.0609500) <@> point(longitude, latitude)) < 10
ORDER BY distance;