# Geometry Processing 2 of PostGIS (st_clipbybox2d, ST_Collect, ST_ConcaveHull, ST_ConvexHull)

## 3. ST_ClipByBox2D

ST_ClipByBox2D - returns the geometric part that falls within a rectangle.

### 3.1. Summary

```geometry ST_ClipByBox2D(geometry geom, box2d box);
```

### 3.2. Description

Use 2D boxes to quickly but possibly unclearly clip geometry. The output geometry is not guaranteed to be valid (self intersection of polygons may be introduced). Topologically invalid input geometry does not cause an exception to be thrown.

Performed by the GEOS module.

### 3.3. Example

```-- The second parameter depends on from geometry to box2d Implicit cast of
SELECT ST_ClipByBox2D(the_geom, ST_MakeEnvelope(0,0,10,10)) FROM mytab;
```

## 4. ST_Collect

ST_Collect - returns the specified st from a collection of other geometry_ Geometry value.

### 4.1. Summary

```geometry ST_Collect(geometry set g1field);
geometry ST_Collect(geometry g1, geometry g2);
geometry ST_Collect(geometry[] g1_array);
```

### 4.2. Description

The output type can be MULTI * or GEOMETRYCOLLECTION. There are two variants. Variant 1 collects two geometries. Variant 2 is an aggregate function that accepts a set of geometry and collects them into an St_ In geometry.

Aggregate version: this function returns a GEOMETRYCOLLECTION or a MULTI object from a set of geometry. In PostgreSQL terms, St_ The collect() function is an aggregate function. This means that it operates on data rows in the same way as the SUM() and AVG() functions. For example, SELECT ST_Collect(GEOM) FROM GEOMTABLE GROUP BY ATTRCOLUMN will return a separate GEOMETRYCOLLECTION for each different value of ATTRCOLUMN.

Non aggregate version: this function returns a geometry that is a collection of two input geometries. The output type can be MULTI * or GEOMETRYCOLLECTION.

ST_Collect and ST_Union is usually interchangeable, except ST_Collect always returns a GeometryCollection or MULTI geometry, and ST_Union may return a single geometry when exploding a boundary. ST_Union will also split the line string at the intersection of nodes, while ST_Collect never splits a line string, but simply returns MULTILINESTRING. To prevent ST_Collect returns Geometry Collection when collecting MULTI geometry. You can use the following techniques to use ST_Dump expands multiple into a single, and then reassembles them.

• This function supports 3d and does not delete z-index.
• This method supports circular strings and curves, but does not return a MULTI curve or MULTI, which, as expected, are not currently supported by PostGIS.

### 4.3. Example

• Examples of aggregation
```SELECT stusps, ST_Collect(f.the_geom) as singlegeom
FROM (SELECT stusps, (ST_Dump(the_geom)).geom As the_geom
FROM somestatetable ) As f
GROUP BY stusps
```
• Examples of non aggregation
```SELECT ST_AsText(ST_Collect(ST_GeomFromText('POINT(1 2)'), ST_GeomFromText('POINT(-2 3)') ));
```
st_astext
MULTIPOINT(1 2,-2 3)
```SELECT ST_AsText(ST_Collect(ST_GeomFromText('POINT(1 2)'),ST_GeomFromText('POINT(1 2)') ) );
```
st_astext
MULTIPOINT(1 2,1 2)
```SELECT ST_AsEWKT(ST_Collect(ST_GeomFromEWKT('POINT(1 2 3)'),ST_GeomFromEWKT('POINT(1 2 4)') ) );
```
st_astext
MULTIPOINT(1 2 3,1 2 4)
```SELECT ST_AsText(ST_Collect(ST_GeomFromText('CIRCULARSTRING(220268 150415,220227 150505,220227 150406)'),ST_GeomFromText('CIRCULARSTRING(220227 150406,2220227 150407,220227 150406)')));
```
st_astext
GEOMETRYCOLLECTION(CIRCULARSTRING(220268 150415,220227 150505,220227 150406),CIRCULARSTRING(220227 150406,2220227 150407,220227 150406))
```SELECT ST_Collect(ARRAY(SELECT the_geom FROM sometable));

SELECT ST_AsText(ST_Collect(ARRAY[ST_GeomFromText('LINESTRING(1 2, 3 4)'),ST_GeomFromText('LINESTRING(3 4, 4 5)')])) As wktcollect;

```

MULTILINESTRING((1 2,3 4),(3 4,4 5))

## 5. ST_ConcaveHull

ST_ConcaveHull - the concave shell of a geometry represents a possible concave geometry that surrounds all geometry in the collection. You can think of it as shrink wrap.

### 5.1. Summary

```geometry ST_ConcaveHull(geometry geomA, float target_percent, boolean allow_holes=false);
```

### 5.2. Description

The concave shell of a geometry represents a possible concave geometry, which surrounds all the geometry in the set. The default is false to allow polygons with holes. The result is never higher than a single polygon.

target_percent is the target percentage of convex hull area that the PostGIS solution will try to approach before abandoning or exiting. You can think of the concave shell as the geometry you get by vacuum sealing a set of geometry. target_percent 1 will give you the same answer as convex hull. target_ A percentage between 0 and 0.99 will give you an area smaller than the convex hull area. This is different from convex hull, which is more like wrapping a rubber band around a set of geometry.

It is commonly used with MULTI and Geometry collections. Although it's not an aggregation -- you can associate it with ST_Collect or ST_Union to get the concave hull of a set of points/linestring/polygons ST_ConcaveHull(ST_Collect(somepointfield),0.80).

Its computing speed is much slower than convex shell, but the package geometry is better, and it is also very useful for image recognition.

Performed by GEOS module

Note: if you use points, line strings, or geometric sets, use ST_Collect. If you are using polygons, use ST_Union, because it may fail because of invalid geometry.

Note - the smaller the target percentage, the longer it takes to process the concave shell, and the more likely it is to encounter topology exceptions. That is, the more floating-point numbers and points you accumulate. First try 0.99, which makes the first jump, usually very fast, sometimes as fast as calculating the convex hull, and usually provides a better contraction than 99%, because it almost always overshoot. The second hope is 0.98, which slows down, and the others are usually twice slow. In order to reduce the precision and floating point number, in St_ Use st after concavehull_ Simplifypreservettopology or ST_SnapToGrid. ST_SnapToGrid is slightly faster, but may result in invalid geometry and St_ Simplify storage topology almost always maintains the validity of geometry

More practical examples and a brief description of the technology are given http://www.bostongis.com/postgis_concavehull.snippet

You can also check out Simon Greener's article on demonstrating the introduction of ConcaveHull in Oracle 11G R2. http://www.spatialdbadvisor.com/oracle_ spatial_ tips_ Tricks / 172 / concate hull geometry in oracle-11gr2. The solution we get at the target percentage of 0.75 convex hull is the same as Simon's Oracle SDO_ ConcaveHull_ The shape obtained by boundary is similar.

### 5.3. Example

• An estimate of the infected area is obtained from point observations
```SELECT d.disease_type,ST_ConcaveHull(ST_Collect(d.pnt_geom), 0.99) As geom
FROM disease_obs As d
GROUP BY d.disease_type;
```
• ST_ConcaveHull's 2 polygons surround the 100% shrinkage concave shell of the target
```-- Geometry covered with concave shells
-- The goal is 100%shrink(This is the same as convex hull-Because there is no contraction)

SELECT ST_ConcaveHull(ST_Union(ST_GeomFromText ('POLYGON((175 150, 20 40,50 60, 125 100,175 150))'),ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)), 1) As convexhull;
```

```-- 90% of the target convex hull area%Geometry covering the pocket at
-- The geometry covering the concave shell is at target 90%shrink
SELECT ST_ConcaveHull(ST_Union(ST_GeomFromText('POLYGON((175 150, 20 40,50 60, 125 100, 175 150))'),ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)), 0.9) As target_90;
```

• Shape points covered by L convex hull
```-- This gives rise to a network of 42 points L Shape table
SELECT (ST_DumpPoints(ST_GeomFromText('MULTIPOINT(14 14,34 14,54 14,74 14,94 14,114 14,134 14,150 14,154 14,154 6,134 6,114 6,94 6,74 6,54 6,34 6,14 6,10 6,8 6,7 7,6 8,6 10,6 30,6 50,6 70,6 90,6 110,6 130,6 150,6 170,6 190,6 194,14 194,14 174,14 154,14 134,14 114,14 94,14 74,14 54,14 34,14 14)'))).geom
INTO TABLE l_shape;

SELECT ST_ConvexHull(ST_Collect(geom)) FROM l_shape;
```

```-- L Pointy ST_ConcaveHull At 99 of the target convex hull%place
SELECT ST_ConcaveHull(ST_Collect(geom), 0.99) FROM l_shape;
```

## 6. ST_ConvexHull

ST_ The convex hull of a geometry represents the minimum convex geometry that contains all geometry in the set.

### 6.1. Summary

```geometry ST_ConvexHull(geometry geomA);
```

### 6.2. Description

The convex hull of geometry represents the smallest convex geometry that contains all geometry in the collection.

You can think of a convex hull as the geometry you get by wrapping a set of geometry with a rubber band. This is different from the concave shell, which is similar to shrink wrapping your geometry.

It is commonly used with MULTI and Geometry collections. Although it is not an aggregation - you can associate it with St_ Collect is used in conjunction to obtain the convex hull of a set of points. ST_ConvexHull (ST_Collect (somepointfield)).

It is usually used to determine the affected area based on a set of point observations.

Performed by GEOS module

This method implements the OpenGIS simple function implementation specification of SQL 1.1. s2.1.1.3

This method implements the SQL/MM specification. SQL-MM 3: 5.1.16

This function supports 3d and does not delete z-index.

### 6.3. Example

• The estimated value of the infected area is obtained from the point observation
```SELECT d.disease_type, ST_ConvexHull(ST_Collect(d.the_geom)) As the_geom FROM disease_obs As d GROUP BY d.disease_type
```

• The convex hull of MultiLinestring and MultiPoint seen together with MultiLinestring and MultiPoint
```SELECT ST_AsText(ST_ConvexHull(ST_Collect(ST_GeomFromText('MULTILINESTRING((100 190,10 8),(150 10, 20 30))'),ST_GeomFromText('MULTIPOINT(50 5, 150 30, 50 10, 10 10)'))) );
```
st_astext
POLYGON((50 5,10 8,10 10,100 190,150 30,150 10,50 5))

Posted by shirvo on Fri, 22 Oct 2021 00:57:51 -0700