Skip to content

Instantly share code, notes, and snippets.

@bangonkali
Last active December 21, 2016 11:14
Show Gist options
  • Save bangonkali/b307a34f24b1ffb2bebf65552207692b to your computer and use it in GitHub Desktop.
Save bangonkali/b307a34f24b1ffb2bebf65552207692b to your computer and use it in GitHub Desktop.
Some notes on PostGis
-- Creates the table
CREATE TABLE geom_points (
id_pk INTEGER PRIMARY KEY
);
-- Adds the columns
SELECT AddGeometryColumn('geom_points','coordinates','4326','POINT',2); -- (long, lat)
-- Inserts the Latitude and Longitude from Geography to Geometry. (long, lat)
INSERT INTO geom_points(id_pk, coordinates)
VALUES(2, ST_SetSRID(ST_MakePoint(121.052030, 14.575008), 4326), 4326));
-- Snippet to convert from geography to geometry. (long, lat)
ST_SetSRID(ST_MakePoint(121.052030, 14.575008), 4326);
-- Simple search by radius (long, lat)
select * from geom_points
where ST_DWithin(
coordinates::geography, -- the column to search
ST_SetSRID(ST_MakePoint(121.052031, 14.575008), 4326)::geography, -- the center
0.19 -- radius
)
-- Get result as lat long (http://gis.stackexchange.com/a/91321)
select
ST_X(ST_Transform(coordinates, 4326)) AS Longitude,
ST_Y(ST_Transform(coordinates, 4326)) AS Latitude
from geom_points
where ST_DWithin(
coordinates::geography,
ST_SetSRID(ST_MakePoint(14.575008, 121.052031), 4326)::geography,
0.19
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment