1

I want to create a trigger (before inserting trigger), to check the geometry type weather is point, polygon, line, or multi-polygon. After that, I want to set the color property. How can I do this?

I am using postgresql 9.4. My OS is Windows 10.

Table Name: Test, column(s): geoData and color.

CREATE OR REPLACE FUNCTION trigger_color()
  RETURNS trigger AS
$BODY$
BEGIN
if(st_geometrytype(NEW.geoData) like 'ST_Point') then
       INSERT INTO "Test"("color") VALUES (red);
          NEW.color = 'red';
elseif(st_geometrytype(NEW.geoData) like 'ST_Polygon') then
       -- your logic here, for example: 
          NEW.color = 'green';
elseif(st_geometrytype(NEW.geoData) like 'ST_Linestring') then
       -- your logic here, for example: 
          NEW.color = 'yellow';
elseif(st_geometrytype(NEW.geoData) like 'ST_MultiPolygon') then
    NEW.color = 'blue';
else
    NEW.color = 'black';
end if;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trigger_color()
  OWNER TO postgres;

It shows me :

ERROR: record "new" has no field "geodata"
SQL state: 42703
Context: SQL statement "SELECT (st_geometrytype(NEW.geoData) like 'ST_Point')"
PL/pgSQL function trigger_color() line 3 at IF

How i can solve this?

1 Answer 1

3

In your trigger before function (using plpgsql language), you can try something like:

if (st_geometrytype(NEW.geom) like 'ST_Point') then
        -- your logic here, for example: 
           NEW.color = 'red';
end if;
7
  • please see the edited code in the above question, i have one issue.
    – Gary
    Apr 4, 2016 at 11:33
  • @Gary Check you geometry field name. Probably "geom" or "the_geom". With capital letter, if any, consider the use of double quotes for your field.
    – WKT
    Apr 4, 2016 at 14:32
  • Thank you but as i mentioned in my question, table name is: Test and it has two columns: geoData and color. i used double quotes too. but error occurs when i fired insert query :: INSERT INTO "Test"("geoData") VALUES (ST_GeomFromText('POINT(25.124 44.215)')); Shows the following error :: ERROR: function st_geometrytype(geography) does not exist SQL state: 42883 Hint: No function matches the given name and argument types. You might need to add explicit type casts. Context: PL/pgSQL function trigger_color1() line 3 at IF
    – Gary
    Apr 5, 2016 at 5:06
  • CREATE OR REPLACE FUNCTION trigger_color1() RETURNS trigger AS $BODY$ BEGIN if(st_geometrytype(NEW."geoData") like 'ST_Point') then NEW.color = 'red'; elseif(st_geometrytype(NEW."geoData") like 'ST_Polygon') then -- your logic here, for example: NEW.color = 'green'; elseif(st_geometrytype(NEW."geoData") like 'ST_Linestring') then -- your logic here, for example: NEW.color = 'yellow'; elseif(st_geometrytype(NEW."geoData") like 'ST_MultiPolygon') then NEW.color = 'blue'; else NEW.color = 'black'; end if; END; $BODY$ LANGUAGE 'plpgsql'
    – Gary
    Apr 5, 2016 at 5:09
  • You are using geography type and, as the error says, ST_geometryType does not exist with that datatype. You can probably cast using: "NEW."geoData"::geometry"
    – WKT
    Apr 5, 2016 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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