concept function in category postgis

This is an excerpt from Manning's book PostGIS in Action, Third Edition.
A spatial database is a database with column data types specifically designed to store objects in space—these data types can be added to database tables. The information stored is usually geographic in nature, such as a point location or the boundary of a lake. The spatial database also provides functions and indexes for querying and manipulating the spatial data, which can be called from a query language such as Structured Query Language (SQL). A spatial database is often used as just a storage container for spatial data, but it can do much more than that. Although a spatial database need not be relational in nature, most are.
To conform with OGC-MM, PostGIS offers other functions such as
ST_PointFromText,ST_LineFromText,ST_PolyFromText,ST_GeometryFromText, and other plain text or binary input functions that require specific geometry subtypes. Our advice, as far as using PostGIS is concerned, is to stay away from them and stick withST_GeomFromTextandST_GeomFromWKB. Functions such asST_PointFromTextare wrappers forST_GeomFromTextandST_GeomFromWKBand perform an additional check to make sure you’re feeding in the correct subtype (returning NULL if not), adding additional overhead for the check and also potentially resulting in data loss if you’re not aware of this side effect. In the course of setting up your tables, you should have already added the necessary subtype constraints. If you want to filter out rows that don’t fit your type constraints and prevent pollution of your table with NULLs, you should filter using theST_GeometryTypeorGeometryTypefunctions.
Listing 11.12. Function to cut linestrings and multilinestrings at nearest point junctions
CREATE OR REPLACE FUNCTION ch11.upgis_cutlineatpoints( param_mlgeom geometry, param_mpgeom geometry, param_tol double precision ) RETURNS geometry AS $$ DECLARE var_resultgeom geometry; var_sline geometry; var_eline geometry; var_perc_line double precision; var_refgeom geometry; var_pset geometry[] := #1 ARRAY(SELECT geom FROM ST_Dump(param_mpgeom)); var_lset geometry[] := ARRAY(SELECT geom FROM ST_Dump(param_mlgeom)); BEGIN FOR i in 1 .. array_upper(var_pset,1) LOOP #2 FOR j in 1 .. array_upper(var_lset,1) LOOP #3 IF ST_DWithin(var_lset[j],var_pset[i],param_tol) AND #4 NOT ST_Intersects(ST_Boundary(var_lset[j]),var_pset[i]) THEN #5 IF ST_NumGeometries(ST_Multi(var_lset[j])) = 1 THEN var_perc_line := ST_LineLocatePoint(var_lset[j],var_pset[i]); IF var_perc_line BETWEEN 0.0001 and 0.9999 THEN var_sline := ST_LineSubstring(var_lset[j],0,var_perc_line); var_eline := ST_LineSubstring(var_lset[j],var_perc_line,1); var_eline := ST_SetPoint(var_eline,0,ST_EndPoint(var_sline)); var_lset[j] := ST_Collect(var_sline,var_eline); END IF; ELSE var_lset[j] := #6 upgis_cutlineatpoints(var_lset[j],var_pset[i]); END IF; END IF; END LOOP; END LOOP; RETURN ST_Union(var_lset); END; $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT PARALLEL SAFE;