Geometry Processing Geometry Processing 6

21. ST_OrientedEnvelope

ST_OrientedEnvelope - Returns a minimum rotating rectangle containing a geometry.

21.1. Summary

geometry ST_OrientedEnvelope( geometry geom );

21.2. Description

Returns the smallest rotating rectangle containing a geometry. Note that there may be more than one minimum rotating rectangle. A Point or LineString may be returned in the case of degenerate input.

21.3. Samples

SELECT ST_AsText(ST_OrientedEnvelope('MULTIPOINT ((0 0), (-1 -1), (3 2))'));
st_astext
POLYGON((3 2,2.88 2.16,-1.12 -0.84,-1 -1,3 2))
SELECT ST_AsText(ST_OrientedEnvelope(ST_Collect(ST_GeomFromText('LINESTRING(55 75,125 150)'),ST_Point(20, 80)))) As wktenv;
wktenv
POLYGON((19.9999999999997 79.9999999999999,33.0769230769229 60.3846153846152,138.076923076924 130.384615384616,125.000000000001 150.000000000001,19.9999999999997 79.9999999999999))

22. ST_Polygonize

ST_Polygonize - Aggregation. Create a GeometryCollection containing possible polygons made up of a set of geometric lines.

22.1. Summary

geometry ST_Polygonize(geometry set geomfield);
geometry ST_Polygonize(geometry[] geom_array);

22.2. Description

Create a GeometryCollection containing possible polygons made up of a set of geometric lines.

  • Geometry collections are often difficult to handle with third-party tools, so ST_Polygonize Binding ST_Dump dumps a polygon as a single polygon.
  • The input line must have the correct point for this function to work properly

22.3. Samples

SELECT ST_AsEWKT(ST_Polygonize(the_geom_4269)) As geomtextrep FROM (SELECT the_geom_4269 FROM ma.suffolk_edges ORDER BY tlid LIMIT 45) As foo;
geomtextrep
SRID=4269;GEOMETRYCOLLECTION(POLYGON((-71.040878 42.285678,-71.040943 42.2856,-71.04096 42.285752,-71.040878 42.285678)),POLYGON((-71.17166 42.353675,-71.172026 42.354044,-71.17239 42.354358,-71.171794 42.354971,-71.170511 42.354855,-71.17112 42.354238,-71.17166 42.353675)))
  • Using ST_Dump dumps polygonized gems into a single polygon
SELECT ST_AsEWKT((ST_Dump(foofoo.polycoll)).geom) As geomtextrep FROM (SELECT ST_Polygonize(the_geom_4269) As polycoll FROM (SELECT the_geom_4269 FROM ma.suffolk_edges ORDER BY tlid LIMIT 45) As foo) As foofoo;
geomtextrep
SRID=4269;POLYGON((-71.040878 42.285678,-71.040943 42.2856,-71.04096 42.285752,-71.040878 42.285678))
SRID=4269;POLYGON((-71.17166 42.353675,-71.172026 42.354044,-71.17239 42.354358,-71.171794 42.354971,-71.170511 42.354855,-71.17112 42.354238,-71.17166 42.353675))

23. ST_Node

ST_Node - Node is a set of line strings.

23.1. Summary

geometry ST_Node(geometry geom);

23.2. Description

Full Node: Use the row string collection of as few nodes as possible while preserving all input nodes.

  • Due to a bug before 3.3.1 in GEOS, this function cannot execute node self-intersections. This is fixed in GEOS 3.3.2 or later.
  • This function uses GEOSNode internally instead of GEOSUnaryUnion. This may result in a different order and direction of line strings than Postgis < 2.4.

23.3. Samples

SELECT ST_AsText(ST_Node('LINESTRINGZ(0 0 0, 10 10 10, 0 10 5, 10 0 3)'::geometry)) As output;
output
MULTILINESTRING Z ((0 0 0,5 5 4.5),(5 5 4.5,10 10 10,0 10 5,5 5 4.5),(5 5 4.5,10 0 3))

24. ST_OffsetCurve

ST_OffsetCurve - Returns an offset line for a given distance and edge from an input line. Used to calculate parallel lines about centerlines

24.1. Summary

geometry ST_OffsetCurve(geometry line, float signed_distance, text style_parameters=");

24.2. Description

Returns the offset row for a given distance and edge from the input line. All points returning a geometry do not exceed the given distance of the input geometry.
For positive distances, the offset will be to the left of the input line and will remain in the same direction. When the distance is negative, it will be on the right and in the opposite direction.

An optional third parameter allows you to specify a space-delimited key=value pair to adjust the operation on the list, as follows:

  • 'quad_segs=#': Number of segments used to approximate 1/4 of a circle (default is 8).
  • 'join=round|mitre|bevel': add style (default is "round")." miter'Is also considered a synonym for'mitre'.
  • 'mitre_limit = #. Mixed scale limit (affects only the style of the mitered connection)." miter_limit'Is also known as'mitre_ Synonym for limit'.

This function ignores the third dimension (z), and always gives a two-dimensional result even if it presents a three-dimensional geometry.

24.3. Samples

  • Calculate open buffers around roads
SELECT ST_Union(ST_OffsetCurve(f.the_geom, f.width/2, 'quad_segs=4 join=round'),ST_OffsetCurve(f.the_geom, -f.width/2, 'quad_segs=4 join=round')) as track FROM someroadstable;
  • 'quad_segs=4 join=round'original straight line and its offset is 15 units.
SELECT ST_AsText(ST_OffsetCurve(ST_GeomFromText('LINESTRING(164 16,144 16,124 16,104 16,84 16,64 16,44 16,24 16,20 16,18 16,17 17,16 18,16 20,16 40,16 60,16 80,16 100,16 120,16 140,16 160,16 180,16 195)'),15, 'quad_segs=4 join=round'));

output
LINESTRING(164 1,18 1,12.2597485145237 2.1418070123307,7.39339828220179 5.39339828220179,5.39339828220179 7.39339828220179,2.14180701233067 12.2597485145237,1 18,1 195)
  • 'quad_segs=4 join=round'original row and its offset-15 units
SELECT ST_AsText(ST_OffsetCurve(geom,-15, 'quad_segs=4 join=round')) As notsocurvy FROM ST_GeomFromText('LINESTRING(164 16,144 16,124 16,104 16,84 16,64 16,44 16,24 16,20 16,18 16,17 17,16 18,16 20,16 40,16 60,16 80,16 100,16 120,16 140,16 160,16 180,16 195)') As geom;

notsocurvy
LINESTRING(31 195,31 31,164 31)
  • Double offset gets more curves, notice the first opposite direction, so -30 + 15 = -15
SELECT ST_AsText(ST_OffsetCurve( ST_OffsetCurve(geom,-30, 'quad_segs=4 join=round'), -15, 'quad_segs=4 join=round')) As morecurvy FROM ST_GeomFromText('LINESTRING(164 16,144 16,124 16,104 16,84 16,64 16,44 16,24 16,20 16,18 16,17 17,16 18,16 20,16 40,16 60,16 80,16 100,16 120,16 140,16 160,16 180,16 195)') As geom;

morecurvy
LINESTRING(164 31,46 31,40.2597485145236 32.1418070123307,35.3933982822018 35.3933982822018,32.1418070123307 40.2597485145237,31 46,31 195)
  • Double offset yields more curves and parallel lines combined with regular offset. With the original coverage.
SELECT ST_AsText(ST_Collect(ST_OffsetCurve(geom, 15, 'quad_segs=4 join=round'),ST_OffsetCurve(ST_OffsetCurve(geom,-30, 'quad_segs=4 join=round'), -15, 'quad_segs=4 join=round'))) As parallel_curvesFROM ST_GeomFromText('LINESTRING(164 16,144 16,124 16,104 16,84 16,64 16,44 16,24 16,20 16,18 16,17 17,16 18,16 20,16 40,16 60,16 80,16 100,16 120,16 140,16 160,16 180,16 195)') As geom;

parallel curves
MULTILINESTRING((164 1,18 1,12.2597485145237 2.1418070123307,7.39339828220179 5.39339828220179,5.39339828220179 7.39339828220179,2.14180701233067 12.2597485145237,1 18,1 195),(164 31,46 31,40.2597485145236 32.1418070123307,35.3933982822018 35.3933982822018,32.1418070123307 40.2597485145237,31 46,31 195))
  • "quad_segs=4 connection=bevel" display with original line
SELECT ST_AsText(ST_OffsetCurve(ST_GeomFromText('LINESTRING(164 16,144 16,124 16,104 16,84 16,64 16,44 16,24 16,20 16,18 16,17 17,
16 18,16 20,16 40,16 60,16 80,16 100,16 120,16 140,16 160,16 180,16 195)'),15, 'quad_segs=4 join=bevel'));

output
LINESTRING(164 1,18 1,7.39339828220179 5.39339828220179,5.39339828220179 7.39339828220179,1 18,1 195)
  • quad_segs=4 join=mitre mitre_limit=2.2
SELECT ST_AsText(ST_Collect(ST_OffsetCurve(geom, 15, ' quad_segs=4 join=mitre mitre_limit=2.2'),ST_OffsetCurve(geom, -15, ' quad_segs=4 join=mitre mitre_limit=2.2')) )FROM ST_GeomFromText('LINESTRING(164 16,144 16,124 16,104 16,84 16,64 16,44 16,24 16,20 16,18 16,17 17,16 18,16 20,16 40,16 60,16 80,16 100,16 120,16 140,16 160,16 180,16 195)') As geom;

output
MULTILINESTRING((164 1,11.7867965644036 1,1 11.7867965644036,1 195),(31 195,31 31,164 31))

25. ST_RemoveRepeatedPoints

ST_RemoveRepeatedPoints - Returns a version of a given geometry, removing duplicate points.

25.1. Summary

geometry ST_RemoveRepeatedPoints(geometry geom, float8 tolerance);

25.2. Description

Returns a version of a given geometry, removing duplicate points. There are actually only a few (many) lines, (many) polygons and points, but you can safely call it with any type of geometry. Since simplification is done on an object-by-object basis, you can also provide a GeometryCollection to the function. If tolerance parameters are provided, vertices within the tolerance range of each other will be considered "identical" for removal purposes.

26. ST_SharedPaths

ST_SharedPaths - Returns a collection of shared paths containing two input line strings/multiple line strings.

26.1. Summary

geometry ST_SharedPaths(geometry lineal1, geometry lineal2);

26.2. Description

26.3. Samples

  • Original patch

  • Shared paths where multiline strings and line strings overlap the original geometry.
SELECT ST_AsText(ST_SharedPaths(ST_GeomFromText('MULTILINESTRING((26 125,26 200,126 200,126 125,26 125),(51 150,101 150,76 175,51 150))'),ST_GeomFromText('LINESTRING(151 100,126 156.25,126 125,90 161, 76 175)'))) As wkt

wkt
GEOMETRYCOLLECTION(MULTILINESTRING((126 156.25,126 125),(101 150,90 161),(90 161,76 175)),MULTILINESTRING EMPTY)
  • Same example, but the direction of the string is reversed
SELECT ST_AsText(ST_SharedPaths(ST_GeomFromText('LINESTRING(76 175,90 161,126 125,126 156.25,151 100)'),ST_GeomFromText('MULTILINESTRING((26 125,26 200,126 200,126 125,26 125),(51 150,101 150,76 175,51 150))'))) As wkt
wkt
GEOMETRYCOLLECTION(MULTILINESTRING EMPTY,MULTILINESTRING((76 175,90 161),(90 161,101 150),(126 125,126 156.25)))

27. ST_ShiftLongitude

ST_ShiftLongitude - Switches geometric coordinates between -180. 180 and 0.. 360 ranges

27.1. Summary

geometry ST_ShiftLongitude(geometry geomA);

27.2. Description

Reads each point/vertex in each component of each feature in a geometry and adds 360 if the longitude coordinate is less than 0. The result will be a 0-360 version of the data drawn on a 180-center map

This is only useful for data from 4326 (WGS 84).

27.3. Samples

SELECT ST_AsEWKT(ST_ShiftLongitude(ST_GeomFromEWKT('SRID=4326;POINT(-118.58 38.38 10)'))) As geomA,
       ST_AsEWKT(ST_ShiftLongitude(ST_GeomFromEWKT('SRID=4326;POINT(241.42 38.38 10)'))) As geomb
geomAgeomB
SRID=4326;POINT(241.42 38.38 10)SRID=4326;POINT(-118.58 38.38 10)
SELECT ST_AsText(ST_ShiftLongitude(ST_GeomFromText('LINESTRING(-118.58 38.38, -118.20 38.45)')))
st_astext
LINESTRING(241.42 38.38,241.8 38.45)

28. ST_WrapX

ST_WrapX - Wraps a geometry around the X value.

28.1. Summary

geometry ST_WrapX(geometry geom, float8 wrap, float8 move);

28.2. Description

This function splits the input geometry, then moves each result component to the right (negative) or left (positive) of the given wrap line in the direction specified by the'move'parameter, eventually merging the parts together again

This is useful for redirecting long latitude inputs so that features of interest do not derive from one side to the other.

28.3. Samples

-- Move the components of all given geometries of its bounding box  
-- Fall completely on x=0 reach+360 Left of  
select ST_WrapX(the_geom, 0, 360);

-- Move all components of a given geometry for its bounding box  
-- Fall completely on x=-30 reach+360 Left of  
select ST_WrapX(the_geom, -30, 360);

29. ST_Simplify

ST_Simplify - Returns a simplified version of a given geometry using the Douglas-Peucker algorithm.

29.1. Summary

geometry ST_Simplify(geometry geomA, float tolerance, boolean preserveCollapsed);

29.2. Description

Returns a simplified version of a given geometry using the Douglas-Peucker algorithm. It will actually only do a few (many) lines and polygons, but you can safely call it with any type of geometry. Since simplification is done on an object-by-object basis, you can also provide a GeometryCollection to the function.

The Save Collapse flag keeps objects that are otherwise too small for tolerance. For example, a 1-meter line is simplified to a 10-meter tolerance. If a flag is given, the line will not disappear. This flag is useful for the rendering engine and prevents a surprising amount of very small objects from disappearing from the map, leaving a blank space

Note that topological diagrams may not be preserved and may result in invalid geometries. Save the topology using (see ST_SimplifyPreserveTopology).

29.3. Samples

  • Too much simplification of a circle turns it into a triangle, an octagon of medium size.
SELECT ST_Npoints(the_geom) As np_before, 
    ST_NPoints(ST_Simplify(the_geom,0.1)) As np01_notbadcircle, 
    ST_NPoints(ST_Simplify(the_geom,0.5)) As np05_notquitecircle,
    ST_NPoints(ST_Simplify(the_geom,1)) As np1_octagon, 
    ST_NPoints(ST_Simplify(the_geom,10)) As np10_triangle,
    (ST_Simplify(the_geom,100) is null) As np100_geometrygoesaway FROM (SELECT ST_Buffer('POINT(1 3)', 10,12) As the_geom) As foo;

np_beforenp01_notbadcirclenp05_notquitecirclenp1_octagonnp10_trianglenp100_geometrygoesaway
49331794t

30. ST_SimplifyPreserveTopology

ST_SimplifyPreserveTopology - Returns a simplified version of a given geometry using the Douglas-Peucker algorithm. Invalid derived geometries (especially polygons) will be avoided.

30.1. Summary

geometry ST_SimplifyPreserveTopology(geometry geomA, float tolerance);

30.2. Description

Returns a simplified version of a given geometry using the Douglas-Peucker algorithm. Invalid derived ge measures (especially polygons) will be avoided. It will actually only do a few (many) lines and polygons, but you can safely call it with any type of geometry. Since simplification is done on an object-by-object basis, you can also provide a GeometryCollection to the function.

30.3. Samples

  • Same as Simplify, but we see that Preserve Topology prevents over simplification. A circle can become at most a square.
SELECT ST_Npoints(the_geom) As np_before,
       ST_NPoints(ST_SimplifyPreserveTopology(the_geom ,0.1)) As np01_notbadcircle, 
       ST_NPoints(ST_SimplifyPreserveTopology(the_geom,0.5)) As np05_notquitecircle,
       ST_NPoints(ST_SimplifyPreserveTopology(the_geom,1)) As np1_octagon, 
       ST_NPoints( ST_SimplifyPreserveTopology(the_geom,10)) As np10_square,
       ST_NPoints(ST_SimplifyPreserveTopology(the_geom,100)) As np100_stillsquare
FROM (SELECT ST_Buffer('POINT(1 3)', 10,12) As the_geom) As foo;
np_beforenp01_notbadcirclenp05_notquitecirclenp1_octagonnp10_squarenp100_stillsquare
493317955

31. ST_SimplifyVW

ST_SimplifyVW - Returns a simplified version of a given geometry using the Visvalingam-Whyatt algorithm

31.1. Summary

geometry ST_SimplifyVW(geometry geomA, float tolerance);

31.2. Description

Returns a simplified version of a given geometry using the Visvalingam-Whyatt algorithm. It will actually only do a few (many) lines and polygons, but you can safely call it with any type of geometry. Since simplification is done on an object-by-object basis, you can also provide a GeometryCollection to the function.

31.3. Samples

  • LineString is simplified to a minimum region threshold of 30.
select ST_AsText(ST_SimplifyVW(geom,30)) simplified
FROM (SELECT 'LINESTRING(5 2, 3 8, 6 20, 7 25, 10 10)'::geometry geom) As foo;

simplified
LINESTRING(5 2,7 25,10 10)

32. ST_ChaikinSmoothing

ST_ChaikinSmoothing - Returns a "smooth" version of a given geometry using the Chaikin algorithm

32.1. Summary

geometry ST_ChaikinSmoothing(geometry geom, integer nIterations = 1, boolean preserveEndPoints = false);

32.2. Description

Returns a "smooth" version of a given geometry using the Chaikin algorithm. See Chaikins-Algorithm for an explanation of this process. For each iteration, the number of vertices doubles. This function places a new vertex at one-fourth of the line before and after each point and deletes the original point. To reduce the number of points, a simplified function is used on the result. The new point gets all the interpolation values containing the dimensions, including z and m.

  • Second parameter, limit iterations to a maximum of five
  • Note that the third parameter is valid only for polygons and is ignored for line strings
  • This function handles 3D, and the third dimension affects the result.
    - Returns the "smooth" version of a given geometry using the Chaikin algorithm

32.1. Summary

geometry ST_ChaikinSmoothing(geometry geom, integer nIterations = 1, boolean preserveEndPoints = false);

32.2. Description

Returns a "smooth" version of a given geometry using the Chaikin algorithm. See Chaikins-Algorithm for an explanation of this process. For each iteration, the number of vertices doubles. This function places a new vertex at one-fourth of the line before and after each point and deletes the original point. To reduce the number of points, a simplified function is used on the result. The new point gets all the interpolation values containing the dimensions, including z and m.

  • Second parameter, limit iterations to a maximum of five
  • Note that the third parameter is valid only for polygons and is ignored for line strings
  • This function handles 3D, and the third dimension affects the result.

Tags: Algorithm UI

Posted on Thu, 25 Nov 2021 13:43:55 -0500 by pestilence669