I need to split polygons (layer 'pol') by both closed and non-closed linestrings (layer 'lin').

enter image description here enter image description here

Unfortunately I don't get proper results running the following query.

CREATE VIEW splitted_pol AS
SELECT 
    g.path[1] as gid, 
    g.geom::geometry(polygon, SRID) as geom 
FROM
    (SELECT 
    (ST_Dump(ST_Split(pol.geom, lin.geom))).* 
    FROM pol, lin
) as g;

In my example ST_Split should create six polygons (layer 'splitted_pol').

enter image description here enter image description here Does anybody know how to use ST_Split with QGIS/PostGIS?

  • You need to feed the the geoms recursively to ST_Split. – Jakub Kania May 11 '15 at 13:07
  • Can you please help me with the SQL query? I'm new to PostGIS. – Lunar Sea May 14 '15 at 14:19
  • What exactly does your input layers contain? I see layer 'pol' with a red square and a triangle polygon and layer 'lin' with only one dark blue vertical line. And why do you expect 6 polygons? I whould not take the "border" into account. – Stefan Jul 12 '15 at 1:03
  • I've added a screenshot to illustrate the layers. – Lunar Sea Jul 12 '15 at 7:39
  • Any chance you could add the input geometries? – John Powell Jul 12 '15 at 7:54
up vote 4 down vote accepted
+50

You could create function something like this:

create or replace function ST_MultiSplit(geom Geometry, blades Geometry) RETURNS Geometry AS $$
BEGIN
  FOR i IN 1..ST_NumGeometries(blades)
     LOOP
        geom = ST_Split(geom, ST_GeometryN(blades, i));
     END LOOP;
  RETURN geom;
END;
$$ LANGUAGE plpgsql;

Then use it like:

Select ST_AsEWKT(a.geom) from (
   select (ST_Dump(ST_MultiSplit(pol.geom, (
      select ST_MemUnion(lin.geom) from lin where ST_Intersects(pol.geom, lin.geom) = 't')
 ))).geom geom from pol) a;

This gives the six records you are expecting. You may want to add some error checking/handling and I'm not sure about scalability.

I am using postGIS sql to split feature by line in JAVA, and my code worked. my code is:

public List splitGeometry(String geom1, String geom2) {

    List<String> result=new ArrayList<String>() ;
    try {

        Statement s = connection.createStatement();
        String sql_stat = null;
        sql_stat = "select st_astext (a.geom)from (select ( st_dump(p_geom)).geom as geom FROM (SELECT  st_split(ST_GeomFromEWKT('"+geom1+"') ,ST_GeomFromEWKT('"+geom2+"'))AS p_geom) AS b) AS a;";
        System.out.println(sql_stat);
        ResultSet rs=  s.executeQuery(sql_stat);
        while (rs.next()){
            result.add(rs.getString(1)) ;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result ;
}

I hope help you.

  • Thanks for your help but I'm trying to figure out a PostGIS only solution since I don't know how to link QGIS with JAVA. – Lunar Sea Jul 11 '15 at 15:30
  • QGIS is written in C++ and there are Python bindings. So you can develop your own applications using those languages. You cannot use Java.You can use instead of postGIS. – Samane Jul 12 '15 at 4:31
  • Is there a way to split polygon features by linestring features using PostGIS without any C++/Python applications? I've no programming experience. – Lunar Sea Jul 12 '15 at 20:46
  • You use PostGIS and Java, Without the need for C++ and Python. You can also use the geotools in Java. – Samane Jul 13 '15 at 4:14

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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