postgres 返回的结果不正确

Incorrect results returned by postgres

我在 posgresql 9.6 中 运行 以下命令:

./bin/createdb testSpatial
./bin/psql -d testSpatial -c "CREATE EXTENSION postgis;"
create table test(name character varying(250), lat_long character varying(90250), the_geom geometry);
\copy test(name,lat_long) FROM 'test.csv' DELIMITERS E'\t' CSV HEADER;
CREATE INDEX spatial_gist_index ON test USING gist (the_geom );
UPDATE test SET the_geom = ST_GeomFromText(lat_long,4326);

在 运行 上:select * from test; 我得到以下输出:

name |                                                                                          lat_long                                       
                                                   |                                                                                            
                                                  the_geom                                                                                      

------+-----------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------+--------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------
 A    | POLYGON((-0.061225 -128.427791,-0.059107 -128.428264,-0.056311 -128.428911,-0.054208 -128.426510,-0.055431 -128.426324,-0.057363 -128.42
6124,-0.059315 -128.425843,-0.061225 -128.427791)) | 0103000020E61000000100000008000000D42B6519E258AFBFBE50C076B00D60C07DE9EDCF4543AEBFBC41B456B
40D60C08063CF9ECBD4ACBFA1BC8FA3B90D60C07BF65CA626C1ABBF58AD4CF8A50D60C0BF805EB87361ACBFFFAF3A72A40D60C0B83A00E2AE5EADBF4D81CCCEA20D60C01F1153228
95EAEBF60C77F81A00D60C0D42B6519E258AFBFBE50C076B00D60C0
 B    | POINT(1.978165 -128.639779)                                                                                                             
                                                   | 0101000020E61000002D78D15790A6FF3F5D35CF11791460C0
(2 rows)

在此之后我 运行 查询:找到彼此相距 5 米以内的所有 "name"。为此,我编写了以下命令。

testSpatial=# select s1.name, s2.name from test s1, test s2 where ST_DWithin(s1.the_geom, s2.the_geom, 5);
 name | name 
------+------
 A    | A
 A    | B
 B    | A
 B    | B
(4 rows)

令我惊讶的是,我得到的输出不正确,因为 "A" 和 "B" 彼此相距 227.301 公里(此处使用半正弦距离计算:http://andrew.hedges.name/experiments/haversine/)。有人可以帮助我了解我哪里出错了。

您已按如下方式定义几何体

 the_geom geometry

也就是说,这不是地理。但是 ST_DWithin docs

For Geometries: The distance is specified in units defined by the spatial reference system of the geometries. For this function to make sense, the source geometries must both be of the same coordinate projection, having the same SRID.

For geography units are in meters and measurement is defaulted to use_spheroid=true, for faster check, use_spheroid=false to measure along sphere.

所以你实际上是在搜索彼此之间 5 度以内的地方。度数大约等于 111 公里,因此您正在寻找彼此相距约 550 公里而不是 5 米的地方。

此外,将 POINT(1.978165 -128.639779) 之类的字符串存储在 table 中没有多大意义。这完全是多余的。这些信息可以很容易地从地理列中生成。