# HG changeset patch # User Sascha L. Teichmann # Date 1559644160 -7200 # Node ID 033366f94abf9463b3d5944794c79fb68155c7b3 # Parent cffa99aa523c273d812395297bd28e4e6e1c12d0 Added a geometry version of best_utm() for convenience. diff -r cffa99aa523c -r 033366f94abf schema/geo_functions.sql --- a/schema/geo_functions.sql Tue Jun 04 12:12:30 2019 +0200 +++ b/schema/geo_functions.sql Tue Jun 04 12:29:20 2019 +0200 @@ -12,16 +12,12 @@ -- * Sascha L. Teichmann -- * Tom Gottfried -CREATE OR REPLACE FUNCTION best_utm(g geography) RETURNS integer AS +CREATE OR REPLACE FUNCTION best_utm(g geometry) RETURNS integer AS $$ DECLARE center geometry; BEGIN - -- Centroid should be calculated on geography to get accurate results - -- from lon/lat coordinates, but the respective PostGIS function returns - -- POINT(-NaN NaN) for some invalid polygons, while the calculation on - -- geometry seems to give reasonable approximations in this context. - SELECT ST_Centroid(CAST(g AS geometry)) INTO center; + SELECT ST_Centroid(g) INTO center; RETURN CASE WHEN ST_Y(center) > 0 THEN @@ -33,3 +29,17 @@ $$ LANGUAGE plpgsql IMMUTABLE; + +CREATE OR REPLACE FUNCTION best_utm(g geography) RETURNS integer AS +$$ +BEGIN + -- Centroid should be calculated on geography to get accurate results + -- from lon/lat coordinates, but the respective PostGIS function returns + -- POINT(-NaN NaN) for some invalid polygons, while the calculation on + -- geometry seems to give reasonable approximations in this context. + RETURN best_utm(CAST(g as geometry)); +END; +$$ +LANGUAGE plpgsql +IMMUTABLE; +