Importing Simple Feature data into topology
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.
Checking the validity of input geometries
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.
Note
It may happen that invalid geometries sneak into some revised version of Natural Earth, in which case the query will return a non-zero result. In that case, the invalid geometries must be repaired...