Skip to content

Instantly share code, notes, and snippets.

@tejainece
Last active August 21, 2018 13:24
Show Gist options
  • Save tejainece/64645607676f1d20355a3ee47006f39d to your computer and use it in GitHub Desktop.
Save tejainece/64645607676f1d20355a3ee47006f39d to your computer and use it in GitHub Desktop.
Experimenting with radius search using postgis.
/* Enable postgis */
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE ex1 (
id VARCHAR(10) NOT NULL,
loc geography(POINT, 4326)
);
CREATE INDEX ex1_loc_gix ON ex1 USING GIST (loc);
INSERT INTO ex1 (id, loc) VALUES ('1', st_geographyfromtext('point(59.3497989 18.0706644)'));
INSERT INTO ex1 (id, loc) VALUES ('2', st_geographyfromtext('point(59.3463858 18.0659866)'));
INSERT INTO ex1 (id, loc) VALUES ('3', st_geographyfromtext('point(59.3324452 18.1188154)'));
SELECT id, st_astext(loc), st_distance(loc, st_geographyfromtext('point(59.3497989 18.0706644)')) FROM ex1;
SELECT id, st_astext(loc), st_distance(loc, st_geographyfromtext('point(59.3497989 18.0706644)')) FROM ex1
WHERE st_distance(loc, st_geographyfromtext('point(59.3497989 18.0706644)')) <= 2000;
SELECT id, st_astext(loc), st_distance(loc, st_geographyfromtext('point(59.3497989 18.0706644)')) FROM ex1
WHERE st_dwithin(loc, st_geographyfromtext('point(59.3497989 18.0706644)'), 2000);
DELETE FROM ex1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment