11

I'm writing a function that has a geometry as a parameter that can be in different projections:

CREATE OR REPLACE FUNCTION foo(in geometry) RETURNS boolean AS $$
DECLARE
    transformed_geom geometry := in;
BEGIN
    IF ST_SRID(transformed_geom) != 32737 THEN
        transformed_geom := ST_Transform(transformed_geom, 32737);
    END IF;
    [ ... ]
END;
$$ language plpgsql;

The function needs the geometry in a specific projection (32737) but I don't want to obligate the caller to make the transformation, I prefer to handle it inside the function.

The question is, instead of check the actual srid, it's safe to always call ST_Transform? Will do anything if the geometry is already in the target projection?

I check with a query like this that it seems to be safe, but it will be awesome if someone can confirm this as I did not see it in the docs.

SELECT ST_AsEWKT(geom), ST_AsEWKT(ST_Transform(geom, 32737)) FROM table_in_32737;
12

The function checks for same in and out srid, and returns untouched if so, as the source code shows:

/*
* If input SRID and output SRID are equal, return geometry
* without transform it
*/
if ( input_srid == output_srid )
    PG_RETURN_POINTER(PG_GETARG_DATUM(0));
  • Thanks for link the source code. That's the unique source of truth. – Francisco Puga Jul 29 '17 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.