我在用postgres和postgis。
我有一个geometry的帖子,有一个属性visible_within_m,这是在结果中应该显示Post的距离的多少米。
我可以通过ST_DWithin(geometry, ST_SetSRID(ST_Point(a, b), 4326), 10000)在一些随机点的随机半径内找到帖子
但是,我想知道有多少帖子是可见的,半径为某个随机点。
我怎样才能在任意点的半径内找到多少个可见的柱子呢?
有更好的方法吗?
发布于 2019-12-02 01:22:10
你可以计算每个点到你的圆心之间的距离。如果距离大于半径,那么它就在外面,否则它就在里面。
const EARTH_RADIUS = 6371000;
const toRad = function(num){return num*Math.PI/180};
var calculateDistance =
function(lat1, lon1, lat2, lon2){
var dLat = toRad(lat2 - lat1);
var dLon = toRad(lon2 - lon1);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRad(lat1)) *
Math.cos(toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
var distance = EARTH_RADIUS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return distance;
}发布于 2019-12-02 01:25:22
不要对距离使用常量值,而是使用存储在visible_within_m中的值
SELECT * FROM mytable
WHERE ST_DWithin(geometry, ST_SetSRID(ST_Point(a, b), 4326), visible_within_m);另外,st_dwithin和几何图形使用投影的距离单位,所以对于4326,它是一个(无意义的)距离,在度,而不是米。
https://stackoverflow.com/questions/59130928
复制相似问题