2

I installed QGIS and PostGIS. I have 200 points that I want to display with a radius of 100 miles on a graph of the US.

I have imported my latitude and longitude in the PostGIS database. So I have three fields: [address], [lat], [lng].

1) Do I need to convert the lat and lng fields into a point or geom field? If so how? (st_buffer?) 2) What command/SQL do I use to display the points with radius?

I can query my points like so..

SELECT * FROM postgis_test

I just don't understand how to display all the points on a map with the radius.

Example points:

city        lat          lng
New York    40.7127753  -74.0059728
Los Angeles 34.0522342  -118.2436849
Chicago     41.8781136  -87.6297982
Jim Jones
  • 18,404
  • 3
  • 35
  • 44
user2012677
  • 5,465
  • 6
  • 51
  • 113

1 Answers1

2

First create a geometry or geography column, e.g. with AddGeometryColumn ..

SELECT AddGeometryColumn ('public','postgis_test','geom',4326,'POINT',2);

.. and then update it with the output of ST_Buffer

UPDATE postgis_test 
SET geom = ST_Buffer(ST_SetSRID(ST_MakePoint(lng,lat),4326),50, 'quad_segs=8');

Parameters of ST_Buffer in detail:

  • ST_SetSRID(ST_MakePoint(lng,lat),4326) : As you table had no geometry or geography column I used the function ST_MakePoint to create one. The value 4326 corresponds to the SRS WGS84 - check which one suits your coordinates.
  • 5 : radius of the buffer in degrees. If the first parameter is of type geography this value is interpreted as meters.
  • 'quad_segs=' : number of segments used to approximate a quarter circle (text from the documentation)

After that you'll be able to import it into QGIS using the Add PostGIS Layer option.

Examples

Creating buffers with a radius of 5 degrees

CREATE TABLE public.postgis_test (city TEXT, lng NUMERIC, lat NUMERIC);
SELECT AddGeometryColumn ('public','postgis_test','geom',4326,'polygon',2);

INSERT INTO postgis_test 
VALUES ('New York',-74.00,40.71),
       ('Los Angeles',-118.24,34.05),
       ('Chicago',-87,41.87);

UPDATE postgis_test 
SET geom = ST_Buffer(ST_SetSRID(ST_MakePoint(lng,lat),4326),5, 'quad_segs=8');

Visualizing buffers in QGIS

enter image description here

If you want to display both points and buffers at the same time, you have to create an extra layer (table):

CREATE TABLE public.postgis_test (city TEXT, lng NUMERIC, lat NUMERIC);
SELECT AddGeometryColumn ('public','postgis_test','geom',4326,'point',2);

INSERT INTO postgis_test 
VALUES ('New York',-74.00,40.71),
       ('Los Angeles',-118.24,34.05),
       ('Chicago',-87,41.87);

UPDATE postgis_test SET geom = ST_SetSRID(ST_MakePoint(lng,lat),4326);

CREATE TABLE buffers AS
SELECT city, ST_Buffer(geom,5, 'quad_segs=8') 
FROM postgis_test;

enter image description here

If you prefer to work with meters just cast the geometry column to geography and pass the parameter in meters.

Creating buffers of 100 miles (~160934 meters)

CREATE TABLE public.postgis_test (city TEXT, lng NUMERIC, lat NUMERIC);
SELECT AddGeometryColumn ('public','postgis_test','geom',4326,'point',2);

INSERT INTO postgis_test 
VALUES ('New York',-74.00,40.71),
       ('Los Angeles',-118.24,34.05),
       ('Chicago',-87,41.87);

UPDATE postgis_test SET geom = ST_SetSRID(ST_MakePoint(lng,lat),4326);

CREATE TABLE buffers AS
SELECT city, ST_Buffer(geom::geography,160934, 'quad_segs=8')::geometry 
FROM postgis_test;

enter image description here

Further reading:

Jim Jones
  • 18,404
  • 3
  • 35
  • 44
  • Amazing! Thank you, but I am getting ERROR: missing data for column "geom", when I run COPY cities FROM 'cities.csv' WITH (FORMAT csv); (file has no headers, just city, lng, lat) – user2012677 Jan 21 '20 at 14:59
  • @user2012677 you have to create the geom column **after** importing the data to the table cities. If you do it before, postgres will try to match the amount of columns from the file to the table.. and it will fail :) – Jim Jones Jan 21 '20 at 15:02
  • Success! Thank you, can you please explain what ST_Buffer(ST_SetSRID(ST_MakePoint(lng,lat),4326),5, 'quad_segs=8') is doing, what is the number 5 – user2012677 Jan 21 '20 at 15:06
  • @user2012677 you might also want to take a look at: https://stackoverflow.com/a/49985371/2275388 – Jim Jones Jan 21 '20 at 15:27
  • I can't thank you enough, I learned a ton, one last questions: https://stackoverflow.com/questions/59845293/caculate-point-50-miles-away-north-45-ne-45-sw – user2012677 Jan 21 '20 at 16:25