1

I've created a function called 'ST_LineFishnet' which makes a grid network of line string geometry. I wanted to split a polygon shapefile with my grid. I've just used the first grid as "LINESTRING(0 0,0 10,10 10,10 0,0 0)" to split my shapefile according to this snippet code, but the problem is it always return geometry even two geometries don't have any intersection.

How can I check if my grid intersects with polygon before split function?

SELECT ST_AsEWKT(wkt) As result FROM 
(SELECT ST_AsText((ST_Dump(ST_Split(circle, line))).geom) As wkt
FROM (SELECT
     (SELECT ST_AsEWKT(geom) FROM (
     SELECT geom FROM ST_LineFishnet(4,6,10,10) As cells WHERE col=1 AND row=1) As koo) As line,
     (SELECT ST_AsEWKT(geo) FROM (SELECT geom As geo FROM block WHERE gid = 1) As k) As circle) As foo) As m;
  • 1
    you seem to like subqueries and unnecessary conversions – pLumo Jul 4 '17 at 14:27
0

I tried to understand your code, eliminated all unnecessary subqueries and conversions.

Then I simply added a join between the circle and the line queries with ST_INTERSECTS() as condition:

SELECT
    ST_AsEWKT((ST_Dump(ST_Split(circle.geom, line.geom))).geom) As result
FROM
    ( SELECT geom FROM block WHERE gid = 1 ) as circle
JOIN
    ( SELECT geom FROM ST_LineFishnet(4,6,10,10) As cells WHERE col=1 AND row=1 ) as line
ON ST_INTERSECTS(line.geom, circle.geom)

or maybe even easier:

SELECT
    ST_AsEWKT((ST_Dump(ST_Split(circle.geom, line.geom))).geom) As result

FROM
    block as circle,
    ST_LineFishnet(4,6,10,10) As line

WHERE circle.gid = 1
AND line.col = 1
AND line.row = 1
AND ST_INTERSECTS(line.geom, circle.geom)

As I'm not really sure what the output of your ST_LineFishnet function is, this might not work out of the box.

  • thank you for simplification. It works with one little change like this: ST_AsEWKT((ST_Dump(ST_Split(circle.geom, line.geom))).geom) As result. It needs an extra parenthesis. Thanks again :) – nasser tahani Jul 4 '17 at 15:15

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.