help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: distance from Easter Island to Chile


From: Emanuel Berg
Subject: Re: distance from Easter Island to Chile
Date: Wed, 23 Apr 2014 04:49:04 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]