PostGIS can convert Simple Feature geometries into topology. Let's assume a countries table containing country boundaries from the Natural Earth dataset, with a geometry column named geom
, and the WGS84 coordinate system.
The first step is to check the validity of input geometries. PostGIS will refuse to convert invalid geometries to topology elements, so it's very important to fix them or filter them out. We will use the ST_IsValidReason
and ST_IsValid
functions that we learned in Chapter 1, Importing Spatial Data:
SELECT ST_IsValidReason(geom) FROM countries WHERE ST_IsValid(geom) = FALSE;
Luckily, in this case the query returned 0
rows, meaning that all geometries are valid, so we can proceed to the next step.