简体   繁体   中英

Efficient way to check Geometry type and SRID of Table in Postgres Function

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM