展会信息港展会大全

用postgreSQL做基于地理位置的app
来源:互联网   发布日期:2016-02-17 09:43:43   浏览:1733次  

导读: 前言:项目中用到了postgreSQL中的earthdistance()函数功能计算地球上两点之间的距离,中文的资料太少了,我找到了一篇英文的、讲的很好的文章 ,特此翻译,希望能够帮助到以...

前言:项目中用到了postgreSQL中的earthdistance()函数功能计算地球上两点之间的距离,中文的资料太少了,我找到了一篇英文的、讲的很好的文章,特此翻译,希望能够帮助到以后用到earthdistance的同学。

一、两种可用的选择

当我们想用Postgres作为GEO函数使用时,我们通常有2中选择(据我所知):

1.PostGIS: 为postgreSQL提供了高级GEO函数功能。我用了它一段时间,但是它对于我的需求来说太笨重了。

2.Cube和Earthdistance: 这两个拓展为轻量级的Geo关系实体提供了简单、快速的实现方法。

二、为什么在数据库服务器端做计算

这是件非常明显的事。服务器存储了所有的数据,服务器拓展是用C/C++实现的,非常快。为数据表做索引也能加快计算速度。

三、使用我的选择--Cube and EarthDistance

作为开始,你应该先建一个数据库(我想你知道该怎么做),然后使它们能用我们的架构。 执行:

?

1

2

CREATE EXTENSION cube;

CREATE EXTENSIONearthdistance;

上面的命令创建了大约40个函数,以后我们做数据查询的时候就可以用了。

在我们的例子中,我创建了名为events的表,字段有:id(serial), name(varchar 255), lat(double),lng(double)。(别忘了~~)

四、计算2个坐标之间的距离

计算2个坐标之间的距离,我们要用到earth_distance(ll_to_earth($latlngcube),ll_to_earth($latlng_cube))这个函数。earth_distance()函数接受2组坐标值,返回值一个以米为单位的的数值。这能用于很多场景,比如根据某一位置找到离其最近的发生的新闻事件的列表。

【译者注】这里要提下几个重要的函数:(参考:http://www.postgresql.org/docs/8.3/static/earthdistance.html)

Table F-3. Cube-based earthdistance functions

Function

Returns

Description

earth()

float8

Returnsthe assumed radius of the Earth.

sec_to_gc(float8)

float8

Convertsthe normal straight line (secant) distance between between two points on thesurface of the Earth to the great circle distance between them.

gc_to_sec(float8)

float8

Convertsthe great circle distance between two points on the surface of the Earth to thenormal straight line (secant) distance between them.

ll_to_earth(float8,float8)

earth

Returnsthe location of a point on the surface of the Earth given its latitude (argument1) and longitude (argument 2) in degrees.

latitude(earth)

float8

Returnsthe latitude in degrees of a point on the surface of the Earth.

longitude(earth)

float8

Returnsthe longitude in degrees of a point on the surface of the Earth.

earth_distance(earth,earth)

float8

Returnsthe great circle distance between two points on the surface of theEarth.

earth_box(earth,float8)

cube

Returnsa box suitable for an indexed search using the cube @> operatorfor points within a given great circle distance of a location. Some points inthis box are further than the specified great circle distance from the location,so a second check using earth_distance should be included in thequery.

数据库的操作可能就像下面这样:

?

1

2

3

SELECT events.id events.name,eaerthdiatance(ll_to_earth({currentuserlat}, {currentuserlng}),llto_earth(events.lat, events.lng))

as distancefromcurrentlocation FROM events

ORDER BY distancefromcurretnlocation ASC;

这将给我们一个很nice的新闻事件列表,按他们的离我们当前位置的距离由近到远排序。第一个是离我们最近的。

五、找到某个半径范围内的记录

Cube和Earthdiatance拓展提供的另一个伟大的函数是earth_box(ll_to_earch($latlngcub),$radiusinmetres)。 这个函数通过简单的比较就能到找到某个半径范围内的所有记录。它是靠返回2点之间的“大圆距离”实现的。

【译者注】大圆距离(Great circledisstance)指的是从球面的一点A出发到达球面上另一点B,所经过的最短路径的长度。一般说来,球面上任意两点A和B都可以与球心确定唯一的大圆,这个大圆被称为黎曼圆,而在大圆上连接这两点的较短的一条弧的长度就是大圆距离。如果想了解更多,请看wiki:大圆距离

它能用于查询我们城市中所有的新闻事件:

?

1

2

SELECT events.id, events.name FROM events

WHERE earth_box({currentuserlat},{currentuserlng}, {radiusinmetres}) @> ll_to_earth(events.lat,events.lng);

这条查询语句仅仅会返回在radius_in_metres指定的半径范围内的记录,非常简单吧!

六、提高查询速度

你可能会发现上面的查询有不小的开销。以我的经验,最好对一些字段建立索引。 (下面这条语句假定你又events表,同时events表有字段lat和lng)

?

1

CREATE INDEX ${nameofindex} on events USING gits(lltoearth(lat,lng));

七、数据类型

我的应用比较简单,所以我把经纬度(lat和lng)都设成了double类型。这使得我用Node.js开发起来更加快速,而不用再去自己定制针对GIST类型的解决方案。

八、就这些!

很神奇,对么?!?我们仅仅用常用的数据类型(double)就足以去用一些GEO函数创建基于地理位置的社交app(【译者注】知乎上的一个回答)!

---------------------------

英语水平有限,如有翻译不周之处,请您指点!

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港