示例#1
0
def test_ogr_fielddomain_coded():

    domain = ogr.CreateCodedFieldDomain('name', 'desc', ogr.OFTInteger,
                                        ogr.OFSTNone, {
                                            1: "one",
                                            "2": None
                                        })
    assert domain.GetName() == 'name'
    assert domain.GetDescription() == 'desc'
    assert domain.GetDomainType() == ogr.OFDT_CODED
    assert domain.GetFieldType() == ogr.OFTInteger
    assert domain.GetEnumeration() == {"1": "one", "2": None}

    domain = ogr.CreateCodedFieldDomain('name', None, ogr.OFTInteger,
                                        ogr.OFSTNone, {})
    assert domain.GetEnumeration() == {}

    with pytest.raises(Exception):
        domain = ogr.CreateCodedFieldDomain('name', 'desc', ogr.OFTInteger,
                                            ogr.OFSTNone, None)

    with pytest.raises(Exception):
        domain = ogr.CreateCodedFieldDomain('name', 'desc', ogr.OFTInteger,
                                            ogr.OFSTNone, 5)

    with pytest.raises(Exception):
        domain = ogr.CreateCodedFieldDomain('name', 'desc', ogr.OFTInteger,
                                            'x')
示例#2
0
def test_ogr2ogr_fielddomain_():

    src_ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0,
                                                   gdal.GDT_Unknown)
    src_lyr = src_ds.CreateLayer('layer')

    src_fld_defn = ogr.FieldDefn('foo')
    src_fld_defn.SetDomainName('my_domain')
    src_lyr.CreateField(src_fld_defn)
    assert src_ds.AddFieldDomain(
        ogr.CreateGlobFieldDomain('my_domain', 'desc', ogr.OFTString,
                                  ogr.OFSTNone, '*'))

    src_fld_defn = ogr.FieldDefn('bar', ogr.OFTInteger)
    src_fld_defn.SetDomainName('coded_domain')
    src_lyr.CreateField(src_fld_defn)
    assert src_ds.AddFieldDomain(
        ogr.CreateCodedFieldDomain('coded_domain', 'desc', ogr.OFTString,
                                   ogr.OFSTNone, {
                                       1: "one",
                                       2: "two",
                                       3: None
                                   }))

    src_fld_defn = ogr.FieldDefn('baz', ogr.OFTInteger)
    src_fld_defn.SetDomainName('non_existant_coded_domain')
    src_lyr.CreateField(src_fld_defn)

    f = ogr.Feature(src_lyr.GetLayerDefn())
    f.SetField('foo', 'foo_content')
    f.SetField('bar', 2)
    f.SetField('baz', 0)
    src_lyr.CreateFeature(f)

    f = ogr.Feature(src_lyr.GetLayerDefn())
    f.SetField('bar', -1)  # does not exist in dictionary
    src_lyr.CreateFeature(f)

    ds = gdal.VectorTranslate('', src_ds, format='Memory')
    lyr = ds.GetLayer(0)
    fld_defn = lyr.GetLayerDefn().GetFieldDefn(0)
    assert fld_defn.GetDomainName() == 'my_domain'
    domain = ds.GetFieldDomain('my_domain')
    assert domain is not None
    assert domain.GetDomainType() == ogr.OFDT_GLOB

    # Test -resolveDomains
    ds = gdal.VectorTranslate('', src_ds, format='Memory', resolveDomains=True)
    lyr = ds.GetLayer(0)
    lyr_defn = lyr.GetLayerDefn()
    assert lyr_defn.GetFieldCount() == 4

    fld_defn = lyr_defn.GetFieldDefn(0)
    assert fld_defn.GetDomainName() == 'my_domain'
    domain = ds.GetFieldDomain('my_domain')
    assert domain is not None
    assert domain.GetDomainType() == ogr.OFDT_GLOB

    fld_defn = lyr_defn.GetFieldDefn(1)
    assert fld_defn.GetName() == 'bar'
    assert fld_defn.GetType() == ogr.OFTInteger

    fld_defn = lyr_defn.GetFieldDefn(2)
    assert fld_defn.GetName() == 'bar_resolved'
    assert fld_defn.GetType() == ogr.OFTString

    fld_defn = lyr_defn.GetFieldDefn(3)
    assert fld_defn.GetName() == 'baz'
    assert fld_defn.GetType() == ogr.OFTInteger

    f = lyr.GetNextFeature()
    assert f['foo'] == 'foo_content'
    assert f['bar'] == 2
    assert f['bar_resolved'] == 'two'
    assert f['baz'] == 0

    f = lyr.GetNextFeature()
    assert f['bar'] == -1
    assert not f.IsFieldSet('bar_resolved')
示例#3
0
def test_ogr_parquet_write_edge_cases():

    outfilename = '/vsimem/out.parquet'

    # No layer
    ds = gdal.GetDriverByName('Parquet').Create(outfilename, 0, 0, 0,
                                                gdal.GDT_Unknown)
    assert ds is not None
    assert ds.GetLayerCount() == 0
    assert ds.GetLayer(0) is None
    assert ds.TestCapability(ogr.ODsCCreateLayer) == 1
    assert ds.TestCapability(ogr.ODsCAddFieldDomain) == 0
    domain = ogr.CreateCodedFieldDomain('name', 'desc', ogr.OFTInteger,
                                        ogr.OFSTNone, {
                                            1: "one",
                                            "2": None
                                        })
    assert ds.AddFieldDomain(domain) == False
    assert ds.GetFieldDomainNames() is None
    assert ds.GetFieldDomain('foo') is None
    ds = None
    gdal.Unlink(outfilename)

    # No field, no record
    ds = gdal.GetDriverByName('Parquet').Create(outfilename, 0, 0, 0,
                                                gdal.GDT_Unknown)
    assert ds is not None
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)
    with gdaltest.error_handler():
        assert ds.CreateLayer('out', srs=srs, geom_type=ogr.wkbPointM) is None
        assert ds.CreateLayer('out',
                              srs=srs,
                              geom_type=ogr.wkbPoint,
                              options=['COMPRESSION=invalid']) is None
    lyr = ds.CreateLayer('out', srs=srs, geom_type=ogr.wkbPoint)
    assert lyr is not None
    assert ds.GetLayerCount() == 1
    assert ds.GetLayer(0) is not None
    assert ds.TestCapability(ogr.ODsCCreateLayer) == 0
    assert ds.TestCapability(ogr.ODsCAddFieldDomain) == 1
    # Test creating a second layer
    with gdaltest.error_handler():
        assert ds.CreateLayer('out2', srs=srs, geom_type=ogr.wkbPoint) is None
    ds = None
    ds = gdal.OpenEx(outfilename)
    assert ds is not None
    lyr = ds.GetLayer(0)
    assert lyr.GetNextFeature() is None
    lyr = None
    ds = None
    gdal.Unlink(outfilename)

    # No geometry field, one record
    ds = gdal.GetDriverByName('Parquet').Create(outfilename, 0, 0, 0,
                                                gdal.GDT_Unknown)
    assert ds is not None
    lyr = ds.CreateLayer('out', geom_type=ogr.wkbNone)
    assert lyr.TestCapability(ogr.OLCCreateField) == 1
    assert lyr.TestCapability(ogr.OLCCreateGeomField) == 1
    assert lyr.TestCapability(ogr.OLCSequentialWrite) == 1
    assert lyr.TestCapability(ogr.OLCStringsAsUTF8) == 1
    fld_defn = ogr.FieldDefn('foo')
    fld_defn.SetNullable(False)
    assert lyr.CreateField(fld_defn) == ogr.OGRERR_NONE
    assert lyr is not None
    f = ogr.Feature(lyr.GetLayerDefn())
    with gdaltest.error_handler():
        # violation of not-null constraint
        assert lyr.CreateFeature(f) != ogr.OGRERR_NONE
    f['foo'] = 'bar'
    assert lyr.CreateFeature(f) == ogr.OGRERR_NONE
    assert lyr.GetFeatureCount() == 1
    assert lyr.TestCapability(ogr.OLCCreateField) == 0
    assert lyr.TestCapability(ogr.OLCCreateGeomField) == 0
    with gdaltest.error_handler():
        assert lyr.CreateField(ogr.FieldDefn('bar')) != ogr.OGRERR_NONE
        assert lyr.CreateGeomField(ogr.GeomFieldDefn(
            'baz', ogr.wkbPoint)) != ogr.OGRERR_NONE
    ds = None
    ds = gdal.OpenEx(outfilename)
    assert ds is not None
    lyr = ds.GetLayer(0)
    assert lyr.GetNextFeature() is not None
    lyr = None
    ds = None
    gdal.Unlink(outfilename)