当前位置:网站首页>[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
边栏推荐
- [SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)
- Disruptor note 2: disruptor class analysis
- Cost composition and calculation method of system software
- [combat power upgrade] Tencent cloud's first arm architecture instance was launched! Experience the new architecture computing power!
- Troubleshooting and repair of easygbs docking with universal video equipment to broadcast message 487
- How to choose a website construction company self-study website or website construction company
- Collation of commonly used glusterfs commands
- It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
- What are the selected CDN content acceleration scenarios of Tencent cloud?
- Gin framework: implementing distributed log tracing
猜你喜欢
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
随机推荐
Software cost evaluation: basic knowledge interpretation of cosmoc method
Why use CDN content distribution network?
Behind the 1.6 trillion requests per day, the secret of DNSPod - state secret DOH
6、 Symbols and commands for numerical calculation of variables
How to build a high-quality website
Digital case show ‖ made in China for the first time! Tencent cloud tdsql landed in Zhangjiagang bank and worked together to build financial business
How to develop the industrial Internet platform?
8、 Pipeline pipeline construction project
Go language core 36 lectures (go language practice and application VII) -- learning notes
Practical case - Tencent security hosting service MSS helped "zero accident" during the period of digital Guangdong re insurance!
[tcapulusdb knowledge base] how does tcapulusdb add a business cluster cluster?
EasyPlayer. JS play m3u8 error net:: err_ EMPTY_ Response, how to solve it?
Implementation of asynchronous notification and event callback based on guava API
What is the cost of domain name trademark registration? What is the use of domain names and trademarks?
How to learn website construction does website construction need code
Note 3 of disruptor: basic operation of ring queue (without disruptor class)
Why promote steam education?
What is pension finance? What are the pension financial products?
Salesforce uses hyperlink formula field to implement custom jump
[tcapulusdb knowledge base] common problems of tcapulusdb local deployment