PostGreSQL的扩展earthdistance以及安装。

         我猜想一般利用PostGreSQL数据库的都是想利用它的空间数据,地理信息相关的函数或功能的特性吧。一般做基于地理信息数据应用的才会考虑此中数据库。否则,还不如使用Mysql呢。

         PostGreSQL想要处理复杂的地理信息系统的相关数据,有专门的扩展,PostGIS。里面具有丰富的数据类型和功能函数。而我目前的应用使用PostGIS太过于笨重。我的需求很简单:

         查找某点附近某个半径范围内的某类商铺,并按照距离排序。举个具体例子:以我的位置为原点,查找半径1公里范围内的火锅,并按照距离排序。

         这样的功能其实利用PostGreSQL的扩展包内的函数即可,无需安装PostGIS了,并且使用很方便。

          Function     Returns     Description
          earth()     float8     Returns the assumed radius of the Earth.
          sec_to_gc(float8)     float8     Converts the normal straight line (secant) distance between two points on the surface of the Earth to the great circle distance between    them.
         gc_to_sec(float8)     float8     Converts the great circle distance between two points on the surface of the Earth to the normal straight line (secant) distance between them.
         ll_to_earth(float8, float8)     earth     Returns the location of a point on the surface of the Earth given its latitude (argument 1) and longitude (argument 2) in degrees.
         latitude(earth)     float8     Returns the latitude in degrees of a point on the surface of the Earth.
         longitude(earth)     float8     Returns the longitude in degrees of a point on the surface of the Earth.
        earth_distance(earth, earth)     float8     Returns the great circle distance between two points on the surface of the Earth.
        earth_box(earth, float8)     cube     Returns a box suitable for an indexed search using the cube @> operator for points within a given great circle distance of a location. Some points in this box are further than the specified great circle distance from the location, so a second check using earth_distance should be included in the query.


         上面的案例其实利用earthdistance扩展包中最重要的两个函数是earth_distance(earth, earth)earth_box(earth, float8)。前一个是计算两点的距离,后一个是计算某个半径范围内球面范围内包含的点。利用这两个函数基本上就满足我的需求了。例子:

        

/*选择(40.059286,116.418773)点半径1000米范围内的记录,并按照距离排序*/  
SELECT id,earth_distance(ll_to_earth(picture.lat, picture.lng), ll_to_earth(40.059286,116.418773))  
AS dis FROM picture where earth_box(ll_to_earth(40.059286,116.418773),1000) @> ll_to_earth(picture.lat, picture.lng) order by dis desc;

         PS:以上可参考文献:

        <1> http://blog.csdn.net/wusuopubupt/article/details/21621477

        <2> http://www.postgresql.org/docs/8.3/static/earthdistance.html


         知道了怎么使用。就看看怎么安装earthdistance扩展包。其实,它需要安装两个扩展包:

         <1> cube;<2>earthdistance;

          如果你是源码安装,那就很简单了,如果不是,下载源码(下载9.x版本的,8.x版本的太老了)。

          在源码包的contrib目录下有我们需要的两个扩展包。(我的是postgresql-9.3.5/contrib/)

          这里有个问题,如果之前你已经安装了PostGreSQL。那么你直接在你之前执行安装的源码包目录中找到contrib目录。

          然后,从第3步开始执行。如果你已经删除了之前安装的源码包的目录。那么你需要解压源码包,并在源码包的目录下,从<1>执行。

          不过,总之,前提都是你之前已经安装了PostGreSQL。没有安装PostGreSQL,先看我之前的一篇关于PostGreSQL的安装博客。再从<3>执行即可。

          首先你要利用这个源码包的目录执行以下操作:

          <1> ./configure或(./configure --prefix==“安装目录”)

          <2> make

          <3> cd 到postgresql-9.x/contrib/cube目录下,执行./configure-->make-->make install;

          <4>cd 到postgresql-9.x/contrib/earthdistance目录下,执行./configure-->make-->make install;

          <5>然后利用psql命令进入到postgresql数据库(你要使用的数据库),然后执行如下操作即可:               

         CREATE EXTENSION cube;
         CREATE EXTENSION earthdistance;

          

           再强调一下,你必须安装好了postgresql,另外,必须在执行过./configure和make后的源码包目录下,再去contrib目录下找curb和earthdistance执行编译和安装,

           否则,编译总是不通过。


          


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要编译并安装 PostgreSQL 扩展程序,您需要按照以下步骤进行操作: 1. 确保您已经安装PostgreSQL,并且能够连接到数据库。 2. 下载并解压缩扩展程序源代码。通常,扩展程序的源代码会以 tar.gz 或 zip 格式提供。 3. 进入解压缩后的源代码目录,并执行以下命令: ``` make make install ``` 此命令将编译并安装扩展程序。如果您需要使用不同的 PostgreSQL 安装目录,则可以使用以下命令: ``` make PG_CONFIG=/path/to/pg_config make PG_CONFIG=/path/to/pg_config install ``` 其中,/path/to/pg_config 是您的 PostgreSQL 安装中 pg_config 工具的路径。 4. 在 PostgreSQL 数据库中启用扩展程序。要启用扩展程序,您需要使用 CREATE EXTENSION 命令。例如,如果您的扩展程序名称为 my_extension,则可以使用以下命令启用它: ``` CREATE EXTENSION my_extension; ``` 如果您需要在特定的数据库中启用扩展程序,则可以在命令中指定数据库名称: ``` CREATE EXTENSION my_extension WITH SCHEMA my_schema; ``` 其中,my_schema 是您要将扩展程序安装到的模式名称。 5. 验证扩展程序已成功安装。您可以使用以下命令查看已安装扩展程序列表: ``` SELECT * FROM pg_extension; ``` 如果您看到您的扩展程序名称在列表中,则表示扩展程序已成功安装。 这些步骤应该能够帮助您编译并安装 PostgreSQL 扩展程序。请注意,每个扩展程序的安装过程可能会有所不同,因此请务必查看扩展程序的文档以获取更详细的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值