我想找出一些东西,我在postgres 9.6上使用postgis 2.3
地理位置上的ST_intersects 会返回false,尽管两个地理位置相交。 为了调试,我尝试使用 Geometry 进行相同的查询,并且两个相交。
有人可以帮我解释一下吗?
示例:
我正在运行此查询:
select ST_intersects(ST_GeogFromText('POLYGON((12.856 41.890,12.856 41.935,12.811 41.935,12.811 41.890,12.856 41.890))'), ST_GeogFromText('POLYGON((-170 -47,174 -47,174 77,-170 77,-170 -47))')) from table limit 1;
我明白了:
st_intersects
---------------
f
(1 row)
当我跑步时:
select ST_intersects(ST_GeomFromText('POLYGON((12.856 41.890,12.856 41.935,12.811 41.935,12.811 41.890,12.856 41.890))'), ST_GeomFromText('POLYGON((-170 -47,174 -47,174 77,-170 77,-170 -47))')) from table limit 1;
我明白了:
st_intersects
---------------
t
(1 row)
答案 0 :(得分:0)
ST_Intersects -- returns True if Geometries/Geography spatially intersect in 2D
。如果您从POLYGON
(在您的情况下使用5个顶点)转换为LINESTRING
,则您的查询应返回相同的结果。测试:
SELECT
ST_Intersects(
ST_GeomFromText('SRID=4326;LINESTRING(12.856 41.890,12.856 41.935,12.811 41.935,12.811 41.890,12.856 41.890)'),
ST_GeomFromText('SRID=4326;LINESTRING(-170 -47,174 -47,174 77,-170 77,-170 -47)'));
SELECT
ST_Intersects(
ST_GeogFromText('SRID=4326; LINESTRING(12.856 41.890,12.856 41.935,12.811 41.935,12.811 41.890,12.856 41.890)'),
ST_GeogFromText('SRID=4326; LINESTRING(-170 -47,174 -47,174 77,-170 77,-170 -47)'));
更新::
实际上,Polygon
使用ST_Intersects
就好了。我意识到Linestring
不是你想要的 - 返回false,因为一个完全在另一个里面。问题是这两个多边形在球面坐标系中相距很远。检查:
SELECT
ST_Distance(
ST_GeomFromText('SRID=4326;POLYGON((12.856 41.890,12.856 41.935,12.811 41.935,12.811 41.890,12.856 41.890))'),
ST_GeomFromText('SRID=4326;POLYGON((-170 -47,174 -47,174 77,-170 77,-170 -47))'));
==> 0
SELECT
ST_Distance(
ST_GeogFromText('SRID=4326;POLYGON((12.856 41.890,12.856 41.935,12.811 41.935,12.811 41.890,12.856 41.890))'),
ST_GeogFromText('SRID=4326;POLYGON((-170 -47,174 -47,174 77,-170 77,-170 -47))'));
==> 6743507.32
您当然可以按如下方式将geography
转换为geometry
类型以获得所需的输出:
SELECT
ST_Intersects(
ST_GeogFromText('SRID=4326;POLYGON((12.856 41.890,12.856 41.935,12.811 41.935,12.811 41.890,12.856 41.890))')::geometry,
ST_GeogFromText('SRID=4326;POLYGON((-170 -47,174 -47,174 77,-170 77,-170 -47))')::geometry);