2

I am using the ST_Difference to erase multipolygon feature by a mask. However, the query returns a dssolved multipolygon.

The query is the following:

'''SELECT ST_AsGeoJSON(ST_Collect(geom))::json
   FROM (SELECT (
     ST_Dump (
         ST_Difference(ST_GeomFromGeoJSON('%s'),ST_GeomFromGeoJSON('%s')))).geom)
             As f''' % (feature,mask)

Please take a look at those images to understand the problem:

Input: enter image description here

Dissolved output: enter image description here

6
  • You could try breaking the Multi Polygon into its components first, do the difference on each piece and then aggregate them back together
    – MickyT
    Sep 4, 2014 at 19:05
  • Is it possible to do this in a unique query? Sep 4, 2014 at 19:07
  • Possibly, but I don't have postGIS to try it on. I'll see if I can mock up a pseudo query for you.
    – MickyT
    Sep 4, 2014 at 19:14
  • Have got postgis now and tested the following query
    – MickyT
    Sep 4, 2014 at 20:47
  • 1
    Keep in mind that your MultiPolygon is invalid, thus will break a few assumptions.
    – Mike T
    Sep 4, 2014 at 22:47

1 Answer 1

2

The general process is use ST_DUMP to break the geometry into parts. Apply a ST_DIFFERENCE to each part and then collect it back together with ST_COLLECT. I've tested this with PostGIS 2.0.1

SELECT ST_AsGeoJSON(ST_MULTI(ST_COLLECT(d.Geom)))::json
FROM (
    SELECT ST_DIFFERENCE(
        (P.Parts).geom,
        ST_GeometryFromText('POLYGON ((25 75, 75 75, 75 125, 25 125, 25 75))', 0)
        ) As Geom
    FROM (SELECT ST_DUMP(
        ST_GeometryFromText('MULTIPOLYGON (((0 0, 50 0, 50 50, 0 50, 0 0)),((0 50, 50 50, 50 100, 0 100, 0 50)))', 0)
        ) AS Parts) AS P
    ) AS D;

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.