I'm writing a postgres function to perform some spatial calculations.
I need to error catch incorrect geometries and mismatched SRID's from the two tables that are arguments to the function
the code below works but seems like a poor way to acheive this.
-- check the geometries in each table
EXECUTE format('SELECT DISTINCT ST_GeometryType(geom) FROM %s', master_table)
INTO master_type_holder;
IF master_type_holder != 'ST_Polygon' THEN
RAISE EXCEPTION 'master table geometries must be type ST_Polygon';
END IF;
EXECUTE format('SELECT DISTINCT ST_GeometryType(geom) FROM %s', ref_table)
INTO ref_type_holder;
IF ref_type_holder != 'ST_Polygon' THEN
RAISE EXCEPTION 'Reference table geometries must be type ST_Polygon';
END IF;
Thanks for any suggestions
Since you expect all records to be of the same type, the fastest and safest would be to restrict the column to the proper type, and to simply ensure the column is of the proper type
CREATE TABLE test (id integer, geom geometry(polygon,4326));
SELECT *
FROM geometry_columns
WHERE f_table_name = 'test'
AND type = 'POLYGON' and srid = 4326;