当前位置:网站首页>[actual combat] how to realize people nearby through PostGIS

[actual combat] how to realize people nearby through PostGIS

2022-06-24 01:44:00 Tencent cloud database tencentdb

| introduction :PostGIS It has the most comprehensive functions in the industry , The most powerful spatial geographic database engine . In real business development , I often meet the needs of so and so nearby , How to realize it quickly ,PostGIS+PostgreSQL Can help you .

This article explains how to pass PostGIS Implement the function of nearby objects , In fact, it is very simple SQL It can be done .

First , We are going to have a PostgreSQL Database instance , And this database instance needs to support PostGIS plug-in unit , No fussy version , This is the basic ability .

First step : Creating plug-ins , Log in to the database instance , In the business database Execute the following command :

\c test

CREATE EXTENSION postgis;

CREATE EXTENSION postgis_topology;

The second step : Create test tables and indexes :

CREATE TABLE t_user(uid int PRIMARY KEY,name varchar(20),location geometry);

CREATE INDEX t_user_location on t_user USING GIST(location);

The third step : Insert test data :

# Create an automatic name generation function :

create or replace function random_string(length integer) returns text as

$$

declare

chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';

result text := '';

i integer := 0;

length2 integer := (select trunc(random() * length + 1));

begin

if length2 < 0 then

raise exception 'Given length cannot be less than 0';

end if;

for i in 1..length2 loop

result := result || chars[1+random()*(array_length(chars, 1)-1)];

end loop;

return result;

end;

$$ language plpgsql;

# Insert 10 million test data

insert into t_user select generate_series(1,10000000), random_string(20),st_setsrid(st_makepoint(150-random()*100, 90-random()*100), 4326);

Step four : Make inquiries from people nearby .

First of all, we can find a coordinate in Baidu map picking coordinate system  http://api.map.baidu.com/lbsapi/getpoint/ . This is used here Take the coordinates of Tiananmen Square as an example :116.404177,39.909652

Step five : Specify the coordinates to query , Just find the nearest coordinate in the database 5 Objects , And output the distance from these five objects to here , The unit here is Hundreds of kilometers .

Be careful :WGS84 It is the most popular geographical coordinate system at present . In the world , Each coordinate system is assigned a EPSG Code ,EPSG:4326 Namely WGS84 Code for .GPS Is based on WGS84 Of , So usually the coordinate data we get is WGS84 Of . Generally, when we store data , Just press WGS84 Storage .

select uid, name, ST_AsText(location), ST_Distance(ST_GeomFromText('POINT(116.404177 39.909652)',4326), location) from t_user order by location <-> 'SRID=4326;POINT(116.404177 39.909652)'::geometry limit 5;

Step six : View this object 1000 All objects and distances within meters :

select uid, name, ST_AsText(location),ST_Distance(ST_GeomFromText('POINT(116.404177 39.909652)',4326), location) from users where ST_DWithin(location::geography, ST_GeographyFromText('POINT(116.404177 39.909652)'), 1000.0);

Step seven : Be accomplished , Is it simple .

Supplementary content : The Martian coordinate system is used domestically , The content in the following links can be converted between several coordinate systems ,https://github.com/geocompass/pg-coordtransform/blob/master/geoc-pg-coordtransform.sql

原网站

版权声明
本文为[Tencent cloud database tencentdb]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211115203515488W.html

随机推荐