示例#1
0
def createCache(server, xml):
   import xml.etree.ElementTree as ET
   import json
   
   # Save out the xml file for later
   utils.saveFile(SERVERCACHEPATH + server['name'] + FILEEXTENSIONXML, xml)
   
   print 'Creating caches...'
   subMasterCache = {}
   subMasterCache['server'] = {}
   
   #ET.register_namespace(NAMESPACE, NAMESPACE)
   root = ET.fromstring(xml)
   
   if root.find('./%sCapability/%sLayer/%sLayer' % (NAMESPACE,NAMESPACE,NAMESPACE)) == None:
      dirtyCaches.append(server)
      return
   
   for service in root.findall('./%sService' % (NAMESPACE)):
      serverTitle = service.find('./%sTitle' % (NAMESPACE)).text
      serverAbstract = service.find('./%sAbstract' % (NAMESPACE)).text if service.find('./%sAbstract' % (NAMESPACE)) is not None else None
   
   for product in root.findall('./%sCapability/%sLayer/%sLayer' % (NAMESPACE,NAMESPACE,NAMESPACE)):
      sensorName = product.find('./%sTitle' % (NAMESPACE)).text
      
      if utils.blackfilter(sensorName, productBlackList):
         sensorName = utils.replaceAll(sensorName, {' ':'_', '(':'_', ')':'_', '/':'_'})
         print sensorName
         layers = []
         
         for layer in product.findall('./%sLayer' % (NAMESPACE)):
            name = layer.find('./%sName' % (NAMESPACE)).text
            title = layer.find('./%sTitle' % (NAMESPACE)).text
            abstract = layer.find('./%sAbstract' % (NAMESPACE)).text
            temporal = False
            
            exGeographicBoundingBox = {"WestBoundLongitude": layer.find('./%sEX_GeographicBoundingBox/%swestBoundLongitude' % (NAMESPACE,NAMESPACE)).text,
                                       "EastBoundLongitude": layer.find('./%sEX_GeographicBoundingBox/%seastBoundLongitude' % (NAMESPACE,NAMESPACE)).text,
                                       "SouthBoundLatitude": layer.find('./%sEX_GeographicBoundingBox/%ssouthBoundLatitude' % (NAMESPACE,NAMESPACE)).text,
                                       "NorthBoundLatitude": layer.find('./%sEX_GeographicBoundingBox/%snorthBoundLatitude' % (NAMESPACE,NAMESPACE)).text}
            
            boundingBox = {"CRS": layer.find('./%sBoundingBox' % (NAMESPACE)).get('CRS'),
                           "MinX": layer.find('./%sBoundingBox' % (NAMESPACE)).get('minx'),
                           "MaxX": layer.find('./%sBoundingBox' % (NAMESPACE)).get('maxx'),
                           "MinY": layer.find('./%sBoundingBox' % (NAMESPACE)).get('miny'),
                           "MaxY": layer.find('./%sBoundingBox' % (NAMESPACE)).get('maxy')}
            
            dimensions = createDimensionsArray(layer, server)
            temporal = dimensions['temporal']
            styles = createStylesArray(layer)



            if utils.blackfilter(name, layerBlackList):
               
               masterLayer = {"Name": name,
                              "Title": title,
                              "Abstract": abstract,
                              "FirstDate": dimensions['firstDate'],
                              "LastDate": dimensions['lastDate'],
                              "EX_GeographicBoundingBox": exGeographicBoundingBox }
                              
               if server['name'] in extraInfo:
                  if name in extraInfo[server['name']]:
                     masterLayer['tags'] = extraInfo[server['name']][name]
               
               # Data to be sent in the mastercache
               layers.append(masterLayer)
               
               # Data to be saved out
               layer = {#"Name": name,
                        #"wmsURL": server['wmsURL'],
                        #"wcsURL": server['wcsURL'],
                        #"Title": title,
                        #"Abstract": abstract,
                        "FirstDate": dimensions['firstDate'],
                        "LastDate": dimensions['lastDate'],
                        #"EX_GeographicBoundingBox": exGeographicBoundingBox,
                        "BoundingBox": boundingBox,
                        "Dimensions": dimensions['dimensions'],
                        "Styles": styles }
               
               cleanServerName = server['name'].replace('/', '-')
               cleanLayerName = name.replace('/', '-')
               
               # Save out layer cache
               utils.saveFile(LAYERCACHEPATH + cleanServerName + "_" + cleanLayerName + FILEEXTENSIONJSON, json.dumps(layer))
               
         subMasterCache['server'][sensorName] = layers
   
   subMasterCache['options'] = server['options']
   subMasterCache['wmsURL'] = server['url']
   subMasterCache['wcsURL'] = server['wcsurl']
   subMasterCache['serverName'] = server['name']
   
   print 'Cache creation complete...'
      
   # Return and save out the cache for this server
   return utils.saveFile(SERVERCACHEPATH + server['name'] + FILEEXTENSIONJSON, json.dumps(subMasterCache))
def createCache(server, capabilitiesXML, coverageXML):
    #import xml.etree.ElementTree as ET
    #from xml.etree.ElementTree import XMLParser

    from lxml import etree as ET

    import json

    # Save out the xml file for later
    utils.saveFile(
        SERVERCACHEPATH + server['name'] + '-GetCapabilities' +
        FILEEXTENSIONXML, capabilitiesXML)
    utils.saveFile(
        SERVERCACHEPATH + server['name'] + '-DescribeCoverage' +
        FILEEXTENSIONXML, coverageXML)

    print 'Creating caches...'
    subMasterCache = {}
    subMasterCache['server'] = {}

    #parse =  XMLParser( encoding="UTF-8" )

    # Parse the GetCapabilities XML
    #root = ET.XML(capabilitiesXML, parser=parse)
    root = ET.fromstring(removeNonUTF8(capabilitiesXML))

    # Parse the DescribeCoverage XML
    coverageRoot = ET.fromstring(removeNonUTF8(coverageXML))

    if root.find('./%sCapability/%sLayer/%sLayer' %
                 (WMS_NAMESPACE, WMS_NAMESPACE, WMS_NAMESPACE)) == None:
        dirtyCaches.append(server)
        return

    for service in root.findall('./%sService' % (WMS_NAMESPACE)):
        serverTitle = service.find('./%sTitle' % (WMS_NAMESPACE)).text
        serverAbstract = service.find(
            './%sAbstract' % (WMS_NAMESPACE)).text if service.find(
                './%sAbstract' % (WMS_NAMESPACE)) is not None else None

    for product in root.findall('./%sCapability/%sLayer/%sLayer' %
                                (WMS_NAMESPACE, WMS_NAMESPACE, WMS_NAMESPACE)):
        sensorName = product.find('./%sTitle' % (WMS_NAMESPACE)).text

        if utils.blackfilter(sensorName, productBlackList):
            sensorName = utils.replaceAll(sensorName, {
                ' ': '_',
                '(': '_',
                ')': '_',
                '/': '_'
            })
            print sensorName
            layers = []

            for layer in product.findall('./%sLayer' % (WMS_NAMESPACE)):
                name = layer.find('./%sName' % (WMS_NAMESPACE)).text
                title = layer.find('./%sTitle' % (WMS_NAMESPACE)).text
                abstract = layer.find('./%sAbstract' % (WMS_NAMESPACE)).text
                temporal = False

                if name not in server['indicators']:
                    print "NOTICE: Indicator '" + name + "' found on WMS server but not in local config file, ignoring."
                    continue

                #Find the CoverageOffering from DescribeCoverage

                coverage = findCoverageNode(coverageRoot, name)
                if coverage == None:
                    print serverTitle + "  " + name + " could not be found in DescribeCoverage. Not including."
                    continue

                offsetVectorsArray = coverage.findall('.//%soffsetVector' %
                                                      (GML_NAMESPACE))
                offsetVectors = []
                for i in range(0, len(offsetVectorsArray)):
                    offsetVectors.append(
                        float(offsetVectorsArray[i].text.split(" ")[i]))

                exGeographicBoundingBox = {
                    "WestBoundLongitude":
                    layer.find(
                        './%sEX_GeographicBoundingBox/%swestBoundLongitude' %
                        (WMS_NAMESPACE, WMS_NAMESPACE)).text,
                    "EastBoundLongitude":
                    layer.find(
                        './%sEX_GeographicBoundingBox/%seastBoundLongitude' %
                        (WMS_NAMESPACE, WMS_NAMESPACE)).text,
                    "SouthBoundLatitude":
                    layer.find(
                        './%sEX_GeographicBoundingBox/%ssouthBoundLatitude' %
                        (WMS_NAMESPACE, WMS_NAMESPACE)).text,
                    "NorthBoundLatitude":
                    layer.find(
                        './%sEX_GeographicBoundingBox/%snorthBoundLatitude' %
                        (WMS_NAMESPACE, WMS_NAMESPACE)).text
                }

                boundingBox = {
                    "CRS":
                    layer.find('./%sBoundingBox' % (WMS_NAMESPACE)).get('CRS'),
                    "MinX":
                    layer.find('./%sBoundingBox' %
                               (WMS_NAMESPACE)).get('minx'),
                    "MaxX":
                    layer.find('./%sBoundingBox' %
                               (WMS_NAMESPACE)).get('maxx'),
                    "MinY":
                    layer.find('./%sBoundingBox' %
                               (WMS_NAMESPACE)).get('miny'),
                    "MaxY":
                    layer.find('./%sBoundingBox' % (WMS_NAMESPACE)).get('maxy')
                }

                dimensions = createDimensionsArray(layer, server)
                temporal = dimensions['temporal']
                styles = createStylesArray(layer)

                if server['options']['providerShortTag'] not in providers:
                    raise Exception("Provider shortTag " +
                                    server['options']['providerShortTag'] +
                                    " was not in the 'providers.py' file")

                # Get the default details for the provider
                providerDetails = providers[server['options']
                                            ['providerShortTag']]
                if (layerHasMoreInfo(server['options']['providerShortTag'])):
                    moreProviderInfo = True
                else:
                    moreProviderInfo = False

                if 'providerDetails' in server['indicators'][name]:
                    # Overwrite any details with the indicator specific details
                    for i in server['indicators'][name]['providerDetails']:
                        providerDetails[i] = server['indicators'][name][
                            'providerDetails'][i]

                #import pprint
                #pprint.pprint(server['indicators'][name])
                #print '-'*40

                if utils.blackfilter(name, layerBlackList):
                    if layerHasMoreInfo(
                            server['indicators'][name]['niceName']):
                        moreIndicatorInfo = True
                    else:
                        moreIndicatorInfo = False
                    masterLayer = {
                        "Name": name,
                        "Title": title,
                        "Abstract": abstract,
                        "FirstDate": dimensions['firstDate'],
                        "LastDate": dimensions['lastDate'],
                        "OffsetVectors": offsetVectors,
                        "ProviderDetails": providerDetails,
                        "EX_GeographicBoundingBox": exGeographicBoundingBox,
                        "MoreIndicatorInfo": moreIndicatorInfo,
                        "MoreProviderInfo": moreProviderInfo
                    }

                    if name in server['indicators']:
                        masterLayer['tags'] = server['indicators'][name]

                    # Data to be sent in the mastercache
                    layers.append(masterLayer)

                    # Data to be saved out
                    layer = {  #"Name": name,
                        #"wmsURL": server['wmsURL'],
                        #"wcsURL": server['wcsURL'],
                        #"Title": title,
                        #"Abstract": abstract,
                        "FirstDate": dimensions['firstDate'],
                        "LastDate": dimensions['lastDate'],
                        "OffsetVectors": offsetVectors,
                        #"EX_GeographicBoundingBox": exGeographicBoundingBox,
                        "BoundingBox": boundingBox,
                        "Dimensions": dimensions['dimensions'],
                        "Styles": styles
                    }

                    cleanServerName = server['name'].replace('/', '-')
                    cleanLayerName = name.replace('/', '-')

                    # Save out layer cache
                    utils.saveFile(
                        LAYERCACHEPATH + cleanServerName + "_" +
                        cleanLayerName + FILEEXTENSIONJSON, json.dumps(layer))

            subMasterCache['server'][sensorName] = layers

    subMasterCache['options'] = server['options']
    subMasterCache['wmsURL'] = server['services']['wms']['url']
    subMasterCache['wcsURL'] = server['services']['wcs']['url']
    subMasterCache['serverName'] = server['name']

    print 'Cache creation complete...'

    # Return and save out the cache for this server
    return utils.saveFile(SERVERCACHEPATH + server['name'] + FILEEXTENSIONJSON,
                          json.dumps(subMasterCache))
def createCache(server, capabilitiesXML, coverageXML):
   #import xml.etree.ElementTree as ET
   #from xml.etree.ElementTree import XMLParser
   
   from lxml import etree as ET
   
   import json
   
   # Save out the xml file for later
   utils.saveFile(SERVERCACHEPATH + server['name'] + '-GetCapabilities' + FILEEXTENSIONXML, capabilitiesXML)
   utils.saveFile(SERVERCACHEPATH + server['name'] + '-DescribeCoverage' + FILEEXTENSIONXML, coverageXML)
   
   print 'Creating caches...'
   subMasterCache = {}
   subMasterCache['server'] = {}
   
   #parse =  XMLParser( encoding="UTF-8" )
   
   # Parse the GetCapabilities XML
   #root = ET.XML(capabilitiesXML, parser=parse)
   root = ET.fromstring( removeNonUTF8(capabilitiesXML) )
   
   # Parse the DescribeCoverage XML
   coverageRoot = ET.fromstring(  removeNonUTF8(coverageXML) )
    
   if root.find('./%sCapability/%sLayer/%sLayer' % (WMS_NAMESPACE,WMS_NAMESPACE,WMS_NAMESPACE)) == None:
      dirtyCaches.append(server)
      return
   
   for service in root.findall('./%sService' % (WMS_NAMESPACE)):
      serverTitle = service.find('./%sTitle' % (WMS_NAMESPACE)).text
      serverAbstract = service.find('./%sAbstract' % (WMS_NAMESPACE)).text if service.find('./%sAbstract' % (WMS_NAMESPACE)) is not None else None
   
   for product in root.findall('./%sCapability/%sLayer/%sLayer' % (WMS_NAMESPACE,WMS_NAMESPACE,WMS_NAMESPACE)):
      sensorName = product.find('./%sTitle' % (WMS_NAMESPACE)).text
      
      if utils.blackfilter(sensorName, productBlackList):
         sensorName = utils.replaceAll(sensorName, {' ':'_', '(':'_', ')':'_', '/':'_'})
         print sensorName
         layers = []
         
         for layer in product.findall('./%sLayer' % (WMS_NAMESPACE)):
            name = layer.find('./%sName' % (WMS_NAMESPACE)).text
            title = layer.find('./%sTitle' % (WMS_NAMESPACE)).text
            abstract = layer.find('./%sAbstract' % (WMS_NAMESPACE)).text
            temporal = False
            

            if name not in server['indicators']:
               print "NOTICE: Indicator '" + name + "' found on WMS server but not in local config file, ignoring."
               continue

            #Find the CoverageOffering from DescribeCoverage
            
            
            coverage = findCoverageNode( coverageRoot, name )
            if coverage == None:
               print serverTitle + "  " + name + " could not be found in DescribeCoverage. Not including."
               continue
            
            offsetVectorsArray = coverage.findall( './/%soffsetVector' % (GML_NAMESPACE) )
            offsetVectors = []
            for i in range( 0 , len( offsetVectorsArray )):
               offsetVectors.append(float(offsetVectorsArray[i].text.split(" ")[i]))
            
            exGeographicBoundingBox = {"WestBoundLongitude": layer.find('./%sEX_GeographicBoundingBox/%swestBoundLongitude' % (WMS_NAMESPACE,WMS_NAMESPACE)).text,
                                       "EastBoundLongitude": layer.find('./%sEX_GeographicBoundingBox/%seastBoundLongitude' % (WMS_NAMESPACE,WMS_NAMESPACE)).text,
                                       "SouthBoundLatitude": layer.find('./%sEX_GeographicBoundingBox/%ssouthBoundLatitude' % (WMS_NAMESPACE,WMS_NAMESPACE)).text,
                                       "NorthBoundLatitude": layer.find('./%sEX_GeographicBoundingBox/%snorthBoundLatitude' % (WMS_NAMESPACE,WMS_NAMESPACE)).text}
            
            boundingBox = {"CRS": layer.find('./%sBoundingBox' % (WMS_NAMESPACE)).get('CRS'),
                           "MinX": layer.find('./%sBoundingBox' % (WMS_NAMESPACE)).get('minx'),
                           "MaxX": layer.find('./%sBoundingBox' % (WMS_NAMESPACE)).get('maxx'),
                           "MinY": layer.find('./%sBoundingBox' % (WMS_NAMESPACE)).get('miny'),
                           "MaxY": layer.find('./%sBoundingBox' % (WMS_NAMESPACE)).get('maxy')}
            
            dimensions = createDimensionsArray(layer, server)
            temporal = dimensions['temporal']
            styles = createStylesArray(layer)

            if server['options']['providerShortTag'] not in providers:
               raise Exception("Provider shortTag " + server['options']['providerShortTag'] + " was not in the 'providers.py' file")

            # Get the default details for the provider
            providerDetails =  providers[ server['options']['providerShortTag'] ]
            if (layerHasMoreInfo(server['options']['providerShortTag'])):
               moreProviderInfo = True
            else:
               moreProviderInfo = False

            if 'providerDetails' in server['indicators'][name]:
               # Overwrite any details with the indicator specific details
               for i in server['indicators'][name]['providerDetails']:
                  providerDetails[ i ] = server['indicators'][name]['providerDetails'][ i ]

            #import pprint
            #pprint.pprint(server['indicators'][name])
            #print '-'*40

            if utils.blackfilter(name, layerBlackList):
               if layerHasMoreInfo(server['indicators'][name]['niceName']):
                  moreIndicatorInfo = True
               else:
                  moreIndicatorInfo = False
               masterLayer = {"Name": name,
                              "Title": title,
                              "Abstract": abstract,
                              "FirstDate": dimensions['firstDate'],
                              "LastDate": dimensions['lastDate'],
                              "OffsetVectors": offsetVectors,
                              "ProviderDetails": providerDetails,
                              "EX_GeographicBoundingBox": exGeographicBoundingBox,
                              "MoreIndicatorInfo" : moreIndicatorInfo,
                              "MoreProviderInfo" : moreProviderInfo }
                              
               if name in server['indicators']:
                  masterLayer['tags'] = server['indicators'][name]
               
               # Data to be sent in the mastercache
               layers.append(masterLayer)
               
               # Data to be saved out
               layer = {#"Name": name,
                        #"wmsURL": server['wmsURL'],
                        #"wcsURL": server['wcsURL'],
                        #"Title": title,
                        #"Abstract": abstract,
                        "FirstDate": dimensions['firstDate'],
                        "LastDate": dimensions['lastDate'],
                        "OffsetVectors": offsetVectors,
                        #"EX_GeographicBoundingBox": exGeographicBoundingBox,
                        "BoundingBox": boundingBox,
                        "Dimensions": dimensions['dimensions'],
                        "Styles": styles }
               
               cleanServerName = server['name'].replace('/', '-')
               cleanLayerName = name.replace('/', '-')
               
               # Save out layer cache
               utils.saveFile(LAYERCACHEPATH + cleanServerName + "_" + cleanLayerName + FILEEXTENSIONJSON, json.dumps(layer))
               
         subMasterCache['server'][sensorName] = layers
   
   subMasterCache['options'] = server['options']
   subMasterCache['wmsURL'] = server['services']['wms']['url']
   subMasterCache['wcsURL'] = server['services']['wcs']['url']
   subMasterCache['serverName'] = server['name']
   
   print 'Cache creation complete...'
      
   # Return and save out the cache for this server
   return utils.saveFile(SERVERCACHEPATH + server['name'] + FILEEXTENSIONJSON, json.dumps(subMasterCache))