Searching in a Radius using Postgres

Creating a GEO application has never been easier. You can have a fully working "What's close to me" in a matter of minutes using some great open-source tools.

Postgres has lots of features. With the biggest in my opinion being extensions. Which take this amazing Database platform to the next level.

Options

When writing a app that wishes to use Postgres for GEO functions you have 2 choices (to the best of my knowledge):

  • PostGis which provides advance GEO functions to Postgres. I've played with it and it seems way to bulky for my needs
  • Cube and EarthDistance, these 2 extensions provide easy to use and very fast methods to accomplish some of the more minor geo related activities.

Why do the calculations in a database server

It should be pretty obvious. The server already has all the data and with extensions being written in c / c++ it's very fast.

An Index can also be added for a even bigger boost.

Using my Choice - Cube and EarthDistance

To start using these 2 extensions you will need to create a database (which I take it you know how to do) and "create" / enable them for use by the schema you'll be using.

To do this run

CREATE EXTENSION cube;

and

CREATE EXTENSION earthdistance;

This will create +-44 functions that will be usable by your queries, and your user will need to have the permission to EXECUTE on that database

For our examples I created a table named events with a id (serial), name (varchar 255), lat (double) and lng (double) columns. Be sure to keep that in mind.

Calculating the distance between coordinates

To calculate the distance between 2 coordinates we use earthdistance(lltoearth($latlngcube), lltoearth($latlng_cube)). This functions allows us to pass 2 sets of coordinates and it will return a numerical value representing metres.

This can be used for many things such as ordering according to events that are nearest to a certain location. An example of which would be:

SELECT events.id, events.name, earth_distance(ll_to_earth( {current_user_lat}, {current_user_lng} ), ll_to_earth(events.lat, events.lng)) as distance_from_current_location FROM events ORDER BY distance_from_current_location ASC;

This will give us a nice list of events sorted by their distance to our current location. With the closest being first.

Finding Records in a Radius

Another great function provided by these extensions is

earth_box(ll_to_earth($lat, $lng), $radius_in_metres)

this function allows us to perform a simple compare to find all records in a certain radius. This is done by the function by returning the great circle distance between the points, a more thorough explanation is located athttp://en.wikipedia.org/wiki/Great_circle. This could be used to show all events in our current city. An example of such a query:

SELECT events.id, events.name FROM events WHERE earth_box( {current_user_lat}, {current_user_lng}, {radius_in_metres}) @> ll_to_earth(events.lat, events.lng);

This Query would then only return the records in that radius. Pretty easy !

Speeding up these queries

You might have noticed that these queries could get pretty expensive. In my experience it's best to ensure that the table has a index for these fields. This is created using:

CREATE INDEX ${name_of_index} on events USING gist(ll_to_earth(lat, lng));

This will pre-calculate the degrees for each of the coordinates that will make the above queries run much faster as it's not doing those calculations on each row for each run.

This of course assumes that you have a table named events which has a lat and lng column.

Datatypes

For my quick app I simply used double datatypes for my lat and lng columns. This allowed me to use it with one of the NodeJS ORM's that made development much quicker, instead of having to find some custom solution to handle the other GIST datatypes.

That's it !

Amazing right ?!? We've just built a quick database that is able to handle some GEO functions to build amazing mapping and GEO social apps !

Searching in a Radius using Postgres[Marked]的更多相关文章

  1. Discovering the Computer Science Behind Postgres Indexes

    This is the last in a series of Postgres posts that Pat Shaughnessy wrote based on his presentation ...

  2. Searching External Data in SharePoint 2010 Using Business Connectivity Services

    from:http://blogs.msdn.com/b/ericwhite/archive/2010/04/28/searching-external-data-in-sharepoint-2010 ...

  3. Postgres全文搜索功能

    当构建一个Web应用时,经常被要求加上搜索功能.其实有时候我们也不知道我要搜索个啥,反正就是要有这个功能.搜索确实很重要的特性,这也是为什么像Elasticsearch和Solr这样基于Lucene的 ...

  4. 在ef core中使用postgres数据库的全文检索功能实战

    起源 之前做的很多项目都使用solr/elasticsearch作为全文检索引擎,它们功能全面而强大,但是对于较小的项目而言,构建和维护成本显然过高,尤其是从关系数据库/文档数据库到全文检索引擎的数据 ...

  5. mysq l错误Table ‘./mysql/proc’ is marked as crashed and should be repaired

    续上一篇,解决了上一篇中的问题后,启动成功,但是在数据库中操作会存在一些问题,一些操作报一下异常: Table './mysql/proc' is marked as crashed and shou ...

  6. postgres创建表的过程以及部分源码分析

    背景:修改pg内核,在创建表时,表名不能和当前的用户名同名. 首先我们知道DefineRelation此函数是最终创建表结构的函数,最主要的参数是CreateStmt这个结构,该结构如下 typede ...

  7. BAS/BRAS/RADIUS简介

    标签: java radius协议   linux radius认证服务器   转自: http://blog.csdn.net/sun93732/article/details/5999274 由R ...

  8. marked.js简易手册

    marked.js简易手册 本文介绍的是marked.js.秉持"来之即用"的原则,对它进行简要的翻译和归纳, 安装 在网上引用或者是引用本地文件即可.要么就用命令行: npm i ...

  9. 解决mysql Table ‘xxx’ is marked as crashed and should be repaired的问题。

    解决mysql Table 'xxx' is marked as crashed and should be repaired的问题. 某个表在进行数据插入和更新时突然出现Table 'xxx' is ...

随机推荐

  1. windows下安装php5.2.*,php5.3.*,php5.4.*版本的memcache扩展(转)

    拓展安装调试方法: 编写调试php文件 <?php  memcache();  通过命令行执行测试,因为php拓展安装成功与否与apache无关,所以没必要不断重启apache去看phpinfo ...

  2. 利用Linq + Jquery + Ajax 异步分页的实现

    在Web显示的时候我们经常会遇到分页显示,而网上的分页方法甚多,但都太过于消耗带宽,所以我想到了用Ajax来分页,利用返回的Json来处理返回的数据, 大大简化了带宽的压力. 先说下思路,无非就是异步 ...

  3. socket异步通信-如何设置成非阻塞模式、非阻塞模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto

    socket异步通信-如何设置成非阻塞模式.非阻塞模式下判断connect成功(失败).判断recv/recvfrom成功(失败).判断send/sendto 博客分类: Linux Socket s ...

  4. JavaScript 性能分析新工具 OneProfile

    OneProfile 是一个网页版的小工具,可以用全新的方式展示 JavaScript 性能分析的结果,帮助开发者洞悉函数调用关系,优化应用性能. 点击打开 OneProfile 背景 Chrome ...

  5. hadoop,spark,linux上常用命令

    记下常用命令,慢慢补充 1.hadoop 查看hdfs上的目录: hadoop fs -ls /给hdfs上目录授予权限:   hadoop fs -chmod 777 /tmp/hive 在hdfs ...

  6. 深入了解linux下的last命令及其数据源

    http://www.9usb.net/200902/linux-last.html http://blog.csdn.net/chaofanwei/article/details/11826567

  7. JAVA WEB新进展

    哈哈,终于搞来页面来了,丑是丑,但是好的进展. 高手的思路,自己用库建连接池,确实利害. 最关键的一个HELPER代码: package org.smart4j.chapter2.helper; im ...

  8. 使用git整体流程

    一.git提交代码走meger请求的整体流程 工作中使用git推代码时,如果走merge请求,那么也就是说拉代码时拉公共代码库的代码,但是提交时需要先提交到自己的代码库,然后在gitlab上提交mer ...

  9. ios 监听app从后台恢复到前台

    正常情况下,在AppDelegate中实现下面两个方法,能够监听从后台恢复到前台 [cpp] - (void)applicationDidEnterBackground:(UIApplication ...

  10. word2vec——高效word特征提取

    继上次分享了经典统计语言模型,最近公众号中有很多做NLP朋友问到了关于word2vec的相关内容, 本文就在这里整理一下做以分享. 本文分为 概括word2vec 相关工作 模型结构 Count-ba ...