Searching in a Radius using Postgres[Marked]
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]的更多相关文章
- 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 ...
- 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 ...
- Postgres全文搜索功能
当构建一个Web应用时,经常被要求加上搜索功能.其实有时候我们也不知道我要搜索个啥,反正就是要有这个功能.搜索确实很重要的特性,这也是为什么像Elasticsearch和Solr这样基于Lucene的 ...
- 在ef core中使用postgres数据库的全文检索功能实战
起源 之前做的很多项目都使用solr/elasticsearch作为全文检索引擎,它们功能全面而强大,但是对于较小的项目而言,构建和维护成本显然过高,尤其是从关系数据库/文档数据库到全文检索引擎的数据 ...
- mysq l错误Table ‘./mysql/proc’ is marked as crashed and should be repaired
续上一篇,解决了上一篇中的问题后,启动成功,但是在数据库中操作会存在一些问题,一些操作报一下异常: Table './mysql/proc' is marked as crashed and shou ...
- postgres创建表的过程以及部分源码分析
背景:修改pg内核,在创建表时,表名不能和当前的用户名同名. 首先我们知道DefineRelation此函数是最终创建表结构的函数,最主要的参数是CreateStmt这个结构,该结构如下 typede ...
- BAS/BRAS/RADIUS简介
标签: java radius协议 linux radius认证服务器 转自: http://blog.csdn.net/sun93732/article/details/5999274 由R ...
- marked.js简易手册
marked.js简易手册 本文介绍的是marked.js.秉持"来之即用"的原则,对它进行简要的翻译和归纳, 安装 在网上引用或者是引用本地文件即可.要么就用命令行: npm i ...
- 解决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 ...
随机推荐
- ubuntu文件定时加密压缩
#!/bin/sh #zip.sh DATE=`date -d yesterday +"%Y%m%d"` #password PASS="123456" if ...
- Linux 的多线程编程的高效开发经验
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- 停止使用循环 教你用underscore优雅的写代码
你一天(一周)内写了多少个循环了? var i; for(i = 0; i < someArray.length; i++) { var someThing = someArray[i]; ...
- Shell实现跳板机,为什么用跳板机
整理自:http://blog.chinaunix.net/uid-22101889-id-3167454.html 注意:请谨慎使用,到现在为止,使用了,我还没找到改回去的方法. 1. 问题 ...
- C#中String跟string的“区别”
string是c#中的类,String是.net Framework的类(在C# IDE中不会显示蓝色) C# string映射为.net Framework的String 如果用string,编译器 ...
- WCF分布式开发步步为赢(7):WCF数据契约与序列化
本节继续学习WCF分布式开发步步为赢(7):WCF数据契约与序列化.数据契约是WCF应用程序开发中一个重要的概念,毫无疑问实现客户端与服务端数据契约的传递中序列化是非常重要的步骤.那么序列化是什么?为 ...
- 配置sshd_config中的PermitRootLogin设置root登录或者禁止root登录
在etc的sshd_config文件中,默认有PermitRootLogin no的配置,这个的意思是禁止root用户登录,如果想要允许root登录,需要su root用户到sshd_config下进 ...
- fuel openstack 在 VirtualBox 上的部署
搞了两天,终于搞好了.说说过程: 一开始用的6.1版本的Fuel.iso,按照国内外的教程装了几遍,但是网路验证始终不能通过.自己又不是很懂网络.后来在网上看到说6.1版的需要fuel-master连 ...
- 欧拉工程第52题:Permuted multiples
题目链接 题目: 125874和它的二倍,251748, 包含着同样的数字,只是顺序不同. 找出最小的正整数x,使得 2x, 3x, 4x, 5x, 和6x都包含同样的数字. 这个题目相对比较简单 暴 ...
- python_pycharm介绍1
1. 常用设置 修改编程风格 File-Setting中,Editor下Colors&Fonts修改即可调整风格. 修改字体大小 pycharm默认字体太小,需调整些,Settings--&g ...