Search code examples
sqlpostgresqlpostgisqgis

QGIS & PostGIS (map points(lat and long with raduis on USA map)


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

Solution

  • 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: