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

我怕爱的太早我们不能终老 提交于 2020-02-05 00:17:06

问题


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

回答1:


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

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;

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;

Further reading:

  • ST_SetSRID
  • World Borders Dataset
  • ST_MakePoint


来源:https://stackoverflow.com/questions/59841660/qgis-postgis-map-pointslat-and-long-with-raduis-on-usa-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!