postgresqlqgispgadmin-4postgresql-triggers

Mixed SRID's in trigger stopping qgis from committing changes


I have a trigger that takes a line, grabs its ST_StartPoint and ST_EndPoint and then grabs the nearest point to those endpoints, and assigns a node_id to a column. This Fiddle shows the trigger as well as some example data. When running this trigger, I am getting an error on QGIS stating the following:

Could not commit changes to layer pipes

Errors: ERROR: 1 feature(s) not added.
  
  Provider errors:
      PostGIS error while adding features: ERROR:  Operation on mixed SRID geometries
    CONTEXT:  SQL statement "SELECT 
      j.node_id,
      i.node_id
                                     FROM ST_Dump(ST_SetSRID(NEW.geom,2346)) dump_line,
      LATERAL (SELECT s.node_id,(ST_SetSRID(s.geom,2346)) 
               FROM structures s
               ORDER BY ST_EndPoint((dump_line).geom)<->s.geom 
               LIMIT 1) j (node_id,geom_closest_downstream),
      LATERAL (SELECT s.node_id,(ST_SetSRID(s.geom,2346))
               FROM structures s
               ORDER BY ST_StartPoint((dump_line).geom)<->s.geom 
               LIMIT 1) i (node_id,geom_closest_upstream)"
    PL/pgSQL function sewers."Up_Str"() line 3 at SQL statement
   

I have attempted to resolve the issue by editing the trigger to this but this has not fixed the problem. Any ideas would be greatly appreciated.


Solution

  • The line ORDER BY ST_EndPoint((dump_line).geom)<->s.geom (and the similar one for the start point) is likely the faulty one.

    You could, again, declare the CRS of s.geom. Note that by doing this any spatial index on structures would not be used, it would have to be created on ST_SetSRID(geom,2346)

    The clean way would be to set the CRS at the column level on the structures table

    alter table structures alter column geom TYPE geometry(point,2346) using st_setSRID(geom,2346);