2

I have 4 vertices and I need to get the bounding box something like the one in the image below. A, B, C and D are the vertices.

The red area is the one which I need to eliminate while I only need to retain the green area.

Maybe, I would need ST_Intersects, but I'm not sure how do I go about using this for the mentioned use.

NOTE I will need the points A, B, C and D as part of the green area, I do not want to eliminate them, I only need to get rid of the center part which is in red.

enter image description here

1

So I will assume, the green box is a full box and you want to clip out an area based on four input coordinates. You just need to create a polygon from the points and run a difference.

So:

SELECT ST_Difference(green.geom, (
ST_MakePolygon(ST_MakeLine(
ST_MakePoint(a.x_coord, a.y_coord),
ST_MakePoint(b.x_coord, b.y_coord),
ST_MakePoint(c.x_coord, c.y_coord),
ST_MakePoint(d.x_coord, d.y_coord),
ST_MakePoint(a.x_coord, a.y_coord),
))
)) from green

It could be simplified depending how your data is stored.

*.*_coord should be the coordinate itself, so in a numeric format.

  • 1
    it is good practice to wrap the st_difference function in the coalesce function in case there is null values in one of the geometries. but your query should be fine for this example – ziggy Aug 15 '17 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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