在postgresql中按经纬度查找最近的位置
find the nearest location by latitude and longitude in postgresql
您好,我正在尝试在 postgresql database.But 中按纬度和经度查找最近的位置,当我 运行 下面的查询显示列距离不存在时。
ERROR: column "distance" does not exist
LINE 1: ... ) ) ) AS distance FROM station_location HAVING distance <...
^
********** Error **********
ERROR: column "distance" does not exist
SQL state: 42703
Character: 218
CREATE TABLE station_location
(
id bigint NOT NULL DEFAULT nextval('location_id_seq'::regclass),
state_name character varying NOT NULL,
country_name character varying NOT NULL,
locality character varying NOT NULL,
created_date timestamp without time zone NOT NULL,
is_delete boolean NOT NULL DEFAULT false,
lat double precision,
lng double precision,
CONSTRAINT location_pkey PRIMARY KEY (id)
)
SELECT *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance
FROM station_location
HAVING distance < 5
ORDER BY distance
LIMIT 20;
select * from (
SELECT *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance
FROM station_location
) al
where distance < 5
ORDER BY distance
LIMIT 20;
请参阅 this gist,您将了解如何在 point
类型上声明域以及如何将 distance
运算符覆盖为 return 顺向距离。
声明从 point
继承的 latlong
类型:
CREATE DOMAIN latlong AS point CHECK (VALUE[0] BETWEEN -90.0 AND 90.0 AND VALUE[1] BETWEEN -180 AND 180);
以公里为单位的正向距离(地球半径球体上的距离):
CREATE OR REPLACE FUNCTION orthodromic_distance(latlong, latlong) RETURNS float AS $_$
SELECT acos(
sin(radians([0]))
*
sin(radians([0]))
+
cos(radians([0]))
*
cos(radians([0]))
*
cos(radians([1])
-
radians([1]))
) * 6370.0;
$_$ LANGUAGE sql IMMUTABLE;
在与 latlongs 一起使用时使用此函数覆盖距离运算符 <->
:
CREATE OPERATOR <-> ( PROCEDURE = orthodromic_distance
, LEFTARG = latlong, RIGHTARG = latlong
);
现在在您的 SQL 查询中,查找最近的实体:
WITH
station_distance AS (
SELECT
id AS station_id,
point(lat, long)::latlong <-> point(6.414478, 12.466646)::latlong AS distance
FROM station_location
WHERE NOT is_deleted
)
SELECT
sl.state_name,
sl.country_name,
sl.locality,
point(sl.lat, sl.long)::latlong AS coordinates,
sd.distance
FROM
station_location sl
JOIN station_distance sd
ON sd.station_id = sl.id
ORDER BY
distance ASC
LIMIT 10
您可能希望使用 latlong 类型将位置 lat
和 long
存储在同一字段中。
PostGIS
不要像这样在 table 上存储纬度和经度。而是使用 PostGIS 几何或 geography type.
CREATE EXTENSION postgis;
CREATE TABLE foo (
geog geography;
);
CREATE INDEX ON foo USING gist(geog);
INSERT INTO foo (geog)
VALUES (ST_MakePoint(x,y));
现在,当您需要查询它时,您可以使用 KNN (<->
),它实际上会在索引上执行此操作。
SELECT *
FROM foo
ORDER BY foo.geog <-> ST_MakePoint(x,y)::geography;
在您的查询中,您明确 HAVING distance < 5
。您也可以在索引上执行此操作。
SELECT *
FROM foo
WHERE ST_DWithin(foo.geog, ST_MakePoint(x,y)::geography, distance_in_meters)
ORDER BY foo.geog <-> ST_MakePoint(x,y)::geography;
这确保如果所有点都位于 distance_in_meters
之外,则不会返回任何内容。
另外x和y是十进制数ST_MakePoint(46.06, 14.505)
手册阐明:
An output column's name can be used to refer to the column's value in
ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses;
there you must write out the expression instead.
您可以使用 PostgreSQL 的 cube and earthdistance 扩展。
像这样启用它们:
CREATE EXTENSION cube;
CREATE EXTENSION earthdistance;
假设您当前的位置是 35.697933, 139.707318
。那么您的查询将是这样的:
SELECT *, point(35.697933, 139.707318) <@> (point(longitude, latitude)::point) as distance
FROM station_location
-- WHERE (point(35.697933, 139.707318) <@> point(longitude, latitude)) < 3
ORDER BY distance;
请注意 distance
以英里为单位(默认)。
您好,我正在尝试在 postgresql database.But 中按纬度和经度查找最近的位置,当我 运行 下面的查询显示列距离不存在时。
ERROR: column "distance" does not exist
LINE 1: ... ) ) ) AS distance FROM station_location HAVING distance <...
^
********** Error **********
ERROR: column "distance" does not exist
SQL state: 42703
Character: 218
CREATE TABLE station_location
(
id bigint NOT NULL DEFAULT nextval('location_id_seq'::regclass),
state_name character varying NOT NULL,
country_name character varying NOT NULL,
locality character varying NOT NULL,
created_date timestamp without time zone NOT NULL,
is_delete boolean NOT NULL DEFAULT false,
lat double precision,
lng double precision,
CONSTRAINT location_pkey PRIMARY KEY (id)
)
SELECT *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance
FROM station_location
HAVING distance < 5
ORDER BY distance
LIMIT 20;
select * from (
SELECT *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance
FROM station_location
) al
where distance < 5
ORDER BY distance
LIMIT 20;
请参阅 this gist,您将了解如何在 point
类型上声明域以及如何将 distance
运算符覆盖为 return 顺向距离。
声明从 point
继承的 latlong
类型:
CREATE DOMAIN latlong AS point CHECK (VALUE[0] BETWEEN -90.0 AND 90.0 AND VALUE[1] BETWEEN -180 AND 180);
以公里为单位的正向距离(地球半径球体上的距离):
CREATE OR REPLACE FUNCTION orthodromic_distance(latlong, latlong) RETURNS float AS $_$
SELECT acos(
sin(radians([0]))
*
sin(radians([0]))
+
cos(radians([0]))
*
cos(radians([0]))
*
cos(radians([1])
-
radians([1]))
) * 6370.0;
$_$ LANGUAGE sql IMMUTABLE;
在与 latlongs 一起使用时使用此函数覆盖距离运算符 <->
:
CREATE OPERATOR <-> ( PROCEDURE = orthodromic_distance
, LEFTARG = latlong, RIGHTARG = latlong
);
现在在您的 SQL 查询中,查找最近的实体:
WITH
station_distance AS (
SELECT
id AS station_id,
point(lat, long)::latlong <-> point(6.414478, 12.466646)::latlong AS distance
FROM station_location
WHERE NOT is_deleted
)
SELECT
sl.state_name,
sl.country_name,
sl.locality,
point(sl.lat, sl.long)::latlong AS coordinates,
sd.distance
FROM
station_location sl
JOIN station_distance sd
ON sd.station_id = sl.id
ORDER BY
distance ASC
LIMIT 10
您可能希望使用 latlong 类型将位置 lat
和 long
存储在同一字段中。
PostGIS
不要像这样在 table 上存储纬度和经度。而是使用 PostGIS 几何或 geography type.
CREATE EXTENSION postgis;
CREATE TABLE foo (
geog geography;
);
CREATE INDEX ON foo USING gist(geog);
INSERT INTO foo (geog)
VALUES (ST_MakePoint(x,y));
现在,当您需要查询它时,您可以使用 KNN (<->
),它实际上会在索引上执行此操作。
SELECT *
FROM foo
ORDER BY foo.geog <-> ST_MakePoint(x,y)::geography;
在您的查询中,您明确 HAVING distance < 5
。您也可以在索引上执行此操作。
SELECT *
FROM foo
WHERE ST_DWithin(foo.geog, ST_MakePoint(x,y)::geography, distance_in_meters)
ORDER BY foo.geog <-> ST_MakePoint(x,y)::geography;
这确保如果所有点都位于 distance_in_meters
之外,则不会返回任何内容。
另外x和y是十进制数ST_MakePoint(46.06, 14.505)
手册阐明:
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.
您可以使用 PostgreSQL 的 cube and earthdistance 扩展。
像这样启用它们:
CREATE EXTENSION cube;
CREATE EXTENSION earthdistance;
假设您当前的位置是 35.697933, 139.707318
。那么您的查询将是这样的:
SELECT *, point(35.697933, 139.707318) <@> (point(longitude, latitude)::point) as distance
FROM station_location
-- WHERE (point(35.697933, 139.707318) <@> point(longitude, latitude)) < 3
ORDER BY distance;
请注意 distance
以英里为单位(默认)。