all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <embe8573@student.uu.se>
To: help-gnu-emacs@gnu.org
Subject: Re: distance from Easter Island to Chile
Date: Wed, 23 Apr 2014 04:49:04 +0200	[thread overview]
Message-ID: <87lhuwu5en.fsf@nl106-137-194.student.uu.se> (raw)
In-Reply-To: 87mwffdkhj.fsf@nl106-137-194.student.uu.se

Made this into a CLI tool that fetches data (with curl)
from Wikipedia.

Even if you don't care for the CLI, the Emacs code is
improved, too.

It is sort of *slow* because of the fetch, and the
start of Emacs, but perhaps the parsing is
heavy-handed, as well.

Try:

distance 'Easter Island' 'Santiago de Chile'
distance Peking Paris
# etc.

Pretty cool, huh?

I put all the files here:

http://user.it.uu.se/~embe8573/distance/

1) loc

#!/bin/zsh

args=$(echo $* | sed 's/ /%20/g')
url=https://en.wikipedia.org/w/index.php\?title=$args\&printable=yes
coords=$(curl -s $url | grep -o '"geo">[-.[:digit:]]*; [-.[:digit:]]*<' | head -n 1 | sed 's/\("geo">\|-\|;\|<\)//g')

2) distance

#!/bin/zsh

coords1=`loc $1`
coords2=`loc $2`
lat1=$(echo $coords1 | cut -d" " -f1)
long1=$(echo $coords1 | cut -d" " -f2)
lat2=$(echo $coords2 | cut -d" " -f1)
long2=$(echo $coords2 | cut -d" " -f2)
emacs -Q -script distance.el $1 $lat1 $long1 $2 $lat2 $long2

3) distance.el

(require 'cl)

(defun sin2 (p)
  (let ((sin-p (sin p)))
    (* sin-p sin-p) ))

(defun haversine (a b)
  (sin2 (/ (- a b) 2) ))

(setq earth-radius 6367.4447)

(defun distance (p1 p2)
  (let ((from  (car p1))
        (to    (car p2))
        (lat1  (degrees-to-radians (caadr  p1)))
        (long1 (degrees-to-radians (cadadr p1)))
        (lat2  (degrees-to-radians (caadr  p2)))
        (long2 (degrees-to-radians (cadadr p2))) )
    (message "%s -> %s: %.2f km" from to
             (* 2 earth-radius
                (asin
                 (sqrt
                  (+ (haversine lat2 lat1)
                     (* (cos lat2)
                        (cos lat1)
                        (haversine long2 long1) ))))))))

(let ((from  (elt argv 0))
      (lat1  (string-to-number (elt argv 1)))
      (long1 (string-to-number (elt argv 2)))
      (to    (elt argv 3))
      (lat2  (string-to-number (elt argv 4)))
      (long2 (string-to-number (elt argv 5))) )
  (distance (list from (list lat1 long1))
            (list to   (list lat2 long2)) ))

-- 
underground experts united:
http://user.it.uu.se/~embe8573


  reply	other threads:[~2014-04-23  2:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-20  0:03 distance from Easter Island to Chile Emanuel Berg
2014-04-20  3:55 ` Frank Stutzman
2014-04-20 15:14   ` Emanuel Berg
2014-04-20 22:39     ` giacomo.boffi
     [not found]     ` <mailman.19957.1398033917.10748.help-gnu-emacs@gnu.org>
2014-04-21  1:40       ` Emanuel Berg
2014-04-21  1:50         ` Emanuel Berg
2014-04-21  9:45         ` giacomo.boffi
     [not found]         ` <mailman.19981.1398073560.10748.help-gnu-emacs@gnu.org>
2014-04-21 10:35           ` Emanuel Berg
2014-04-21 10:50             ` Emanuel Berg
2014-04-23  2:49               ` Emanuel Berg [this message]
2014-04-21  2:16       ` Emanuel Berg
2014-04-20  4:44 ` Barry Margolin
2014-04-20 15:06   ` Emanuel Berg
2014-04-20  6:28 ` Eli Zaretskii
     [not found] ` <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>
2014-04-20  7:01   ` Barry Margolin
2014-04-20  7:48     ` Eli Zaretskii
2014-04-20 10:26       ` Thorsten Jolitz
2014-04-20 11:03         ` Eli Zaretskii
2014-04-20 15:22     ` Emanuel Berg
2014-04-21  6:04       ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lhuwu5en.fsf@nl106-137-194.student.uu.se \
    --to=embe8573@student.uu.se \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.