Skip to content

Instantly share code, notes, and snippets.

@fvanderbiest
Created October 31, 2014 11:05
Show Gist options
  • Save fvanderbiest/2ffb679457f705b43ed4 to your computer and use it in GitHub Desktop.
Save fvanderbiest/2ffb679457f705b43ed4 to your computer and use it in GitHub Desktop.
POI table - postgresql - postgis - index - gist - trigger
CREATE TABLE pois (
id serial primary key,
lon numeric(9,6),
lat numeric(9,6),
name character varying(255),
geom geometry(Point,3857) -- EPSG:3857 is the spherical mercator projection
);
CREATE INDEX geom_idx ON pois USING GIST (geom GIST_GEOMETRY_OPS);
CREATE OR REPLACE FUNCTION update_geom() RETURNS "trigger" AS
$BODY$
BEGIN
IF NEW.lon IS NOT NULL AND NEW.lat IS NOT NULL THEN
NEW.geom:=Transform(GeomFromEWKT('SRID=4326;POINT( ' || NEW.lon || ' ' || NEW.lat || ' )'), 3857);
ELSE
NEW.geom:=null;
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER update_geom_poi BEFORE INSERT OR UPDATE ON pois FOR EACH ROW EXECUTE PROCEDURE update_geom();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment