Exemplo n.º 1
0
 def testError(self):
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     symbola = mapscript.symbolObj('testa')
     symbolb = mapscript.symbolObj('testb')
     symbolset.appendSymbol(symbola)
     symbolset.appendSymbol(symbolb)
     self.assertRaises(mapscript.MapServerError, symbolset.save, '/bogus/new_symbols.txt')
Exemplo n.º 2
0
 def testSaveNewSymbolSet(self):
     """save a new SymbolSet to disk"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     symbola = mapscript.symbolObj('testa')
     symbolb = mapscript.symbolObj('testb')
     symbolset.appendSymbol(symbola)
     symbolset.appendSymbol(symbolb)
     assert symbolset.save('new_symbols.txt') == mapscript.MS_SUCCESS
Exemplo n.º 3
0
 def testSaveNewSymbolSet(self):
     """save a new SymbolSet to disk"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     symbola = mapscript.symbolObj('testa')
     symbolb = mapscript.symbolObj('testb')
     symbolset.appendSymbol(symbola)
     symbolset.appendSymbol(symbolb)
     assert symbolset.save('new_symbols.txt') == mapscript.MS_SUCCESS
Exemplo n.º 4
0
 def testAddSymbolToNewSymbolSet(self):
     """add two new symbols to a SymbolSet"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     symbola = mapscript.symbolObj('testa')
     symbolb = mapscript.symbolObj('testb')
     symbolset.appendSymbol(symbola)
     symbolset.appendSymbol(symbolb)
     num = symbolset.numsymbols
     assert num == 6, num
     names = [None, 'circle', 'xmarks-png', 'home-png', 'testa', 'testb']
     for i in range(symbolset.numsymbols):
         symbol = symbolset.getSymbol(i)
         assert symbol.name == names[i], symbol.name
Exemplo n.º 5
0
 def testAddSymbolToNewSymbolSet(self):
     """add two new symbols to a SymbolSet"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     symbola = mapscript.symbolObj('testa')
     symbolb = mapscript.symbolObj('testb')
     symbolset.appendSymbol(symbola)
     symbolset.appendSymbol(symbolb)
     num = symbolset.numsymbols
     assert num == 6, num
     names = [None, 'circle', 'xmarks-png', 'home-png', 'testa', 'testb']
     for i in range(symbolset.numsymbols):
         symbol = symbolset.getSymbol(i)
         assert symbol.name == names[i], symbol.name
    def _create_symbolset(self, symbolset_path):
        '''Creates a symbolset (containing the square symbol)
        and saves it as symbols.sym'''
        mapscript = self.mapscript
        
        symbolset = mapscript.symbolSetObj()
        new_symbol = mapscript.symbolObj('square')
        line = mapscript.lineObj()
        line.add(mapscript.pointObj(0.0, 4.0))
        line.add(mapscript.pointObj(4.0, 4.0))
        line.add(mapscript.pointObj(4.0, 0.0))
        line.add(mapscript.pointObj(0.0, 0.0))
        line.add(mapscript.pointObj(0.0, 4.0))

        new_symbol.setPoints(line)
        new_symbol.filled = True
        symbolset.appendSymbol(new_symbol)
        symbolset.save(symbolset_path)
Exemplo n.º 7
0
    def _create_symbolset(self, symbolset_path):
        '''Creates a symbolset (containing the square symbol)
        and saves it as symbols.sym'''
        mapscript = self.mapscript

        symbolset = mapscript.symbolSetObj()
        new_symbol = mapscript.symbolObj('square')
        line = mapscript.lineObj()
        line.add(mapscript.pointObj(0.0, 4.0))
        line.add(mapscript.pointObj(4.0, 4.0))
        line.add(mapscript.pointObj(4.0, 0.0))
        line.add(mapscript.pointObj(0.0, 0.0))
        line.add(mapscript.pointObj(0.0, 4.0))

        new_symbol.setPoints(line)
        new_symbol.filled = True
        symbolset.appendSymbol(new_symbol)
        symbolset.save(symbolset_path)
def serializeSvgSymbol(svgPath):
    """Serialize an SVG symbol into a mapscript.symbolObj()

        We have a couple of problems here though:

        1. SVGs with embedded raster images

            As MapServer seems to be unable to handle SVG files with an embedded <image />, we use
            some magic here to extract said image into a separate file and use a pixmap symbol
            instead. Please note the following:
                a. We only consider the first embedded image in the SVG file.
                b. Embedded images are saved to the directory the SVG file resides in.

        2. As it is currently (MapServer 7.0.0-beta) impossible to set the `imagepath` attribute on
           a symbolObj() we use a workaround that involves manually writing, then re-parsing
           a symbol set definition file.

            Possibly relevant MapServer bugs:
                https://github.com/mapserver/mapserver/issues/4501
                https://github.com/mapserver/mapserver/issues/5074
                https://github.com/mapserver/mapserver/issues/5109
    """

    # Check if the SVG file contains an embedded image
    with codecs.open(svgPath, 'r', 'utf-8') as fin:
        svgContents = fin.read().replace('\n', '')
    
    rx = re.compile(u'<image[^>]+xlink:href="([^"]+)"')
    m = rx.search(svgContents)

    if (m is not None):
        # We have an image, check if its a data URI or a general one
        uri = m.group(1)
        imageType = 'PIXMAP'
        symbolUUID = makeSymbolUUID('svgraster')

        if uri[:10] == u'data:image':
            # We have a data URI, save the image into an external file.
            # Please note that we only consider base64-encoded images here.
            #
            dataURIRx = re.compile('data:image/(\w+);base64,(.+)')
            dm = dataURIRx.match(uri)

            if (dm is not None):
                imageExt = dm.group(1)
                try:
                    imageData = bytearray(binascii.a2b_base64(dm.group(2)))
                except:
                    raise ValueError('Cannot decode base64 URI in embedded image while parsing SVG.') 
                
                imageName = '%s.%s' % (symbolUUID, imageExt)
                imageDir = os.path.join(os.path.dirname(svgPath), SVG_IMAGE_DIR)
                
                if not os.path.exists(imageDir):
                    os.makedirs(imageDir)

                imagePath = os.path.join(imageDir, imageName).encode('utf-8')

                with open(imagePath, 'wb') as imageOut:
                    imageOut.write(imageData)

            else:
                raise ValueError('Invalid data URI encountered while parsing SVG.')
            
        else:
            # We have a non-data URI.
            # We only want to consider relative URIs here so perform some naive sanity checks on it

            if uri.startswith('file://'):
                uri = uri[7:]
                if (uri.find('..') == -1) and (not uri.startswith('/')):
                    imagePath = os.path.join(os.path.dirname(svgPath), uri)
                else:
                    raise ValueError('Invalid URI encountered while parsing SVG.')
            else:
                raise ValueError('Invalid URI encountered while parsing SVG.')
    else:
        # We do not have an embedded image thus the SVG is all vector and can probably be 
        # rendered without a hitch

        imageType = 'SVG' 
        imagePath = svgPath

    symbolSetData = """
        SYMBOLSET
            SYMBOL
                NAME "%s"
                TYPE %s
                IMAGE "%s"
                ANCHORPOINT 0.5 0.5
            END
        END
    """

    # Create a temporary file and open it
    (tempHandle, tempName) = mkstemp()
    
    # Write symbol set data
    os.write(tempHandle, symbolSetData % (makeSymbolUUID('svg'), imageType, imagePath))
    os.close(tempHandle)

    # Load and parse the symbol set
    msSymbolSet = mapscript.symbolSetObj(tempName)

    # Remove the temporary file
    # os.unlink(tempName)

    # Fetch and return our SVG symbol
    msSymbol = msSymbolSet.getSymbol(1)
    msSymbol.inmapfile = True

    return msSymbol
Exemplo n.º 9
0
 def testRemoveSymbolFromNewSymbolSet(self):
     """after removing a symbol, expect numsymbols -= 1"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     symbolset.removeSymbol(1)
     num = symbolset.numsymbols
     assert num == 3, num
Exemplo n.º 10
0
 def testConstructorFile(self):
     """new instance of symbolSetObj from symbols.txt"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     num = symbolset.numsymbols
     assert num == 4, num
Exemplo n.º 11
0
 def testConstructorNoArgs(self):
     """new instance of symbolSetObj should have one symbol"""
     symbolset = mapscript.symbolSetObj()
     num = symbolset.numsymbols
     assert num == 1, num
Exemplo n.º 12
0
 def testRemoveSymbolFromNewSymbolSet(self):
     """after removing a symbol, expect numsymbols -= 1"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     symbolset.removeSymbol(1)
     num = symbolset.numsymbols
     assert num == 3, num
Exemplo n.º 13
0
 def testConstructorFile(self):
     """new instance of symbolSetObj from symbols.txt"""
     symbolset = mapscript.symbolSetObj(SYMBOLSET)
     num = symbolset.numsymbols
     assert num == 4, num
Exemplo n.º 14
0
 def testConstructorNoArgs(self):
     """new instance of symbolSetObj should have one symbol"""
     symbolset = mapscript.symbolSetObj()
     num = symbolset.numsymbols
     assert num == 1, num