2

I have a polygon shapefile, and theoretically, all features in the shape are bounded by the union of some lines in another shapefile. Is there any way to confirm if this is true?

I'm using QGis, FME and PostGIS, but don't know tools for doing it in any of this softwares. I've tried to use FME's SpatialRelator, but couldn't find a check that assesses this situation.

2 Answers 2

1

A slighty more exteme version than that posted by @Guy, would be the following.

1). Break the Polygons into constituent linestring. You can do this using ST_Dump and two sets of generate series offset by one, to get each pair of adjacent points, which is then handed to ST_MakeLine.

2). Get a list of distinct linestrings. ie, removing duplicates, as this will distort the counts for number of equal line segments otherwise.

3). Create a table called same using the ST_Equals function.

4). Get a table with the number of polygon lines, which is one less than the number of points, given repeated start/end point.

5). Create a table called equals where the number of linestrings are the same from steps 3 and 4.

6). Any id from step 5, is fully covered by linestrings.

Depending on how many lines/polygons you have, this could be quite inefficient, as it compares comparing all line segments from both tables.

WITH poly_lines (id, poly_line, numpoints) as
 (SELECT id, ST_MakeLine(startpoint, endpoint), numpoints
     FROM (SELECT 
               ST_PointN(geom, generate_series(1, ST_Npoints(geom) -1 )) as startpoint,
               ST_PointN(geom, generate_series(2, ST_Npoints(geom))) as endpoint, 
               id, numpoints 
         FROM (SELECT 
                 id, (ST_Dump(ST_Exteriorring(geom))).geom, ST_NPoints(ST_Exteriorring(geom))-1 as numpoints
              FROM polygons)
         as pts) 
     as g), 
lines (lines, id) as (SELECT DISTINCT on (geom) geom, id 
                     FROM linestrings ), 
same (pid, lid) as (
    SELECT 
       p.id, l.id 
    FROM poly_lines p, lines l 
    WHERE ST_Equals(p.poly_line, l.lines) = 't'), 
polygon_lines (id, num_poly_pts) as 
    (SELECT 
      id, ST_NPoints(ST_Exteriorring(geom))-1 
    FROM polygons),
equals (pid, nlines, npoints) as 
    (SELECT s.pid, count(lid) as num_line_pts, num_poly_pts
        FROM same s INNER JOIN polygon_lines pp on s.pid = pp.id 
        GROUP BY pid, id, num_poly_pts) 
SELECT pid from equals WHERE nlines = npoints;
0

Assuming they exactly align, then you can load in your polygons into a PostGIS table a and your lines into table b and use the following in SQL to return confirmation

SELECT ST_Covers(ST_Collect(b.line), ST_Collect(ST_ExteriorRing(a.polygon)) FROM a,b

(untested)

There are plenty of ways you could achieve the same result, but it really depends on the size of your two feature collections as to which application or method is best to use.

See http://postgis.net/docs/ST_ExteriorRing.html and http://postgis.net/docs/manual-2.1/ST_Covers.html for further details.

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.