In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
People nearby:
The location data of map elements are represented by two-dimensional longitude and latitude, longitude range (- 180,180), latitude range (- 90,90], latitude positive and negative by the equator, north positive and negative south, longitude positive and negative by the prime meridian (Greenwich Observatory, UK). East plus or minus west. For example, the Nuggets Office in Wangjing SOHO, its latitude and longitude coordinates are (116.48105) 39.996794), are positive numbers, because China is located in the northeast hemisphere.
When the distance between two elements is not very far, the distance between elements can be calculated by using Pythagorean theorem directly. We usually use the "nearby people" function, the element distance is not very large, Pythagorean theorem to calculate the distance is enough. However, it should be noted that the density of longitude and latitude coordinates is different (the earth is an ellipse). When calculating the square deviation by Pythagorean law, it needs to be weighted according to a certain coefficient ratio. If it is not accurate, it does not need to be weighted.
If the latitude and longitude coordinates of an element are now stored in a relational database (element id, longitude x, latitude y), how do you calculate it?
First of all, you can't calculate the distance between all the elements and the target elements all the time, and then sort them. The amount of calculation is too large to meet the performance indicators. The general method is to limit the number of elements through a rectangular region, and then calculate the full distance of the elements in the region and then sort them. In this way, the amount of calculation can be reduced obviously. How to divide the rectangular area? You can specify a radius r, and you can circle it with a SQL. When users are not satisfied with the results of the screening, then expand the radius to continue the screening.
Select id from positions where x0Murr
< x < x0+r and y0-r < y < y0+r 为了满足高性能的矩形区域算法,数据表需要在经纬度坐标加上双向复合索引 (x, y),这样可以最大优化查询性能。 但是数据库查询性能毕竟有限,如果「附近的人」查询请求非常多,在高并发场合,这可能并不是一个很好的方案。 业界比较通用的地理位置距离排序算法是 GeoHash 算法,Redis 也使用 GeoHash 算法。GeoHash 算法将二维的经纬度数据映射到一维的整数,这样所有的元素都将在挂载到一条线上,距离靠近的二维坐标映射到一维后的点之间距离也会很接近。当我们想要计算「附近的人时」,首先将目标位置映射到这条线上,然后在这个一维的线上获取附近的点就行了。 Redis 的 Geo 指令基本使用 geoadd 指令携带集合名称以及多个经纬度名称三元组,注意这里可以加入多个三元组 127.0.0.1:6379>Geoadd company 116.48105 39.996794 juejin
Distanc
The geodist instruction can be used to calculate the distance between two elements, carrying the set name, 2 names, and distance units.
127.0.0.1 purl 6379 > geodist company juejin ireader km
Get element location
The geopos instruction can get the latitude and longitude coordinates of any element in the collection, and can get more than one at a time.
127.0.0.1 purl 6379 > geopos company juejin
Get the hash value of the element
Geohash can get the latitude and longitude encoding string of an element, which, as mentioned above, is base32 encoding. You can use this code value to go straight to geohash.org/$ {hash}. The standard coding value of geohash.
127.0.0.1 purl 6379 > geohash company ireader
Http://geohash.org/
A nearby company
The georadiusbymember instruction is the most critical instruction, which can be used to query other elements near the specified element, and its parameters are very complex.
Within a range of 20 kilometers, up to three elements are arranged according to distance, and it will not exclude itself.
127.0.0.1 asc 6379 > georadiusbymember company ireader 20 km count 3
Http://researchlab.github.io/2018/10/06/redis-09-geohash/
The above content comes from Lao Qian's redis in-depth adventure, and it seems that the paper version is also available now. You can buy it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.