<s> from xml . dom import minidom <EOL> from xml . dom import Node <EOL> import re <EOL> import sys , getopt <EOL> class SLD_v1_0_0_ColorMapEntry ( ) : <EOL> quantity = None <EOL> opacity = None <EOL> rgb = [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> def __hash__ ( self ) : <EOL> return hash ( self . quantity ) <EOL> def __cmp__ ( self , other ) : <EOL> return self . quantity . cmp ( other . quantity ) <EOL> def __eq__ ( self , other ) : <EOL> return self . quantity . eq ( other . quantity ) <EOL> class GIBS_ColorMapEntry ( ) : <EOL> rgb = [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> label = "<STR_LIT>" <EOL> value = None <EOL> sourceValue = None <EOL> transparent = False <EOL> nodata = False <EOL> ref = - <NUM_LIT:1> <EOL> def __hash__ ( self ) : <EOL> return hash ( self . rgb ) <EOL> def __cmp__ ( self , other ) : <EOL> return self . rgb . cmp ( other . rgb ) <EOL> def __eq__ ( self , other ) : <EOL> return self . rgb . eq ( other . rgb ) <EOL> class GIBS_ColorMap ( ) : <EOL> showUnits = False <EOL> showLegend = False <EOL> minLabel = None <EOL> maxLabel = None <EOL> cmEntries = [ ] <EOL> def hexToRGB ( hexValue ) : <EOL> if re . match ( "<STR_LIT>" , hexValue ) : <EOL> r = int ( hexValue [ <NUM_LIT:1> : <NUM_LIT:3> ] , <NUM_LIT:16> ) <EOL> g = int ( hexValue [ <NUM_LIT:3> : <NUM_LIT:5> ] , <NUM_LIT:16> ) <EOL> b = int ( hexValue [ <NUM_LIT:5> : <NUM_LIT:7> ] , <NUM_LIT:16> ) <EOL> elif re . match ( "<STR_LIT>" , hexValue ) : <EOL> r = int ( hexValue [ <NUM_LIT:0> : <NUM_LIT:2> ] , <NUM_LIT:16> ) <EOL> g = int ( hexValue [ <NUM_LIT:2> : <NUM_LIT:4> ] , <NUM_LIT:16> ) <EOL> b = int ( hexValue [ <NUM_LIT:4> : <NUM_LIT:6> ] , <NUM_LIT:16> ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> r = g = b = - <NUM_LIT:1> <EOL> return [ r , g , b ] <EOL> def dataDensify ( startValue , endValue , steps ) : <EOL> dataSteps = [ ] <EOL> stepsize = ( endValue - startValue ) / steps <EOL> for step in range ( <NUM_LIT:1> , steps ) : <EOL> dataSteps . append ( startValue + ( stepsize * step ) ) <EOL> dataSteps . append ( endValue ) <EOL> return dataSteps <EOL> def rampDensify ( startColor , endColor , steps ) : <EOL> colorSteps = [ ] <EOL> f2c = lambda f : int ( f * <NUM_LIT> ) & <NUM_LIT> <EOL> c2f = lambda c : float ( c ) / <NUM_LIT> <EOL> r1 = c2f ( startColor [ <NUM_LIT:0> ] ) <EOL> g1 = c2f ( startColor [ <NUM_LIT:1> ] ) <EOL> b1 = c2f ( startColor [ <NUM_LIT:2> ] ) <EOL> r2 = c2f ( endColor [ <NUM_LIT:0> ] ) <EOL> g2 = c2f ( endColor [ <NUM_LIT:1> ] ) <EOL> b2 = c2f ( endColor [ <NUM_LIT:2> ] ) <EOL> delta = <NUM_LIT:1.0> / steps <EOL> for j in range ( int ( steps ) ) : <EOL> t = j * delta <EOL> a = <NUM_LIT:1.0> <EOL> r = ( <NUM_LIT:1.0> - t ) * r1 + t * r2 <EOL> g = ( <NUM_LIT:1.0> - t ) * g1 + t * g2 <EOL> b = ( <NUM_LIT:1.0> - t ) * b1 + t * b2 <EOL> color = [ f2c ( r ) , f2c ( g ) , f2c ( b ) ] <EOL> colorSteps . append ( color ) <EOL> return colorSteps <EOL> def stepDensify ( startColor , steps , direction ) : <EOL> colorSteps = [ ] <EOL> colorSteps . append ( [ startColor [ <NUM_LIT:0> ] , startColor [ <NUM_LIT:1> ] , startColor [ <NUM_LIT:2> ] ] ) <EOL> lastColor = startColor <EOL> colorsAdded = <NUM_LIT:0> <EOL> colorsAvail = [ True , True , True ] <EOL> while colorsAdded < steps : <EOL> for index in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> newColor = lastColor [ index ] + ( <NUM_LIT:1> * direction ) <EOL> if newColor < <NUM_LIT:0> or newColor > <NUM_LIT:255> : <EOL> colorsAvail [ index ] = False <EOL> continue <EOL> lastColor [ index ] = newColor <EOL> colorSteps . append ( [ lastColor [ <NUM_LIT:0> ] , lastColor [ <NUM_LIT:1> ] , lastColor [ <NUM_LIT:2> ] ] ) <EOL> colorsAdded = colorsAdded + <NUM_LIT:1> <EOL> if colorsAdded >= steps : break <EOL> if ( not colorsAvail [ <NUM_LIT:0> ] ) and ( not colorsAvail [ <NUM_LIT:1> ] ) and ( not colorsAvail [ <NUM_LIT:2> ] ) : <EOL> exit ( "<STR_LIT>" ) <EOL> return colorSteps <EOL> def parseSLD_v1_0_0 ( sourceXml , layerName , units , offset , factor , format ) : <EOL> gibsColorMaps = [ ] <EOL> xmldoc = minidom . parse ( sourceXml ) <EOL> for nlNode in xmldoc . documentElement . getElementsByTagName ( "<STR_LIT>" ) : <EOL> nameNode = nlNode . getElementsByTagName ( "<STR_LIT:Name>" ) [ <NUM_LIT:0> ] <EOL> if nameNode . firstChild . nodeValue != layerName : <EOL> break <EOL> for userStyleNode in nlNode . getElementsByTagName ( "<STR_LIT>" ) : <EOL> for rasterSymNode in userStyleNode . getElementsByTagName ( "<STR_LIT>" ) : <EOL> colorMapNode = rasterSymNode . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> sldCMapEntries = [ ] <EOL> for colorMapEntryNode in colorMapNode . getElementsByTagName ( "<STR_LIT>" ) : <EOL> attrDict = dict ( colorMapEntryNode . attributes . items ( ) ) <EOL> sldCMapEntry = SLD_v1_0_0_ColorMapEntry ( ) <EOL> if '<STR_LIT>' in attrDict : <EOL> sldCMapEntry . opacity = float ( attrDict [ '<STR_LIT>' ] ) <EOL> else : <EOL> sldCMapEntry . opacity = <NUM_LIT:1.0> <EOL> if '<STR_LIT>' in attrDict : <EOL> sldCMapEntry . quantity = float ( attrDict [ '<STR_LIT>' ] ) <EOL> sldCMapEntry . rgb = hexToRGB ( str ( attrDict [ '<STR_LIT>' ] ) ) <EOL> sldCMapEntries . append ( sldCMapEntry ) <EOL> gibsCMap = GIBS_ColorMap ( ) <EOL> prevValue = sldCMapEntries [ <NUM_LIT:0> ] . quantity <EOL> prevOpacity = ( sldCMapEntries [ <NUM_LIT:0> ] . opacity == <NUM_LIT:0.0> ) <EOL> prevRGB = sldCMapEntries [ <NUM_LIT:0> ] . rgb <EOL> gibsCMap . minLabel = "<STR_LIT>" + format . format ( prevValue ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> for sldCMapEntry in sldCMapEntries [ <NUM_LIT:1> : - <NUM_LIT:1> ] : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . rgb = prevRGB <EOL> gibsCMapEntry . label = format . format ( prevValue ) + "<STR_LIT>" + format . format ( sldCMapEntry . quantity ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , sldCMapEntry . quantity ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , ( ( sldCMapEntry . quantity - offset ) / factor ) ] <EOL> gibsCMapEntry . transparent = prevOpacity <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> prevValue = sldCMapEntry . quantity <EOL> prevOpacity = ( sldCMapEntry . opacity == <NUM_LIT:0.0> ) <EOL> prevRGB = sldCMapEntry . rgb <EOL> if prevRGB == sldCMapEntries [ - <NUM_LIT:1> ] . rgb : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . rgb = prevRGB <EOL> gibsCMapEntry . label = "<STR_LIT>" + format . format ( prevValue ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , sldCMapEntries [ - <NUM_LIT:1> ] . quantity ] <EOL> gibsCMapEntry . sourceValue = [ prevValue , ( ( sldCMapEntries [ - <NUM_LIT:1> ] . quantity - offset ) / factor ) ] <EOL> gibsCMapEntry . transparent = prevOpacity <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> else : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . rgb = prevRGB <EOL> gibsCMapEntry . label = format . format ( prevValue ) + "<STR_LIT>" + format . format ( sldCMapEntries [ - <NUM_LIT:1> ] . quantity ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , sldCMapEntries [ - <NUM_LIT:1> ] . quantity ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , ( ( sldCMapEntries [ - <NUM_LIT:1> ] . quantity - offset ) / factor ) ] <EOL> gibsCMapEntry . transparent = prevOpacity <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . rgb = sldCMapEntries [ - <NUM_LIT:1> ] . rgb <EOL> gibsCMapEntry . label = format . format ( sldCMapEntries [ - <NUM_LIT:1> ] . quantity ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ sldCMapEntries [ - <NUM_LIT:1> ] . quantity ] <EOL> gibsCMapEntry . sourceValue = [ ( ( sldCMapEntries [ - <NUM_LIT:1> ] . quantity - offset ) / factor ) , "<STR_LIT>" ] <EOL> gibsCMapEntry . transparent = ( sldCMapEntries [ - <NUM_LIT:1> ] . opacity == <NUM_LIT:0.0> ) <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> gibsCMap . maxLabel = gibsCMap . cmEntries [ - <NUM_LIT:1> ] . label <EOL> gibsCMap . showLegend = True <EOL> gibsColorMaps . append ( gibsCMap ) <EOL> return gibsColorMaps <EOL> def parseSLD_v1_1_0 ( sourceXml , layerName , units , offset , factor , rgbOrder , format , densify ) : <EOL> gibsColorMaps = [ ] <EOL> xmldoc = minidom . parse ( sourceXml ) <EOL> for nlNode in xmldoc . documentElement . getElementsByTagName ( "<STR_LIT>" ) : <EOL> nameNode = nlNode . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> if nameNode . firstChild . nodeValue != layerName : <EOL> break <EOL> for userStyleNode in nlNode . getElementsByTagName ( "<STR_LIT>" ) : <EOL> for rasterSymNode in userStyleNode . getElementsByTagName ( "<STR_LIT>" ) : <EOL> defaultOpacity = rasterSymNode . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . firstChild . nodeValue <EOL> colorMapNode = rasterSymNode . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> categorizeNode = rasterSymNode . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> attrDict = dict ( categorizeNode . attributes . items ( ) ) <EOL> if '<STR_LIT>' in attrDict : <EOL> hexValue = "<STR_LIT>" <EOL> for c in "<STR_LIT>" : <EOL> s = ( - <NUM_LIT:2> * rgbOrder [ : : - <NUM_LIT:1> ] . find ( c ) ) - <NUM_LIT:2> <EOL> e = ( - <NUM_LIT:2> * rgbOrder [ : : - <NUM_LIT:1> ] . find ( c ) ) <EOL> e = e if e != <NUM_LIT:0> else None <EOL> hexValue = hexValue + attrDict [ '<STR_LIT>' ] [ s : e ] <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . rgb = hexToRGB ( hexValue ) <EOL> gibsCMapEntry . transparent = True <EOL> gibsCMapEntry . label = "<STR_LIT>" <EOL> gibsCMapEntry . nodata = True <EOL> gibsCMapEntry . ref = <NUM_LIT:1> <EOL> gibsCMap = GIBS_ColorMap ( ) <EOL> gibsCMap . cmEntries = [ ] <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> gibsColorMaps . append ( gibsCMap ) <EOL> gibsCMap = GIBS_ColorMap ( ) <EOL> prevValue = sys . maxint <EOL> prev2Value = sys . maxint <EOL> currValue = sys . maxint <EOL> firstValue = sys . maxint <EOL> prevRGB = [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> prev2RGB = [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> currRGB = [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> firstRGB = [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> currRef = <NUM_LIT:1> <EOL> for catChildNode in [ n for n in categorizeNode . childNodes if n . nodeType == Node . ELEMENT_NODE ] : <EOL> if catChildNode . nodeName == "<STR_LIT>" : <EOL> currRGB = hexToRGB ( catChildNode . firstChild . nodeValue ) <EOL> if catChildNode . nodeName == "<STR_LIT>" : <EOL> currValue = float ( catChildNode . firstChild . nodeValue ) <EOL> if prevValue == sys . maxint : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . transparent = ( defaultOpacity == <NUM_LIT:0.0> ) <EOL> gibsCMapEntry . rgb = currRGB <EOL> gibsCMapEntry . label = "<STR_LIT>" + format . format ( currValue ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ "<STR_LIT>" , currValue ] <EOL> gibsCMapEntry . sourceValue = [ "<STR_LIT>" , ( ( currValue - offset ) / factor ) ] <EOL> gibsCMapEntry . ref = currRef <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> gibsCMap . minLabel = "<STR_LIT>" + format . format ( currValue ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> currRef = currRef + <NUM_LIT:1> <EOL> elif not densify : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . transparent = ( defaultOpacity == <NUM_LIT:0.0> ) <EOL> gibsCMapEntry . rgb = currRGB <EOL> gibsCMapEntry . label = format . format ( prevValue ) + "<STR_LIT>" + format . format ( currValue ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , currValue ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , ( ( currValue - offset ) / factor ) ] <EOL> gibsCMapEntry . ref = currRef <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> currRef = currRef + <NUM_LIT:1> <EOL> else : <EOL> m = re . match ( r"<STR_LIT>" , densify ) <EOL> if m . group ( <NUM_LIT:1> ) == "<STR_LIT:s>" : <EOL> colorSteps = stepDensify ( currRGB , int ( m . group ( <NUM_LIT:2> ) ) , <NUM_LIT:1> ) <EOL> dataSteps = dataDensify ( prevValue , currValue , int ( m . group ( <NUM_LIT:2> ) ) ) <EOL> for step in range ( <NUM_LIT:0> , int ( m . group ( <NUM_LIT:2> ) ) ) : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . transparent = ( defaultOpacity == <NUM_LIT:0.0> ) <EOL> gibsCMapEntry . rgb = colorSteps [ step ] <EOL> gibsCMapEntry . label = format . format ( prevValue ) + "<STR_LIT>" + format . format ( dataSteps [ step ] ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , dataSteps [ step ] ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , ( ( dataSteps [ step ] - offset ) / factor ) ] <EOL> gibsCMapEntry . ref = currRef <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> prevValue = dataSteps [ step ] <EOL> currRef = currRef + <NUM_LIT:1> <EOL> elif m . group ( <NUM_LIT:1> ) == "<STR_LIT:r>" and prev2Value != sys . maxint : <EOL> colorSteps = rampDensify ( prevRGB , currRGB , int ( m . group ( <NUM_LIT:2> ) ) + <NUM_LIT:1> ) <EOL> dataSteps = dataDensify ( prev2Value , prevValue , int ( m . group ( <NUM_LIT:2> ) ) ) <EOL> prevValue = prev2Value <EOL> for step in range ( <NUM_LIT:0> , int ( m . group ( <NUM_LIT:2> ) ) ) : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . transparent = ( defaultOpacity == <NUM_LIT:0.0> ) <EOL> gibsCMapEntry . rgb = colorSteps [ step ] <EOL> gibsCMapEntry . label = format . format ( prevValue ) + "<STR_LIT>" + format . format ( dataSteps [ step ] ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , dataSteps [ step ] ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , ( ( dataSteps [ step ] - offset ) / factor ) ] <EOL> gibsCMapEntry . ref = currRef <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> prevValue = dataSteps [ step ] <EOL> currRef = currRef + <NUM_LIT:1> <EOL> elif m . group ( <NUM_LIT:1> ) == "<STR_LIT:o>" and firstValue == sys . maxint : <EOL> firstValue = prevValue <EOL> firstRGB = currRGB <EOL> prev2Value = prevValue <EOL> prevValue = currValue <EOL> prev2RGB = prevRGB <EOL> prevRGB = currRGB <EOL> if densify : <EOL> m = re . match ( r"<STR_LIT>" , densify ) <EOL> if m . group ( <NUM_LIT:1> ) == "<STR_LIT:r>" : <EOL> colorSteps = rampDensify ( prevRGB , currRGB , int ( m . group ( <NUM_LIT:2> ) ) + <NUM_LIT:1> ) <EOL> dataSteps = dataDensify ( prev2Value , prevValue , int ( m . group ( <NUM_LIT:2> ) ) ) <EOL> prevValue = prev2Value <EOL> for step in range ( <NUM_LIT:0> , int ( m . group ( <NUM_LIT:2> ) ) ) : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . transparent = ( defaultOpacity == <NUM_LIT:0.0> ) <EOL> gibsCMapEntry . rgb = colorSteps [ step ] <EOL> gibsCMapEntry . label = format . format ( prevValue ) + "<STR_LIT>" + format . format ( dataSteps [ step ] ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , dataSteps [ step ] ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , ( ( dataSteps [ step ] - offset ) / factor ) ] <EOL> gibsCMapEntry . ref = currRef <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> prevValue = dataSteps [ step ] <EOL> currRef = currRef + <NUM_LIT:1> <EOL> if m . group ( <NUM_LIT:1> ) == "<STR_LIT:o>" : <EOL> colorSteps = rampDensify ( firstRGB , currRGB , int ( m . group ( <NUM_LIT:2> ) ) + <NUM_LIT:1> ) <EOL> dataSteps = dataDensify ( firstValue , prevValue , int ( m . group ( <NUM_LIT:2> ) ) ) <EOL> prevValue = firstValue <EOL> for step in range ( <NUM_LIT:0> , int ( m . group ( <NUM_LIT:2> ) ) ) : <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . transparent = ( defaultOpacity == <NUM_LIT:0.0> ) <EOL> gibsCMapEntry . rgb = colorSteps [ step ] <EOL> gibsCMapEntry . label = format . format ( prevValue ) + "<STR_LIT>" + format . format ( dataSteps [ step ] ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , dataSteps [ step ] ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , ( ( dataSteps [ step ] - offset ) / factor ) ] <EOL> gibsCMapEntry . ref = currRef <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> prevValue = dataSteps [ step ] <EOL> currRef = currRef + <NUM_LIT:1> <EOL> gibsCMapEntry = GIBS_ColorMapEntry ( ) <EOL> gibsCMapEntry . rgb = currRGB <EOL> gibsCMapEntry . transparent = ( defaultOpacity == <NUM_LIT:0.0> ) <EOL> gibsCMapEntry . label = "<STR_LIT>" + format . format ( prevValue ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMapEntry . value = [ prevValue , "<STR_LIT>" ] <EOL> gibsCMapEntry . sourceValue = [ ( ( prevValue - offset ) / factor ) , "<STR_LIT>" ] <EOL> gibsCMapEntry . ref = currRef <EOL> gibsCMap . cmEntries . append ( gibsCMapEntry ) <EOL> gibsCMap . maxLabel = "<STR_LIT>" + format . format ( prevValue ) + ( "<STR_LIT>" if not units else ( "<STR_LIT:U+0020>" + units ) ) <EOL> gibsCMap . showUnits = True <EOL> gibsCMap . showLegend = True <EOL> gibsColorMaps . append ( gibsCMap ) <EOL> return gibsColorMaps <EOL> def generateColorMap ( gibsColorMaps , units , format ) : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> for colorMap in gibsColorMaps : <EOL> print ( "<STR_LIT>" + <EOL> ( ( "<STR_LIT>" + units + "<STR_LIT>" ) if colorMap . showUnits and units else "<STR_LIT>" ) + "<STR_LIT:>>" ) <EOL> print ( "<STR_LIT>" + <EOL> ( "<STR_LIT>" if not colorMap . minLabel else ( "<STR_LIT>" + colorMap . minLabel + "<STR_LIT>" ) ) + <EOL> ( "<STR_LIT>" if not colorMap . maxLabel else ( "<STR_LIT>" + colorMap . maxLabel + "<STR_LIT>" ) ) + "<STR_LIT:>>" ) <EOL> for cMapEntry in colorMap . cmEntries : <EOL> rgb = str ( cMapEntry . rgb [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( cMapEntry . rgb [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( cMapEntry . rgb [ <NUM_LIT:2> ] ) <EOL> if cMapEntry . value : <EOL> if len ( cMapEntry . value ) == <NUM_LIT:1> : <EOL> value = "<STR_LIT:[>" + ( cMapEntry . value [ <NUM_LIT:0> ] if isinstance ( cMapEntry . value [ <NUM_LIT:0> ] , str ) else format . format ( cMapEntry . value [ <NUM_LIT:0> ] ) ) + "<STR_LIT:]>" <EOL> else : <EOL> value = "<STR_LIT:[>" + ( cMapEntry . value [ <NUM_LIT:0> ] if isinstance ( cMapEntry . value [ <NUM_LIT:0> ] , str ) else format . format ( cMapEntry . value [ <NUM_LIT:0> ] ) ) + "<STR_LIT:U+002C>" + ( cMapEntry . value [ <NUM_LIT:1> ] if isinstance ( cMapEntry . value [ <NUM_LIT:1> ] , str ) else format . format ( cMapEntry . value [ <NUM_LIT:1> ] ) ) + "<STR_LIT:)>" <EOL> if cMapEntry . sourceValue : <EOL> if len ( cMapEntry . sourceValue ) == <NUM_LIT:1> : <EOL> sourceValue = "<STR_LIT:[>" + ( cMapEntry . sourceValue [ <NUM_LIT:0> ] if isinstance ( cMapEntry . sourceValue [ <NUM_LIT:0> ] , str ) else format . format ( cMapEntry . sourceValue [ <NUM_LIT:0> ] ) ) + "<STR_LIT:]>" <EOL> else : <EOL> sourceValue = "<STR_LIT:[>" + ( cMapEntry . sourceValue [ <NUM_LIT:0> ] if isinstance ( cMapEntry . sourceValue [ <NUM_LIT:0> ] , str ) else format . format ( cMapEntry . sourceValue [ <NUM_LIT:0> ] ) ) + "<STR_LIT:U+002C>" + ( cMapEntry . sourceValue [ <NUM_LIT:1> ] if isinstance ( cMapEntry . sourceValue [ <NUM_LIT:1> ] , str ) else format . format ( cMapEntry . sourceValue [ <NUM_LIT:1> ] ) ) + "<STR_LIT:)>" <EOL> print ( "<STR_LIT>" + rgb + "<STR_LIT>" + <EOL> "<STR_LIT>" + ( "<STR_LIT:true>" if cMapEntry . transparent else "<STR_LIT:false>" ) + "<STR_LIT>" + <EOL> ( "<STR_LIT>" if not cMapEntry . sourceValue else ( "<STR_LIT>" + value + "<STR_LIT>" ) ) + <EOL> ( "<STR_LIT>" if not cMapEntry . value else ( "<STR_LIT>" + value + "<STR_LIT>" ) ) + <EOL> "<STR_LIT>" + cMapEntry . label + "<STR_LIT>" + <EOL> ( "<STR_LIT>" if not cMapEntry . nodata else ( "<STR_LIT>" ) ) + <EOL> ( "<STR_LIT>" if not colorMap . showLegend else ( "<STR_LIT>" + str ( cMapEntry . ref ) + "<STR_LIT>" ) ) + <EOL> "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if colorMap . showLegend : <EOL> print ( "<STR_LIT>" + <EOL> ( "<STR_LIT>" if not colorMap . minLabel else ( "<STR_LIT>" + colorMap . minLabel + "<STR_LIT>" ) ) + <EOL> ( "<STR_LIT>" if not colorMap . maxLabel else ( "<STR_LIT>" + colorMap . maxLabel + "<STR_LIT>" ) ) + "<STR_LIT:>>" ) <EOL> prevRef = - <NUM_LIT:1> <EOL> for cMapEntry in colorMap . cmEntries : <EOL> if cMapEntry . ref != prevRef : <EOL> rgb = str ( cMapEntry . rgb [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( cMapEntry . rgb [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( cMapEntry . rgb [ <NUM_LIT:2> ] ) <EOL> if cMapEntry . value : <EOL> if len ( cMapEntry . value ) == <NUM_LIT:1> : <EOL> value = "<STR_LIT:[>" + ( cMapEntry . value [ <NUM_LIT:0> ] if isinstance ( cMapEntry . value [ <NUM_LIT:0> ] , str ) else format . format ( cMapEntry . value [ <NUM_LIT:0> ] ) ) + "<STR_LIT:]>" <EOL> else : <EOL> value = "<STR_LIT:[>" + ( cMapEntry . value [ <NUM_LIT:0> ] if isinstance ( cMapEntry . value [ <NUM_LIT:0> ] , str ) else format . format ( cMapEntry . value [ <NUM_LIT:0> ] ) ) + "<STR_LIT:U+002C>" + ( cMapEntry . value [ <NUM_LIT:1> ] if isinstance ( cMapEntry . value [ <NUM_LIT:1> ] , str ) else format . format ( cMapEntry . value [ <NUM_LIT:1> ] ) ) + "<STR_LIT:)>" <EOL> print ( "<STR_LIT>" + rgb + "<STR_LIT>" + <EOL> ( "<STR_LIT>" if not colorMap . showLegend else ( "<STR_LIT>" + str ( cMapEntry . ref ) + "<STR_LIT>" ) ) + "<STR_LIT>" ) <EOL> prevRef = cMapEntry . ref <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> def main ( argv ) : <EOL> gibsColorMaps = [ ] <EOL> sldFile = "<STR_LIT>" <EOL> units = None <EOL> layerName = "<STR_LIT>" <EOL> offset = <NUM_LIT:0.0> <EOL> factor = <NUM_LIT:1.0> <EOL> rgbaOrder = "<STR_LIT>" <EOL> format = "<STR_LIT:{}>" <EOL> densify = None <EOL> try : <EOL> opts , args = getopt . getopt ( argv , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> except getopt . GetoptError : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> for opt , arg in opts : <EOL> if opt == '<STR_LIT>' : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> sldFile = arg <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> layerName = arg <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> units = arg <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> factor = float ( arg ) <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> offset = float ( arg ) <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> format = "<STR_LIT>" + arg + "<STR_LIT:}>" <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> rgbaOrder = arg <EOL> elif opt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if not re . match ( "<STR_LIT>" , arg ) : <EOL> print ( "<STR_LIT>" ) <EOL> exit ( - <NUM_LIT:1> ) <EOL> else : <EOL> densify = arg <EOL> sldXmldoc = minidom . parse ( sldFile ) <EOL> attrDict = dict ( sldXmldoc . documentElement . attributes . items ( ) ) <EOL> if '<STR_LIT:version>' in attrDict : <EOL> if attrDict [ '<STR_LIT:version>' ] == "<STR_LIT>" : <EOL> gibsColorMaps = parseSLD_v1_0_0 ( sldFile , layerName , units , offset , factor ) <EOL> elif attrDict [ '<STR_LIT:version>' ] == "<STR_LIT>" : <EOL> gibsColorMaps = parseSLD_v1_1_0 ( sldFile , layerName , units , offset , factor , rgbaOrder , format , densify ) <EOL> else : <EOL> print ( "<STR_LIT>" + attrDict [ '<STR_LIT:version>' ] ) <EOL> exit ( - <NUM_LIT:1> ) <EOL> generateColorMap ( gibsColorMaps , units , format ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> exit ( - <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
