Difficulty: intermediate
Estimated Time: 20 minutes

# Geometry Constructing Functions

This scenario gives you an introduction to geometry constructing functions in PostGIS.

All the functions we have seen so far work with geometries "as they are" and returns

• analyses of the objects (`ST_Length(geometry)`, `ST_Area(geometry)`),
• serializations of the objects (`ST_AsText(geometry)`, `ST_AsGML(geometry)`),
• parts of the object (`ST_RingN(geometry,n)`) or
• true/false tests (`ST_Contains(geometry,geometry)`, `ST_Intersects(geometry,geometry)`).

"Geometry constructing functions" take geometries as inputs and output new shapes.

The database has already been started and the spatial data has already been loaded. This scenario will use data from New York City (NYC). If you want to dig in deeper on the data please go ahead and do this scenario first. Data from this scenario will be used in all the other exercises as well.

We have already logged you into the PostgreSQL command line but, if you get disconnected here are the details on the database we are connecting to:

3. A database named: nyc

And with that, let's dig in.

You have now learned about how PostGIS supports a set of powerful functions for constructing new geometries of various kinds.

The geometry constructing functions are summarized below:

• `ST_Centroid(geometry)`: Returns a point that is on the center of mass of the input geometry

• `ST_PointOnSurface(geometry)`: Returns a point that is guaranteed to be inside the input geometry

• `ST_Buffer(geometry, distance)`: For geometry: Returns a geometry that represents all points whose distance from this Geometry is less than or equal to distance. Calculations are in the Spatial Reference System of this Geometry. For geography: Uses a planar transform wrapper.

• `ST_Intersection(geometry A, geometry B)`: Returns a geometry that represents the shared portion of geomA and geomB. The geography implementation does a transform to geometry to do the intersection and then transform back to WGS84.

• `ST_Union()`: Returns a geometry that represents the point set union of the Geometries.

### Geometry Constructing Functions

Step 1 of 4

#### ST_Centroid / ST_PointOnSurface

A common need when composing a spatial query is to replace a polygon feature with a point representation of the feature. For example, this is useful for spatial joins, because using `ST_Intersects(geometry,geometry)` on two polygon layers often results in double-counting: a polygon on a boundary will intersect an object on both sides. Using "proxy points" for one layer of polygons ensures each will fall into only one of the other polygons.

• `ST_Centroid(geometry)` returns a point that is approximately on the center of mass of the input argument. This simple calculation is fast, but sometimes not desirable, because the returned point is not necessarily in the feature itself. If the input feature has a convexity (imagine the letter 'C') the returned centroid might not be in the interior of the feature.
• `ST_PointOnSurface(geometry)` returns a point that is guaranteed to be inside the input geometry. This makes it more useful for computing "proxy points".

The picture below shows the difference between the two functions. Note that the result of `ST_Centroid` does not lie inside the polygon. We can check this using the `ST_Intersects` spatial relationship function. The following query compares the locations of `ST_Centroid` and `ST_PointOnSurface` for a concave polygon:

``````SELECT ST_Intersects(geom, ST_Centroid(geom)) AS centroid_inside,
ST_Intersects(geom, ST_PointOnSurface(geom)) AS pos_inside
FROM (VALUES ('POLYGON ((30 0, 30 10, 10 10, 10 40, 30 40, 30 50, 0 50, 0 0, 0 0, 30 0))'::geometry)) AS t(geom);
``````
Terminal
Terminal 2