# Calculate Distance Between Latitude/Longitude Points

This page presents a variety of calculations for latitude/longitude points, with the formula and code fragments for implementing them.

All these formula are for calculations on the basis of a spherical earth (ignoring ellipsoidal effects) – which is accurate enough for most purposes [In fact, the earth is very slightly ellipsoidal; using a spherical model gives errors typically up to 0.3%].

### Distance

This uses the haversine formula to calculate the great-circle distance between two points – that is, the shortest distance over the earth’s surface – giving an “as-the-crow-flies” distance between the points (ignoring any hills they fly over, of course!).

Haversine formula: $$haversin{\left(\dfrac{d}{r}\right)} = haversin{\left(\phi_2-\phi_1\right)} + \cos{\phi_1}\cos{\phi_2}\,haversin{\left(\lambda_2-\lambda_1\right)}$$

where

$haversin(\theta) = \sin^2\left(\dfrac{\theta}{2}\right) = \dfrac{1-\cos(\theta)}{2}$

$$d$$ is the distance between the two points (along a great circle of the sphere)

$$r$$ is the radius of the sphere

$$\phi_1$$, $$\phi_2$$: latitude of point 1 and latitude of point 2

$$\lambda_1$$, $$\lambda_2$$: longitude of point 1 and longitude of point 2

# Downgrade MongoDB From 3.0 to 2.6

Due to a Meteor bug (actually it’s a bug from MongoDB nodejs driver, but the bug has already been fixed by the nodejs driver, while the issue is Meteor is referencing an old version of this driver), I have to downgrade MongoDB from 3.0.5 to 2.6.10. Normally this is a trival work, but it cost me a whole afternoon to do this, because there are serveral traps in there, so I decide to write this blog to help other people who meet the same issue with me.

The main problem is MongoDB 3.0 uses a new version of authentication algorithom SCRAM-SHA-1, while 2.6 uses an old version MONGODB-CR. Actually there are other methods besides these two. These two are the default configuration. If one upgrades MongoDB from 2.6 to 3.0, the authentication schema version can be upgraded by authSchemaUpgrade from 3 to 5, and in the same time the authentication algorithom will be upgraded from MONGODB-CR to SCRAM-SHA-1. However, when one downgrades MongoDB from 3.0 to 2.6, you can not use authSchemaUpgrade to downgrade the authentication schema, and it will remain in 5 and the authentication algorithm will remain in SCRAM-SHA-1, which are not supported by MongoDB 2.6. So yes, without additional work, you are not able to log into the database.

This article provides a method to solve this issue, which is not documented by MongoDB official website (and appearantly not documented in anywhere).

# Automatically Backup MongoDB With AWS CLI

Before that, let’s review some existing methods. One method is use the open source project s3cmd. Originally it doesn’t support the v4 signature, and after a long time, it finally supports v4 signature. The issue is it still doesn’t support the China region S3 server. Due to the maintainer is only one person and he doesn’t have enough time on this project, I guess it will another long period of time to fix this. Another method is to use mongodb-s3-backup. After a little modification, it can handle Chinese region S3 server well, but it doesn’t support v4 signature.

So I just creat a new script: https://github.com/leonsim/mongo-s3-backup.

Even though the serieze of HeartBleeding bugs makes HTTPS (SSL) look vulnerable, I still believe after bugs fixed, HTTPS is more secure than HTTP. Actually we can archieve this in a few simple steps with technologies like OpenSSL, free certificate provider, nginx configuration.

# Setting Up MongoDB Replication on EC2 With Ubuntu 14.02

To setup a meteor production environment on EC2, we need to install mongoDB as the main database. To use the MUP(Meteor Up tool), it’s easier to use Ubuntu operating system.

### Gentoo

emerge --sync
emerge --update --deep --with-bdeps=y --newuse world
revdep-rebuild -ip


eselect news list


### Debian

apt-get update


### Red hat

yum update


# Compile Customized Vim on Mac OS

For Mac OS developers, it’s essential to build your own arsenal. For most Linux utilities missed on Mac OS, just use homebrew to install. There is one exeption I make: Vim.

There is an integrated Vim on Mac OS, and also several brew version of Vim for choose. However, sometimes you find you have to compile Vim by source code, for getting the latest version of Vim, for customizing some configuration, for using some special features, or something else.

# Automatically Mount an Amazon EBS Volume

After you attach an Amazon EBS volume to your instance, it is exposed as a block device. You can format the volume with any file system and then mount it. After you make the EBS volume available for use, you can access it in the same ways that you access any other volume. Any data written to this file system is written to the EBS volume and is transparent to applications using the device.

Note that you can take snapshots of your EBS volume for backup purposes or to use as a baseline when you create another volume. For more information, see Amazon EBS Snapshots.

Use the following procedure to make the volume available. Note that you can get directions for volumes on a Windows instance from Making the Volume Available on Windows in the Amazon EC2 User Guide for Microsoft Windows Instances.

# Mercurial Wrong Architecture Error on Mac OS X

If you install Mercurial(hg) via brew install mercurial, it succeeds to install, but when you run hg command, it fails with the following error message:

# Switch Netgear WNDR4300 From DD-WRT to Open-WRT

DD-WRT is a really nice way to free your router. It has a polished web interface, gives you far greater control than most proprietary firmware, and is supported on a large number of devices. However, Open-WRT is better! In my installed version of DD-WRT, ssh doesn’t work, and I can not install an external driver like rtl8187 for my Alfa AWUS036H. After seaching Google for some time, I realise that I need to change the operating system, even though installing DD-WRT cost me several hours.

# Root Android 5 in a Hacker Way

So why bother to root an Android device?

Because Android’s permission control system is so weak and unsatisfied. Any app may apply more permissions than need, and if you really need some apps but don’t like to give them some permissions, there is no way to use them with selected permissions unless to root the device. Aother drawback is the battery usage. Apps like to live as long as they can. They don’t want to die or be killed. In my experience, the battery usage for a constant time increases almost linearly for the number of apps installed. So to save battery and make a fair enviroment for all apps installed, you need manual control of apps, which needs a root access of a device.

Before ROOTing, install adb and fastboot from Android’s official website.

# Diversity and Possibility

Diversity and Possibility, two of the most important things to me.

That’s why I use Nexus (that can not be bought directly in my country). That’s why I use both Chromebook and Macbook. That’s why I use Dvorak keyboard layout. That’s why I joined big companies, middle companies and start-ups. That’s why I learn different kinds of languages.

I will pursue them for all my life, and that’s why I came to California a second time.

# How to Tip Properly in the US

It’s really a headache problem for a non-local person to tip properly. Before I come to the US, I have no experience about this. Unfortunately, I didn’t do any preparation about tipping before I come here. Fortunately, I find this table :D

# POJ 3468 a Simple Problem With Integers

Like POJ 3264 Balanced Lineup, segment tree is used.

The point is not to push down delta value to descendents, just keeping it on current node if the addition is for all its descendents.

Source code:

# POJ 3264 Balanced Lineup

This is a classic range minimum query and range maximum query problem.

Segment tree, with $$O(\log_2{n})$$ updating complexity and $$O(\log_2{n})$$ query complexity, is a perfect data structure for this problem.

Actually this is really easy once segment tree is imported.

Source code:

# Phonetic Alphabet

Today I study phonetic alphabet, which is really helpful the moment like pronounsing B as D.

# Open Source Python Based Wiki: First Wiki

First Wiki is a wiki written in Python. I write this because I counln’t find a beautiful wiki in Python.

First Wiki is based on shire, which is a web framework based on Tornado Web Server, MongoDB and Bootstrap.

First Wiki is open source, license is https://github.com/leonsim/firstwiki/blob/master/LICENSE. Source code is hosted on GitHub.

# Resetting Default Input Method in Mac OS

I became a Dvorak guy. Before that, I had initialized my Mac to a U.S. keyboard layout. After I changed my Mac to Dvorak keyboard layout, everything worked like a charm except the login window was still the U.S. layout. This frustrates me a lot. So today I spend a little time to reslove this issue.

# How to Get Coordinate of a ClickableSpan Inside a TextView

I have a TextView with many ClickableSpan.

On click on a ClickableSpan, I have to get the coordinate on screen of it (to show a custom View at his position).

The problem is that the onClick() method of the ClickableSpan gives me in parameter a View, the TextView which contains the ClickableSpan.

Thanks to Google, I find this solution. This helps me to win a Leopard Buffet. :D

# Write LaTeX in Octopress With MathJax

The easiest method is to add by Mathjax CDN, but the drawback are it’s not always stable and it doesn’t work if you want to develop offline and want to preview via the rake preview command. So I have to embed a customized version of Mathjax into Octopress. With this method, I can preview equations offline and it’s more stable – it doesn’t depend on the public CDN of Mathjax. OK, DIY start.