I have a grid line table to use as the blade for splitting my polygon table in PostGIS with a ST_Split method. It works fine but now I want to find which parts of polygons are located within my grid.I thought about using ST_Centroid to find the centroid of split parts and then applying ST_Within. My question is: can I apply these two steps in one SQL query or should I compute centroid first and then use ST_whitin? Or is there anything else to do this? Maybe something like this:

    FROM gridpoly) as center
    ( SELECT geom FROM gridline As cells WHERE col=1 AND row=2 ) as line
ON ST_Within(center.geom,line.geom)

The gridpoly table is the result of the split function which contains polygons. But in this code I don't have any column as 'center'.

2 Answers 2


To refer to the centroid geometry, you must name it otherwise the column is called st_centroid

st_centroid(gridpoly.geom) as geom

Note that you can't select gridpoly.* because gridpoly is not in the from clause. You can only use center.* and/or line.*. To get access to the gridpoly data, you need to join it. Maybe fetch the gridpolyID at the same time as the centroid so you can join on it.

At last, the query will likely return nothing as the polygon centroid is typically not within the cut line. It would work if you have cells (as polygons) instead of lines.

  • Thanks, @JGH. You are right, the result of ST_Whitin is always false. So how can I find polygons in my grid? Commented Jul 6, 2017 at 13:44
  • You can use ST_Envelope(ST_Collect(line.geom)) to create a polygon with the bounding box of your lines. This should work if they form a regular closed grid. Otherwise, look at ST_Polygonize().
    – amball
    Commented Jul 9, 2017 at 0:43

To find which polygon sections are within my grid line, I created another grid with polygon type and then used them to check polygon contained in my new grid network.

    ( SELECT * from gridpolygon WHERE col=1 AND row=2 ) as line
ON ST_Within(gridpoly.result, line.geom)

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.