Changeset 3312


Ignore:
Timestamp:
05/02/08 18:44:51 (12 years ago)
Author:
mkochan
Message:

Changed PointSeriesConvertor? to handle conversion using quad-tree. Therefore created a new module QuadTree?.py.

Location:
DPPP/kml/csml2kml
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/csml2kml/PointSeriesConvertor.py

    r3306 r3312  
    55import csml 
    66import urllib 
     7from KMLDocument import * 
     8from QuadTree import * 
     9from utils import wget 
    710 
    8 from KMLDocument import * 
    9 from utils import wget 
     11class Station: 
     12 
     13    def __init__(self, id, desc, lon, lat): 
     14        self.id = id 
     15        self.desc = desc 
     16        self.lon = lon; self.lat = lat 
     17 
     18    def __repr__(self): 
     19        return 'Station %s ("%s")' % (self.id, self.desc) 
    1020 
    1121class PointSeriesConvertor: 
     
    2636        def _parseGMLStationsCollection(textGml): 
    2737            ''' 
    28             Parse the GML returned from GeoServer and yield tuples containing information (gml_id, name, lon, lat) 
    29             about each station. 
     38            Parse the GML returned from GeoServer and yield Station objects with info about each station. 
    3039            ''' 
    3140            root = XML(textGml) 
     
    3746                posElement = pointElement.getchildren()[0] 
    3847                (lon,lat) = map(float, posElement.text.split()) 
    39                 yield (stationElement.get('{http://www.opengis.net/gml}id'), stationNameElement.text, lon, lat) 
     48                yield Station(stationElement.get('{http://www.opengis.net/gml}id'), stationNameElement.text, lon, lat) 
     49 
     50        def _buildKmlDocumentDirectly(kmlDocument, stations, placemarkKmlStyle, stationData): 
     51            for station in stations: 
     52                kmlStation = KMLPlacemark(station.id, station.desc, station.lon, station.lat, 
     53                                          styleID = placemarkKmlStyle.id, data = stationData 
     54                                          ) 
     55                kmlDocument.elements.append(kmlStation) 
     56            return kmlDocument 
     57 
     58        def _buildKmlDocumentUsingQuadTree(kmlDocument, stations, placemarkKmlStyle, stationData): 
     59 
     60            def _quadTreeToKMLElement(t): 
     61                ''' 
     62                Convert the quad-tree into a KMLElement instance containing a folder hierarchy with placemark leaves. 
     63                ''' 
     64                if isinstance(t, NilLeaf):                      # Do nothing 
     65                    pass 
     66                elif isinstance(t, LocLeaf):                    # Create a folder with an embedded region and placemarks 
     67                    kmlRegion = KMLRegion(t.bbox.west, t.bbox.south, t.bbox.east, t.bbox.north) 
     68                    kmlFolder = KMLFolder(repr(t.bbox), [], region = kmlRegion) 
     69                    for location in t.locations: 
     70                        station = location.obj 
     71                        kmlPlacemark = KMLPlacemark( 
     72                            station.desc, station.desc, station.lon, station.lat, 
     73                            styleID = placemarkKmlStyle.id, data = stationData 
     74                            ) 
     75                        kmlFolder.children.append(kmlPlacemark) 
     76                    return kmlFolder 
     77                elif isinstance(t, Node):                       # Create a folder containing nested elements 
     78                    kmlRegion = KMLRegion(t.bbox.west, t.bbox.south, t.bbox.east, t.bbox.north) 
     79                    kmlFolder = KMLFolder(repr(t.bbox), [], region = kmlRegion) 
     80                    for i in t.children: 
     81                        kmlChild = _quadTreeToKMLElement(i) 
     82                        if kmlChild: 
     83                            kmlFolder.children.append(kmlChild) 
     84                    return kmlFolder 
     85             
     86            locations = [] 
     87            for station in stations: 
     88                locations.append( Location(station.lon, station.lat, station) ) 
     89            quadTree = build_quadtree(locations) 
     90            kmlFolder = _quadTreeToKMLElement(quadTree) 
     91 
     92            lockedFolderStyle = KMLStyle('locked_folder_style', listItemType = 'checkHideChildren') 
     93            kmlFolder.styleID = lockedFolderStyle.id 
     94            kmlDocument.styles.append(lockedFolderStyle) 
     95 
     96            kmlDocument.elements.append(kmlFolder) 
     97            return kmlDocument 
     98 
     99        #----------------------------------------------------------------------------- 
    40100 
    41101        # Read conversion configuration 
     
    49109            datumValue = datum.text 
    50110            stationData[datumName] = datumValue 
     111        useQuadTree = self.config.find('UseQuadTree').text == 'yes' 
    51112 
    52113        # Perform the conversion 
    53114        textGml = wget(url) 
    54115        placemarkKmlStyle = createDefaultPlacemarKMLStyle(balloonTemplate = balloonTemplate) 
     116 
     117        stations = _parseGMLStationsCollection(textGml) 
     118 
    55119        kmlDocument = KMLDocument(documentName, [placemarkKmlStyle]) 
    56         generator = _parseGMLStationsCollection(textGml) 
    57         for (gml_id, name, lon, lat) in generator: 
    58             kmlStation = KMLPlacemark(gml_id, name, lon, lat, styleID = placemarkKmlStyle.id, data = stationData) 
    59             kmlDocument.elements.append(kmlStation) 
     120        if useQuadTree: 
     121            kmlDocument = _buildKmlDocumentUsingQuadTree(kmlDocument, stations, placemarkKmlStyle, stationData) 
     122        else: 
     123            kmlDocument = _buildKmlDocumentDirectly(kmlDocument, stations, placemarkKmlStyle, stationData) 
    60124 
    61125        # Save the KML document 
  • DPPP/kml/csml2kml/testdata/midas.csml2kml.conf.xml

    r3306 r3312  
    22  <CSMLStationPointSeries> 
    33    <name>MIDAS stations (example)</name> 
     4    <UseQuadTree>yes</UseQuadTree> 
    45    <GeoServerRequest> 
    56      <URL>http://bond.badc.rl.ac.uk:8089/dummyGeoServer/GetStationCSMLFeatures?gml_id=MIDAS_Stations</URL> 
Note: See TracChangeset for help on using the changeset viewer.