#!/usr/bin/env python

import sys
import kmldom, kmlengine

argc = len(sys.argv)
if argc != 2:
  print 'usage: %s input.kml' % sys.argv[0]
  sys.exit(1)

inputkml = sys.argv[1]

def ReadFile(filename):
  f = open(filename, 'r')
  data = f.read()
  return data

def Indent(depth):
  while depth:
    print '  ',
    depth -= 1

def PrFeatureType(type):
  if type == kmldom.Type_Placemark:
    #print 'Placemark',
    pass
  else:
    #print 'some other Feature',
    #print type
    pass

# This visits a feature.  The type of feature is printed.  If the feature
# is a container such is visited recursively.
"""
| {{:viatges:1250245857762.jpg?300x300|}} | 
<googlemap width="280" height="280" type="map" zoom="14" lat="32.634" 
lon="-16.940" text="Funchal Airport">32.634704,-16.940193,Funchal Airport</googlemap> |
"""
def VisitFeature(feature, depth):    
  Indent(depth)
  PrFeatureType(feature.Type())
  if feature.Type() == 49:
     nom=feature.get_name()
     status,lat,lon = kmlengine.GetFeatureLatLon(feature)
     vlat=str(lat)
     vlon=str(lon)
     abstractview=feature.get_abstractview()
     description=feature.get_description()
     address=feature.get_address()
     region=feature.get_region()
     snippet=feature.get_snippet()
     open=feature.get_open()
     targetid=feature.get_targetid()
     link_googlemap = '<googlemap width="280" height="280" type="map" zoom="14" lat="'+vlat+'" lon="'+vlon+'" text="'+nom+'">'+vlat+','+vlon+','+nom+'</googlemap> |'
     extdata=feature.get_extendeddata()
     if extdata is not None:
         for i in range(extdata.get_data_array_size()):
             clau=extdata.get_data_array_at(i).get_name()
             valor=extdata.get_data_array_at(i).get_value()
             if  clau == 'image':
                 link_imatge = '| {{:viatges:'+valor+'?300x300|}} |'
     print link_imatge+link_googlemap
  
  container=kmldom.AsContainer(feature)
  if container:
    WalkContainer(container, depth+1)

# This visits each feature in the given container (<Document> or <Folder>).
def WalkContainer(container, depth):
  for i in range(container.get_feature_array_size()):
    VisitFeature(container.get_feature_array_at(i), depth)

# The root feature of a KML file is the child feature of the <kml>
# element or the root xml element if that is a Feature.
# If element is neither <kml> nor a feature None is returned.
def GetRootFeature(element):
  kml = kmldom.AsKml(element)
  if kml:
    if kml.has_feature():
      
      return kml.get_feature()
    else:
      return None
  feature = kmldom.AsFeature(element)
  if feature:
    return feature
  return None

# Program main: read the file to memory, parse it, get and visit
# the root feature if such exists.
def main():
  feature = GetRootFeature(kmldom.ParseKml(ReadFile(inputkml)))
  if feature:
    VisitFeature(feature, 0)
    # Python deletes the feature and all of its descendant elements in turn.
  else:
    # The file is a KML fragment.
    print 'No root feature in %s' % inputkml

if __name__ == '__main__':
  main()


