域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)
之前用的是ST_Distance 函數(shù),但是貌似需要進(jìn)行一次單位的轉(zhuǎn)換,而且網(wǎng)上有說(shuō)那種轉(zhuǎn)換不是特別準(zhǔn)確,現(xiàn)在暫時(shí)將該算法記錄在此:
1select st_distance(ST_GeomFromText('POINT(120.451737 36.520975)',900913),ST_GeomFromText('POINT(120.455636 36.520885)',900913))*60*1.852;
這里的計(jì)算方式倒是可以換坐標(biāo)系,但是,測(cè)試了兩個(gè)坐標(biāo)系都沒(méi)有起作用。而且該種方式轉(zhuǎn)換過(guò)單位后跟arcgis計(jì)算出的結(jié)果相差甚遠(yuǎn),最終決定使用下面的方式;
今天發(fā)現(xiàn)了另外一種方式來(lái)計(jì)算距離,這種方式可以直接生成單位為米的結(jié)果:
1select ST_Length(Geography(ST_GeomFromText('LINESTRING(120.451737 36.520975,120.455636 36.520885)')));
這種方式的不便在于:
1.要把點(diǎn)轉(zhuǎn)換成線或者其他的圖形而不是點(diǎn);
2.geography函數(shù)現(xiàn)在只支持4326坐標(biāo)系,不能換成其他的。
追加:
上面的方式是計(jì)算點(diǎn)到點(diǎn)的距離,但是如果要想知道某一個(gè)點(diǎn)到某條線的距離是不是在某個(gè)范圍內(nèi),又該如何計(jì)算呢;如下:
1select ST_Contains(St_Astext(ST_Buffer(geography(geomfromtext('MULTILINESTRING((線的坐標(biāo)點(diǎn)))')),25.00{以米為單位的距離})),st_astext(geography(geomfromtext('POINT(121.37805 37.54142)')))) as result
使用類(lèi)似上面的方式,就可以輸入以米為單位的距離判斷某個(gè)點(diǎn)是否在某個(gè)距離范圍內(nèi);
補(bǔ)充:postgresql 搜索指定距離內(nèi)的記錄 按近到遠(yuǎn)排序 并返回距離
實(shí)例如下:
CREATE TABLE mylocation (
id SERIAL PRIMARY KEY,
geom GEOMETRY(Point, 4326),
name VARCHAR(128),
x double precision,
y double precision
);
INSERT INTO mylocation (geom,name,x,y) VALUES (
ST_GeomFromText('POINT(0.0001 0)', 4326),'zhangsan',0.0001,0
);
INSERT INTO mylocation (geom,name,x,y) VALUES (
ST_GeomFromText('POINT(0.001 0)', 4326),'zhangsan',0.001,0
);
INSERT INTO mylocation (geom,name,x,y) VALUES (
ST_GeomFromText('POINT(0.001 0)', 4326),'zhangsan',0.001,0
);
INSERT INTO mylocation (geom,name,x,y) VALUES (
ST_GeomFromText('POINT(0.1 0)', 4326),'zhangsan',0.1,0
);
SELECT id, name,geom,x,y, ST_DistanceSphere(
geom,
ST_GeometryFromText('POINT(0 0)')) distance
FROM mylocation
WHERE ST_DWithin(
geom,
ST_GeomFromText('POINT(0 0)', 4326),
0.001
)ORDER BY distance asc;;
查詢(xún)語(yǔ)句 下面距離單位為m
SELECT id, name,geom,x,y, ST_DistanceSphere(
geom,
ST_GeometryFromText('POINT(0 0)')) distance
FROM mylocation
WHERE ST_DWithin(
geom::geography,
ST_GeomFromText('POINT(0 0)', 4326)::geography,
1000
) ORDER BY distance asc;
搜索結(jié)果
文章來(lái)源:腳本之家
來(lái)源地址:https://www.jb51.net/article/204784.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!