[postgis-users] Question about st_difference

Nicolas Ribot nicolas.ribot at gmail.com
Wed Sep 7 08:22:15 PDT 2011


> Hello Group,
>
> About st_difference I've seen several topics but unfortunately with none of them I could solve my problem.
>
> What I want to do is subtract one layer from the other. I could simplify this down to the following;
>
> Tbl_a contains 1 record
> "POLYGON(((168119.443682473 451093.811197312,197555.469699649 451093.811197312,197555.469699649 437539.54805452,168119.443682473 437539.54805452,168119.443682473 437539.54805452,168119.443682473 451093.811197312)))"
>
> Tbl_b contains 2 records of which both intersects with the object in tbl_a
>
> "POLYGON(((171424.484910418 453680.34544058,171424.484910418 435697.56759188,174568.140889894 435697.56759188,174568.140889894 453680.34544058,174568.140889894 453680.34544058,171424.484910418 453680.34544058)))"
>
> "POLYGON(((184904.90800885 453627.063135843,184904.90800885 435644.285287143,188048.563988327 435644.285287143,188048.563988327 453627.063135843,188048.563988327 453627.063135843,184904.90800885 453627.063135843)))"
>
> The result I'm looking for is the object of tbl_a abstracted with both objects from tbl_b, since they are both intersecting. In this example the result should be 3 squares since it is cut twice by tbl_b.
>
>
> Does anyone know a solution for this?
>
> Many thanks in advance,
>
> Ge

Hi Ge,

You may want to union objects in table B to create a single polygon,
then perfom the difference:


select st_difference(a.geom, b.geom)
from
(select 'POLYGON((168119.443682473 451093.811197312,197555.469699649
451093.811197312,197555.469699649 437539.54805452,168119.443682473
437539.54805452,168119.443682473 437539.54805452,168119.443682473
451093.811197312))'::geometry as geom) as a,
(
select st_union(geom) as geom
from (
	select 'POLYGON((171424.484910418 453680.34544058,171424.484910418
435697.56759188,174568.140889894 435697.56759188,174568.140889894
453680.34544058,174568.140889894 453680.34544058,171424.484910418
453680.34544058))'::geometry as geom
	UNION
	select 'POLYGON((184904.90800885 453627.063135843,184904.90800885
435644.285287143,188048.563988327 435644.285287143,188048.563988327
453627.063135843,188048.563988327 453627.063135843,184904.90800885
453627.063135843))'::geometry as geom
) as foo
) as b;

(see attached pictures)

Nicolas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen shot 2011-09-07 at 5.21.36 PM.png
Type: image/png
Size: 18193 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20110907/4cc2f2a7/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen shot 2011-09-07 at 5.21.51 PM.png
Type: image/png
Size: 22419 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20110907/4cc2f2a7/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen shot 2011-09-07 at 5.22.02 PM.png
Type: image/png
Size: 21770 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20110907/4cc2f2a7/attachment-0002.png>


More information about the postgis-users mailing list