您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用postgresql怎么計(jì)算兩點(diǎn)之間的距離,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
postgresql計(jì)算兩點(diǎn)距離
select ST_Distance( ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography, ST_SetSRID(ST_MakePoint(106.00231199774656,29.719258550486572),4326)::geography ), ST_Length( ST_MakeLine( ST_MakePoint(115.97166453999147,28.716493914230423), ST_MakePoint(106.00231199774656,29.719258550486572) )::geography )
備注:
ST_GeomFromText('LINESTRING(115.97166453999147 28.716493914230423,106.00231199774656 29.719258550486572)')與 ST_MakeLine( ST_MakePoint(115.97166453999147,28.716493914230423), ST_MakePoint(106.00231199774656,29.719258550486572) )等價(jià) ST_GeomFromText('POINT(115.97166453999147 28.716493914230423)',4326)與 ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)等價(jià) ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography與 Geography(ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326))、 ST_GeographyFromText('SRID=4326;POINT(115.97166453999147 28.716493914230423)')等價(jià) (::geography是postgis中的轉(zhuǎn)換類型語法,把geometry轉(zhuǎn)成geography)
補(bǔ)充:postgresql計(jì)算兩點(diǎn)歐式距離(經(jīng)緯度地理位置)
我就廢話不多說了,大家還是直接看代碼吧~
create or replace function getdistance ( lon1 numeric, lat1 numeric, lon2 numeric, lat2 numeric ) returns int as $body$ declare v_distance numeric; v_earth_radius numeric; radLat1 numeric; radLat2 numeric; v_radlatdiff numeric; v_radlngdiff numeric; begin --地球半徑 v_earth_radius:=6378137; radLat1 := lat1 * pi()/180.0; radLat2 := lat2 * pi()/180.0; v_radlatdiff := radLat1 - radLat2; v_radlngdiff := lon1 * pi()/180.0 - lon2 * pi()/180.0; v_distance := 2 * asin(sqrt(power(sin(v_radlatdiff / 2), 2) + cos(radLat1) * cos(radLat2) * power(sin(v_radlngdiff/2),2))); v_distance := round(v_distance * v_earth_radius); return v_distance; end; $body$ language 'plpgsql' volatile;
create or replace function getdistance ( i_lngbegin real, i_latbegin real, i_lngend real, i_latend real ) returns float as $body$ /* * * select getdistance_bygispoint(116.281524,39.957202,117.648673,38.42584) as distance; * */ declare v_distance real; v_earth_radius real; v_radlatbegin real; v_radlatend real; v_radlatdiff real; v_radlngdiff real; begin --地球半徑 v_earth_radius:=6378.137; v_radlatbegin := i_latbegin * pi()/180.0; v_radlatend := i_latend * pi()/180.0; v_radlatdiff := v_radlatbegin - v_radlatend; v_radlngdiff := i_lngbegin * pi()/180.0 - i_lngend * pi()/180.0; v_distance := 2 * asin(sqrt(power(sin(v_radlatdiff / 2), 2) + cos(v_radlatbegin) * cos(v_radlatend) * power(sin(v_radlngdiff/2),2))); v_distance := v_distance * v_earth_radius*1000; return v_distance; end; $body$ language 'plpgsql' volatile;
上述內(nèi)容就是使用postgresql怎么計(jì)算兩點(diǎn)之間的距離,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。