Ejemplo n.º 1
0
def jpeg2000_10():

    if gdaltest.jpeg2000_drv is None:
        return 'skip'

    src_ds = gdal.GetDriverByName('GTiff').Create(
        '/vsimem/jpeg2000_10_src.tif', 128, 128, 5)
    for i in range(src_ds.RasterCount):
        src_ds.GetRasterBand(i + 1).Fill(10 * i + 1)

    ds = gdaltest.jpeg2000_drv.CreateCopy('/vsimem/jpeg2000_10_dst.tif',
                                          src_ds)
    ds = None

    ds = gdal.Open('/vsimem/jpeg2000_10_dst.tif')
    if ds is None:
        return 'fail'
    for i in range(src_ds.RasterCount):
        if ds.GetRasterBand(i + 1).Checksum() != src_ds.GetRasterBand(
                i + 1).Checksum():
            gdaltest.post_reason('bad checksum for band %d' % (i + 1))
            print(ds.GetRasterBand(i + 1).Checksum())
            print(src_ds.GetRasterBand(i + 1).Checksum())
            return 'fail'
    ds = None
    src_ds = None

    gdal.Unlink('/vsimem/jpeg2000_10_src.tif')
    gdal.Unlink('/vsimem/jpeg2000_10_dst.tif')

    return 'success'
Ejemplo n.º 2
0
def ogr_gml_34():

    if not gdaltest.have_gml_reader:
        return 'skip'

    drv = ogr.GetDriverByName('GML')
    ds = drv.CreateDataSource('/vsimem/ogr_gml_34.gml')
    lyr = ds.CreateLayer('test')
    lyr.CreateField(ogr.FieldDefn("name", ogr.OFTString))
    feat = ogr.Feature(lyr.GetLayerDefn())
    feat.SetField(0, '\xc4\x80liamanu<&')
    lyr.CreateFeature(feat)
    feat = None
    ds = None

    ds = ogr.Open('/vsimem/ogr_gml_34.gml')
    lyr = ds.GetLayer(0)
    feat = lyr.GetNextFeature()
    if feat.GetFieldAsString('name') != '\xc4\x80liamanu<&':
        print(feat.GetFieldAsString('name'))
        return 'fail'
    ds = None

    gdal.Unlink('/vsimem/ogr_gml_34.gml')
    gdal.Unlink('/vsimem/ogr_gml_34.gfs')

    return 'success'
Ejemplo n.º 3
0
def fill_no_data(dsm_content, filename):
    file_srtm = gdal.Open("./dems/srtm30_merged.tif")
    interpolated = interpolate(file_srtm, dsm_content, "/vsimem/lowest_res")
    file_eu_dem = gdal.Open("./dems/eu_dem.tif")
    interpolated_eu_dem = interpolate(file_eu_dem, dsm_content,
                                      "/vsimem/eu_dem")
    content = gdal.Warp(filename,
                        [interpolated, interpolated_eu_dem, dsm_content])
    gdal.Unlink("/vsimem/lowest_res")
    gdal.Unlink("/vsimem/eu_dem")
    return content
Ejemplo n.º 4
0
def load_float32_image(buffer):
    try:
        gdal.FileFromMemBuffer('/vsimem/temp', buffer)
        ds = gdal.Open('/vsimem/temp')
        channel = ds.GetRasterBand(1).ReadAsArray()
        ds = None  #cleanup
        gdal.Unlink('/vsimem/temp')  #cleanup
        return channel
    except Exception, e:
        ds = None  #cleanup
        gdal.Unlink('/vsimem/temp')  #cleanup
        raise e
Ejemplo n.º 5
0
def ogr_gml_30():

    if not gdaltest.have_gml_reader:
        return 'skip'

    field1 = " "
    for i in range(11):
        field1 = field1 + field1

    geom = "0 1 "
    for i in range(9):
        geom = geom + geom

    data = """<FeatureCollection xmlns:gml="http://www.opengis.net/gml">
  <gml:featureMember>
    <layer1>
      <geometry><gml:LineString><gml:posList>%s</gml:posList></gml:LineString></geometry>
      <field1>A%sZ</field1>
    </layer1>
  </gml:featureMember>
</FeatureCollection>""" % (geom, field1)

    f = gdal.VSIFOpenL("/vsimem/ogr_gml_30.gml", "wb")
    gdal.VSIFWriteL(data, 1, len(data), f)
    gdal.VSIFCloseL(f)

    ds = ogr.Open("/vsimem/ogr_gml_30.gml")
    lyr = ds.GetLayer(0)
    feat = lyr.GetNextFeature()
    field1 = feat.GetField(0)
    geom_wkt = feat.GetGeometryRef().ExportToWkt()
    ds = None

    gdal.Unlink("/vsimem/ogr_gml_30.gml")
    gdal.Unlink("/vsimem/ogr_gml_30.gfs")

    if len(field1) != 2050:
        gdaltest.post_reason('did not get expected len(field1)')
        print(field1)
        print(len(field1))
        return 'fail'

    if len(geom_wkt) != 2060:
        gdaltest.post_reason('did not get expected len(geom_wkt)')
        print(geom_wkt)
        print(len(geom_wkt))
        return 'fail'

    return 'success'
Ejemplo n.º 6
0
def tiff_srs_without_linear_units():

    sr = osr.SpatialReference()
    sr.ImportFromProj4('+proj=vandg +datum=WGS84')

    ds = gdal.GetDriverByName('GTiff').Create(
        '/vsimem/tiff_srs_without_linear_units.tif', 1, 1)
    ds.SetProjection(sr.ExportToWkt())
    ds = None

    ds = gdal.Open('/vsimem/tiff_srs_without_linear_units.tif')
    wkt = ds.GetProjectionRef()
    sr2 = osr.SpatialReference()
    sr2.SetFromUserInput(wkt)
    ds = None

    gdal.Unlink('/vsimem/tiff_srs_without_linear_units.tif')

    if sr.IsSame(sr2) != 1:

        gdaltest.post_reason('did not get expected SRS')
        print(sr)
        print(sr2)
        return 'fail'

    return 'success'
Ejemplo n.º 7
0
def pcidsk_13():
    if gdaltest.pcidsk_new == 0:
        return 'skip'

    if gdal.GetDriverByName('JPEG') is None:
        return 'skip'

    src_ds = gdal.Open('data/byte.tif')
    ds = gdal.GetDriverByName('PCIDSK').CreateCopy(
        '/vsimem/pcidsk_13.pix',
        src_ds,
        options=['INTERLEAVING=TILED', 'COMPRESSION=JPEG'])
    src_ds = None

    gdal.Unlink('/vsimem/pcidsk_13.pix.aux.xml')

    ds = None
    ds = gdal.Open('/vsimem/pcidsk_13.pix')
    band = ds.GetRasterBand(1)
    desc = band.GetDescription()
    cs = band.Checksum()
    ds = None

    gdal.GetDriverByName('PCIDSK').Delete('/vsimem/pcidsk_13.pix')

    if cs != 4645:
        gdaltest.post_reason('bad checksum')
        print(cs)
        return 'fail'

    return 'success'
Ejemplo n.º 8
0
def vsizip_3():

    fmain = gdal.VSIFOpenL("/vsizip/vsimem/test3.zip", "wb")

    f = gdal.VSIFOpenL("/vsizip/vsimem/test3.zip/foo", "wb")
    gdal.VSIFWriteL("foo", 1, 3, f)
    gdal.VSIFCloseL(f)
    f = gdal.VSIFOpenL("/vsizip/vsimem/test3.zip/bar", "wb")
    gdal.VSIFWriteL("bar", 1, 3, f)
    gdal.VSIFCloseL(f)

    gdal.VSIFCloseL(fmain)

    gdal.ReadDir("/vsizip/vsimem/test3.zip")

    f = gdal.VSIFOpenL("/vsizip/vsimem/test3.zip/baz", "wb")
    gdal.VSIFWriteL("baz", 1, 3, f)
    gdal.VSIFCloseL(f)

    res = gdal.ReadDir("/vsizip/vsimem/test3.zip")

    gdal.Unlink("/vsimem/test3.zip")

    if res != ['foo', 'bar', 'baz']:
        print(res)
        return 'fail'

    return 'success'
Ejemplo n.º 9
0
def hfa_createcopy_statistics():

    try:
        os.remove('../gcore/data/byte.tif.aux.xml')
    except:
        pass
    ds_src = gdal.Open('../gcore/data/byte.tif')
    out_ds = gdal.GetDriverByName('HFA').CreateCopy( '/vsimem/byte.img', ds_src, options = ['STATISTICS=YES'] )
    out_ds = None
    ds_src = None
    os.remove('../gcore/data/byte.tif.aux.xml')

    gdal.Unlink( '/vsimem/byte.img.aux.xml' )

    ds = gdal.Open('/vsimem/byte.img')
    md = ds.GetRasterBand(1).GetMetadata()
    ds = None

    gdal.GetDriverByName('HFA').Delete('/vsimem/byte.img')

    if md['STATISTICS_MINIMUM'] != '74':
        gdaltest.post_reason( 'STATISTICS_MINIMUM is wrong.' )
        print(md['STATISTICS_MINIMUM'])
        return 'fail'

    return 'success'
Ejemplo n.º 10
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'
Ejemplo n.º 11
0
def ogr_libkml_cleanup():
    if not ogrtest.have_read_libkml:
        return 'skip'

    if ogrtest.kml_ds is not None:
        ogrtest.kml_ds.Destroy()

    gdal.Unlink('/vsimem/libkml.kml')
    gdal.Unlink('/vsimem/libkml.kmz')

    # Re-register KML driver if necessary
    if ogrtest.kml_drv is not None:
        print('Re-register KML driver')
        ogrtest.kml_drv.Register()

    return 'success'
Ejemplo n.º 12
0
def webp_5():

    if gdaltest.webp_drv is None:
        return 'skip'

    md = gdaltest.webp_drv.GetMetadata()
    if md['DMD_CREATIONOPTIONLIST'].find('LOSSLESS') == -1:
        return 'skip'

    src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
    out_ds = gdaltest.webp_drv.CreateCopy('/vsimem/webp_5.webp', src_ds, options = ['LOSSLESS=YES'])
    src_ds = None
    cs1 = out_ds.GetRasterBand(1).Checksum()
    cs4 = out_ds.GetRasterBand(4).Checksum()
    out_ds = None
    gdal.Unlink('/vsimem/webp_5.webp')

    if cs1 != 12603:
        gdaltest.post_reason('did not get expected checksum on band 1')
        print(cs1)
        return 'fail'

    if cs4 != 10807:
        gdaltest.post_reason('did not get expected checksum on band 4')
        print(cs4)
        return 'fail'

    return 'success'
Ejemplo n.º 13
0
    def read_tiff(self, buffer, create_alpha=True):
        """ Reads image using GDAL from a buffer, and returns a CV2 image.
        """
        mem_map_name = '/vsimem/{}'.format(uuid.uuid4().get_hex())
        gdal_img = None
        try:
            gdal.FileFromMemBuffer(mem_map_name, buffer)
            gdal_img = gdal.Open(mem_map_name)

            channels = [
                gdal_img.GetRasterBand(i).ReadAsArray()
                for i in range(1, gdal_img.RasterCount + 1)
            ]

            if len(channels) >= 3:  # opencv is bgr not rgb.
                red_channel = channels[0]
                channels[0] = channels[2]
                channels[2] = red_channel

            if len(channels) < 4 and create_alpha:
                self.no_data_value = gdal_img.GetRasterBand(1).GetNoDataValue()
                channels.append(
                    numpy.float32(
                        gdal_img.GetRasterBand(1).GetMaskBand().ReadAsArray()))
            return cv.fromarray(cv2.merge(channels))
        finally:
            gdal_img = None
            gdal.Unlink(mem_map_name)  # Cleanup.
Ejemplo n.º 14
0
def envi_12():

    src_ds = gdal.Open('data/testenviclasses')
    out_ds = gdal.GetDriverByName('ENVI').CreateCopy('/vsimem/testenviclasses',
                                                     src_ds)
    src_ds = None
    out_ds = None

    gdal.Unlink('/vsimem/testenviclasses.aux.xml')

    ds = gdal.Open('/vsimem/testenviclasses')
    category = ds.GetRasterBand(1).GetCategoryNames()
    ct = ds.GetRasterBand(1).GetColorTable()

    if category != ['Black', 'White']:
        gdaltest.post_reason('bad category names')
        print(category)
        return 'fail'

    if ct.GetCount() != 2:
        gdaltest.post_reason('bad color entry count')
        print(ct.GetCount())
        return 'fail'

    if ct.GetColorEntry(0) != (0, 0, 0, 255):
        gdaltest.post_reason('bad color entry')
        print(ct.GetColorEntry(0))
        return 'fail'

    ds = None
    gdal.GetDriverByName('ENVI').Delete('/vsimem/testenviclasses')

    return 'success'
Ejemplo n.º 15
0
def webp_4():

    if gdaltest.webp_drv is None:
        return 'skip'

    md = gdaltest.webp_drv.GetMetadata()
    if md['DMD_CREATIONOPTIONLIST'].find('LOSSLESS') == -1:
        return 'skip'

    src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
    out_ds = gdaltest.webp_drv.CreateCopy('/vsimem/webp_4.webp', src_ds)
    src_ds = None
    cs1 = out_ds.GetRasterBand(1).Checksum()
    cs4 = out_ds.GetRasterBand(4).Checksum()
    out_ds = None
    gdal.Unlink('/vsimem/webp_4.webp')

    # 22849 is for libwebp 0.3.0
    if cs1 != 22001 and cs1 != 22849:
        gdaltest.post_reason('did not get expected checksum on band 1')
        print(cs1)
        return 'fail'

    if cs4 != 10807: # lossless alpha
        gdaltest.post_reason('did not get expected checksum on band 4')
        print(cs4)
        return 'fail'

    return 'success'
Ejemplo n.º 16
0
def vrt_read_7():

    filename = "/vsimem/vrt_read_7.vrt"

    content = """<VRTDataset rasterXSize="20" rasterYSize="20">
  <VRTRasterBand dataType="Byte" band="1">
    <SimpleSource>
      <SourceFilename relativeToVRT="1">%s</SourceFilename>
      <SourceBand>1</SourceBand>
      <SrcRect xOff="0" yOff="0" xSize="20" ySize="20" />
      <DstRect xOff="0" yOff="0" xSize="20" ySize="20" />
    </SimpleSource>
  </VRTRasterBand>
</VRTDataset>""" % filename

    gdal.FileFromMemBuffer(filename, content)
    gdal.PushErrorHandler('CPLQuietErrorHandler')
    ds = gdal.Open(filename)
    gdal.PopErrorHandler()
    error_msg = gdal.GetLastErrorMsg()
    gdal.Unlink(filename)

    if ds is not None:
        return 'fail'

    if error_msg != 'GDALOpen() called with too many recursion levels':
        return 'fail'

    return 'success'
Ejemplo n.º 17
0
def ace2_1():

    f = gdal.VSIFOpenL('/vsimem/45N015E_5M.ACE2', 'wb')
    gdal.VSIFSeekL(f, 180 * 180 * 4 - 1, 0)
    gdal.VSIFWriteL('\0', 1, 1, f)
    gdal.VSIFCloseL(f)

    tst = gdaltest.GDALTest('ACE2',
                            '/vsimem/45N015E_5M.ACE2',
                            1,
                            0,
                            filename_absolute=1)
    expected_gt = [
        15.0, 0.08333333333333333, 0.0, 60.0, 0.0, -0.08333333333333333
    ]
    expected_srs = """GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        TOWGS84[0,0,0,0,0,0,0],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9108"]],
    AUTHORITY["EPSG","4326"]]"""
    ret = tst.testOpen(check_gt=expected_gt, check_prj=expected_srs)

    gdal.Unlink('/vsimem/45N015E_5M.ACE2')

    return ret
Ejemplo n.º 18
0
def numpy_rw_11():

    if gdaltest.numpy_drv is None:
        return 'skip'

    import numpy

    type_tuples = [
        ('uint8', gdal.GDT_Byte, numpy.uint8, 255),
        ('uint16', gdal.GDT_UInt16, numpy.uint16, 65535),
        ('int16', gdal.GDT_Int16, numpy.int16, -32767),
        ('uint32', gdal.GDT_UInt32, numpy.uint32, 4294967295),
        ('int32', gdal.GDT_Int32, numpy.int32, -2147483648),
        ('float32', gdal.GDT_Float32, numpy.float32, 1.23),
        ('float64', gdal.GDT_Float64, numpy.float64, 1.23456789),
        ('cint16', gdal.GDT_CInt16, numpy.complex64, -32768 + 32767j),
        ('cint32', gdal.GDT_CInt32, numpy.complex64, -32769 + 32768j),
        ('cfloat32', gdal.GDT_CFloat32, numpy.complex64, -32768.5 + 32767.5j),
        ('cfloat64', gdal.GDT_CFloat64, numpy.complex128,
         -32768.123456 + 32767.123456j)
    ]

    for type_tuple in type_tuples:
        ds = gdal.GetDriverByName('GTiff').Create('/vsimem/' + type_tuple[0],
                                                  1, 1, 1, type_tuple[1])
        tmp = ds.ReadAsArray()
        if tmp.dtype != type_tuple[2]:
            gdaltest.post_reason('did not get expected numpy type')
            print(type_tuple)
            return 'fail'

        ar = numpy.empty([1, 1], dtype=type_tuple[2])
        ar[0][0] = type_tuple[3]
        ds.GetRasterBand(1).WriteArray(ar)
        ds = None

        ds = gdal.Open('/vsimem/' + type_tuple[0])
        ar2 = ds.ReadAsArray()
        ar3 = numpy.empty_like(ar2)
        ds.GetRasterBand(1).ReadAsArray(buf_obj=ar3)
        ds = None

        gdal.Unlink('/vsimem/' + type_tuple[0])

        if (type_tuple[0] == 'float32' and abs(ar2[0][0] - type_tuple[3]) > 1e-6) or \
           (type_tuple[0] != 'float32' and ar2[0][0] != type_tuple[3]):
            gdaltest.post_reason('did not get expected result (1)')
            print(ar2)
            print(type_tuple)
            return 'fail'

        if (type_tuple[0] == 'float32' and abs(ar3[0][0] - type_tuple[3]) > 1e-6) or \
           (type_tuple[0] != 'float32' and ar3[0][0] != type_tuple[3]):
            gdaltest.post_reason('did not get expected result (2)')
            print(ar3)
            print(type_tuple)
            return 'fail'

    return 'success'
Ejemplo n.º 19
0
 def read_geometry_with_gdal(self, response):
     gdal.FileFromMemBuffer('/vsimem/temp', response.content)
     dataset = gdal.ogr.Open('/vsimem/temp')
     layer = dataset.GetLayer(0)
     for _ in range(layer.GetFeatureCount()):
         yield layer.GetNextFeature()
     layer = None
     gdal.Unlink('/vsimem/temp')
Ejemplo n.º 20
0
    def test_from_array(self):
        array = gdal.Open(self.test_file_gcps).ReadAsArray()[1, 10:, :]
        vrt = VRT.from_array(array)

        self.assertEqual(vrt.dataset.RasterXSize, array.shape[1])
        self.assertEqual(vrt.dataset.RasterYSize, array.shape[0])
        self.assertEqual(vrt.dataset.RasterCount, 1)
        self.assertIn('filename', list(vrt.dataset.GetMetadata().keys()))
        self.assertEqual(gdal.Unlink(vrt.filename.replace('.vrt', '.raw')), 0)
Ejemplo n.º 21
0
def rfc30_2():

    if version_info >= (3, 0, 0):
        filename = 'tmp/yy\u4E2D\u6587.\u4E2D\u6587'
    else:
        exec("filename =  u'tmp/yy\u4E2D\u6587.\u4E2D\u6587'")
        # The typemaps should accept Unicode strings directly
        #filename = filename.encode( 'utf-8' )

    fd = gdal.VSIFOpenL(filename, 'w')
    if fd is None:
        gdaltest.post_reason('failed to create utf-8 named file.')
        return 'failure'

    gdal.VSIFWriteL('abc', 3, 1, fd)
    gdal.VSIFCloseL(fd)

    # rename

    if version_info >= (3, 0, 0):
        new_filename = 'tmp/yy\u4E2D\u6587.\u4E2D\u6587'
        filename_for_rename = filename
    else:
        exec("new_filename = u'tmp/yy\u4E2D\u6587.\u4E2D\u6587'")
        filename_for_rename = filename.encode(
            'utf-8')  # FIXME ? rename should perhaps accept unicode strings
        new_filename = new_filename.encode(
            'utf-8')  # FIXME ? rename should perhaps accept unicode strings

    if gdal.Rename(filename_for_rename, new_filename) != 0:
        gdaltest.post_reason('utf-8 rename failed.')
        return 'failure'

    fd = gdal.VSIFOpenL(new_filename, 'r')
    if fd is None:
        gdaltest.post_reason('reopen failed with utf8')
        return 'failure'

    data = gdal.VSIFReadL(3, 1, fd)
    gdal.VSIFCloseL(fd)

    if version_info >= (3, 0, 0):
        ok = eval("data == b'abc'")
    else:
        ok = data == 'abc'
    if not ok:
        gdaltest.post_reason('did not get expected data.')
        return 'failure'

    gdal.Unlink(new_filename)

    fd = gdal.VSIFOpenL(new_filename, 'r')
    if fd is not None:
        gdaltest.post_reason('did unlink fail on utf8 filename?')
        return 'failure'

    return 'success'
Ejemplo n.º 22
0
def vsizip_6():

    # Maintain ZIP file opened
    fmain = gdal.VSIFOpenL("/vsizip/vsimem/test6.zip", "wb")
    f = gdal.VSIFOpenL("/vsizip/vsimem/test6.zip/foo.bar", "wb")
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFWriteL("12345", 1, 5, f)
    gdal.VSIFCloseL(f)
    f = None

    gdal.PushErrorHandler('CPLQuietErrorHandler')
    f = gdal.VSIFOpenL("/vsizip/vsimem/test6.zip/foo.bar", "wb")
    gdal.PopErrorHandler()
    if f is not None:
        gdaltest.post_reason('fail')
        gdal.VSIFCloseL(f)
        return 'fail'
    gdal.VSIFCloseL(fmain)
    fmain = None

    gdal.Unlink("/vsimem/test6.zip")

    # Now close it each time
    f = gdal.VSIFOpenL("/vsizip/vsimem/test6.zip/foo.bar", "wb")
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFWriteL("12345", 1, 5, f)
    gdal.VSIFCloseL(f)
    f = None

    gdal.PushErrorHandler('CPLQuietErrorHandler')
    f = gdal.VSIFOpenL("/vsizip/vsimem/test6.zip/foo.bar", "wb")
    gdal.PopErrorHandler()
    if f is not None:
        gdaltest.post_reason('fail')
        gdal.VSIFCloseL(f)
        return 'fail'

    gdal.Unlink("/vsimem/test6.zip")

    return 'success'
Ejemplo n.º 23
0
def show_url(url, *args, **kwargs):
    try:
        urlfd = urlopen(url, timeout=15)
        mmapfn = "/vsimem/" + uuid4().hex
        gdal.FileFromMemBuffer(mmapfn, urlfd.read())
        show_fd(gdal.Open(mmapfn), *args, **kwargs)
    except URLError as e:
        print(e)
    finally:
        gdal.Unlink(mmapfn)
Ejemplo n.º 24
0
def ecrgtoc_3():

    ds = gdal.Open('ECRG_TOC_ENTRY:ProductTitle:DiscId:/vsimem/TOC.xml')
    if ds is None:
        return 'fail'
    ds = None

    gdal.PushErrorHandler('CPLQuietErrorHandler')
    ds = gdal.Open('ECRG_TOC_ENTRY:ProductTitle:DiscId2:/vsimem/TOC.xml')
    gdal.PopErrorHandler()
    if ds is not None:
        return 'fail'

    gdal.Unlink('/vsimem/TOC.xml')
    gdal.Unlink('/vsimem/TOC.xml.1.ovr')
    gdal.Unlink('/vsimem/clfc/2/000000009s0013.lf2')
    gdal.Unlink('/vsimem/clfc/2/000000009t0013.lf2')

    return 'success'
Ejemplo n.º 25
0
def ogr_libkml_check_write_dir():
    if not ogrtest.have_read_libkml:
        return 'skip'

    ret = ogr_libkml_check_write('/vsimem/libkmldir')
    files = gdal.ReadDir('/vsimem/libkmldir')
    for filename in files:
        gdal.Unlink('/vsimem/libkmldir/' + filename)
    gdal.Rmdir('/vsimem/libkmldir')
    return ret
Ejemplo n.º 26
0
def queryRaster(conn,
                rastertable,
                tiletable,
                tileid,
                date,
                rtype,
                level,
                bands,
                verbose=False):
    curs = conn.cursor()
    # convert band names to band numbers using config file

    sql = """
        select
            ST_astiff(ST_UNION(ST_CLIP(r.rast, t.geom)),ARRAY{bands})
        from
            {rastertable} r, {tiletable} t
        where
            t.id={tileid} and
            ST_INTERSECTS(r.rast,t.geom) and
            r.type='{rtype}' and
            r.level='{level}' and
            date='{date}'
        """.format(rastertable=rastertable,
                   tiletable=tiletable,
                   tileid=tileid,
                   rtype=rtype,
                   level=level,
                   date=date.strftime("%Y-%m-%d"),
                   bands=bands)

    if verbose: print sql
    # following https://gis.stackexchange.com/questions/130139/downloading-raster-data-into-python-from-postgis-using-psycopg2

    # Use a virtual memory file, which is named like this
    vsipath = '/vsimem/from_postgis'

    # Download raster data into Python as GeoTIFF, and make a virtual file for GDAL
    curs.execute(sql)

    gdal.FileFromMemBuffer(vsipath, bytes(curs.fetchone()[0]))

    # Read first band of raster with GDAL
    ds = gdal.Open(vsipath)
    arrays = []
    for b in range(len(bands)):
        band = ds.GetRasterBand(b + 1)
        arrays.append(band.ReadAsArray())

    # Close and clean up virtual memory file
    ds = band = None
    gdal.Unlink(vsipath)
    curs.close()

    return np.stack(arrays, axis=2)
Ejemplo n.º 27
0
Archivo: wms.py Proyecto: noltingj/gdal
def wms_15():

    if gdaltest.wms_drv is None:
        return 'skip'
    src_ds = gdal.Open(
        "http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true"
    )
    if src_ds is None:
        srv = 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true'
        if gdaltest.gdalurlopen(srv) is None:
            return 'skip'
        return 'fail'
    ds = gdal.GetDriverByName("WMS").CreateCopy("/vsimem/wms.xml", src_ds)
    src_ds = None

    if ds is None:
        return ' fail'

    if ds.RasterXSize != 134217728 \
       or ds.RasterYSize != 134217728 \
       or ds.RasterCount != 3:
        gdaltest.post_reason('wrong size or bands')
        return 'fail'

    wkt = ds.GetProjectionRef()
    if wkt.find('PROJCS["WGS 84 / Pseudo-Mercator"') != 0:
        gdaltest.post_reason('Got wrong SRS: ' + wkt)
        return 'fail'

    gt = ds.GetGeoTransform()
    if abs(gt[0]- -20037508.342787001) > 0.00001 \
       or abs(gt[3]- 20037508.342787001) > 0.00001 \
       or abs(gt[1] - 0.298582141697407) > 0.00001 \
       or abs(gt[2] - 0) > 0.00001 \
       or abs(gt[5] - -0.298582141697407) > 0.00001 \
       or abs(gt[4] - 0) > 0.00001:
        gdaltest.post_reason('wrong geotransform')
        print(gt)
        return 'fail'

    if ds.GetRasterBand(1).GetOverviewCount() != 19:
        gdaltest.post_reason('bad overview count')
        print(ds.GetRasterBand(1).GetOverviewCount())
        return 'fail'

    (block_xsize, block_ysize) = ds.GetRasterBand(1).GetBlockSize()
    if block_xsize != 256 or block_ysize != 256:
        gdaltest.post_reason('bad block size')
        print("(%d, %d)" % (block_xsize, block_ysize))
        return 'fail'

    ds = None
    gdal.Unlink("/vsimem/wms.xml")

    return 'success'
Ejemplo n.º 28
0
def ogr_gml_32():

    if not gdaltest.have_gml_reader:
        return 'skip'

    # Test without .xsd or .gfs
    f = gdal.VSIFOpenL("data/testfmegml.gml", "rb")
    data = gdal.VSIFReadL(1, 10000, f)
    gdal.VSIFCloseL(f)

    f = gdal.VSIFOpenL("/vsimem/ogr_gml_31.gml", "wb")
    gdal.VSIFWriteL(data, 1, len(data), f)
    gdal.VSIFCloseL(f)

    ds = ogr.Open('/vsimem/ogr_gml_31.gml')

    lyr = ds.GetLayer(1)
    feat = lyr.GetNextFeature()
    feat = lyr.GetNextFeature()
    if feat.GetFID() != 1:
        gdaltest.post_reason(
            'did not get feature when reading directly second layer')
        return 'fail'

    ds = None

    f = gdal.VSIFOpenL("/vsimem/ogr_gml_31.gfs", "rb")
    data = gdal.VSIFReadL(1, 10000, f)
    gdal.VSIFCloseL(f)

    data = str(data)

    if data.find("<SequentialLayers>true</SequentialLayers>") == -1:
        gdaltest.post_reason(
            'did not find <SequentialLayers>true</SequentialLayers> in .gfs')
        return 'fail'

    gdal.Unlink("/vsimem/ogr_gml_31.gml")
    gdal.Unlink("/vsimem/ogr_gml_31.gfs")

    return 'success'
Ejemplo n.º 29
0
def buff2rast(buff):
    # Use a virtual memory file, which is named like this
    vsipath = '/vsimem/from_postgis'

    gdal.FileFromMemBuffer(vsipath, bytes(buff))

    # Read first band of raster with GDAL
    ds = gdal.Open(vsipath)
    band = ds.GetRasterBand(1)

    gdal.Unlink(vsipath)
    return np.flipud(band.ReadAsArray())
Ejemplo n.º 30
0
def ogr_kml_write_schema():

    ds = ogr.GetDriverByName('KML').CreateDataSource(
        '/vsimem/ogr_kml_write_schema.kml')
    lyr = ds.CreateLayer("lyr")
    lyr.CreateField(ogr.FieldDefn('strfield', ogr.OFTString))
    lyr.CreateField(ogr.FieldDefn('intfield', ogr.OFTInteger))
    lyr.CreateField(ogr.FieldDefn('realfield', ogr.OFTReal))
    feat = ogr.Feature(lyr.GetLayerDefn())
    feat.SetField('strfield', 'strfield_val')
    feat.SetField('intfield', '1')
    feat.SetField('realfield', '2.34')
    lyr.CreateFeature(feat)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/ogr_kml_write_schema.kml', 'rb')
    content = gdal.VSIFReadL(1, 1000, f)
    gdal.VSIFCloseL(f)

    gdal.Unlink('/vsimem/ogr_kml_write_schema.kml')

    expected_content = """<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document><Folder><name>lyr</name>
  <Placemark>
    <ExtendedData><SchemaData schemaUrl="#lyr">
        <SimpleData name="strfield">strfield_val</SimpleData>
        <SimpleData name="intfield">1</SimpleData>
        <SimpleData name="realfield">2.34</SimpleData>
    </SchemaData></ExtendedData>
  </Placemark>
</Folder>
<Schema name="lyr" id="lyr">
    <SimpleField name="strfield" type="string"></SimpleField>
    <SimpleField name="intfield" type="int"></SimpleField>
    <SimpleField name="realfield" type="float"></SimpleField>
</Schema>
</Document></kml>"""

    content_lines = content.strip().split('\n')
    expected_lines = expected_content.strip().split('\n')

    if len(content_lines) != len(expected_lines):
        gdaltest.post_reason('fail')
        print(content)
        return 'fail'
    for i in range(len(content_lines)):
        if content_lines[i].strip() != expected_lines[i].strip():
            gdaltest.post_reason('fail')
            print(content)
            return 'fail'

    return 'success'