Beispiel #1
0
def minixml_3():

    fp = open('data/doctype.xml', 'r')
    text = fp.read()
    tree = gdal.ParseXMLString(text)

    if tree[0] != gdal.CXT_Element:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    # Check <chapter> element
    node = tree[6]

    if node[0] != gdal.CXT_Element:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    if node[1] != 'chapter':
        gdaltest.post_reason('Wrong element name')
        return 'fail'

    if len(node) != 7:
        gdaltest.post_reason('Wrong number of children.')
        return 'fail'

    # Check <chapter><title> subelement
    subnode = node[2]

    if subnode[0] != gdal.CXT_Element:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    if subnode[1] != 'title':
        gdaltest.post_reason('Wrong element name')
        return 'fail'

    if len(subnode) != 3:
        gdaltest.post_reason('Wrong number of children.')
        return 'fail'

    if subnode[2][1] != 'Chapter 1':
        gdaltest.post_reason('Wrong element content.')
        return 'fail'

    # Check fist <chapter><para> subelement
    subnode = node[3]

    if subnode[0] != gdal.CXT_Element:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    if subnode[1] != 'para':
        gdaltest.post_reason('Wrong element name')
        return 'fail'

    if len(subnode) != 3:
        gdaltest.post_reason('Wrong number of children.')
        return 'fail'

    return 'success'
def test_vrtderived_3():
    filename = 'tmp/derived.vrt'
    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)

    options = [
        'subClass=VRTDerivedRasterBand',
        'PixelFunctionType=dummy',
        'SourceTransferType=Byte',
    ]
    vrt_ds.AddBand(gdal.GDT_Byte, options)

    simpleSourceXML = '''    <SimpleSource>
      <SourceFilename>data/byte.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SimpleSource>'''

    md = {}
    md['source_0'] = simpleSourceXML

    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
    vrt_ds = None

    xmlstring = open(filename).read()
    gdal.Unlink(filename)

    node = gdal.ParseXMLString(xmlstring)
    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
    node = _xmlsearch(node, gdal.CXT_Element, 'SourceTransferType')
    node = _xmlsearch(node, gdal.CXT_Text, 'Byte')
    assert node is not None, 'incorrect SourceTransferType value'
Beispiel #3
0
def minixml_6():

    test_pairs = (
        ('<', 'element token after open angle bracket'),
        ('<a>', 'not all elements have been closed'),
        ('<a><b>', 'not all elements have been closed'),
        ('<a><b></a></b>', 'have matching'),
        ('<a foo=></a>', 'attribute value'),
        ('<></>', 'element token'),
        ('<&></&>', 'matching'),
        ('<a></a', 'Missing close angle'),
        ('<a foo=2\'> foo=2\'>', 'unexpected token'),
        ('<a?>', 'without matching'),
        ('<a/.', 'for value of attribute '),
        ('<a\'>', 'reached EOF before closing quote'),
    )

    for xml_str, expect in test_pairs:
        with gdaltest.error_handler():
            tree = gdal.ParseXMLString(xml_str)

        found = gdal.GetLastErrorMsg()
        if expect not in found:
            gdaltest.post_reason('Did not find expected error message: "%s"  '
                                 'Found: "%s"  '
                                 'For test string: "%s""' %
                                 (expect, found, xml_str))
            return 'fail'

        if tree is not None:
            gdaltest.post_reason('Tree is not None: "%s"' % tree)
            return 'fail'

    return 'success'
Beispiel #4
0
def vrtderived_2():
    filename = 'tmp/derived.vrt'
    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)

    options = [
        'subClass=VRTDerivedRasterBand',
        'PixelFunctionType=dummy',
    ]
    vrt_ds.AddBand(gdal.GDT_Byte, options)

    simpleSourceXML = '''    <SimpleSource>
      <SourceFilename>data/byte.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SimpleSource>'''

    md = {}
    md['source_0'] = simpleSourceXML

    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
    vrt_ds = None

    xmlstring = open(filename).read()
    gdal.Unlink(filename)

    node = gdal.ParseXMLString(xmlstring)
    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
    node = _xmlsearch(node, gdal.CXT_Element, 'PixelFunctionType')
    node = _xmlsearch(node, gdal.CXT_Text, 'dummy')
    if node is None:
        gdaltest.post_reason('incorrect PixelFunctionType value')
        return 'fail'

    return 'success'
Beispiel #5
0
def test_vrtrawlink_5():

    driver = gdal.GetDriverByName("VRT")
    ds = driver.Create('tmp/rawlink.vrt', 31, 35, 0)

    # Add a new band pointing to this bogus file.
    options = [
        'subClass=VRTRawRasterBand', 'SourceFilename=rawlink5.dat',
        'relativeToVRT=1', 'ImageOffset=100', 'PixelOffset=3', 'LineOffset=93',
        'ByteOrder=MSB'
    ]

    result = ds.AddBand(gdal.GDT_UInt16, options)
    assert result == gdal.CE_None, 'AddBand() returned error code'

    gdaltest.rawlink_ds.FlushCache()

    # Close and reopen to ensure we are getting data from disk.
    ds = None
    xmlstring = open('tmp/rawlink.vrt').read()

    root = gdal.ParseXMLString(xmlstring)
    node = _xmlsearch(root, gdal.CXT_Element, 'VRTRasterBand')
    node = _xmlsearch(node, gdal.CXT_Element, 'SourceFilename')
    node = _xmlsearch(node, gdal.CXT_Attribute, 'relativeToVRT')

    assert node is not None and node[2][
        1] == "1", 'incorrect relativeToVRT value'

    assert ('<ImageOffset>100</ImageOffset>' in xmlstring and \
       '<PixelOffset>3</PixelOffset>' in xmlstring and \
       '<LineOffset>93</LineOffset>' in xmlstring)
Beispiel #6
0
def test_vrtderived_1():
    filename = 'tmp/derived.vrt'
    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)

    options = [
        'subClass=VRTDerivedRasterBand',
    ]
    vrt_ds.AddBand(gdal.GDT_Byte, options)

    simpleSourceXML = '''    <SimpleSource>
      <SourceFilename>data/byte.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SimpleSource>'''

    md = {}
    md['source_0'] = simpleSourceXML

    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
    md_read = vrt_ds.GetRasterBand(1).GetMetadata('vrt_sources')
    vrt_ds = None

    expected_md_read = (
        '<SimpleSource>\n'
        '  <SourceFilename relativeToVRT="0">data/byte.tif</SourceFilename>\n'
        '  <SourceBand>1</SourceBand>\n')
    assert expected_md_read in md_read['source_0']

    xmlstring = open(filename).read()
    gdal.Unlink(filename)

    node = gdal.ParseXMLString(xmlstring)
    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
    node = _xmlsearch(node, gdal.CXT_Attribute, 'subClass')
    node = _xmlsearch(node, gdal.CXT_Text, 'VRTDerivedRasterBand')
    assert node is not None, 'invalid subclass'
def test_vsifile_19():

    for prefix in gdal.GetFileSystemsPrefixes():
        options = gdal.GetFileSystemOptions(prefix)
        # Check that the options is XML correct
        if options is not None:
            ret = gdal.ParseXMLString(options)
            assert ret is not None, (prefix, options)
Beispiel #8
0
def test_minixml_8():

    xml_str = '<a>' * 10001
    xml_str += '</a>' * 10001

    gdal.ErrorReset()
    with gdaltest.error_handler():
        tree = gdal.ParseXMLString(xml_str)
    assert tree is None, 'expected None tree'
    assert gdal.GetLastErrorMsg() != '', 'expected error message'
Beispiel #9
0
def minixml_4():

    xml = """<?xml encoding="utf-8"?>\n<foo />\n"""
    got_xml = gdal.SerializeXMLTree(gdal.ParseXMLString(xml))
    if xml != got_xml:
        gdaltest.post_reason('serialize xml tree failed.')
        print(got_xml)
        return 'fail'

    return 'success'
Beispiel #10
0
def vsifile_19():

    for prefix in gdal.GetFileSystemsPrefixes():
        options = gdal.GetFileSystemOptions(prefix)
        # Check that the options is XML correct
        if options is not None:
            ret = gdal.ParseXMLString(options)
            if ret is None:
                gdaltest.post_reason('fail')
                print(prefix, options)
                return 'fail'

    return 'success'
Beispiel #11
0
def vrtderived_2():
    filename = 'tmp/derived.vrt'
    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)

    options = [
        'subClass=VRTDerivedRasterBand',
        'PixelFunctionType=dummy',
        'PixelFunctionLanguage=Python',
    ]
    vrt_ds.AddBand(gdal.GDT_Byte, options)

    simpleSourceXML = '''    <SimpleSource>
      <SourceFilename>data/byte.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SimpleSource>'''

    md = {}
    md['source_0'] = simpleSourceXML

    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
    with gdaltest.error_handler():
        cs = vrt_ds.GetRasterBand(1).Checksum()
    if cs != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    with gdaltest.error_handler():
        ret = vrt_ds.GetRasterBand(1).WriteRaster(0, 0, 1, 1, ' ')
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'
    vrt_ds = None

    xmlstring = open(filename).read()
    gdal.Unlink(filename)

    node = gdal.ParseXMLString(xmlstring)
    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
    pixelfunctiontype = _xmlsearch(node, gdal.CXT_Element, 'PixelFunctionType')
    pixelfunctiontype = _xmlsearch(pixelfunctiontype, gdal.CXT_Text, 'dummy')
    if pixelfunctiontype is None:
        gdaltest.post_reason('incorrect PixelFunctionType value')
        return 'fail'
    pixelfunctionlanguage = _xmlsearch(node, gdal.CXT_Element,
                                       'PixelFunctionLanguage')
    pixelfunctionlanguage = _xmlsearch(pixelfunctionlanguage, gdal.CXT_Text,
                                       'Python')
    if pixelfunctionlanguage is None:
        gdaltest.post_reason('incorrect PixelFunctionLanguage value')
        return 'fail'

    return 'success'
Beispiel #12
0
def minixml_8():

    xml_str = ''.join('<a>' for i in range(10001))
    xml_str += ''.join('</a>' for i in range(10001))

    gdal.ErrorReset()
    with gdaltest.error_handler():
        tree = gdal.ParseXMLString(xml_str)
    if tree is not None:
        gdaltest.post_reason('expected None tree')
        return 'fail'
    if gdal.GetLastErrorMsg() == '':
        gdaltest.post_reason('expected error message')
        return 'fail'

    return 'success'
Beispiel #13
0
def test_minixml_7():

    test_strings = (
        '<1></1>',
        '<-></->',
        '<.></.>',
        '<![CDATA[',
    )

    for xml_str in test_strings:
        gdal.ErrorReset()
        tree = gdal.ParseXMLString(xml_str)

        found = gdal.GetLastErrorMsg()
        assert found == '', ('Unexpected msg "%s"' % found)

        assert tree is not None, ('Tree is None: "%s"' % tree)
Beispiel #14
0
def vrtderived_1():
    filename = 'tmp/derived.vrt'
    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)

    options = [
        'subClass=VRTDerivedRasterBand',
    ]
    vrt_ds.AddBand(gdal.GDT_Byte, options)

    simpleSourceXML = '''    <SimpleSource>
      <SourceFilename>data/byte.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SimpleSource>'''

    md = {}
    md['source_0'] = simpleSourceXML

    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
    md_read = vrt_ds.GetRasterBand(1).GetMetadata('vrt_sources')
    vrt_ds = None

    expected_md_read = (
        '<SimpleSource>\n'
        '  <SourceFilename relativeToVRT="0">data/byte.tif</SourceFilename>\n'
        '  <SourceBand>1</SourceBand>\n'
        '  <SourceProperties RasterXSize="20" RasterYSize="20" DataType="Byte" '
        'BlockXSize="20" BlockYSize="20" />\n'
        '</SimpleSource>\n')
    if md_read['source_0'] != expected_md_read:
        gdaltest.post_reason('fail')
        print(md_read['source_0'])
        return 'fail'

    xmlstring = open(filename).read()
    gdal.Unlink(filename)

    node = gdal.ParseXMLString(xmlstring)
    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
    node = _xmlsearch(node, gdal.CXT_Attribute, 'subClass')
    node = _xmlsearch(node, gdal.CXT_Text, 'VRTDerivedRasterBand')
    if node is None:
        gdaltest.post_reason('invalid subclass')
        return 'fail'

    return 'success'
Beispiel #15
0
def test_minixml_5():

    test_pairs = (
        ('<a></A>', 'case'),
        ('<a b=c></a>', 'quoted'),
    )

    for xml_str, expect in test_pairs:
        with gdaltest.error_handler():
            tree = gdal.ParseXMLString(xml_str)

        found = gdal.GetLastErrorMsg()
        assert expect in found, ('Did not find expected error message: "%s"  '
                                 'Found: "%s"  '
                                 'For test string: "%s""' %
                                 (expect, found, xml_str))

        assert tree is not None, ('Tree is None: "%s"' % tree)
Beispiel #16
0
def vrtrawlink_5():

    driver = gdal.GetDriverByName("VRT")
    ds = driver.Create('tmp/rawlink.vrt', 31, 35, 0)

    # Add a new band pointing to this bogus file.
    options = [
        'subClass=VRTRawRasterBand',
        'SourceFilename=rawlink5.dat',
        'relativeToVRT=1',
        'ImageOffset=100',
        'PixelOffset=3',
        'LineOffset=93',
        'ByteOrder=MSB'
        ]

    result = ds.AddBand(gdal.GDT_UInt16, options)
    if result != gdal.CE_None:
        gdaltest.post_reason('AddBand() returned error code')
        return 'fail'

    gdaltest.rawlink_ds.FlushCache()

    # Close and reopen to ensure we are getting data from disk.
    ds = None
    xmlstring = open('tmp/rawlink.vrt').read()

    root = gdal.ParseXMLString(xmlstring)
    node = _xmlsearch(root, gdal.CXT_Element, 'VRTRasterBand')
    node = _xmlsearch(node, gdal.CXT_Element, 'SourceFilename')
    node = _xmlsearch(node, gdal.CXT_Attribute, 'relativeToVRT')

    if node is None or node[2][1] != "1":
        gdaltest.post_reason('incorrect relativeToVRT value')
        return 'fail'

    if xmlstring.find('<ImageOffset>100</ImageOffset>') < 0 or \
       xmlstring.find('<PixelOffset>3</PixelOffset>') < 0 or \
       xmlstring.find('<LineOffset>93</LineOffset>') < 0 :
        gdaltest.post_reason('fail')
        print(xmlstring)
        return 'fail'

    return 'success'
def build_file(inname, outname):

    inpath = os.path.dirname(inname)
    xml_tree = gdal.ParseXMLString(open(inname).read())
    if xml_tree is None:
        print('Cannot parse %s' % inname)
        return False

    # out_f = open(outname, 'wb+')
    out_f = VSILFile(outname, 'wb+')
    if xml_tree[XML_TYPE_IDX] == gdal.CXT_Element and xml_tree[XML_VALUE_IDX] == 'JP2File':
        ret = parse_jp2file(inpath, xml_tree, out_f)
    elif xml_tree[XML_TYPE_IDX] == gdal.CXT_Element and xml_tree[XML_VALUE_IDX] == 'JP2KCodeStream':
        ret = parse_jp2codestream(inpath, xml_tree, out_f)
    else:
        print('Unexpected node: %s' % xml_tree[XML_VALUE_IDX])
        ret = False
    out_f.close()

    return ret
Beispiel #18
0
def test_minixml_1():

    tree = gdal.ParseXMLString(
        '<TestDoc style="123"><sub1/><sub2>abc</sub2></TestDoc>')

    assert tree[0] == gdal.CXT_Element, 'wrong node type.'

    assert tree[1] == 'TestDoc', 'Wrong element name'

    assert len(tree) == 5, 'Wrong number of children.'

    # Check style attribute
    node = tree[2]

    assert node[0] == gdal.CXT_Attribute, 'wrong node type.'

    assert node[1] == 'style', 'Wrong element name'

    assert len(node) == 3, 'Wrong number of children.'

    assert node[2][1] == '123', 'Wrong element content.'

    # Check <sub1> element
    node = tree[3]

    assert node[0] == gdal.CXT_Element, 'wrong node type.'

    assert node[1] == 'sub1', 'Wrong element name'

    assert len(node) == 2, 'Wrong number of children.'

    # Check <sub2> element
    node = tree[4]

    assert node[0] == gdal.CXT_Element, 'wrong node type.'

    assert node[1] == 'sub2', 'Wrong element name'

    assert len(node) == 3, 'Wrong number of children.'

    assert node[2][1] == 'abc', 'Wrong element content.'
Beispiel #19
0
def minixml_7():

    test_strings = (
        '<1></1>',
        '<-></->',
        '<.></.>',
        '<![CDATA[',
    )

    for xml_str in test_strings:
        tree = gdal.ParseXMLString(xml_str)

        found = gdal.GetLastErrorMsg()
        if found != '':
            gdaltest.post_reason('Unexpected msg "%s"' % found)
            return 'fail'

        if tree is None:
            gdaltest.post_reason('Tree is None: "%s"' % tree)
            return 'fail'

    return 'success'
Beispiel #20
0
def test_minixml_3():

    fp = open('data/doctype.xml', 'r')
    text = fp.read()
    tree = gdal.ParseXMLString(text)

    assert tree[0] == gdal.CXT_Element, 'wrong node type.'

    # Check <chapter> element
    node = tree[6]

    assert node[0] == gdal.CXT_Element, 'wrong node type.'

    assert node[1] == 'chapter', 'Wrong element name'

    assert len(node) == 7, 'Wrong number of children.'

    # Check <chapter><title> subelement
    subnode = node[2]

    assert subnode[0] == gdal.CXT_Element, 'wrong node type.'

    assert subnode[1] == 'title', 'Wrong element name'

    assert len(subnode) == 3, 'Wrong number of children.'

    assert subnode[2][1] == 'Chapter 1', 'Wrong element content.'

    # Check fist <chapter><para> subelement
    subnode = node[3]

    assert subnode[0] == gdal.CXT_Element, 'wrong node type.'

    assert subnode[1] == 'para', 'Wrong element name'

    assert len(subnode) == 3, 'Wrong number of children.'
Beispiel #21
0
def minixml_5():

    test_pairs = (
        ('<a></A>', 'case'),
        ('<a b=c></a>', 'quoted'),
    )

    for xml_str, expect in test_pairs:
        with gdaltest.error_handler():
            tree = gdal.ParseXMLString(xml_str)

        found = gdal.GetLastErrorMsg()
        if expect not in found:
            gdaltest.post_reason('Did not find expected error message: "%s"  '
                                 'Found: "%s"  '
                                 'For test string: "%s""' %
                                 (expect, found, xml_str))
            return 'fail'

        if tree is None:
            gdaltest.post_reason('Tree is None: "%s"' % tree)
            return 'fail'

    return 'success'
Beispiel #22
0
def test_minixml_processing_instruction():

    xml = """<?a b c d?>\n<foo />\n"""
    got_xml = gdal.SerializeXMLTree(gdal.ParseXMLString(xml))
    assert xml == got_xml, 'serialize xml tree failed.'
Beispiel #23
0
def minixml_1():

    tree = gdal.ParseXMLString(
        '<TestDoc style="123"><sub1/><sub2>abc</sub2></TestDoc>')

    if tree[0] != gdal.CXT_Element:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    if tree[1] != 'TestDoc':
        gdaltest.post_reason('Wrong element name')
        return 'fail'

    if len(tree) != 5:
        gdaltest.post_reason('Wrong number of children.')
        return 'fail'

    # Check style attribute
    node = tree[2]

    if node[0] != gdal.CXT_Attribute:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    if node[1] != 'style':
        gdaltest.post_reason('Wrong element name')
        return 'fail'

    if len(node) != 3:
        gdaltest.post_reason('Wrong number of children.')
        return 'fail'

    if node[2][1] != '123':
        gdaltest.post_reason('Wrong element content.')
        return 'fail'

    # Check <sub1> element
    node = tree[3]

    if node[0] != gdal.CXT_Element:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    if node[1] != 'sub1':
        gdaltest.post_reason('Wrong element name')
        return 'fail'

    if len(node) != 2:
        gdaltest.post_reason('Wrong number of children.')
        return 'fail'

    # Check <sub2> element
    node = tree[4]

    if node[0] != gdal.CXT_Element:
        gdaltest.post_reason('wrong node type.')
        return 'fail'

    if node[1] != 'sub2':
        gdaltest.post_reason('Wrong element name')
        return 'fail'

    if len(node) != 3:
        gdaltest.post_reason('Wrong number of children.')
        return 'fail'

    if node[2][1] != 'abc':
        gdaltest.post_reason('Wrong element content.')
        return 'fail'

    return 'success'
Beispiel #24
0
def test_minixml_4():

    xml = """<?xml encoding="utf-8"?>\n<foo />\n"""
    got_xml = gdal.SerializeXMLTree(gdal.ParseXMLString(xml))
    assert xml == got_xml, 'serialize xml tree failed.'