ST_DIFFERENCE返回GeometryCollection而不是MultiPoint

时间:2016-04-13 20:23:26

标签: sql postgresql postgis

我试图弄清楚两个多点之间的区别。我使用查询location = ST_Difference(location, other_geo)执行此操作。这在结果不为空时有效,但是,如果两个多点完全相同,则生成的对象为GeometryCollection,而不是从ST_geomFromText('MULTIPOINT EMPTY')返回的空MultiPoint。如何将结果作为空的多点对象?

以下查询会产生多点:

SELECT ST_asGeoJSON(ST_Difference(ST_geomFromText('MultiPoint(1 2, 3 4)', 4326), ST_geomFromText('MultiPoint(1 2)', 4326)));

结果:{"type":"Point","coordinates":[3,4]}

这一个导致空GeometryCollection

SELECT ST_asGeoJSON(ST_Difference(ST_geomFromText('MultiPoint(1 2)', 4326), ST_geomFromText('MultiPoint(1 2)', 4326)));

结果:{"type":"GeometryCollection","geometries":[]}

1 个答案:

答案 0 :(得分:1)

尝试使用ST_Multi和ST_CollectionExtract始终返回零点或多点的MultiPoint几何体:

SELECT ST_AsGeoJSON(ST_Multi(ST_CollectionExtract(ST_Difference(a, b), 1)))
FROM (
  SELECT 'MultiPoint(1 2, 3 4)'::geometry a, 'MultiPoint(1 2)'::geometry b
  UNION SELECT 'MultiPoint(1 2)', 'MultiPoint(1 2)'
) data;
                st_asgeojson
---------------------------------------------
 {"type":"MultiPoint","coordinates":[]}
 {"type":"MultiPoint","coordinates":[[3,4]]}
(2 rows)