Search code examples
postgresqlgispostgis

Why do I get different geometry value when using st_transform vs changing the table SRID?


I have a table "people" with SRID set to 4326 for the geometry column in the "geometry_columns" postgis view. The value of the geometry column for my record X is:

"0101000020E6100000000000000080404000000000008040C0"

I know I can change the SRID (e.g. from 4326 to 3003) of that geometry column by updating it in the mentioned view, as follows:

ALTER TABLE people
  ALTER COLUMN geometry_column TYPE geometry(POINT, 3003)
    USING ST_SetSRID(geometry_column,3003);

The value of the geometry column for my record X is now:

0101000020BB0B0000000000000080404000000000008040C0

Why do I get a different value in the geometry column by doing so compared to extracting (with TS_TRANSFORM) the transformed geometry like in the query below while keeping SRID to 4326?

SELECT St_transform(geometry_column,3003) FROM people;

The result is the following and it differs from the previous one while I expected it identical:

0101000020BB0B0000328D4934B2BF4C413783531F94E74DC1


Solution

  • In the ALTER TABLE statement you have to transform the geometry, not just set another SRS. In your code you're simply "changing" the SRID of the coordinate, which means that the coordinate pair itself remains unchanged. The only way to transform coordinates from one SRS to another is to use ST_Transform:

    ALTER TABLE people
      ALTER COLUMN geometry_column TYPE geometry(POINT, 3003)
        USING ST_Transform(geometry_column,3003);
    

    Demo: db<>fiddle

    CREATE TABLE people (geometry_column geometry(point,4326));
    INSERT INTO people VALUES ('SRID=4326;POINT(33 -33)');
    
    ALTER TABLE people
      ALTER COLUMN geometry_column TYPE geometry(POINT, 3003)
        USING ST_Transform(geometry_column,3003);
    
    SELECT ST_AsEWKT(geometry_column) FROM people;
    
                           st_asewkt                        
    --------------------------------------------------------
     SRID=3003;POINT(3768164.4084946155 -3919656.244736101)