[postgis-users] Polygon to line

Edward Mac Gillavry emacgillavry at hotmail.com
Thu Jun 24 13:24:14 PDT 2010


Hi Ricardo,

Having been through the mailing list's archives myself over the last few weeks, i came up with the following recipe:




	
	
	
	First you dump all multipolygons into polygons:

SELECT
    (ST_Dump(the_geom)).geom AS the_geom
INTO  polygon_table
FROM multipolygon_table 

and create an index on that one

CREATE INDEX geo_idx_polygon_temp ON polygon_table_temp USING GIST (the_geom);
VACUUM ANALYZE polygon_table_temp;

then you create a new table that contains the borders using ST_Intersection:

CREATE TABLE borders AS 
SELECT
    ST_Intersection(t1.the_geom,t2.the_geom) AS the_geom
FROM
    polygon_table_temp AS t1,
    polygon_table_temp AS t2
WHERE
    t1.the_geom && t2.the_geom;

These borders are duplicated, as the intersection is computed twice (A intersects B and B intersects A). Also contains records of geometry other than multilinestring

DELETE FROM borders WHERE GeometryType(the_geom) <> 'MULTILINESTRING'::text; 
SELECT Populate_Geometry_Columns('borders'::regclass);

ALTER TABLE borders ADD COLUMN gid SERIAL;

ALTER TABLE borders ADD PRIMARY KEY (gid);

CREATE INDEX geo_idx_borders ON borders USING GIST (the_geom);
VACUUM ANALYZE borders;

 so remove duplicates:

DELETE FROM borders WHERE
borders.gid <  
(SELECT  MAX(b.gid )
    FROM borders b    
    WHERE b.the_geom && borders.the_geom 
        AND ST_Equals(borders.the_geom, b.the_geom));

REINDEX TABLE borders;
VACUUM ANALYZE borders;

That should do it. Thanks to previous postings to this mailing list!

regards,

Edward



Date: Thu, 24 Jun 2010 16:53:54 -0300
From: vilella.ricardo at gmail.com
To: postgis-users at postgis.refractions.net
Subject: [postgis-users] Polygon to line

Hi all, is the first time I use the list. I have a polygon that represent a country, if i want extract only the sides that touch with other polygon. How i have to do? The result should be a MultiLineString or a LineString representing the limits of the country but without the coast line if it has one. I tried with ST_Touches but i can't get it work.
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20100624/a43063ef/attachment.html>


More information about the postgis-users mailing list