7

I'm experimenting a result issue using the ST_Difference spatial function in PostGIS.

Input :

Feature 1 and 2 overlaps each other. Both contains 5 vertices, like a rectangle (4 plus an additional one to close the polygon).

I want to get the geometry difference between feature 2 and feature 1. (st_difference(geom(feature2),geom(feature1))

input

Result :

After executing the function, the result of geom(feature2)-geom(feature1), in green, contains unexpected residual vertices (highlighted), inside feature 1 :

difference

Result only :

result

Anyone knows how to avoid this issue, or how to get a "cleaned" difference of 2 input polygons?


The input data is Polygon, the result (after st_difference) is multipolygon. I get the same result with Polygon geometry for the result.

Input : enter image description here

Result :

enter image description here

Edit 2 :

Input polygon geometry :

Feature 1 :

"POLYGON((363011.683640322 5784798.67947383,363061.369728361 5784794.01684973,363060.058365334 5784774.34640432,363009.64374228 5784780.75751245,363011.683640322 5784798.67947383))"

Feature 2 :

"POLYGON((363079.583103742 5784786.58579258,363079.583103742 5784777.55195839,363053.501550197 5784780.32039144,363054.230085212 5784789.06281163,363079.583103742 5784786.58579258))"

Result of st_difference(feature2(geom),feature1(geom)) :

MULTIPOLYGON(((363079.583103742 5784786.58579258,363079.583103742 5784777.55195839,363053.501550197 5784780.32039144,363060.407760381 5784779.58733003,363060.995393925 5784788.40183319,363054.230085212 5784789.06281163,363079.583103742 5784786.58579258)))

As shown on the screenshot, the result contains 7 vertices (instead of 5 as expected).

CRS : 28355

7
  • Can you share the data needed to reproduce this? Commented Jul 4, 2018 at 9:19
  • It seems to me like your geometries are not actually polygons, but just their boundaries. Can you confirm this ? If it's true, you can run a ST_MakePolygon, or ST_Polygonize before you do the differences.
    – GHRF
    Commented Jul 4, 2018 at 9:23
  • I just add an edit with data screenshots. I'm happy to share the data too. Should I upload the shp file and add a hyperlink on the post?
    – Olivier
    Commented Jul 4, 2018 at 10:11
  • 3
    Easiest is to just post the result of st_astext(geom). If it is only 2 polygons with 5 vertex points that is the best. Commented Jul 4, 2018 at 10:56
  • 1
    WITH geoms(g1, g2) AS (VALUES(ST_GeomFromText('POLYGON((363011.683640322 5784798.67947383,363061.369728361 5784794.01684973,363060.058365334 5784774.34640432,363009.64374228 5784780.75751245,363011.683640322 5784798.67947383))'), ST_GeomFromText('POLYGON((363079.583103742 5784786.58579258,363079.583103742 5784777.55195839,363053.501550197 5784780.32039144,363054.230085212 5784789.06281163,363079.583103742 5784786.58579258))'))) SELECT ST_NPoints(ST_Difference(g2, g1)) from geoms; does indeed return a polygon with 5 vertices. Commented Jul 4, 2018 at 16:39

1 Answer 1

0

The answer was given in a comment. Issue must have been in some old PostGIS version. Like PostGIS 2.4 five years ago, PostGIS 3.4 returns a polygon with 5 vertices. The computed vertices differ a bit, though.

select ST_AsText(
ST_Difference(
ST_GeomFromText(
'POLYGON((363079.583103742 5784786.58579258,363079.583103742 5784777.55195839,363053.501550197 5784780.32039144,363054.230085212 5784789.06281163,363079.583103742 5784786.58579258))'
),ST_GeomFromText(
'POLYGON((363011.683640322 5784798.67947383,363061.369728361 5784794.01684973,363060.058365334 5784774.34640432,363009.64374228 5784780.75751245,363011.683640322 5784798.67947383))'
)));

Output:

POLYGON((363079.583103742 5784777.55195839,363060.407760381 5784779.587330026,363060.9953939253 5784788.401833192,363079.583103742 5784786.58579258,363079.583103742 5784777.55195839))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.