Changeset 3289 for DPPP/kml


Ignore:
Timestamp:
30/01/08 12:29:32 (12 years ago)
Author:
mkochan
Message:

Moved code for building placemarks into KMLDocument.py

Location:
DPPP/kml/csml2kml/python/prototypes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/prototypes/KMLDocument.py

    r3287 r3289  
    1818            self.documentElement.append(style.build()) 
    1919 
    20     def append(self, element): 
     20    def appendElement(self, element): 
    2121        '''Append directly to the <Document> element''' 
    2222        self.documentElement.append(element) 
     
    7878    basicIcon = 'http://maps.google.com/mapfiles/kml/shapes/target.png' 
    7979    return KMLStyle('basic_style', basicIcon, basicBalloonTemplate) 
     80 
     81class KMLPlacemark: 
     82     
     83    def __init__(self, id, name, lon, lat, style='basic_style', data=None): 
     84        self.id = id 
     85        self.name = name 
     86        self.lon = lon 
     87        self.lat = lat 
     88        self.style = style 
     89        self.data = data 
     90 
     91    def build(self): 
     92        placemarkElement = Element('Placemark') 
     93        SubElement(placemarkElement, 'name').text = self.name 
     94        SubElement(placemarkElement, 'open').text = '0' 
     95        SubElement(placemarkElement, 'visibility').text = '0' 
     96        SubElement(placemarkElement, 'styleUrl').text = '#' + self.style 
     97 
     98        lookAtElement = SubElement(placemarkElement, 'LookAt') 
     99        SubElement(lookAtElement, 'longitude').text = str(self.lon) 
     100        SubElement(lookAtElement, 'latitude').text = str(self.lat) 
     101 
     102        pointElement = SubElement(placemarkElement, 'Point') 
     103        SubElement(pointElement, 'coordinates').text = '%f,%f,%f' % (self.lon, self.lat, 0.) 
     104 
     105        # If the "data" dictionary is provided, create the additional <ExtendedData> element, 
     106        # which contains specific data items, which will be automatically substituted 
     107        # for placemarks in the balloon template when the user views the document in Google Earth. 
     108        if self.data: 
     109            extendedDataElement = SubElement(placemarkElement, 'ExtendedData') 
     110            for key in self.data: 
     111                dataElement = SubElement(extendedDataElement, 'Data') 
     112                dataElement.set('name', key) 
     113                value = self.data[key] 
     114                value = value.replace('#ID#', self.id) 
     115                value = value.replace('#NAME#', self.name) 
     116                value = value.replace('#LON#', str(self.lon)) 
     117                value = value.replace('#LAT#', str(self.lat)) 
     118                SubElement(dataElement, 'value').text = value 
     119 
     120        return placemarkElement 
  • DPPP/kml/csml2kml/python/prototypes/buildPointSeriesWithReferencedPlotsUsingGeoServerOO.py

    r3287 r3289  
    66import urllib 
    77 
    8 from KMLDocument import KMLDocument, KMLStyle, getBasicKMLStyle 
     8from KMLDocument import KMLDocument, KMLStyle, KMLPlacemark, getBasicKMLStyle 
    99 
    1010# -------------------------------------------------------------------------------------------------------------------------- 
     
    2424            pass  # socket undefined -- no need to close 
    2525    return content 
    26  
    27 def buildPlacemarkElement(id,name,lon,lat): 
    28  
    29     placemarkElement = Element('Placemark') 
    30     SubElement(placemarkElement, 'name').text = name 
    31     SubElement(placemarkElement, 'open').text = '0' 
    32     SubElement(placemarkElement, 'visibility').text = '0' 
    33     SubElement(placemarkElement, 'styleUrl').text = '#basic_style' 
    34  
    35     lookAtElement = SubElement(placemarkElement, 'LookAt') 
    36     SubElement(lookAtElement, 'longitude').text = str(lon) 
    37     SubElement(lookAtElement, 'latitude').text = str(lat) 
    38  
    39     pointElement = SubElement(placemarkElement, 'Point') 
    40     SubElement(pointElement, 'coordinates').text = '%f,%f,%f' % (lon, lat, 0.) 
    41  
    42     extendedDataElement = SubElement(placemarkElement, 'ExtendedData') 
    43  
    44     dataElement = SubElement(extendedDataElement, 'Data') 
    45     dataElement.set('name', 'feature_id') 
    46     SubElement(dataElement, 'value').text = id 
    47  
    48     return placemarkElement 
    4926 
    5027def parseGMLStationsCollection(textGml): 
     
    7047 
    7148balloonTemplate = '<img src="http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot?feature_id=$[feature_id]">' 
     49eachStationData = {'feature_id': '#ID#'} 
     50 
    7251basicKmlStyle = getBasicKMLStyle(balloonTemplate) 
    7352kmlDocument = KMLDocument('MIDAS stations (example)', basicKmlStyle) 
    7453 
    75 for (gml_id, name, lon, lat) in parseGMLStationsCollection(textGml): 
     54generator =  parseGMLStationsCollection(textGml) 
     55for (gml_id, name, lon, lat) in generator: 
    7656    print 'Station %s ("%s"): @ (%f,%f)' %  (gml_id, name, lon, lat) 
    77     kmlDocument.append( buildPlacemarkElement(gml_id, name, lon, lat) ) 
    78  
     57    kmlStation = KMLPlacemark(gml_id, name, lon, lat, data=eachStationData) 
     58    kmlDocument.appendElement(kmlStation.build()) 
    7959kmlDocument.save('./../../output/exampleps_stations.kml') 
Note: See TracChangeset for help on using the changeset viewer.