In the previous chapters we defined the various kinds of geometries that PostGIS provides, how to create them, and how to add them to the database. In this chapter and the next we’ll introduce the core set of functions that work with geometries. This chapter will concentrate on functions that tend to work with single geometries. In the next, we’ll work with functions that relate two or more geometries.
PostGIS offers well over 300 functions and operands. To get an overview, we’ve developed a taxonomy that’s driven by intent of use. This is by no means a rigorous classification nor one that will neatly sort each function into a unique classification without ambiguities. Grouping functions by the types of tasks that we’re trying to accomplish has been the handiest approach in our experience. Before delving into the functions themselves, let’s go through our classification scheme:
In keeping with the fundamental mission of this book, which is to show how to use PostGIS rather than serve as a reference volume, we’ll introduce a dozen functions that are commonly used. You can find an exhaustive listing of all functions and their usage in the official PostGIS manual.
You’ll notice that almost all functions start with the two letters ST. The S stands for “spatial” and the T stands for “temporal,” even though support in the temporal dimension never gained much popularity.
The ST prefix is usually set aside for SQL/MM functions in other spatial databases, but PostGIS uses the prefix both for SQL/MM and for functions unique to PostGIS.
We’ll start with constructors.
As the name implies, constructor functions create geometries. There are two common ways to create new geometries. The first uses raw data in an acceptable format and builds the geometry from scratch. The second way is to take existing geometries and either decompose, splice, slice, dice, or morph them to form new ones. In this section, we start with the first approach. We’ll go through the list of common representations of geometric data and the functions used to transform them into bona fide PostGIS geometry objects. Following that we’ll introduce some handy functions that create new geometries from existing ones.
These indispensable functions will output geometries when you feed them various text or binary representations. They are especially useful for quick viewing of geometries in various desktop tools. In tools that understand only geometries, the use of these functions becomes almost perfunctory.
Recall from chapter 1 that a common way to represent geometries is through well-known text representations. PostGIS provides a function called ST_GeomFromText that can be used to build 2D geometries. This function is an SQL/MM standard function that can be found in other SQL/MM–compliant spatial databases. It supports only 2D because the SQL/MM–released specs for this function don’t support M and Z coordinates. Following are examples of its use:
SELECT * INTO table1 FROM ( VALUES ( ST_GeomFromText('POINT(-100 28)', 4326) ), ( ST_GeomFromText('LINESTRING(-80 28, -90 29)', 4326) ), ( ST_GeomFromText('POLYGON((10 28, 9 29, 7 30, 10 28))' ) ) ) As foo(geom);
PostGIS provides another function called ST_GeomFromEWKT. This is a PostGIS-only function and accepts input from a PostGIS-only format—EWKT (extended WKT)—with the intent of making up for deficiencies in the WKT format. EWKT encodes SRID information directly into the WKT and also supports 3D and 4D geometries. We show you how to use ST_GeomFromEWKT here. Note that EWKT explicitly prepends the SRID of the geometry.
SELECT * INTO table2 FROM ( VALUES (ST_GeomFromEWKT('SRID=4326;POINT(-100 28)')), (ST_GeomFromEWKT('SRID=4326;LINESTRING(-80 28,-90 29)')), (ST_GeomFromEWKT('SRID=4326;POLYGON((10 28, 9 29, 7 30, 10 28))' ) ) ) As foo(geom);
ST_GeomFromEWKT can accept geometries in plain WKT format as well, so it’s often preferred when SQL/MM compliance isn’t a concern.
On many occasions, you’ll find yourself needing to import data from a client application where geometries are already stored in binary representations. This is where the functions ST_GeomFromWKB and ST_GeomFromEWKB come into play. Again, ST_GeomFromWKB is an SQL/MM–defined function, and ST_GeomFromEWKB is a PostGIS extension offering SRID encoding and support for 3D and 4D geometries. These two functions accept byte arrays instead of text strings. One advantage of byte arrays is that they’re exact, whereas the ST_GeomFromText and ST_GeomFromEWKT functions truncate at about the fifteenth digit after the decimal point. Following is an example of using ST_GeomFromWKB:
SELECT ST_GeomFromWKB(E'\001\001\000\000\000\321\256B\312O\304Q\300\ 347\030\220\275\336%E@',4326);
Observe that if you were to output the well-known binary of this function,
SELECT ST_AsBinary(ST_GeomFromWKB(E'\001\001\000\000\000\321\256B\312O\304Q \300\347\030\220\275\336%E@',4326));
it would look like this in pre-PostgreSQL 9.0, but it may look different in newer versions depending on your PostgreSQL bytea_output setting: