Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
-- split lines with point, the Spatialite way
-- di Salvatore Fiandaca
-- e-mail:
CREATE TABLE "points_over_lines" AS
SELECT AS id,ST_ClosestPoint(ST_Union(b.geom), a.geom) AS geom
FROM civici a, strade b
GROUP BY a.geom,;
SELECT RecoverGeometryColumn('points_over_lines','geom',3004,'POINT','XY');
CREATE TABLE "lines_nodes_densified" AS
SELECT pk AS id, ST_Union(ST_DissolvePoints(st_segmentize(geom,1))) AS geom
FROM strade;
SELECT RecoverGeometryColumn('lines_nodes_densified','geom',3004,'MULTIPOINT','XY');
CREATE TABLE "points_snapped" AS
SELECT AS id, ST_snap(ST_Union(b.geom),a.geom, ST_Distance(a.geom,b.geom)*1.01) AS geom
FROM "lines_nodes_densified" a, "points_over_lines" b
GROUP BY a.geom, b.geom,;
SELECT RecoverGeometryColumn('points_snapped','geom',3004,'POINT','XY');
CREATE TABLE "lines_split" AS
SELECT AS id, ST_LinesCutAtNodes(st_segmentize(a.geom,1),ST_Union(b.geom)) AS geom
FROM strade a, "points_snapped" b
GROUP BY,a.geom;
SELECT RecoverGeometryColumn('lines_split','geom',3004,'MULTILINESTRING','XY');
CREATE TABLE "lines_split_dump" AS
SELECT AS multi_id, e.item_no,e.geometry
FROM "lines_split" t
JOIN ElementaryGeometries e ON (e.f_table_name = 'lines_split' AND e.origin_rowid =;
SELECT RecoverGeometryColumn('lines_split_dump','geometry',3004,'LINESTRING','XY');
-- cancello le tabelle inutili, i primi 4 step
drop table points_over_lines;
drop table lines_nodes_densified;
drop table points_snapped;
drop table lines_split;
-- aggiorno statistiche e VACUUM
UPDATE geometry_columns_statistics set last_verified = 0;
SELECT UpdateLayerStatistics('geometry_table_name');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment