postgresql - Postgres Postgis ST_DWithin query is not accurate - Stack Overflow
I have the following table:
CREATE TABLE locations (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
location GEOMETRY(Point, 4326)
);
Now if I insert two:
INSERT INTO locations (name, location)
VALUES ('Berlin', ST_SetSRID(ST_MakePoint(13.405, 52.52), 4326));
INSERT INTO locations (name, location)
VALUES ('Krakow', ST_SetSRID(ST_MakePoint(19.945, 50.0647), 4326));
and use the following query:
select * from locations where ST_DWithin(location::geography, ST_GeographyFromText('SRID=4326;POINT(13.405 52.52)'), 531000);
I only get Berlin. The distance between them is only 530100
though. So even with 900 meters more, it is not returned. It is only returned when I change it to 531500
. But that's like 1400 meters from the original point away, which is far too much.
Am I doing something wrong here?
I have the following table:
CREATE TABLE locations (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
location GEOMETRY(Point, 4326)
);
Now if I insert two:
INSERT INTO locations (name, location)
VALUES ('Berlin', ST_SetSRID(ST_MakePoint(13.405, 52.52), 4326));
INSERT INTO locations (name, location)
VALUES ('Krakow', ST_SetSRID(ST_MakePoint(19.945, 50.0647), 4326));
and use the following query:
select * from locations where ST_DWithin(location::geography, ST_GeographyFromText('SRID=4326;POINT(13.405 52.52)'), 531000);
I only get Berlin. The distance between them is only 530100
though. So even with 900 meters more, it is not returned. It is only returned when I change it to 531500
. But that's like 1400 meters from the original point away, which is far too much.
Am I doing something wrong here?
Share Improve this question asked yesterday NiklasNiklas 25.3k35 gold badges135 silver badges183 bronze badges 3 |1 Answer
Reset to default 3It looks like the distance used by geography is the most accurate of the two.
From st_distance
doc:
For geography types defaults to return the minimum geodesic distance between two geographies in meters, compute on the spheroid determined by the SRID. If use_spheroid is false, a faster spherical calculation is used.
From st_distanceSphere
doc :
Returns minimum distance in meters between two lon/lat points. Uses a spherical earth and radius derived from the spheroid defined by the SRID. Faster than ST_DistanceSpheroid, but less accurate
Comparing the computed distances using either a sphere or the more accurate spheroid, we confirm that st_dwithin
also use the distance computed on the spheroid. You can still force st_dwithin
to use a sphere instead of a spheroid, but it is less accurate.
SELECT st_distance(Berlin::geography,Krakow::geography) as dist_geog,
st_distance(Berlin::geography,Krakow::geography, false) as dist_geog_sphere,
st_distanceSphere(Berlin,Krakow) as dist_sphere,
st_distanceSpheroid(Berlin,Krakow) as dist_spheroid,
ST_DWithin(Berlin::geography,Krakow::geography,530200, false) dwithin_sphere
FROM geo;
dist_geog | dist_geog_sphere | dist_sphere | dist_spheroid | dwithin_sphere
-----------------+------------------+-----------------+-------------------+----------------
531430.81283859 | 530123.37723304 | 530123.37723304 | 531430.8128385914 | t
(1 row)
- 忘记安卓电脑 ChromeWin双系统PC更有前途
- 微软答疑Surface平板:32GB版可用空间20GB
- 2012年NEC云时代平台软件全国巡展启动
- Google的AR眼镜
- 分析:Windows 8平板电脑称雄市场需三大因素
- anaconda - Trouble using fiona in conda environment: ImportError: DLL load failed while importing _env - Stack Overflow
- Python Selenium Error - Sandbox cannot access executable - Stack Overflow
- How to import a svelte.ts component from another app where the import is with $lib - Stack Overflow
- Fixing Crashing on load of 3D model on React Native - Stack Overflow
- c# - After debugging, Visual Studio 2022 holding on to .NET Core program's .dll and I can't complete next build
- VS Code extension for collapsing sub-folders - Stack Overflow
- regex - Change text block with nearest search pattern - Stack Overflow
- c# - What could be wrong with the Import method on the oSets object here? - Stack Overflow
- javascript - Firebase Auth link - Problem with the Google login, no possibility to change to own project name - Stack Overflow
- sql - How to add constraint if not exists - Stack Overflow
- zip - Download and unzip file from URL with Github Action - Stack Overflow
- Make property of python class dependent on external variable? - Stack Overflow
acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon2-lon1))*6371
– Niklas Commented yesterday