title: distance calculation in python for google earth coordinates
body: <p>Hi I have a kml file called Placemark and 4 nodes (placemarks) that are located in an area on Google Earth. Each placemark-node has a longitude and a latitude. </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"&gt;
  &lt;Document&gt;
      &lt;name&gt;placemarks.kml&lt;/name&gt;
      &lt;StyleMap id="m_ylw-pushpin"&gt;
          &lt;Pair&gt;
              &lt;key&gt;normal&lt;/key&gt;
              &lt;styleUrl&gt;#s_ylw-pushpin&lt;/styleUrl&gt;
          &lt;/Pair&gt;
          &lt;Pair&gt;
              &lt;key&gt;highlight&lt;/key&gt;
              &lt;styleUrl&gt;#s_ylw-pushpin_hl&lt;/styleUrl&gt;
          &lt;/Pair&gt;
      &lt;/StyleMap&gt;
      &lt;Style id="s_ylw-pushpin_hl"&gt;
          &lt;IconStyle&gt;
              &lt;scale&gt;1.3&lt;/scale&gt;
              &lt;Icon&gt;
                  &lt;href&gt;http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png&lt;/href&gt;
              &lt;/Icon&gt;
              &lt;hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/&gt;
          &lt;/IconStyle&gt;
      &lt;/Style&gt;
      &lt;Style id="s_ylw-pushpin"&gt;
          &lt;IconStyle&gt;
              &lt;scale&gt;1.1&lt;/scale&gt;
              &lt;Icon&gt;
                  &lt;href&gt;http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png&lt;/href&gt;
              &lt;/Icon&gt;
              &lt;hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/&gt;
          &lt;/IconStyle&gt;
      &lt;/Style&gt;
      &lt;Folder&gt;
          &lt;name&gt;placemarks&lt;/name&gt;
          &lt;open&gt;1&lt;/open&gt;
          &lt;Placemark&gt;
              &lt;name&gt;node0&lt;/name&gt;
              &lt;LookAt&gt;
                  &lt;longitude&gt;21.78832062146911&lt;/longitude&gt;
                  &lt;latitude&gt;38.28791526390673&lt;/latitude&gt;
                  &lt;altitude&gt;0&lt;/altitude&gt;
                  &lt;heading&gt;0.001539813336055052&lt;/heading&gt;
                  &lt;tilt&gt;44.99941206765101&lt;/tilt&gt;
                  &lt;range&gt;990.2435222326291&lt;/range&gt;
                  &lt;gx:altitudeMode&gt;relativeToSeaFloor&lt;/gx:altitudeMode&gt;
              &lt;/LookAt&gt;
              &lt;styleUrl&gt;#m_ylw-pushpin&lt;/styleUrl&gt;
              &lt;Point&gt;
                  &lt;coordinates&gt;21.78400936610002,38.2874355527483,67.51641688336248&lt;/coordinates&gt;
              &lt;/Point&gt;
          &lt;/Placemark&gt;
          &lt;Placemark&gt;
              &lt;name&gt;node1&lt;/name&gt;
              &lt;LookAt&gt;
                  &lt;longitude&gt;21.78832062146911&lt;/longitude&gt;
                  &lt;latitude&gt;38.28791526390673&lt;/latitude&gt;
                  &lt;altitude&gt;0&lt;/altitude&gt;
                  &lt;heading&gt;0.001539813336055052&lt;/heading&gt;
                  &lt;tilt&gt;44.99941206765101&lt;/tilt&gt;
                  &lt;range&gt;990.2435222326291&lt;/range&gt;
                  &lt;gx:altitudeMode&gt;relativeToSeaFloor&lt;/gx:altitudeMode&gt;
              &lt;/LookAt&gt;
              &lt;styleUrl&gt;#m_ylw-pushpin&lt;/styleUrl&gt;
              &lt;Point&gt;
                  &lt;coordinates&gt;21.78453228393861,38.28690995466475,67.51641688336248&lt;/coordinates&gt;
              &lt;/Point&gt;
          &lt;/Placemark&gt;
          &lt;Placemark&gt;
              &lt;name&gt;node2&lt;/name&gt;
              &lt;LookAt&gt;
                  &lt;longitude&gt;21.78832062146911&lt;/longitude&gt;
                  &lt;latitude&gt;38.28791526390673&lt;/latitude&gt;
                  &lt;altitude&gt;0&lt;/altitude&gt;
                  &lt;heading&gt;0.001539813336055052&lt;/heading&gt;
                  &lt;tilt&gt;44.99941206765101&lt;/tilt&gt;
                  &lt;range&gt;990.2435222326291&lt;/range&gt;
                  &lt;gx:altitudeMode&gt;relativeToSeaFloor&lt;/gx:altitudeMode&gt;
              &lt;/LookAt&gt;
              &lt;styleUrl&gt;#m_ylw-pushpin&lt;/styleUrl&gt;
              &lt;Point&gt;
                  &lt;coordinates&gt;21.7848823502596,38.2869152766261,67.51641688336248&lt;/coordinates&gt;
              &lt;/Point&gt;
          &lt;/Placemark&gt;
          &lt;Placemark&gt;
              &lt;name&gt;node3&lt;/name&gt;
              &lt;LookAt&gt;
                  &lt;longitude&gt;21.78832062146911&lt;/longitude&gt;
                  &lt;latitude&gt;38.28791526390673&lt;/latitude&gt;
                  &lt;altitude&gt;0&lt;/altitude&gt;
                  &lt;heading&gt;0.001539813336055052&lt;/heading&gt;
                  &lt;tilt&gt;44.99941206765101&lt;/tilt&gt;
                  &lt;range&gt;990.2435222326291&lt;/range&gt;
                  &lt;gx:altitudeMode&gt;relativeToSeaFloor&lt;/gx:altitudeMode&gt;
              &lt;/LookAt&gt;
              &lt;styleUrl&gt;#m_ylw-pushpin&lt;/styleUrl&gt;
              &lt;Point&gt;
                  &lt;coordinates&gt;21.78459887820567,38.28740826552452,67.51641688336248&lt;/coordinates&gt;
              &lt;/Point&gt;
          &lt;/Placemark&gt;
      &lt;/Folder&gt;
  &lt;/Document&gt;
  &lt;/kml&gt;
  </code></pre>
  
  <p>What I want is to calculate the distance between node0 and node2,3,4... (keeping node0 constant in the distance function) and then, print the results.</p>
  
  <p>By using the code below: (I need to modify it in order to have the output below)</p>
  
  <pre><code> # adapted from haversine.py &lt;https://gist.github.com/rochacbruno/2883505&gt;
      # see also &lt;http://en.wikipedia.org/wiki/Haversine_formula&gt;
      from math import atan2, cos, sin, sqrt, radians
  
      def calc_distance(origin, destination):
          """great-circle distance between two points on a sphere
             from their longitudes and latitudes"""
          lat1, lon1 = origin
          lat2, lon2 = destination
          radius = 6371 # km. earth
  
          dlat = radians(lat2-lat1)
          dlon = radians(lon2-lon1)
          a = (sin(dlat/2) * sin(dlat/2) + cos(radians(lat1)) * cos(radians(lat2)) *
               sin(dlon/2) * sin(dlon/2))
          c = 2 * atan2(sqrt(a), sqrt(1-a))
          d = radius * c
  
          return d
  
      from xml.dom import minidom
  
      xmldoc = minidom.parse("placemarks.kml")
      kml = xmldoc.getElementsByTagName("kml")[0]
      document = kml.getElementsByTagName("Document")[0]
      placemarks = document.getElementsByTagName("Placemark")
  
      nodes = {}
      for placemark in placemarks:
          nodename = placemark.getElementsByTagName("name")[0].firstChild.data[:-1]
          coords = placemark.getElementsByTagName("coordinates")[0].firstChild.data
          lst1 = coords.split(",")
          longitude = float(lst1[0])
          latitude = float(lst1[1])
          nodes[nodename] = (latitude, longitude)
  </code></pre>
  
  <p>Ang get an output like:</p>
  
  <pre><code>node1: (21.78453228393861, 38.28690995466475), distance from node0: 
  node2: (21.78488235025960, 38.28691527662610), distance from node0: 
  node3: (21.78459887820567, 38.28740826552452), distance from node0: 
  </code></pre>