Yesterday I had no time to create it in details... See my solution in 4 steps:
CREATE OR REPLACE VIEW bd_segment ASSELECT ST_PointN(geom, generate_series(1, ST_NPoints(geom)-1)) AS sp, ST_PointN(geom, generate_series(2, ST_NPoints(geom) )) AS ep FROM -- extract the individual linestrings (SELECT (ST_Dump(ST_Boundary(the_geom))).geom FROM bd) AS linestrings;CREATE OR REPLACE VIEW bd_segment_geom ASSELECT sp, ep, st_makeline(sp, ep) FROM bd_segment;CREATE OR REPLACE view bd_segment_id AS SELECT bd.gid, row_number() OVER (order by bd.gid), degrees(st_azimuth(ff.sp, ff.ep)-1.57079633) AS az_deg, ST_LENGTH(ff.st_makeline) , ff.st_makeline FROM bd_segment_geom ffJOIN bd ON st_touches(ff.st_makeline, bd.the_geom)GROUP BY bd.gid, ff.sp, ff.ep, ff.st_makeline;UPDATE bd_segment_idSET az_deg = az_deg + 360WHERE az_deg < 0;
The last query give you the building ids with a spatial join using st_touches. Hope it helps.Update - In qgis the solution looks like this: