10

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?

6
  • You need to feed the the geoms recursively to ST_Split. Commented May 11, 2015 at 13:07
  • Can you please help me with the SQL query? I'm new to PostGIS.
    – Lunar Sea
    Commented May 14, 2015 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
    Commented Jul 12, 2015 at 1:03
  • I've added a screenshot to illustrate the layers.
    – Lunar Sea
    Commented Jul 12, 2015 at 7:39
  • Any chance you could add the input geometries? Commented Jul 12, 2015 at 7:54

2 Answers 2

6
+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.

4

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.

4
  • 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
    Commented Jul 11, 2015 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
    Commented Jul 12, 2015 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
    Commented Jul 12, 2015 at 20:46
  • You use PostGIS and Java, Without the need for C++ and Python. You can also use the geotools in Java.
    – Samane
    Commented Jul 13, 2015 at 4:14

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.