当前位置:网站首页>[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
边栏推荐
- Troubleshooting and repair of easygbs docking with universal video equipment to broadcast message 487
- Why use CDN content distribution network?
- MySQL architecture
- How does Huawei weautomate RPA achieve the natural growth of government enterprise automation?
- [tcapulusdb knowledge base] how does tcapulusdb add a business cluster cluster?
- 6、 Symbols and commands for numerical calculation of variables
- It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
- AES encryption analysis of CNKI academic translation
- How to select storage space for website construction what factors should be considered in selecting space
- Output type SPED trigger inbound delivery after PGI for inter-company STO's outb
猜你喜欢

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
![[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)
![[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)
随机推荐
On November 11, 2021, live broadcast e-commerce opened a new way to play
Property management source code based on wechat applet
Istio practice manual | meeting the new generation of microservice architecture
Easynvr background channel list timing request touchstreamclient interface optimization
SMS marketing is the key to retain customers
Build your own DSL with go and HCl
Troubleshooting and repair of easygbs docking with universal video equipment to broadcast message 487
Tcapulusdb Jun · industry news collection
Blog platform was falsely blackmailed and the new hacker organization claimed responsibility for the Israeli attack | November 16 global network security hotspot
Dart series: generics in dart classes
Comparison between rule engine and ML model - xlaszlo
Qu'est - ce que le financement des pensions? Quels sont les produits financiers pour les personnes âgées?
3、 Shell variable substring
What you don't know about traifik
Smart supply chain collaborative management platform for the home industry integrated upstream and downstream collaboration of the supply chain to improve management efficiency
How to select storage space for website construction what factors should be considered in selecting space
[tcapulusdb knowledge base] how does tcapulusdb add a business cluster cluster?
Go language core 36 lectures (go language practice and application VII) -- learning notes
Moment. JS how to use epoch time to construct objects
6、 Symbols and commands for numerical calculation of variables