4

This question already has an answer here:

I'm trying to use the postgis's st_difference() function, but with no luck.

I have a layer of polygons A

Pol A

And a layer of Polygons B

Pol B

enter image description here

What I want to achieve is this (created with QGIS algorithm): enter image description here

But with postGIS, what I get is this with more than 40 geometries:

select          
        ST_Difference("polA"."geom","polB"."geom") geom,
        row_number() over() as gid

    from 
        "public"."polA",
        "public"."polB"

enter image description here

I tried to do it in several PostGIS versions in different Operative systems, but always with the same result. I'm doing something wrong, but still I don't know what is it. Anyone could help me in this?

If you need more information, just ask, I'll add as fast as I can.


EDIT EPSG: 25830

Pol A:

POLYGON((728657.64100211 4381544.72549744,728640.581502109 4381505.17549744,728603.972502107 4381521.85549744,728620.222002108 4381560.74549744,728657.64100211 4381544.72549744))
POLYGON((728607.472002108 4381679.13549744,728613.432002108 4381691.38599744,728613.792002108 4381692.12599744,728616.872002108 4381698.46599744,728620.372002108 4381705.65599744,728623.891502109 4381712.90599744,728633.211502109 4381709.04599744,728636.591502109 4381707.64599744,728647.92100211 4381702.94599744,728645.09100211 4381696.66599744,728640.001502109 4381685.40599744,728638.051502109 4381680.87599744,728640.871502109 4381679.51599744,728646.20100211 4381676.97599744,728648.85100211 4381675.71599744,728650.90100211 4381674.74599744,728653.78100211 4381673.37599744,728662.13050211 4381669.40599744,728655.40100211 4381655.94599744,728655.14100211 4381655.83599744,728654.84100211 4381655.97599744,728649.68100211 4381658.46599744,728647.05100211 4381659.72599744,728639.451502109 4381663.37599744,728632.811502109 4381666.56549744,728629.511502109 4381668.15549744,728626.281502109 4381669.70549744,728626.151502109 4381669.76549744,728619.572002108 4381672.92549744,728613.922002108 4381675.64549744,728613.402002108 4381675.89549744,728611.112002108 4381676.99549744,728607.542002108 4381678.71549744,728607.472002108 4381679.13549744))
POLYGON((728657.01100211 4381699.24599744,728666.120502111 4381695.55599744,728659.31100211 4381680.36599744,728650.37100211 4381684.40599744,728657.01100211 4381699.24599744))
POLYGON((728636.591502109 4381715.88599744,728627.301502109 4381719.71599744,728629.861502109 4381725.03599744,728632.181502109 4381729.84599744,728648.58100211 4381723.25599744,728655.70050211 4381720.39599744,728674.190002111 4381712.83599744,728676.300002111 4381711.93649744,728685.260002112 4381708.11649744,728695.089502112 4381704.08649744,728696.729502112 4381703.44649743,728692.530002112 4381694.85599744,728680.730002111 4381670.32599744,728676.680502111 4381662.73599744,728670.130502111 4381648.66599744,728664.930502111 4381651.14599744,728659.59100211 4381653.68599744,728659.54100211 4381653.82599744,728666.130502111 4381667.62599744,728669.950502111 4381675.63599744,728666.010502111 4381677.38599744,728676.260002111 4381699.60599744,728669.730502111 4381702.21599744,728660.09050211 4381706.06599744,728651.02100211 4381709.94599744,728644.121002109 4381712.78599744,728641.681002109 4381713.79599744,728636.591502109 4381715.88599744))
POLYGON((728577.323002106 4381693.22549744,728572.253002106 4381695.76549744,728572.103002106 4381696.01549744,728575.553002106 4381704.57549744,728577.193002106 4381708.64549744,728578.733002106 4381712.48599744,728581.683002106 4381719.80599744,728583.913002107 4381725.34599744,728586.942502107 4381732.87599744,728592.362502107 4381746.32599744,728623.661502109 4381732.72599744,728617.222002108 4381719.58599744,728617.162002108 4381719.46599744,728613.682002108 4381712.56599744,728610.892002108 4381707.01599744,728613.412002108 4381705.91599744,728610.132002108 4381699.16599744,728609.872002108 4381698.61599744,728608.112002108 4381695.00599744,728607.282002108 4381693.29549744,728605.832502108 4381690.30549744,728601.582502107 4381681.54549744,728601.332502107 4381681.22549744,728595.252502107 4381684.26549744,728587.193002107 4381688.29549744,728583.633002107 4381690.07549744,728582.423002106 4381690.67549744,728582.123002107 4381690.82549744,728577.593002106 4381693.09549744,728577.323002106 4381693.22549744))
POLYGON((728675.100502111 4381584.11599744,728672.480502111 4381577.96599744,728663.38050211 4381557.42599744,728643.711002109 4381565.90549744,728625.561502109 4381573.73549744,728636.831502109 4381600.04549744,728636.931502109 4381600.27549744,728640.761502109 4381609.29549744,728645.74100211 4381621.04599744,728647.97100211 4381626.08599744,728647.36100211 4381626.36599744,728647.52100211 4381626.74599744,728656.51100211 4381646.61599744,728667.860502111 4381641.21599744,728678.660002111 4381636.07599744,728694.100002112 4381628.72599744,728675.100502111 4381584.11599744))
POLYGON((728535.974502104 4381547.26499745,728528.834502104 4381550.40499745,728526.004502104 4381553.23499745,728515.705002103 4381557.59499745,728519.764502103 4381567.61499745,728521.824502103 4381572.71499745,728524.084502104 4381578.29499745,728527.224502104 4381586.04499745,728530.364502104 4381593.67499745,728533.604502104 4381601.54499745,728544.314002105 4381597.05499745,728548.944002105 4381595.11499745,728553.854002105 4381593.05499745,728552.704002105 4381590.22499745,728570.273502106 4381582.54549744,728586.703002107 4381575.41549744,728602.432502107 4381568.53549744,728599.232502107 4381561.19549744,728595.782502107 4381553.25549744,728592.912502107 4381546.62549744,728589.702502107 4381539.17549744,728588.953002107 4381539.11549744,728573.983002106 4381545.86549745,728557.483502105 4381553.29499744,728553.273502105 4381543.66549745,728537.353002104 4381550.47549745,728535.974502104 4381547.26499745))
POLYGON((728622.622002108 4381617.08549744,728620.392002108 4381611.69549744,728616.072002108 4381601.80549744,728611.572002108 4381591.57549744,728607.222002108 4381581.62549744,728574.653002106 4381595.85549744,728557.963502105 4381603.14549745,728553.764002105 4381604.85499745,728549.404002105 4381606.63499745,728546.554002105 4381607.79499745,728537.664002104 4381611.61499745,728540.214002104 4381617.85499745,728542.334002104 4381623.04499745,728545.524002105 4381630.82499745,728547.464002105 4381635.57549745,728551.194002105 4381644.69549745,728554.873502105 4381653.69549745,728557.763502105 4381660.77549744,728559.843502105 4381665.86549745,728562.313502106 4381671.92549745,728565.943502106 4381680.81549744,728568.973502106 4381688.25549745,728569.153502106 4381688.35549744,728577.823002106 4381684.25549744,728581.973002106 4381682.28549744,728586.073002107 4381680.33549744,728589.313002107 4381678.79549744,728593.922502107 4381676.61549744,728599.642502107 4381673.90549744,728604.372502108 4381671.63549744,728614.292002108 4381666.87549744,728619.052002108 4381664.59549744,728624.072002109 4381662.18549744,728634.901502109 4381656.98549744,728634.801502109 4381656.74549744,728635.471502109 4381656.50549744,728645.23100211 4381652.03599744,728645.74100211 4381651.77599744,728634.641502109 4381625.87549744,728631.511502109 4381618.57549744,728620.682002108 4381623.03549744,728613.311502108 4381626.07249744,728611.362002108 4381626.87549744,728606.322502108 4381628.94549744,728601.282502107 4381631.02549744,728596.192502107 4381633.11549744,728591.782502107 4381634.93549744,728590.132502107 4381631.18549744,728593.182502107 4381629.91549744,728611.632002108 4381621.87549744,728622.622002108 4381617.08549744))

Pol B:

POLYGON((728657.64100211 4381544.72549744,728640.581502109 4381505.17549744,728603.972502107 4381521.85549744,728620.222002108 4381560.74549744,728657.64100211 4381544.72549744))
POLYGON((728675.100502111 4381584.11599744,728672.480502111 4381577.96599744,728663.38050211 4381557.42599744,728643.711002109 4381565.90549744,728625.561502109 4381573.73549744,728636.831502109 4381600.04549744,728636.931502109 4381600.27549744,728640.761502109 4381609.29549744,728645.74100211 4381621.04599744,728647.97100211 4381626.08599744,728647.36100211 4381626.36599744,728647.52100211 4381626.74599744,728656.51100211 4381646.61599744,728667.860502111 4381641.21599744,728678.660002111 4381636.07599744,728694.100002112 4381628.72599744,728675.100502111 4381584.11599744))
POLYGON((728573.983002106 4381545.86549745,728557.483502105 4381553.29499744,728554.833502105 4381547.23499745,728554.279002105 4381545.96599745,728553.273502105 4381543.66549745,728537.353002104 4381550.47549745,728538.894502104 4381554.06499745,728540.844002104 4381558.60499745,728540.414002104 4381558.78499745,728541.324002104 4381561.09499745,728542.484002104 4381564.03499745,728544.640002105 4381569.86999745,728547.524002105 4381577.64499745,728549.653002105 4381582.81549745,728550.604002105 4381585.12499745,728551.213502105 4381586.60499745,728552.704002105 4381590.22499745,728570.273502106 4381582.54549744,728586.703002107 4381575.41549744,728583.583002106 4381568.17549744,728580.063002106 4381559.99549744,728577.253002106 4381553.45549744,728573.983002106 4381545.86549745))
POLYGON((728611.572002108 4381591.57549744,728607.222002108 4381581.62549744,728574.653002106 4381595.85549744,728579.003002106 4381605.80549744,728581.263002106 4381610.94549744,728583.493002106 4381616.03549744,728587.823002107 4381625.92549744,728590.132502107 4381631.18549744,728593.182502107 4381629.91549744,728611.632002108 4381621.87549744,728622.622002108 4381617.08549744,728620.392002108 4381611.69549744,728616.072002108 4381601.80549744,728611.572002108 4381591.57549744))
POLYGON((728551.194002105 4381644.69549745,728554.873502105 4381653.69549745,728557.763502105 4381660.77549744,728559.843502105 4381665.86549745,728562.313502106 4381671.92549745,728566.043502106 4381670.26549745,728570.853502106 4381668.12549744,728573.513002106 4381666.94549744,728576.543002106 4381665.59549744,728580.643002106 4381663.77549744,728582.923002106 4381662.75549744,728587.253002107 4381660.83549744,728592.782502107 4381658.37549744,728596.522502107 4381652.98549744,728606.302502108 4381648.93549744,728608.312002108 4381648.10549744,728601.282502107 4381631.02549744,728596.192502107 4381633.11549744,728591.782502107 4381634.93549744,728590.132502107 4381631.18549744,728584.683002106 4381633.44549744,728584.893002107 4381633.95549744,728581.333002106 4381638.40549744,728580.793002106 4381639.09549744,728578.263002106 4381640.23549744,728574.473002106 4381641.92549744,728573.183002106 4381638.78549744,728572.643002106 4381637.46549744,728571.703502106 4381635.17549745,728568.173502106 4381626.67549745,728565.803502106 4381627.69549744,728559.633502105 4381630.34549744,728556.273502105 4381631.78549745,728551.074002105 4381634.02549744,728547.464002105 4381635.57549745,728551.194002105 4381644.69549745))

marked as duplicate by mgri, nmtoken, Kersten, John Powell, Evil Genius Jul 8 '17 at 11:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Can you upload the geometries or dump them as text into your question. – Evan Carroll Jul 7 '17 at 16:47
  • geometries added! – Luismi Jul 7 '17 at 17:07
  • does those geometries work for you when you give those strings to ST_GeomFromText()? – Evan Carroll Jul 7 '17 at 17:30
  • 1
    Use ST_AsEWKT() to dump the geometries so we can load them. – Evan Carroll Jul 7 '17 at 17:49
  • 1
    Look at this answer. You must combine the geometries else you are doing st_difference over a complete cross join, and most of these features are not overlapping each other – JGH Jul 8 '17 at 8:34