-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- -- -- PostGIS - Spatial Types for PostgreSQL -- http://postgis.net -- -- Copyright (C) 2025 Sandro Santilli -- -- This is free software; you can redistribute and/or modify it under -- the terms of the GNU General Public Licence. See the COPYING file. -- -- Author: Sandro Santilli -- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- { -- Invalidities are represented by any vertex that does not fall on -- the topology precision grid. -- }{ -- Availability: 3.6.0 CREATE OR REPLACE FUNCTION topology.MakeTopologyPrecise(toponame name, bbox GEOMETRY DEFAULT NULL, gridSize float8 DEFAULT NULL) RETURNS void AS $BODY$ DECLARE topo topology.topology; imprecisePoints GEOMETRY; sql TEXT; dataBox GEOMETRY; dataMagnitude FLOAT8; minGridSize FLOAT8; minEdgeExtent FLOAT8; BEGIN topo := findTopology(toponame); IF topo.id IS NULL THEN RAISE EXCEPTION 'Could not find topology "%"', toponame; END IF; IF gridSize IS NULL THEN gridSize := topo.precision; END IF; IF gridSize <= 0 THEN RAISE NOTICE 'Every vertex is precise with grid size %', gridSize; RETURN; END IF; -- TODO: compute real extent instead of estimated/float4 one? -- TODO: generalize a topology.MinTolerance(toponame) ? SELECT ST_Union(g) b FROM ( SELECT ST_EstimatedExtent(topo.name, 'edge_data', 'geom')::geometry g UNION SELECT ST_EstimatedExtent(topo.name, 'node', 'geom')::geometry ) foo INTO dataBox; IF dataBox IS NULL THEN RAISE NOTICE 'Every vertex is precise in an empty topology'; RETURN; END IF; dataMagnitude = greatest( abs(ST_Xmin(dataBox)), abs(ST_Xmax(dataBox)), abs(ST_Ymin(dataBox)), abs(ST_Ymax(dataBox)) ); -- TODO: restrict data magnitude computation to requested bbox ? minGridSize := topology._st_mintolerance(dataMagnitude); IF minGridSize > gridSize THEN RAISE EXCEPTION 'Presence of max ordinate value % requires a minimum grid size of %', dataMagnitude, minGridSize; END IF; -- TODO: recursively grow working bbox to include all edges connected -- to all endpoints of edges intersecting it ? -- -- Guard against snapping a whole edge to a single grid point. -- With a grid size wider than the smallest edge extent every vertex would -- quantize to the same coordinate, producing EMPTY geometries and breaking -- the topology. Raising early keeps the caller in control of a safer grid -- selection. -- sql := format( $$ SELECT min(greatest( abs(ST_XMax(geom) - ST_XMin(geom)), abs(ST_YMax(geom) - ST_YMin(geom)) )) FROM %1$I.edge_data WHERE ($1 IS NULL OR geom && $1) $$, topo.name ); EXECUTE sql USING bbox INTO minEdgeExtent; IF minEdgeExtent IS NOT NULL AND gridSize > minEdgeExtent THEN RAISE EXCEPTION 'Grid size % exceeds smallest edge extent % and would erase edges', gridSize, minEdgeExtent; END IF; sql := format( $$ UPDATE %1$I.edge_data SET geom = ST_SnapToGrid(geom, $2) WHERE ( $1 IS NULL OR geom && $1 ) $$, topo.name ); EXECUTE sql USING bbox, gridSize; sql := format( $$ UPDATE %1$I.node SET geom = ST_SnapToGrid(geom, $2) WHERE ( $1 IS NULL OR geom && $1 ) $$, topo.name ); EXECUTE sql USING bbox, gridSize; sql := format( $$ UPDATE %1$I.face SET mbr = ST_SnapToGrid(mbr, $2) WHERE ( $1 IS NULL OR mbr && $1 ) $$, topo.name ); EXECUTE sql USING bbox, gridSize; -- TODO: validate topology if requested ? END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; --}