rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Apr 03
2008

Euclidean Distance Calculator digg

The following snippet returns the euclidean distance between two places on the globe using the Yahoo Maps API. Replace API_KEY with your Yahoo Maps API key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# Distance Module
# by Pravin Paratey (pravinp at gmail dot com)
# Code is licenced under Creative Commons Attribution-Noncommercial-Share Alike 2.5 India
# http://creativecommons.org/licenses/by-nc-sa/2.5/in/
 
import urllib2, cgi, re
from math import sqrt
 
class Distance:
    """
    Using yahoo maps api (http://developer.yahoo.com/maps/rest/V1/geocode.html),
    this class is responsible for returning the euclidean distance between
    two places
    """
    def getDistance(self, start, end):
        """ Gets the euclidean distance between start and end """
        (start_x, start_y) = self.getCoords(start)
        (end_x, end_y) = self.getCoords(end)
        # 1 degree = 111.12 kms or 69.047 miles
        return sqrt((start_x - end_x) ** 2 + (start_y - end_y) ** 2) * 111.12
 
 
    def getCoords(self, location):
        """ Gets the co-ordinates for the given location """
        url = 'http://local.yahooapis.com/MapsService/V1/geocode?appid=' +
                API_KEY + '&street=' + urllib2.quote(location)
        response = urllib2.urlopen(url)
        (x, y) = self._parseXML(response.read())
        return float(x), float(y)
 
 
    def _parseXML(self, xml):
        """ Parses XML and returns latitude and longitude """
        m = re.findall('<latitude>(\d+.\d+)</latitude><longitude>(\d+.\d+)</longitude>', xml)
        # In case of multiple matches, return 1st match
        return m[0]
 
if __name__ == '__main__':
    d = Distance()
    print 'Distance in kms: '
    print d.getDistance("Hiranandani, Powai, Mumbai", "Dadar Station, Mumbai")

2 Responses (rss) (trackback)

#1

prixie

April 26th, 2008 at 2:42 pm

uh oh- programming ;p

#2

Vartika

May 14th, 2008 at 11:27 am

Hey! Long time, no new postings!?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Apr
07

Palindromic sub-sequences in python

This bit of python code returns all palindromic subsequences in the input string.

[Read More]
Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it!

[Read More]

Featured Projects

Indic to English Transliterator

Indic to English Transliterator

Transliteration is the process of converting a word from one language to another while retaining its phonetic characteristics. This application lets you convert a word from any major Indian language (currently supports Hindi, Marathi, Sanskrit and Bengali) to English.

[Read More]

Deebot

Deebot

Deeb0t is an IRC chat bot capable of making meaningful conversation with other users. It also responds to commands issued by its owner.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.