def eedai_2(): if gdaltest.eedai_drv is None: return 'skip' gdal.FileFromMemBuffer( '/vsimem/ee/assets/image', json.dumps({ 'type': 'IMAGE', 'properties': { 'foo': 'bar', 'prop_B9': 'the_prop_B9', 'prop_BAND_2': 'the_prop_B2' }, 'bands': [ { "id": "B1", "dataType": { "precision": "INTEGER", "range": { "max": 65535 } }, "grid": { "crsCode": "EPSG:32610", "affineTransform": { "translateX": 499980, "translateY": 4200000, "scaleX": 60, "scaleY": -60 }, "dimensions": { "width": 1830, "height": 1831 } } }, { "id": "B2", "dataType": { "precision": "INTEGER", "range": { "max": 65535 } }, "grid": { "crsCode": "EPSG:32610", "affineTransform": { "translateX": 499980, "translateY": 4200000, "scaleX": 60, "scaleY": -60 }, "dimensions": { "width": 10980, "height": 10981 } } }, { "id": "B9", "dataType": { "precision": "INTEGER", "range": { "max": 65535 } }, "grid": { "crsCode": "EPSG:32610", "affineTransform": { "translateX": 499980, "translateY": 4200000, "scaleX": 60, "scaleY": -60 }, "dimensions": { "width": 1830, "height": 1831 } } }, ] })) # To please the unregistering of the persistent connection gdal.FileFromMemBuffer('/vsimem/ee/', '') gdal.SetConfigOption('EEDA_BEARER', 'mybearer') gdal.SetConfigOption('EEDA_URL', '/vsimem/ee/') ds = gdal.OpenEx('EEDAI:image', open_options=['PIXEL_ENCODING=NPY']) gdal.SetConfigOption('EEDA_URL', None) expected_info = { "metadata": { "": { "foo": "bar" }, "SUBDATASETS": { "SUBDATASET_1_DESC": "Bands B1,B9 of image", "SUBDATASET_2_DESC": "Band B2 of image", "SUBDATASET_2_NAME": "EEDAI:image:B2", "SUBDATASET_1_NAME": "EEDAI:image:B1,B9" }, "IMAGE_STRUCTURE": { "INTERLEAVE": "PIXEL" } }, "size": [1830, 1831], "driverLongName": "Earth Engine Data API Image", "bands": [{ "colorInterpretation": "Undefined", "block": [256, 256], "metadata": {}, "band": 1, "description": "B1", "type": "UInt16", "overviews": [{ "size": [915, 915] }, { "size": [457, 457] }, { "size": [228, 228] }] }, { "colorInterpretation": "Undefined", "block": [256, 256], "metadata": { "": { "prop": "the_prop_B9" } }, "band": 2, "description": "B9", "type": "UInt16", "overviews": [{ "size": [915, 915] }, { "size": [457, 457] }, { "size": [228, 228] }] }], "cornerCoordinates": { "center": [554880.0, 4145070.0], "upperRight": [609780.0, 4200000.0], "lowerLeft": [499980.0, 4090140.0], "lowerRight": [609780.0, 4090140.0], "upperLeft": [499980.0, 4200000.0] }, "files": [], "description": "EEDAI:image", "geoTransform": [499980.0, 60.0, 0.0, 4200000.0, 0.0, -60.0] } info = gdal.Info(ds, format='json') for key in expected_info: if not (key in info and info[key] == expected_info[key]): gdaltest.post_reason('fail') print('Got difference for key %s' % key) if key in info: print('Got: ' + str(info[key])) else: print('Does not exist in got info') print('Expected: ' + str(expected_info[key])) print('Whole info:') print(json.dumps(info, indent=4)) return 'fail' if ds.GetProjectionRef().find('32610') < 0: gdaltest.post_reason('fail') print(ds.GetProjectionRef()) return 'fail' if ds.GetRasterBand(1).GetOverview(-1) is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(3) is not None: gdaltest.post_reason('fail') return 'fail' npy_serialized = struct.pack('B' * 8, 0x93, ord('N'), ord('U'), ord('M'), ord('P'), ord('Y'), 1, 0) descr = "{'descr': [('B1', '<u2'), ('B9', '<u2')], 'fortran_order': False, 'shape': (39, 38), }".encode( 'ascii') npy_serialized += struct.pack('<h', len(descr)) npy_serialized += descr val = struct.pack('<h', 12345) + struct.pack('<h', 23456) npy_serialized += ''.encode('ascii').join(val for i in range(38 * 39)) gdal.FileFromMemBuffer( '/vsimem/ee/assets:getPixels&CUSTOMREQUEST=POST&POSTFIELDS={ "path": "image", "encoding": "NPY", "bandIds": [ "B1", "B9" ], "grid": { "origin": { "x": 607500.0, "y": 4092480.0 }, "pixelSize": { "x": 60.0, "y": -60.0 }, "dimensions": { "width": 38, "height": 39 } } }', npy_serialized) got_data = ds.GetRasterBand(1).ReadRaster(1800, 1810, 1, 1) got_data = struct.unpack('h', got_data)[0] if got_data != 12345: gdaltest.post_reason('fail') print(got_data) return 'fail' got_data = ds.GetRasterBand(2).ReadRaster(1800, 1810, 1, 1) got_data = struct.unpack('h', got_data)[0] if got_data != 23456: gdaltest.post_reason('fail') print(got_data) return 'fail' ds = None gdal.SetConfigOption('EEDA_URL', '/vsimem/ee/') sub_ds = gdal.Open('EEDAI:image:B1,B9') gdal.SetConfigOption('EEDA_URL', None) if sub_ds.RasterCount != 2: gdaltest.post_reason('fail') print(sub_ds.RasterCount) return 'fail' gdal.SetConfigOption('EEDA_URL', '/vsimem/ee/') sub_ds = gdal.Open('EEDAI:image:B2') gdal.SetConfigOption('EEDA_URL', None) if sub_ds.RasterCount != 1: gdaltest.post_reason('fail') print(sub_ds.RasterCount) return 'fail' got_md = sub_ds.GetRasterBand(1).GetMetadata() expected_md = {'prop': 'the_prop_B2'} if got_md != expected_md: gdaltest.post_reason('fail') print(got_md) return 'fail' gdal.SetConfigOption('EEDA_BEARER', None) return 'success'
def test_rasterio_resampled_value_is_nodata(): gdal.FileFromMemBuffer( '/vsimem/in.asc', """ncols 4 nrows 4 xllcorner 440720.000000000000 yllcorner 3750120.000000000000 cellsize 60.000000000000 nodata_value 0 -1.1 -1.1 1.1 1.1 -1.1 -1.1 1.1 1.1 -1.1 -1.1 1.1 1.1 -1.1 -1.1 1.1 1.1""") ds = gdal.Open('/vsimem/in.asc') data = ds.GetRasterBand(1).ReadRaster(buf_xsize=1, buf_ysize=1, resample_alg=gdal.GRIORA_Lanczos) data_ar = struct.unpack('f' * 1, data) expected_ar = (1.1754943508222875e-38, ) assert data_ar == expected_ar data = ds.GetRasterBand(1).ReadRaster(buf_xsize=1, buf_ysize=1, resample_alg=gdal.GRIORA_Average) data_ar = struct.unpack('f' * 1, data) expected_ar = (1.1754943508222875e-38, ) assert data_ar == expected_ar gdal.Unlink('/vsimem/in.asc') gdal.FileFromMemBuffer( '/vsimem/in.asc', """ncols 4 nrows 4 xllcorner 440720.000000000000 yllcorner 3750120.000000000000 cellsize 60.000000000000 nodata_value 0 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1""") ds = gdal.Open('/vsimem/in.asc') data = ds.GetRasterBand(1).ReadRaster(buf_xsize=1, buf_ysize=1, resample_alg=gdal.GRIORA_Lanczos) data_ar = struct.unpack('I' * 1, data) expected_ar = (1, ) assert data_ar == expected_ar data = ds.GetRasterBand(1).ReadRaster(buf_xsize=1, buf_ysize=1, resample_alg=gdal.GRIORA_Average) data_ar = struct.unpack('I' * 1, data) expected_ar = (1, ) assert data_ar == expected_ar gdal.Unlink('/vsimem/in.asc')
def generate_libkml(filename): gdal.Unlink(filename) content = """eiffel_tower_normal:SYMBOL(id:"http://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Eiffel_Tower_from_north_Avenue_de_New_York%2C_Aug_2010.jpg/220px-Eiffel_Tower_from_north_Avenue_de_New_York%2C_Aug_2010.jpg");LABEL(c:#FF0000FF) eiffel_tower_highlight:SYMBOL(id:"http://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Eiffel_Tower_from_north_Avenue_de_New_York%2C_Aug_2010.jpg/220px-Eiffel_Tower_from_north_Avenue_de_New_York%2C_Aug_2010.jpg");LABEL(c:#0000FFFF)""" gdal.FileFromMemBuffer("/vsimem/style.txt", content) style_table = ogr.StyleTable() style_table.LoadStyleTable("/vsimem/style.txt") gdal.Unlink("/vsimem/style.txt") ds_options = ['author_name=Even Rouault', 'author_uri=http://gdal.org', '[email protected]', 'link=http://gdal.org', 'phonenumber=tel:12345678', 'NLC_MINREFRESHPERIOD=3600', 'NLC_MAXSESSIONLENGTH=-1', 'NLC_COOKIE=cookie', 'NLC_MESSAGE=message', 'NLC_LINKNAME=linkname', 'NLC_LINKDESCRIPTION=linkdescription', 'NLC_LINKSNIPPET=linksnippet', 'NLC_EXPIRES=2014-12-31T23:59:59Z', 'LISTSTYLE_ICON_HREF=http://www.gdal.org/gdalicon.png', 'eiffel_tower_normal_balloonstyle_bgcolor=#FFFF00'] ds = ogr.GetDriverByName('LIBKML').CreateDataSource(filename, options=ds_options) ds.SetStyleTable(style_table) lyr_options = ['LOOKAT_LONGITUDE=2.2945', 'LOOKAT_LATITUDE=48.85825', 'LOOKAT_RANGE=300', 'LOOKAT_ALTITUDE=30', 'LOOKAT_HEADING=0', 'LOOKAT_TILT=70', 'LOOKAT_ALTITUDEMODE=relativeToGround', 'ADD_REGION=YES', 'REGION_MIN_LOD_PIXELS=128', 'REGION_MAX_LOD_PIXELS=10000000', 'REGION_MIN_FADE_EXTENT=1', 'REGION_MAX_FADE_EXTENT=2', 'SO_HREF=http://www.gdal.org/gdalicon.png', 'LISTSTYLE_ICON_HREF=http://www.gdal.org/gdalicon.png'] lyr = ds.CreateLayer('test', options=lyr_options) lyr.CreateField(ogr.FieldDefn('name', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('description', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('nom_francais', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('int_value', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('double_value', ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn('timestamp', ogr.OFTDateTime)) lyr.CreateField(ogr.FieldDefn('begin', ogr.OFTDateTime)) lyr.CreateField(ogr.FieldDefn('end', ogr.OFTDateTime)) lyr.CreateField(ogr.FieldDefn('snippet', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('altitudeMode', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('extrude', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('tessellate', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('model', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("scale_x", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("scale_y", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("scale_z", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("heading", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("tilt", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("roll", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("networklink", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("networklink_refreshvisibility", ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn("networklink_flytoview", ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn("networklink_refreshMode", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("networklink_refreshInterval", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("networklink_viewRefreshMode", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("networklink_viewRefreshTime", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("networklink_viewBoundScale", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("networklink_viewFormat", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("networklink_httpQuery", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("camera_longitude", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("camera_latitude", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("camera_altitude", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("camera_altitudemode", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("photooverlay", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("leftfov", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("rightfov", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("bottomfov", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("topfov", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("near", ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn("photooverlay_shape", ogr.OFTString)) lyr.CreateField(ogr.FieldDefn("imagepyramid_tilesize", ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn("imagepyramid_maxwidth", ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn("imagepyramid_maxheight", ogr.OFTInteger)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('name', 'Eiffel tower') feat.SetField('description', 'Famous Paris attraction. Built by Gustave Eiffel in 1889.') feat.SetField('nom_francais', 'Tour Eiffel') feat.SetField('int_value', 12) feat.SetField('double_value', 34.56) feat.SetField('snippet', 'Very cool snippet') feat.SetField('begin', '1889/05/06') feat.SetField('end', '9999/12/31') feat.SetStyleString('@eiffel_tower') feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2.2945 48.85825)')) lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('name', 'Avenue Gustave Eiffel') feat.SetField('timestamp', '2014/02/22') feat.SetGeometry(ogr.CreateGeometryFromWkt('LINESTRING(2.29420 48.85746,2.29540 48.85833)')) feat.SetStyleString('PEN(c:#00FF00)') feat.SetField('tessellate', 1) lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('name', 'Ecole Militaire') feat.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON((2.30383 48.85162 15,2.30460 48.85220 15,2.30581 48.85152 15,2.30507 48.85083 15,2.30383 48.85162 15))')) feat.SetField('altitudeMode', 'relativeToGround') feat.SetField('extrude', 1) feat.SetStyleString('BRUSH(fc:#0000FF)') lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('name', 'Champ de Mars') feat.SetGeometry(ogr.CreateGeometryFromWkt('MULTIPOLYGON(((2.29413 48.85703,2.29606 48.85847,2.29837 48.85679,2.29676 48.85543,2.29413 48.85703)),((2.29656 48.85504,2.29929 48.85674,2.30359 48.85364,2.30164 48.85226,2.29656 48.85504)))')) lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2.2945 48.85825 10)')) feat.SetField("tilt", 75) feat.SetField("roll", 10) feat.SetField("heading", -70) feat.SetField("scale_x", 2) feat.SetField("scale_y", 3) feat.SetField("scale_z", 4) feat.SetField("altitudeMode", "relativeToGround") feat.SetField("model", "http://even.rouault.free.fr/kml/gdal_2.1/dummy.dae") lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField("name", "a network link") feat.SetField("networklink", "http://developers.google.com/kml/documentation/Point.kml") feat.SetField("networklink_refreshVisibility", 1) feat.SetField("networklink_flyToView", 1) feat.SetField("networklink_refreshInterval", 60) feat.SetField("networklink_httpQuery", "[clientVersion]") lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField("networklink", "http://developers.google.com/kml/documentation/Point.kml") feat.SetField("networklink_viewRefreshTime", 30) lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField("networklink", "http://developers.google.com/kml/documentation/Point.kml") feat.SetField("networklink_refreshMode", 'onExpire') feat.SetField("networklink_viewRefreshMode", 'onRegion') feat.SetField("networklink_viewBoundScale", 0.5) feat.SetField("networklink_viewFormat", 'BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]') lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField("photooverlay", "http://tile.openstreetmap.org/$[level]/$[x]/$[y].png") feat.SetField("imagepyramid_tilesize", 256) feat.SetField("imagepyramid_maxwidth", 512) feat.SetField("imagepyramid_maxheight", 512) feat.SetField("camera_longitude", 2.2946) feat.SetField("camera_latitude", 48.8583) feat.SetField("camera_altitude", 20) feat.SetField("camera_altitudemode", "relativeToGround") feat.SetField("leftfov", -60) feat.SetField("rightfov", 60) feat.SetField("bottomfov", -60) feat.SetField("topfov", 60) feat.SetField("near", 100) feat.SetField("heading", 0) feat.SetField("tilt", 90) feat.SetField("roll", 0) feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2.2945 48.85825)')) lyr.CreateFeature(feat) # feat = ogr.Feature(lyr.GetLayerDefn()) # feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT EMPTY')) # lyr.CreateFeature(feat) # feat = ogr.Feature(lyr.GetLayerDefn()) # # feat.SetGeometry(ogr.CreateGeometryFromWkt('LINESTRING EMPTY')) # lyr.CreateFeature(feat) # feat = ogr.Feature(lyr.GetLayerDefn()) # feat.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON EMPTY')) # lyr.CreateFeature(feat) # feat = ogr.Feature(lyr.GetLayerDefn()) # feat.SetGeometry(ogr.CreateGeometryFromWkt('GEOMETRYCOLLECTION (POINT EMPTY,POINT(1 2))')) # lyr.CreateFeature(feat) lyr_options = ['CAMERA_LONGITUDE=2.2945', 'CAMERA_LATITUDE=48.85825', 'CAMERA_ALTITUDE=30', 'CAMERA_HEADING=120', 'CAMERA_TILT=70', 'CAMERA_ROLL=10', 'CAMERA_ALTITUDEMODE=relativeToGround', 'FOLDER=YES', 'NAME=layer_name', 'DESCRIPTION=description', 'OPEN=1', 'VISIBILITY=1', 'SNIPPET=snippet'] ds.CreateLayer('test2', options=lyr_options) gdal.SetConfigOption('LIBKML_USE_SIMPLEFIELD', 'NO') lyr = ds.CreateLayer('test_data') gdal.SetConfigOption('LIBKML_USE_SIMPLEFIELD', None) lyr.CreateField(ogr.FieldDefn('foo', ogr.OFTString)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField("foo", "bar") feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2.2945 48.85825)')) lyr.CreateFeature(feat) ds = None
def vrt_read_21(): ds = gdal.Open('data/byte.tif') data = ds.ReadRaster(0, 0, 20, 20, 400, 400) ds = None ds = gdal.GetDriverByName('GTiff').Create('/vsimem/byte.tif', 400, 400) ds.WriteRaster(0, 0, 400, 400, data) ds.BuildOverviews('NEAR', [2]) ds = None gdal.FileFromMemBuffer('/vsimem/vrt_read_21.vrt', """<VRTDataset rasterXSize="800" rasterYSize="800"> <VRTRasterBand dataType="Byte" band="1"> <SimpleSource> <SourceFilename>/vsimem/byte.tif</SourceFilename> <SourceBand>1</SourceBand> <SourceProperties RasterXSize="400" RasterYSize="400" DataType="Byte" BlockXSize="400" BlockYSize="1" /> <SrcRect xOff="100" yOff="100" xSize="200" ySize="250" /> <DstRect xOff="300" yOff="400" xSize="200" ySize="250" /> </SimpleSource> </VRTRasterBand> </VRTDataset>""") ds = gdal.Open('/vsimem/vrt_read_21.vrt') if ds.GetRasterBand(1).GetOverviewCount() != 1: gdaltest.post_reason('failure') print(ds.GetRasterBand(1).GetOverviewCount()) return 'fail' data_ds_one_band = ds.ReadRaster(0, 0, 800, 800, 400, 400) ds = None gdal.FileFromMemBuffer('/vsimem/vrt_read_21.vrt', """<VRTDataset rasterXSize="800" rasterYSize="800"> <VRTRasterBand dataType="Byte" band="1"> <SimpleSource> <SourceFilename>/vsimem/byte.tif</SourceFilename> <SourceBand>1</SourceBand> <SourceProperties RasterXSize="400" RasterYSize="400" DataType="Byte" BlockXSize="400" BlockYSize="1" /> <SrcRect xOff="100" yOff="100" xSize="200" ySize="250" /> <DstRect xOff="300" yOff="400" xSize="200" ySize="250" /> </SimpleSource> </VRTRasterBand> <VRTRasterBand dataType="Byte" band="2"> <ComplexSource> <SourceFilename>/vsimem/byte.tif</SourceFilename> <SourceBand>1</SourceBand> <SourceProperties RasterXSize="400" RasterYSize="400" DataType="Byte" BlockXSize="400" BlockYSize="1" /> <SrcRect xOff="100" yOff="100" xSize="200" ySize="250" /> <DstRect xOff="300" yOff="400" xSize="200" ySize="250" /> <ScaleOffset>10</ScaleOffset> </ComplexSource> </VRTRasterBand> </VRTDataset>""") ds = gdal.Open('/vsimem/vrt_read_21.vrt') if ds.GetRasterBand(1).GetOverviewCount() != 1: gdaltest.post_reason('failure') print(ds.GetRasterBand(1).GetOverviewCount()) return 'fail' ds = gdal.Open('/vsimem/vrt_read_21.vrt') ovr_band = ds.GetRasterBand(1).GetOverview(-1) if ovr_band is not None: gdaltest.post_reason('failure') return 'fail' ovr_band = ds.GetRasterBand(1).GetOverview(1) if ovr_band is not None: gdaltest.post_reason('failure') return 'fail' ovr_band = ds.GetRasterBand(1).GetOverview(0) if ovr_band is None: gdaltest.post_reason('failure') return 'fail' cs = ovr_band.Checksum() cs2 = ds.GetRasterBand(2).GetOverview(0).Checksum() data = ds.ReadRaster(0, 0, 800, 800, 400, 400) if data != data_ds_one_band + ds.GetRasterBand(2).ReadRaster(0, 0, 800, 800, 400, 400): gdaltest.post_reason('failure') return 'fail' mem_ds = gdal.GetDriverByName('MEM').Create('', 400, 400, 2) mem_ds.WriteRaster(0, 0, 400, 400, data) ref_cs = mem_ds.GetRasterBand(1).Checksum() ref_cs2 = mem_ds.GetRasterBand(2).Checksum() mem_ds = None if cs != ref_cs: gdaltest.post_reason('failure') print(cs) print(ref_cs) return 'fail' if cs2 != ref_cs2: gdaltest.post_reason('failure') print(cs2) print(ref_cs2) return 'fail' ds.BuildOverviews('NEAR', [2]) expected_cs = ds.GetRasterBand(1).GetOverview(0).Checksum() expected_cs2 = ds.GetRasterBand(2).GetOverview(0).Checksum() ds = None if cs != expected_cs: gdaltest.post_reason('failure') print(cs) print(expected_cs) return 'fail' if cs2 != expected_cs2: gdaltest.post_reason('failure') print(cs2) print(expected_cs2) return 'fail' gdal.Unlink('/vsimem/vrt_read_21.vrt') gdal.Unlink('/vsimem/vrt_read_21.vrt.ovr') gdal.Unlink('/vsimem/byte.tif') return 'success'
def ogr_vdv_8(): gdal.PushErrorHandler() ds = ogr.GetDriverByName('VDV').CreateDataSource('/does/not_exist') gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() ds = ogr.GetDriverByName('VDV').CreateDataSource( '/does/not_exist', options=['SINGLE_FILE=FALSE']) gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') return 'fail' # Add layer in non writable directory if sys.platform.startswith('linux'): os.mkdir('tmp/ogr_vdv_8') open('tmp/ogr_vdv_8/empty.x10', 'wb').write('tbl; foo\natr;\nfrm;\n'.encode('latin1')) # 0555 = 365 os.chmod('tmp/ogr_vdv_8', 365) try: open('tmp/ogr_vdv_8/another_file', 'wb').close() shutil.rmtree('tmp/ogr_vdv_8') do_test = False except: do_test = True if do_test: ds = ogr.Open('tmp/ogr_vdv_8', update=1) gdal.PushErrorHandler() lyr = ds.CreateLayer('another_layer') gdal.PopErrorHandler() # 0755 = 493 os.chmod('tmp/ogr_vdv_8', 493) ds = None shutil.rmtree('tmp/ogr_vdv_8') if lyr is not None: gdaltest.post_reason('fail') return 'fail' out_filename = '/vsimem/vdv/ogr_vdv_8.x10' ds = ogr.GetDriverByName('VDV').CreateDataSource(out_filename) # File already exists gdal.PushErrorHandler() ds2 = ogr.GetDriverByName('VDV').CreateDataSource(out_filename) gdal.PopErrorHandler() if ds2 is not None: gdaltest.post_reason('fail') return 'fail' if ds.TestCapability(ogr.ODsCCreateLayer) != 1: gdaltest.post_reason('fail') return 'fail' lyr1 = ds.CreateLayer("lyr1") if lyr1.TestCapability(ogr.OLCSequentialWrite) != 1: gdaltest.post_reason('fail') return 'fail' if lyr1.TestCapability(ogr.OLCCreateField) != 1: gdaltest.post_reason('fail') return 'fail' lyr1.ResetReading() gdal.PushErrorHandler() lyr1.GetNextFeature() gdal.PopErrorHandler() lyr1.CreateFeature(ogr.Feature(lyr1.GetLayerDefn())) # Layer structure is now frozen if lyr1.TestCapability(ogr.OLCCreateField) != 0: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() ret = lyr1.CreateField(ogr.FieldDefn('not_allowed')) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' lyr2 = ds.CreateLayer("lyr2") lyr2.CreateFeature(ogr.Feature(lyr2.GetLayerDefn())) # Test interleaved writing if lyr1.TestCapability(ogr.OLCSequentialWrite) != 0: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() ret = lyr1.CreateFeature(ogr.Feature(lyr1.GetLayerDefn())) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' if lyr1.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' ds = None # Test appending new layer to file without eof gdal.FileFromMemBuffer(out_filename, 'tbl; foo\natr; atr\nfrm; char[40]\nrec; "foo"\n') ds = ogr.Open(out_filename, update=1) lyr = ds.CreateLayer('new_layer') lyr.CreateField(ogr.FieldDefn('atr')) f = ogr.Feature(lyr.GetLayerDefn()) f['atr'] = 'bar' lyr.CreateFeature(f) f = None ds = None expected = """tbl; foo atr; atr frm; char[40] rec; "foo" tbl; new_layer atr; atr frm; char[80] rec; "bar" end; 1 eof; 2 """ f = gdal.VSIFOpenL(out_filename, 'rb') got = gdal.VSIFReadL(1, 10000, f).decode('latin1') gdal.VSIFCloseL(f) if got != expected: gdaltest.post_reason('fail') print(got) return 'fail' # Test we are robust against missing end; ds = ogr.Open(out_filename) for i in range(2): lyr = ds.GetLayer(i) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' lyr.ResetReading() fc = 0 for f in lyr: fc += 1 if fc != 1: gdaltest.post_reason('fail') return 'fail' lyr = None ds = None # Test appending new layer to file without terminating \n gdal.FileFromMemBuffer( out_filename, 'tbl; foo\natr; atr\nfrm; char[40]\nrec; "foo"\neof; 1') ds = ogr.Open(out_filename, update=1) lyr = ds.CreateLayer('new_layer') lyr.CreateField(ogr.FieldDefn('atr')) f = ogr.Feature(lyr.GetLayerDefn()) f['atr'] = 'bar' lyr.CreateFeature(f) f = None ds = None f = gdal.VSIFOpenL(out_filename, 'rb') got = gdal.VSIFReadL(1, 10000, f).decode('latin1') gdal.VSIFCloseL(f) if got != expected: gdaltest.post_reason('fail') print(got) return 'fail' gdal.Unlink(out_filename) return 'success'
def pds4_14(): filename = '/vsimem/test.xml' gdal.FileFromMemBuffer( filename, "Product_Observational http://pds.nasa.gov/pds4/pds/v1") with gdaltest.error_handler(): ds = gdal.Open(filename) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( filename, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1"> <File_Area_Observational/> <File_Area_Observational> <File/> </File_Area_Observational> <File_Area_Observational> <File> <file_name>i_do_not_exist.img</file_name> </File> <Array> <axes>3</axes> </Array> </File_Area_Observational> <File_Area_Observational> <File> <file_name>i_do_not_exist.img</file_name> </File> <Array> <axes>3</axes> <axis_index_order>Last Index Fastest</axis_index_order> </Array> </File_Area_Observational> <File_Area_Observational> <File> <file_name>i_do_not_exist.img</file_name> </File> <Array> <axes>3</axes> <axis_index_order>Last Index Fastest</axis_index_order> <Element_Array> <data_type>SignedByte</data_type> </Element_Array> <Axis_Array> </Axis_Array> <Axis_Array> <axis_name>x</axis_name> <elements>1</elements> <sequence_number>1</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Band</axis_name> <elements>0</elements> <sequence_number>1</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Band</axis_name> <elements>1</elements> <sequence_number>0</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Band</axis_name> <elements>1</elements> <sequence_number>4</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Band</axis_name> <elements>1</elements> <sequence_number>1</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Band</axis_name> <elements>1</elements> <sequence_number>1</sequence_number> </Axis_Array> </Array> </File_Area_Observational> </Product_Observational>""") with gdaltest.error_handler(): ds = gdal.Open(filename) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( filename, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1"> <File_Area_Observational> <File> <file_name>i_do_not_exist.img</file_name> </File> <Array_3D> <axes>3</axes> <axis_index_order>Last Index Fastest</axis_index_order> <Element_Array> <data_type>UnsignedByte</data_type> </Element_Array> <Axis_Array> <axis_name>Band</axis_name> <elements>65537</elements> <sequence_number>1</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Line</axis_name> <elements>1</elements> <sequence_number>2</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Sample</axis_name> <elements>1</elements> <sequence_number>3</sequence_number> </Axis_Array> </Array_3D> </File_Area_Observational> </Product_Observational>""") with gdaltest.error_handler(): ds = gdal.Open(filename) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( filename, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1"> <File_Area_Observational> <File> <file_name>i_do_not_exist.img</file_name> </File> <Array_2D> <axes>2</axes> <axis_index_order>Last Index Fastest</axis_index_order> <Element_Array> <data_type>SignedByte</data_type> </Element_Array> <Axis_Array> <axis_name>Line</axis_name> <elements>1</elements> <sequence_number>1</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Sample</axis_name> <elements>1</elements> <sequence_number>2</sequence_number> </Axis_Array> </Array_2D> </File_Area_Observational> </Product_Observational>""") with gdaltest.error_handler(): ds = gdal.Open(filename) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( filename, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1"> <File_Area_Observational> <File> <file_name>i_do_not_exist.img</file_name> </File> <Array_2D> <axes>2</axes> <axis_index_order>Last Index Fastest</axis_index_order> <Element_Array> <data_type>ComplexMSB16</data_type> </Element_Array> <Axis_Array> <axis_name>Line</axis_name> <elements>1</elements> <sequence_number>1</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Sample</axis_name> <elements>2000000000</elements> <sequence_number>2</sequence_number> </Axis_Array> </Array_2D> </File_Area_Observational> </Product_Observational>""") with gdaltest.error_handler(): ds = gdal.Open(filename) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( filename, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1"> <File_Area_Observational> <File> <file_name>i_do_not_exist.img</file_name> </File> <Array_2D> <axes>2</axes> <axis_index_order>Last Index Fastest</axis_index_order> <Element_Array> <data_type>ComplexMSB16</data_type> </Element_Array> <Axis_Array> <axis_name>Sample</axis_name> <elements>1</elements> <sequence_number>1</sequence_number> </Axis_Array> <Axis_Array> <axis_name>Line</axis_name> <elements>2000000000</elements> <sequence_number>2</sequence_number> </Axis_Array> </Array_2D> </File_Area_Observational> </Product_Observational>""") with gdaltest.error_handler(): ds = gdal.Open(filename) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink(filename) # Invalid value for INTERLEAVE with gdaltest.error_handler(): ds = gdal.GetDriverByName('PDS4').Create( '/vsimem/out.xml', 1, 1, options=['INTERLEAVE=INVALID']) if ds is not None: gdaltest.post_reason('fail') return 'fail' # INTERLEAVE=BIL not supported for GeoTIFF in PDS4 with gdaltest.error_handler(): ds = gdal.GetDriverByName('PDS4').Create( '/vsimem/out.xml', 1, 1, options=['INTERLEAVE=BIL', 'IMAGE_FORMAT=GEOTIFF']) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Cannot create GeoTIFF file with gdaltest.error_handler(): ds = gdal.GetDriverByName('PDS4').Create( '/i/do_not/exist.xml', 1, 1, options=['IMAGE_FORMAT=GEOTIFF']) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.Translate('/vsimem/test.tif', 'data/byte.tif') # Output file has same name as input file with gdaltest.error_handler(): ds = gdal.Translate('/vsimem/test.xml', '/vsimem/test.tif', format='PDS4', creationOptions=['IMAGE_FORMAT=GEOTIFF']) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/test.tif') template = '/vsimem/template.xml' # Missing Product_Observational root gdal.FileFromMemBuffer(template, """<foo/>""") ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, options=['TEMPLATE=' + template]) gdal.ErrorReset() with gdaltest.error_handler(): ds = None if gdal.GetLastErrorMsg( ) != 'Cannot find Product_Observational element in template': gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' # Missing Target_Identification gdal.FileFromMemBuffer( template, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd"> </Product_Observational>""") ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, options=['TEMPLATE=' + template]) sr = osr.SpatialReference() sr.ImportFromProj4('+proj=longlat +a=2439400 +b=2439400 +no_defs') ds.SetProjection(sr.ExportToWkt()) ds.SetGeoTransform([2, 1, 0, 49, 0, -2]) gdal.ErrorReset() with gdaltest.error_handler(): ds = None if gdal.GetLastErrorMsg( ) != 'Cannot find Target_Identification element in template': gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' # Missing Observation_Area gdal.FileFromMemBuffer( template, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd"> </Product_Observational>""") ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, options=['TEMPLATE=' + template]) gdal.ErrorReset() with gdaltest.error_handler(): ds = None if gdal.GetLastErrorMsg() != 'Cannot find Observation_Area in template': gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' # Unexpected content found after Observation_Area in template gdal.FileFromMemBuffer( template, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd"> <Observation_Area/> <!-- --> <foo/> </Product_Observational>""") ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, options=['TEMPLATE=' + template]) gdal.ErrorReset() with gdaltest.error_handler(): ds = None if gdal.GetLastErrorMsg( ) != 'Unexpected content found after Observation_Area in template': gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.Unlink(template) gdal.Unlink(filename) gdal.Unlink('/vsimem/test.img') return 'success'
def ogr_csw_vsimem_csw_output_schema_csw(): if gdaltest.csw_drv is None: return 'skip' ds = gdal.OpenEx('CSW:/vsimem/csw_endpoint', open_options=['OUTPUT_SCHEMA=CSW']) lyr = ds.GetLayer(0) gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" outputSchema="http://www.opengis.net/cat/csw/2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<invalid_xml """) lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" outputSchema="http://www.opengis.net/cat/csw/2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<csw:GetRecordsResponse/>""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" outputSchema="http://www.opengis.net/cat/csw/2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="1" numberOfRecordsReturned="1" recordSchema="http://www.isotc211.org/2005/gmd" elementSet="full"> <csw:Record> <!-- lots of missing stuff --> <ows:BoundingBox crs="urn:x-ogc:def:crs:EPSG:6.11:4326" dimensions="2"> <ows:LowerCorner>-90 -180</ows:LowerCorner> <ows:UpperCorner>90 180</ows:UpperCorner> </ows:BoundingBox> <!-- lots of missing stuff --> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) lyr.ResetReading() f = lyr.GetNextFeature() if f['raw_xml'].find('<csw:Record') != 0 or \ f['boundingbox'].ExportToWkt() != 'POLYGON ((-180 -90,-180 90,180 90,180 -90,-180 -90))': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' return 'success'
def vsigs_2(): if gdaltest.webserver_port == 0: return 'skip' # header file gdal.FileFromMemBuffer('/vsimem/my_headers.txt', 'foo: bar') def method(request): if 'foo' not in request.headers or request.headers['foo'] != 'bar': sys.stderr.write('Bad headers: %s\n' % str(request.headers)) request.send_response(403) request.send_header('Content-Length', 0) request.end_headers() return request.send_response(200) request.send_header('Content-type', 'text/plain') request.send_header('Content-Length', 1) request.end_headers() request.wfile.write("""Y""".encode('ascii')) handler = webserver.SequentialHandler() handler.add('GET', '/gs_fake_bucket_http_header_file/resource', custom_method=method) with webserver.install_http_handler(handler): with gdaltest.config_option('GDAL_HTTP_HEADER_FILE', '/vsimem/my_headers.txt'): f = open_for_read( '/vsigs/gs_fake_bucket_http_header_file/resource') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if len(data) != 1: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/my_headers.txt') gdal.SetConfigOption('GS_SECRET_ACCESS_KEY', 'GS_SECRET_ACCESS_KEY') gdal.SetConfigOption('GS_ACCESS_KEY_ID', 'GS_ACCESS_KEY_ID') gdal.SetConfigOption('CPL_GS_TIMESTAMP', 'my_timestamp') def method(request): request.protocol_version = 'HTTP/1.1' if 'Authorization' not in request.headers: sys.stderr.write('Bad headers: %s\n' % str(request.headers)) request.send_response(403) return expected_authorization = 'GOOG1 GS_ACCESS_KEY_ID:8tndu9//BfmN+Kg4AFLdUMZMBDQ=' if request.headers['Authorization'] != expected_authorization: sys.stderr.write("Bad Authorization: '%s'\n" % str(request.headers['Authorization'])) request.send_response(403) return request.send_response(200) request.send_header('Content-type', 'text/plain') request.send_header('Content-Length', 3) request.send_header('Connection', 'close') request.end_headers() request.wfile.write("""foo""".encode('ascii')) handler = webserver.SequentialHandler() handler.add('GET', '/gs_fake_bucket/resource', custom_method=method) with webserver.install_http_handler(handler): f = open_for_read('/vsigs/gs_fake_bucket/resource') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 4, f).decode('ascii') gdal.VSIFCloseL(f) if data != 'foo': gdaltest.post_reason('fail') print(data) return 'fail' handler = webserver.SequentialHandler() handler.add('GET', '/gs_fake_bucket/resource', custom_method=method) with webserver.install_http_handler(handler): f = open_for_read('/vsigs_streaming/gs_fake_bucket/resource') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 4, f).decode('ascii') gdal.VSIFCloseL(f) if data != 'foo': gdaltest.post_reason('fail') print(data) return 'fail' handler = webserver.SequentialHandler() handler.add('GET', '/gs_fake_bucket/resource2.bin', 206, {'Content-Range': 'bytes 0-0/1000000'}, 'x') with webserver.install_http_handler(handler): stat_res = gdal.VSIStatL('/vsigs/gs_fake_bucket/resource2.bin') if stat_res is None or stat_res.size != 1000000: gdaltest.post_reason('fail') if stat_res is not None: print(stat_res.size) else: print(stat_res) return 'fail' handler = webserver.SequentialHandler() handler.add('HEAD', '/gs_fake_bucket/resource2.bin', 200, {'Content-Length': 1000000}) with webserver.install_http_handler(handler): stat_res = gdal.VSIStatL( '/vsigs_streaming/gs_fake_bucket/resource2.bin') if stat_res is None or stat_res.size != 1000000: gdaltest.post_reason('fail') if stat_res is not None: print(stat_res.size) else: print(stat_res) return 'fail' return 'success'
def vsigs_read_credentials_oauth2_service_account(): if gdaltest.webserver_port == 0: return 'skip' gdal.SetConfigOption('GS_SECRET_ACCESS_KEY', '') gdal.SetConfigOption('GS_ACCESS_KEY_ID', '') # Generated with 'openssl genrsa -out rsa-openssl.pem 1024' and # 'openssl pkcs8 -nocrypt -in rsa-openssl.pem -inform PEM -topk8 -outform PEM -out rsa-openssl.pkcs8.pem' # DO NOT USE in production !!!! key = """-----BEGIN PRIVATE KEY----- MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOlwJQLLDG1HeLrk VNcFR5Qptto/rJE5emRuy0YmkVINT4uHb1be7OOo44C2Ev8QPVtNHHS2XwCY5gTm i2RfIBLv+VDMoVQPqqE0LHb0WeqGmM5V1tHbmVnIkCcKMn3HpK30grccuBc472LQ DVkkGqIiGu0qLAQ89JP/r0LWWySRAgMBAAECgYAWjsS00WRBByAOh1P/dz4kfidy TabiXbiLDf3MqJtwX2Lpa8wBjAc+NKrPXEjXpv0W3ou6Z4kkqKHJpXGg4GRb4N5I 2FA+7T1lA0FCXa7dT2jvgJLgpBepJu5b//tqFqORb4A4gMZw0CiPN3sUsWsSw5Hd DrRXwp6sarzG77kvZQJBAPgysAmmXIIp9j1hrFSkctk4GPkOzZ3bxKt2Nl4GFrb+ bpKSon6OIhP1edrxTz1SMD1k5FiAAVUrMDKSarbh5osCQQDwxq4Tvf/HiYz79JBg Wz5D51ySkbg01dOVgFW3eaYAdB6ta/o4vpHhnbrfl6VO9oUb3QR4hcrruwnDHsw3 4mDTAkEA9FPZjbZSTOSH/cbgAXbdhE4/7zWOXj7Q7UVyob52r+/p46osAk9i5qj5 Kvnv2lrFGDrwutpP9YqNaMtP9/aLnwJBALLWf9n+GAv3qRZD0zEe1KLPKD1dqvrj j+LNjd1Xp+tSVK7vMs4PDoAMDg+hrZF3HetSQM3cYpqxNFEPgRRJOy0CQQDQlZHI yzpSgEiyx8O3EK1iTidvnLXbtWabvjZFfIE/0OhfBmN225MtKG3YLV2HoUvpajLq gwE6fxOLyJDxuWRf -----END PRIVATE KEY----- """ for i in range(2): gdal.SetConfigOption( 'GO2A_AUD', 'http://localhost:%d/oauth2/v4/token' % gdaltest.webserver_port) gdal.SetConfigOption('GOA2_NOW', '123456') if i == 0: gdal.SetConfigOption('GS_OAUTH2_PRIVATE_KEY', key) else: gdal.FileFromMemBuffer('/vsimem/pkey', key) gdal.SetConfigOption('GS_OAUTH2_PRIVATE_KEY_FILE', '/vsimem/pkey') gdal.SetConfigOption('GS_OAUTH2_CLIENT_EMAIL', 'CLIENT_EMAIL') gdal.VSICurlClearCache() handler = webserver.SequentialHandler() def method(request): content = request.rfile.read(int( request.headers['Content-Length'])).decode('ascii') content_8080 = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiAiQ0xJRU5UX0VNQUlMIiwgInNjb3BlIjogImh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvZGV2c3RvcmFnZS5yZWFkX3dyaXRlIiwgImF1ZCI6ICJodHRwOi8vbG9jYWxob3N0OjgwODAvb2F1dGgyL3Y0L3Rva2VuIiwgImlhdCI6IDEyMzQ1NiwgImV4cCI6IDEyNzA1Nn0%3D.DAhqWtBgKpObxZ%2BGiXqwF%2Fa4SS%2FNWQRhLCI7DYZCuOTuf2w7dL8j4CdpiwwzQg1diIus7dyViRfzpsFmuZKAXwL%2B84iBoVVqnJJZ4TgwH49NdfMAnc4Rgm%2Bo2a2nEcMjX%2FbQ3jRY%2B9WNVl96hzULGvLrVeyego2f06wivqmvxHA%3D' content_8081 = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiAiQ0xJRU5UX0VNQUlMIiwgInNjb3BlIjogImh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvZGV2c3RvcmFnZS5yZWFkX3dyaXRlIiwgImF1ZCI6ICJodHRwOi8vbG9jYWxob3N0OjgwODEvb2F1dGgyL3Y0L3Rva2VuIiwgImlhdCI6IDEyMzQ1NiwgImV4cCI6IDEyNzA1Nn0%3D.0abOEg4%2FRApWTSeAs6YTHaNzdwOgZLm8DTMO2MKlOA%2Fiagyb4cBJxDpkD5gECPvi7qhkg7LsyFuj0a%2BK48Bsuj%2FgLHOU4MpB0dHwYnDO2UXzH%2FUPdgFCVak1P1V%2ByiDA%2B%2Ft4aDI5fD9qefKQiu3wsMDHzP71MNLzayrjqaqKKS4%3D' if content not in [content_8080, content_8081]: sys.stderr.write('Bad POST content: %s\n' % content) request.send_response(403) return request.send_response(200) request.send_header('Content-type', 'text/plain') content = """{ "access_token" : "ACCESS_TOKEN", "token_type" : "Bearer", "expires_in" : 3600, }""" request.send_header('Content-Length', len(content)) request.end_headers() request.wfile.write(content.encode('ascii')) handler.add('POST', '/oauth2/v4/token', custom_method=method) def method(request): if 'Authorization' not in request.headers: sys.stderr.write('Bad headers: %s\n' % str(request.headers)) request.send_response(403) return expected_authorization = 'Bearer ACCESS_TOKEN' if request.headers['Authorization'] != expected_authorization: sys.stderr.write("Bad Authorization: '%s'\n" % str(request.headers['Authorization'])) request.send_response(403) return request.send_response(200) request.send_header('Content-type', 'text/plain') request.send_header('Content-Length', 3) request.end_headers() request.wfile.write("""foo""".encode('ascii')) handler.add('GET', '/gs_fake_bucket/resource', custom_method=method) try: with webserver.install_http_handler(handler): f = open_for_read('/vsigs/gs_fake_bucket/resource') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 4, f).decode('ascii') gdal.VSIFCloseL(f) except: if gdal.GetLastErrorMsg().find( 'CPLRSASHA256Sign() not implemented') >= 0: return 'skip' finally: gdal.SetConfigOption('GO2A_AUD', None) gdal.SetConfigOption('GO2A_NOW', None) gdal.SetConfigOption('GS_OAUTH2_PRIVATE_KEY', '') gdal.SetConfigOption('GS_OAUTH2_PRIVATE_KEY_FILE', '') gdal.SetConfigOption('GS_OAUTH2_CLIENT_EMAIL', '') if data != 'foo': gdaltest.post_reason('fail') print(data) return 'fail' gdal.Unlink('/vsimem/pkey') return 'success'
def test_eeda_4(): if ogrtest.eeda_drv is None: pytest.skip() gdal.SetConfigOption('EEDA_BEARER', 'mybearer') gdal.SetConfigOption('EEDA_URL', '/vsimem/ee/') # User asset ID ("users/**"). ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/earthengine-legacy/assets/users/foo:listImages?pageSize=1' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({ 'images': [{ 'name': 'projects/earthengine-legacy/assets/users/foo/bar' }] })) assert ogr.Open('EEDA:users/foo').GetLayer(0) gdal.Unlink(ogrtest.eeda_drv_tmpfile) # Project asset ID ("projects/**"). ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/earthengine-legacy/assets/projects/foo:listImages?pageSize=1' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({ 'images': [{ 'name': 'projects/earthengine-legacy/assets/projects/foo/bar' }] })) ds = ogr.Open('EEDA:projects/foo') assert ds.GetLayer(0) gdal.Unlink(ogrtest.eeda_drv_tmpfile) ds = None # Multi-folder project asset ID ("projects/foo/bar/baz"). ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/earthengine-legacy/assets/projects/foo/bar/baz:listImages?pageSize=1' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({ 'images': [{ 'name': 'projects/earthengine-legacy/assets/projects/foo/bar/baz/qux' }] })) ds = ogr.Open('EEDA:projects/foo/bar/baz') assert ds.GetLayer(0) gdal.Unlink(ogrtest.eeda_drv_tmpfile) ds = None # Public-catalog asset ID (e.g. "LANDSAT"). ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/earthengine-public/assets/foo:listImages?pageSize=1' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({ 'images': [{ 'name': 'projects/earthengine-public/assets/foo/bar' }] })) ds = ogr.Open('EEDA:foo') assert ds.GetLayer(0) gdal.Unlink(ogrtest.eeda_drv_tmpfile) ds = None # Asset name ("projects/*/assets/**"). ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/foo/assets/bar:listImages?pageSize=1' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({'images': [{ 'name': 'projects/foo/assets/bar/baz' }]})) ds = ogr.Open('EEDA:projects/foo/assets/bar') assert ds.GetLayer(0) gdal.Unlink(ogrtest.eeda_drv_tmpfile) ds = None gdal.SetConfigOption('EEDA_BEARER', None) gdal.SetConfigOption('EEDA_URL', None)
def test_eeda_2(): if ogrtest.eeda_drv is None: pytest.skip() gdal.FileFromMemBuffer( '/vsimem/ee/projects/earthengine-public/assets/collection:listImages?pageSize=1', json.dumps({ 'images': [{ 'properties': { 'string_field': 'bar', 'int_field': 1, 'int64_field': 123456789012, 'double_field': 1.23 } }] })) # To please the unregistering of the persistent connection gdal.FileFromMemBuffer('/vsimem/ee/', '') gdal.SetConfigOption('EEDA_BEARER', 'mybearer') gdal.SetConfigOption('EEDA_URL', '/vsimem/ee/') ds = ogr.Open('EEDA:collection') gdal.SetConfigOption('EEDA_URL', None) lyr = ds.GetLayer(0) assert lyr.TestCapability(ogr.OLCStringsAsUTF8) == 1 assert lyr.TestCapability('foo') == 0 assert lyr.GetLayerDefn().GetFieldCount() == 8 + 7 + 4 assert lyr.GetExtent() == (-180.0, 180.0, -90.0, 90.0) assert lyr.GetFeatureCount() == -1 gdal.FileFromMemBuffer( '/vsimem/ee/projects/earthengine-public/assets/collection:listImages', json.dumps({ 'images': [{ 'name': 'projects/earthengine-public/assets/collection/first_feature', 'id': 'collection/first_feature', 'updateTime': '2017-01-04T12:34:56.789Z', 'startTime': '2017-01-02T12:34:56.789Z', 'endTime': '2017-01-03T12:34:56.789Z', 'sizeBytes': 1, 'geometry': { 'type': 'Polygon', 'coordinates': [[[2, 49], [2.1, 49], [2.1, 49.1], [2, 49.1], [2, 49]]] }, 'properties': { 'string_field': 'bar', 'int_field': 1, 'int64_field': 123456789012, 'double_field': 1.23, 'another_prop': 3 }, 'bands': [{ "id": "B1", "dataType": { "precision": "INT", "range": { "max": 255 } }, "grid": { "crsCode": "EPSG:32610", "affineTransform": { "translateX": 499980, "translateY": 4200000, "scaleX": 60, "scaleY": -60 }, "dimensions": { "width": 1830, "height": 1831 } } }] }, { 'name': 'projects/earthengine-public/assets/collection/second_feature' }], 'nextPageToken': 'myToken' })) f = lyr.GetNextFeature() if f.GetField('name') != 'projects/earthengine-public/assets/collection/first_feature' or \ f.GetField('id') != 'collection/first_feature' or \ f.GetField('gdal_dataset') != 'EEDAI:projects/earthengine-public/assets/collection/first_feature' or \ f.GetField('updateTime') != '2017/01/04 12:34:56.789+00' or \ f.GetField('startTime') != '2017/01/02 12:34:56.789+00' or \ f.GetField('endTime') != '2017/01/03 12:34:56.789+00' or \ f.GetField('sizeBytes') != 1 or \ f.GetField('band_count') != 1 or \ f.GetField('band_max_width') != 1830 or \ f.GetField('band_max_height') != 1831 or \ f.GetField('band_min_pixel_size') != 60 or \ f.GetField('band_upper_left_x') != 499980 or \ f.GetField('band_upper_left_y') != 4200000 or \ f.GetField('band_crs') != 'EPSG:32610' or \ f.GetField('string_field') != 'bar' or \ f.GetField('int_field') != 1 or \ f.GetField('int64_field') != 123456789012 or \ f.GetField('double_field') != 1.23 or \ f.GetField('other_properties') != '{ "another_prop": 3 }' or \ f.GetGeometryRef().ExportToWkt() != 'MULTIPOLYGON (((2 49,2.1 49.0,2.1 49.1,2.0 49.1,2 49)))': f.DumpReadable() pytest.fail() f = lyr.GetNextFeature() if f.GetField( 'name' ) != 'projects/earthengine-public/assets/collection/second_feature': f.DumpReadable() pytest.fail() gdal.FileFromMemBuffer( '/vsimem/ee/projects/earthengine-public/assets/collection:listImages?pageToken=myToken', json.dumps({ 'images': [{ 'name': 'projects/earthengine-public/assets/collection/third_feature' }] })) f = lyr.GetNextFeature() if f.GetField( 'name' ) != 'projects/earthengine-public/assets/collection/third_feature': f.DumpReadable() pytest.fail() f = lyr.GetNextFeature() assert f is None lyr.ResetReading() f = lyr.GetNextFeature() if f.GetField( 'name' ) != 'projects/earthengine-public/assets/collection/first_feature': f.DumpReadable() pytest.fail() lyr.SetAttributeFilter('EEDA:raw_filter') gdal.FileFromMemBuffer( '/vsimem/ee/projects/earthengine-public/assets/collection:listImages?filter=raw%5Ffilter', json.dumps({ 'images': [{ 'name': 'projects/earthengine-public/assets/collection/raw_filter' }] })) f = lyr.GetNextFeature() assert f.GetField( 'name') == 'projects/earthengine-public/assets/collection/raw_filter' lyr.SetAttributeFilter(None) lyr.SetAttributeFilter("startTime >= '1980-01-01T00:00:00Z' AND " + "startTime <= '2100-01-01T00:00:00Z' AND " + "string_field = 'bar' AND " + "int_field > 0 AND " + "int_field < 2 AND " + "int64_field >= 0 AND " + "int64_field <= 9999999999999 AND " + "double_field != 3.5 AND " + "string_field IN ('bar', 'baz') AND " + "NOT( int_field IN (0) OR double_field IN (3.5) )") ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/earthengine-public/assets/collection:listImages?region=%7B%20%22type%22%3A%20%22Polygon%22%2C%20%22coordinates%22%3A%20%5B%20%5B%20%5B%20%2D180%2E0%2C%20%2D90%2E0%20%5D%2C%20%5B%20%2D180%2E0%2C%2090%2E0%20%5D%2C%20%5B%20180%2E0%2C%2090%2E0%20%5D%2C%20%5B%20180%2E0%2C%20%2D90%2E0%20%5D%2C%20%5B%20%2D180%2E0%2C%20%2D90%2E0%20%5D%20%5D%20%5D%20%7D&filter=%28%28%28%28%28%28%28string%5Ffield%20%3D%20%22bar%22%20AND%20int%5Ffield%20%3E%200%29%20AND%20int%5Ffield%20%3C%202%29%20AND%20int64%5Ffield%20%3E%3D%200%29%20AND%20int64%5Ffield%20%3C%3D%209999999999999%29%20AND%20double%5Ffield%20%21%3D%203%2E5%29%20AND%20string%5Ffield%20%3D%20%22bar%22%20OR%20string%5Ffield%20%3D%20%22baz%22%29%20AND%20%28NOT%20%28int%5Ffield%20%3D%200%20OR%20double%5Ffield%20%3D%203%2E5%29%29%29&startTime=1980%2D01%2D01T00%3A00%3A00Z&endTime=2100%2D01%2D01T00%3A00%3A00Z' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({ 'images': [{ 'name': 'projects/earthengine-public/assets/collection/filtered_feature', 'updateTime': '2017-01-03T12:34:56.789Z', 'startTime': '2017-01-02T12:34:56.789Z', 'sizeBytes': 1, 'geometry': { 'type': 'Polygon', 'coordinates': [[[2, 49], [2.1, 49], [2.1, 49.1], [2, 49.1], [2, 49]]] }, 'properties': { 'string_field': 'bar', 'int_field': 1, 'int64_field': 123456789012, 'double_field': 1.23, 'another_prop': 3 } }, { 'name': 'projects/earthengine-public/assets/collection/second_feature' }] })) lyr.SetSpatialFilterRect(-180, -90, 180, 90) f = lyr.GetNextFeature() gdal.Unlink(ogrtest.eeda_drv_tmpfile) assert f.GetField( 'name' ) == 'projects/earthengine-public/assets/collection/filtered_feature' lyr.SetSpatialFilter(None) # Test time equality with second granularity lyr.SetAttributeFilter("startTime = '1980-01-01T00:00:00Z'") ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/earthengine-public/assets/collection:listImages?startTime=1980%2D01%2D01T00%3A00%3A00Z&endTime=1980%2D01%2D01T00%3A00%3A01Z' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({ 'images': [{ 'name': 'projects/earthengine-public/assets/collection/filtered_feature', 'startTime': '1980-01-01T00:00:00Z' }, { 'name': 'projects/earthengine-public/assets/collection/second_feature' }] })) f = lyr.GetNextFeature() gdal.Unlink(ogrtest.eeda_drv_tmpfile) assert f.GetField( 'name' ) == 'projects/earthengine-public/assets/collection/filtered_feature' # Test time equality with day granularity lyr.SetAttributeFilter("startTime = '1980-01-01'") ogrtest.eeda_drv_tmpfile = '/vsimem/ee/projects/earthengine-public/assets/collection:listImages?startTime=1980%2D01%2D01T00%3A00%3A00Z&endTime=1980%2D01%2D01T23%3A59%3A59Z' gdal.FileFromMemBuffer( ogrtest.eeda_drv_tmpfile, json.dumps({ 'images': [{ 'name': 'projects/earthengine-public/assets/collection/filtered_feature', 'startTime': '1980-01-01T12:00:00Z', }, { 'name': 'projects/earthengine-public/assets/collection/second_feature' }] })) f = lyr.GetNextFeature() gdal.Unlink(ogrtest.eeda_drv_tmpfile) assert f.GetField( 'name' ) == 'projects/earthengine-public/assets/collection/filtered_feature' ds = None gdal.SetConfigOption('EEDA_BEARER', None)
def load_hourly_climate_data(end_date, climate_data_provider): year = end_date.year start_date = date(year, 1, 1) num_days = (end_date - start_date).days + 1 if num_days > 240: num_days = 240 # Load raster from postgis into a virtual memory file vsipath = '/vsimem/from_postgis' table_name = climate_data_provider + '_' + str(year) for day in range(0, num_days): t00 = time.time() print('day: ' + str(day)) current_date = start_date + td(days=day) #get daily tmax tmax_table_name = climate_data_provider + '_tmax_' + str(year) curs = Six.conn.cursor() query = "SELECT ST_AsGDALRaster(ST_Union(rast), 'Gtiff') FROM %s WHERE rast_date = %s;" data = (AsIs(tmax_table_name), current_date.strftime("%Y-%m-%d")) curs.execute(query, data) gdal.FileFromMemBuffer(vsipath, bytes(curs.fetchone()[0])) ds = gdal.Open(vsipath) tmax_band = ds.GetRasterBand(1) tmax = tmax_band.ReadAsArray() # convert -9999 values to not a number so we don't have to worry about manipulating them tmax[tmax == -9999.0] = np.nan # convert to fahrenheit tmax *= 1.8 tmax += 32 if day == 0: Six.max_temps = np.zeros( (num_days, ds.RasterYSize, ds.RasterXSize)) Six.max_temps[day] = tmax curs.close() for hour in range(0, 24): curs = Six.conn.cursor() print('hour: ' + str(hour)) query = "SELECT ST_AsGDALRaster(ST_Union(rast), 'Gtiff') FROM %s WHERE rast_date = %s AND rast_hour = %s;" data = (AsIs(table_name), current_date.strftime("%Y-%m-%d"), hour) curs.execute(query, data) gdal.FileFromMemBuffer(vsipath, bytes(curs.fetchone()[0])) curs.close() # Read first band of raster with GDAL ds = gdal.Open(vsipath) band = ds.GetRasterBand(1) if day == 0 and hour == 0: Six.gdh = np.zeros( (num_days, ds.RasterYSize, ds.RasterXSize)) # get the x and y dimensions of the input rasters, they will be used later to create the output rasters Six.ydim = ds.GetGeoTransform()[ 5] # pixel height (note: can be negative) Six.xdim = ds.GetGeoTransform()[1] # pixel width hour_temps = band.ReadAsArray() # convert -9999 values to not a number so we don't have to worry about manipulating them hour_temps[hour_temps == -9999.0] = np.nan # convert to fahrenheit hour_temps *= 1.8 hour_temps += 32 # compute growing degree hours from the hourly temps hour_temps -= Six.base_temp hour_temps[hour_temps < 0] = 0 Six.gdh[day] += hour_temps t01 = time.time() print('time for day: ') print(t01 - t00) # reshape the array to be station lat, station long, day of year, temperature Six.gdh = np.swapaxes(Six.gdh, 1, 0) Six.gdh = np.swapaxes(Six.gdh, 2, 1) Six.max_temps = np.swapaxes(Six.max_temps, 1, 0) Six.max_temps = np.swapaxes(Six.max_temps, 2, 1) # Get geometry information from raster Six.geo_transform = ds.GetGeoTransform() # Get projection information from raster Six.projection = ds.GetProjection() # Get additional information about the raster Six.no_data_value = band.GetNoDataValue() # Close and clean up virtual memory file gdal.Unlink(vsipath)
def load_daily_climate_data(start_date, end_date, climate_data_provider, region): year = start_date.year num_days = (end_date - start_date).days + 1 if num_days > 240: num_days = 240 # Load raster from postgis into a virtual memory file vsipath = '/vsimem/from_postgis' if climate_data_provider == 'prism': table_name = "prism_tmin_" + str(year) elif region == 'alaska': table_name = "tmin_alaska_" + str(year) else: table_name = "tmin_" + str(year) for day in range(0, num_days): curs = Six.conn.cursor() current_date = start_date + td(days=day) # print('getting ' + table_name + ' ' + current_date.strftime("%Y-%m-%d")) query = "SELECT ST_AsGDALRaster(ST_Union(rast), 'Gtiff') FROM %s WHERE rast_date = %s;" data = (AsIs(table_name), current_date.strftime("%Y-%m-%d")) curs.execute(query, data) gdal.FileFromMemBuffer(vsipath, bytes(curs.fetchone()[0])) curs.close() # Read first band of raster with GDAL ds = gdal.Open(vsipath) band = ds.GetRasterBand(1) if day == 0: Six.min_temps = np.empty( (num_days, ds.RasterYSize, ds.RasterXSize)) # get the x and y dimensions of the input rasters, they will be used later to create the output rasters Six.xdim = Six.ydim = -ds.GetGeoTransform()[5] # Six.xdim = Six.ydim = ds.GetGeoTransform()[1] Six.min_temps[day] = band.ReadAsArray() # reshape the array to be station lat, station long, day of year, temperature Six.min_temps = np.swapaxes(Six.min_temps, 1, 0) Six.min_temps = np.swapaxes(Six.min_temps, 2, 1) if climate_data_provider == 'prism': table_name = "prism_tmax_" + str(year) elif region == 'alaska': table_name = "tmax_alaska_" + str(year) else: table_name = "tmax_" + str(year) for day in range(0, num_days): curs = Six.conn.cursor() current_date = start_date + td(days=day) query = "SELECT ST_AsGDALRaster(ST_Union(rast), 'Gtiff') FROM %s WHERE rast_date = %s;" data = (AsIs(table_name), current_date.strftime("%Y-%m-%d")) curs.execute(query, data) gdal.FileFromMemBuffer(vsipath, bytes(curs.fetchone()[0])) curs.close() # Read first band of raster with GDAL ds = gdal.Open(vsipath) band = ds.GetRasterBand(1) if day == 0: Six.max_temps = np.empty( (num_days, ds.RasterYSize, ds.RasterXSize)) Six.max_temps[day] = band.ReadAsArray() # Get geometry information from raster Six.geo_transform = ds.GetGeoTransform() # Get projection information from raster Six.projection = ds.GetProjection() # prj = ds.GetProjectionRef() # srs = osr.SpatialReference(prj.title()) # srid = srs.GetAuthorityCode('GEOGCS') # Get additional information about the raster Six.no_data_value = band.GetNoDataValue() # band_minimum = band.GetMinimum() # band_maximum = band.GetMaximum() # scale = band.GetScale() # band_unit_type = band.GetUnitType() # Close and clean up virtual memory file gdal.Unlink(vsipath) # reshape the array to be station lat, station long, day of year, temperature Six.max_temps = np.swapaxes(Six.max_temps, 1, 0) Six.max_temps = np.swapaxes(Six.max_temps, 2, 1) # convert -9999 values to not a number so we don't have to worry about manipulating them Six.max_temps[Six.max_temps == -9999.0] = np.nan Six.min_temps[Six.min_temps == -9999.0] = np.nan # convert to fahrenheit Six.max_temps *= 1.8 Six.max_temps += 32 Six.min_temps *= 1.8 Six.min_temps += 32
def mrf_versioned(): gdal.Unlink('/vsimem/out.mrf') gdal.Unlink('/vsimem/out.mrf.aux.xml') gdal.Unlink('/vsimem/out.idx') gdal.Unlink('/vsimem/out.ppg') gdal.Unlink('/vsimem/out.til') # Caching MRF gdal.Translate('/vsimem/out.mrf', 'data/byte.tif', format='MRF') gdal.FileFromMemBuffer( '/vsimem/out.mrf', """<MRF_META> <Raster versioned="on"> <Size x="20" y="20" c="1" /> <PageSize x="512" y="512" c="1" /> </Raster> <GeoTags> <BoundingBox minx="440720.00000000" miny="3750120.00000000" maxx="441920.00000000" maxy="3751320.00000000" /> <Projection>PROJCS["NAD27 / UTM zone 11N",GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.9786982138982,AUTHORITY["EPSG","7008"]],AUTHORITY["EPSG","6267"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4267"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","26711"]]</Projection> </GeoTags> </MRF_META>""") ds = gdal.Open('/vsimem/out.mrf', gdal.GA_Update) ds.GetRasterBand(1).Fill(0) ds = None ds = gdal.Open('/vsimem/out.mrf') cs = ds.GetRasterBand(1).Checksum() expected_cs = 0 if cs != expected_cs: gdaltest.post_reason('fail') print(cs) print(expected_cs) return 'fail' ds = None ds = gdal.Open('/vsimem/out.mrf:MRF:V0') cs = ds.GetRasterBand(1).Checksum() expected_cs = 0 if cs != expected_cs: gdaltest.post_reason('fail') print(cs) print(expected_cs) return 'fail' ds = None ds = gdal.Open('/vsimem/out.mrf:MRF:V1') cs = ds.GetRasterBand(1).Checksum() expected_cs = 4672 if cs != expected_cs: gdaltest.post_reason('fail') print(cs) print(expected_cs) return 'fail' ds = None with gdaltest.error_handler(): ds = gdal.Open('/vsimem/out.mrf:MRF:V2') if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/out.mrf') gdal.Unlink('/vsimem/out.mrf.aux.xml') gdal.Unlink('/vsimem/out.idx') gdal.Unlink('/vsimem/out.ppg') gdal.Unlink('/vsimem/out.til') return 'success'
def pds4_16(): template = '/vsimem/template.xml' filename = '/vsimem/out.xml' gdal.FileFromMemBuffer( template, """<Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd"> <Identification_Area> <logical_identifier>${LOGICAL_IDENTIFIER}</logical_identifier> <version_id>1.0</version_id> <title>${TITLE}</title> <information_model_version>1.8.0.0</information_model_version> <product_class>Product_Observational</product_class> </Identification_Area> <Observation_Area> <Time_Coordinates> <start_date_time xsi:nil="true" /> <stop_date_time xsi:nil="true" /> </Time_Coordinates> <Investigation_Area> <name>${INVESTIGATION_AREA_NAME}</name> <type>Mission</type> <Internal_Reference> <lid_reference>${INVESTIGATION_AREA_LID_REFERENCE}</lid_reference> <reference_type>data_to_investigation</reference_type> </Internal_Reference> </Investigation_Area> <Observing_System> <Observing_System_Component> <name>${OBSERVING_SYSTEM_NAME}</name> <type>Spacecraft</type> </Observing_System_Component> </Observing_System> <Target_Identification> <name>Earth</name> <type>Planet</type> <Internal_Reference> <lid_reference>urn:nasa:pds:context:target:planet.earth</lid_reference> <reference_type>data_to_target</reference_type> </Internal_Reference> </Target_Identification> </Observation_Area> <File_Area_Observational> </File_Area_Observational> </Product_Observational>""") ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, options=['TEMPLATE=' + template]) sr = osr.SpatialReference() sr.ImportFromProj4('+proj=longlat +a=2439400 +b=2439400 +no_defs') ds.SetProjection(sr.ExportToWkt()) ds.SetGeoTransform([2, 1, 0, 49, 0, -2]) with hide_substitution_warnings_error_handler(): ds = None ret = validate_xml(filename) if ret == 'fail': gdaltest.post_reason('validation failed') return 'fail' f = gdal.VSIFOpenL(filename, 'rb') if f: data = gdal.VSIFReadL(1, 10000, f).decode('ascii') gdal.VSIFCloseL(f) if data.find( 'http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd http://pds.nasa.gov/pds4/cart/v1 https://pds.nasa.gov/pds4/cart/v1/PDS4_CART_1700.xsd"' ) < 0: gdaltest.post_reason('fail') print(data) return 'fail' if data.find('xmlns:cart="http://pds.nasa.gov/pds4/cart/v1"') < 0: gdaltest.post_reason('fail') print(data) return 'fail' if data.find('<cart:Cartography>') < 0: gdaltest.post_reason('fail') print(data) return 'fail' gdal.GetDriverByName('PDS4').Delete(filename) gdal.Unlink(template) return 'success'
def vsigs_read_credentials_file(): if gdaltest.webserver_port == 0: return 'skip' gdal.SetConfigOption('GS_SECRET_ACCESS_KEY', '') gdal.SetConfigOption('GS_ACCESS_KEY_ID', '') gdal.SetConfigOption('CPL_GS_CREDENTIALS_FILE', '/vsimem/.boto') gdal.VSICurlClearCache() gdal.FileFromMemBuffer( '/vsimem/.boto', """ [unrelated] gs_access_key_id = foo gs_secret_access_key = bar [Credentials] gs_access_key_id = GS_ACCESS_KEY_ID gs_secret_access_key = GS_SECRET_ACCESS_KEY [unrelated] gs_access_key_id = foo gs_secret_access_key = bar """) def method(request): if 'Authorization' not in request.headers: sys.stderr.write('Bad headers: %s\n' % str(request.headers)) request.send_response(403) return expected_authorization = 'GOOG1 GS_ACCESS_KEY_ID:8tndu9//BfmN+Kg4AFLdUMZMBDQ=' if request.headers['Authorization'] != expected_authorization: sys.stderr.write("Bad Authorization: '%s'\n" % str(request.headers['Authorization'])) request.send_response(403) return request.send_response(200) request.send_header('Content-type', 'text/plain') request.send_header('Content-Length', 3) request.end_headers() request.wfile.write("""foo""".encode('ascii')) handler = webserver.SequentialHandler() handler.add('GET', '/gs_fake_bucket/resource', custom_method=method) with webserver.install_http_handler(handler): f = open_for_read('/vsigs/gs_fake_bucket/resource') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 4, f).decode('ascii') gdal.VSIFCloseL(f) if data != 'foo': gdaltest.post_reason('fail') print(data) return 'fail' gdal.SetConfigOption('CPL_GS_CREDENTIALS_FILE', '') gdal.Unlink('/vsimem/.boto') return 'success'
def pds4_9(): ds = gdal.Open('data/byte_pds4.xml') ndv = ds.GetRasterBand(1).GetNoDataValue() if ndv != 74: gdaltest.post_reason('fail') print(ndv) return 'fail' cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 4800: gdaltest.post_reason('fail') print(cs) return 'fail' ds = None filename = '/vsimem/out.xml' # Test copy of all specialConstants with hide_substitution_warnings_error_handler(): gdal.Translate(filename, 'data/byte_pds4.xml', format='PDS4') ret = validate_xml(filename) if ret == 'fail': gdaltest.post_reason('validation failed') return 'fail' ds = gdal.Open(filename) ndv = ds.GetRasterBand(1).GetNoDataValue() if ndv != 74: gdaltest.post_reason('fail') print(ndv) return 'fail' cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 4800: gdaltest.post_reason('fail') print(cs) return 'fail' ds = None filename = '/vsimem/out.xml' # Test copy of all specialConstants and override noData for frmt in ['RAW', 'GEOTIFF']: with hide_substitution_warnings_error_handler(): gdal.Translate(filename, 'data/byte_pds4.xml', format='PDS4', noData=75, creationOptions=['IMAGE_FORMAT=' + frmt]) ret = validate_xml(filename) if ret == 'fail': gdaltest.post_reason('validation failed') return 'fail' ds = gdal.Open(filename) ndv = ds.GetRasterBand(1).GetNoDataValue() if ndv != 75: gdaltest.post_reason('fail') print(ndv) return 'fail' flag = ds.GetRasterBand(1).GetMaskFlags() if flag != 0: gdaltest.post_reason('fail') print(flag) return 'fail' cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 4833: gdaltest.post_reason('fail') print(cs) return 'fail' ds = None # Test just setting noData for frmt in ['RAW', 'GEOTIFF']: with hide_substitution_warnings_error_handler(): gdal.Translate( filename, 'data/byte_pds4.xml', format='PDS4', creationOptions=['USE_SRC_LABEL=NO', 'IMAGE_FORMAT=' + frmt]) ret = validate_xml(filename) if ret == 'fail': gdaltest.post_reason('validation failed') return 'fail' ds = gdal.Open(filename) ndv = ds.GetRasterBand(1).GetNoDataValue() if ndv != 74: gdaltest.post_reason('fail') print(frmt) print(ndv) return 'fail' ds = None # Test filling with nodata ds = gdal.GetDriverByName('PDS4').Create( filename, 1, 1, options=['IMAGE_FORMAT=' + frmt]) ds.GetRasterBand(1).SetNoDataValue(1) with hide_substitution_warnings_error_handler(): ds = None ds = gdal.Open(filename) cs = ds.GetRasterBand(1).Checksum() if cs != 1: gdaltest.post_reason('fail') print(frmt) print(cs) return 'fail' ds = None # Test setting nodata and then explicit Fill() ds = gdal.GetDriverByName('PDS4').Create( filename, 1, 1, options=['IMAGE_FORMAT=' + frmt]) ds.GetRasterBand(1).SetNoDataValue(10) ds.GetRasterBand(1).Fill(1) with hide_substitution_warnings_error_handler(): ds = None ds = gdal.Open(filename) cs = ds.GetRasterBand(1).Checksum() if cs != 1: gdaltest.post_reason('fail') print(frmt) print(cs) return 'fail' ds = None template = '/vsimem/template.xml' # Empty Special_Constants gdal.FileFromMemBuffer( template, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd"> <Identification_Area> <logical_identifier>${LOGICAL_IDENTIFIER}</logical_identifier> <version_id>1.0</version_id> <title>${TITLE}</title> <information_model_version>1.8.0.0</information_model_version> <product_class>Product_Observational</product_class> </Identification_Area> <Observation_Area> <Time_Coordinates> <start_date_time xsi:nil="true" /> <stop_date_time xsi:nil="true" /> </Time_Coordinates> <Investigation_Area> <name>${INVESTIGATION_AREA_NAME}</name> <type>Mission</type> <Internal_Reference> <lid_reference>${INVESTIGATION_AREA_LID_REFERENCE}</lid_reference> <reference_type>data_to_investigation</reference_type> </Internal_Reference> </Investigation_Area> <Observing_System> <Observing_System_Component> <name>${OBSERVING_SYSTEM_NAME}</name> <type>Spacecraft</type> </Observing_System_Component> </Observing_System> <Target_Identification> <name>Earth</name> <type>Planet</type> <Internal_Reference> <lid_reference>urn:nasa:pds:context:target:planet.earth</lid_reference> <reference_type>data_to_target</reference_type> </Internal_Reference> </Target_Identification> </Observation_Area> <File_Area_Observational> <Array_2D> <Special_Constants /> </Array_2D> </File_Area_Observational> </Product_Observational>""") ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, options=['TEMPLATE=' + template]) ds.GetRasterBand(1).SetNoDataValue(10) with hide_substitution_warnings_error_handler(): ds = None ds = gdal.Open(filename) ndv = ds.GetRasterBand(1).GetNoDataValue() if ndv != 10: gdaltest.post_reason('fail') print(ndv) return 'fail' ds = None ret = validate_xml(filename) if ret == 'fail': gdaltest.post_reason('validation failed') return 'fail' # Special_Constants with just saturated_constant gdal.FileFromMemBuffer( template, """ <Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd"> <Identification_Area> <logical_identifier>${LOGICAL_IDENTIFIER}</logical_identifier> <version_id>1.0</version_id> <title>${TITLE}</title> <information_model_version>1.8.0.0</information_model_version> <product_class>Product_Observational</product_class> </Identification_Area> <Observation_Area> <Time_Coordinates> <start_date_time xsi:nil="true" /> <stop_date_time xsi:nil="true" /> </Time_Coordinates> <Investigation_Area> <name>${INVESTIGATION_AREA_NAME}</name> <type>Mission</type> <Internal_Reference> <lid_reference>${INVESTIGATION_AREA_LID_REFERENCE}</lid_reference> <reference_type>data_to_investigation</reference_type> </Internal_Reference> </Investigation_Area> <Observing_System> <Observing_System_Component> <name>${OBSERVING_SYSTEM_NAME}</name> <type>Spacecraft</type> </Observing_System_Component> </Observing_System> <Target_Identification> <name>Earth</name> <type>Planet</type> <Internal_Reference> <lid_reference>urn:nasa:pds:context:target:planet.earth</lid_reference> <reference_type>data_to_target</reference_type> </Internal_Reference> </Target_Identification> </Observation_Area> <File_Area_Observational> <Array_2D> <Special_Constants> <saturated_constant>255</saturated_constant> </Special_Constants> </Array_2D> </File_Area_Observational> </Product_Observational>""") ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, options=['TEMPLATE=' + template]) ds.GetRasterBand(1).SetNoDataValue(10) with hide_substitution_warnings_error_handler(): ds = None ds = gdal.Open(filename) ndv = ds.GetRasterBand(1).GetNoDataValue() if ndv != 10: gdaltest.post_reason('fail') print(ndv) return 'fail' ds = None ret = validate_xml(filename) if ret == 'fail': gdaltest.post_reason('validation failed') return 'fail' gdal.GetDriverByName('PDS4').Delete(filename) gdal.Unlink(template) return 'success'
def vsigs_read_credentials_file_refresh_token(): if gdaltest.webserver_port == 0: return 'skip' gdal.SetConfigOption('GS_SECRET_ACCESS_KEY', '') gdal.SetConfigOption('GS_ACCESS_KEY_ID', '') gdal.SetConfigOption('CPL_GS_CREDENTIALS_FILE', '/vsimem/.boto') gdal.SetConfigOption( 'GOA2_AUTH_URL_TOKEN', 'http://localhost:%d/accounts.google.com/o/oauth2/token' % gdaltest.webserver_port) gdal.VSICurlClearCache() gdal.FileFromMemBuffer( '/vsimem/.boto', """ [Credentials] gs_oauth2_refresh_token = REFRESH_TOKEN [OAuth2] client_id = CLIENT_ID client_secret = CLIENT_SECRET """) handler = webserver.SequentialHandler() def method(request): content = request.rfile.read(int( request.headers['Content-Length'])).decode('ascii') if content != 'refresh_token=REFRESH_TOKEN&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=refresh_token': sys.stderr.write('Bad POST content: %s\n' % content) request.send_response(403) return request.send_response(200) request.send_header('Content-type', 'text/plain') content = """{ "access_token" : "ACCESS_TOKEN", "token_type" : "Bearer", "expires_in" : 3600, }""" request.send_header('Content-Length', len(content)) request.end_headers() request.wfile.write(content.encode('ascii')) handler.add('POST', '/accounts.google.com/o/oauth2/token', custom_method=method) def method(request): if 'Authorization' not in request.headers: sys.stderr.write('Bad headers: %s\n' % str(request.headers)) request.send_response(403) return expected_authorization = 'Bearer ACCESS_TOKEN' if request.headers['Authorization'] != expected_authorization: sys.stderr.write("Bad Authorization: '%s'\n" % str(request.headers['Authorization'])) request.send_response(403) return request.send_response(200) request.send_header('Content-type', 'text/plain') request.send_header('Content-Length', 3) request.end_headers() request.wfile.write("""foo""".encode('ascii')) handler.add('GET', '/gs_fake_bucket/resource', custom_method=method) with webserver.install_http_handler(handler): f = open_for_read('/vsigs/gs_fake_bucket/resource') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 4, f).decode('ascii') gdal.VSIFCloseL(f) if data != 'foo': gdaltest.post_reason('fail') print(data) return 'fail' gdal.SetConfigOption('CPL_GS_CREDENTIALS_FILE', '') gdal.SetConfigOption('GOA2_AUTH_URL_TOKEN', None) gdal.Unlink('/vsimem/.boto') return 'success'
def ogr_csw_vsimem_csw_minimal_instance(): if gdaltest.csw_drv is None: return 'skip' gdal.SetConfigOption('CPL_CURL_ENABLE_VSIMEM', 'YES') # Invalid response, but enough for use gdal.FileFromMemBuffer('/vsimem/csw_endpoint?SERVICE=CSW&REQUEST=GetCapabilities', """ <Capabilities version="2.0.2"/> """) ds = ogr.Open('CSW:/vsimem/csw_endpoint') if ds is None: gdaltest.post_reason('fail') return 'fail' ds.TestCapability('foo') if ds.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' if ds.GetLayer(-1) is not None or ds.GetLayer(1) is not None: gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayer(0) lyr.TestCapability('foo') gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find('Empty content returned by server') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<invalid_xml""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find('Error: cannot parse') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<dummy_xml/>""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find('Error: cannot parse') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<ServiceExceptionReport/>""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find('Error returned by server') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="3" numberOfRecordsMatched="2" numberOfRecordsReturned="3" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> <csw:Record> <dc:identifier>an_identifier</dc:identifier> <dc:identifier>another_identifier</dc:identifier> <dc:title>a_title</dc:title> <dc:type>dataset</dc:type> <dc:subject>a_subject</dc:subject> <dc:subject>another_subject</dc:subject> <dct:references scheme="None">http://foo/</dct:references> <dct:references scheme="None">http://bar/</dct:references> <dct:modified>2015-04-27</dct:modified> <dct:abstract>an_abstract</dct:abstract> <dc:date>2015-04-27</dc:date> <dc:language>eng</dc:language> <dc:format>a_format</dc:format> <dc:format>another_format</dc:format> <ows:BoundingBox crs="urn:x-ogc:def:crs:EPSG:6.11:4326" dimensions="2"> <ows:LowerCorner>-90 -180</ows:LowerCorner> <ows:UpperCorner>90 180</ows:UpperCorner> </ows:BoundingBox> </csw:Record> <csw:Record> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) lyr.ResetReading() f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' if f['identifier'] != 'an_identifier' or f['other_identifiers'] != ['another_identifier'] or \ f['subject'] != 'a_subject' or f['other_subjects'] != ['another_subject'] or \ f['references'] != 'http://foo/' or f['other_references'] != ['http://bar/'] or \ f['format'] != 'a_format' or f['other_formats'] != ['another_format'] or \ f['boundingbox'].ExportToWkt() != 'POLYGON ((-180 -90,-180 90,180 90,180 -90,-180 -90))': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 2: gdaltest.post_reason('fail') return 'fail' lyr.ResetReading() f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="3" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="3" numberOfRecordsReturned="1" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> <csw:Record> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="4" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="3" numberOfRecordsReturned="0" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> </csw:SearchResults> </csw:GetRecordsResponse> """) f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<dummy_xml/>""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<invalid_xml>""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<ServiceExceptionReport/>""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="200" numberOfRecordsReturned="0" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> </csw:SearchResults> </csw:GetRecordsResponse> """) fc = lyr.GetFeatureCount() if fc != 200: gdaltest.post_reason('fail') return 'fail' lyr.SetAttributeFilter("identifier = 'an_identifier'") lyr.SetSpatialFilterRect(-180, -90, 180, 90) gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName><csw:Constraint version="1.1.0"><ogc:Filter><ogc:And><ogc:BBOX><ogc:PropertyName>ows:BoundingBox</ogc:PropertyName><gml:Envelope srsName="urn:ogc:def:crs:EPSG::4326"><gml:lowerCorner>-90 -180</gml:lowerCorner><gml:upperCorner>90 180</gml:upperCorner></gml:Envelope></ogc:BBOX><ogc:PropertyIsEqualTo><ogc:PropertyName>dc:identifier</ogc:PropertyName><ogc:Literal>an_identifier</ogc:Literal></ogc:PropertyIsEqualTo></ogc:And></ogc:Filter></csw:Constraint></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="3" numberOfRecordsReturned="1" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> <csw:Record> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName><csw:Constraint version="1.1.0"><ogc:Filter><ogc:And><ogc:BBOX><ogc:PropertyName>ows:BoundingBox</ogc:PropertyName><gml:Envelope srsName="urn:ogc:def:crs:EPSG::4326"><gml:lowerCorner>-90 -180</gml:lowerCorner><gml:upperCorner>90 180</gml:upperCorner></gml:Envelope></ogc:BBOX><ogc:PropertyIsEqualTo><ogc:PropertyName>dc:identifier</ogc:PropertyName><ogc:Literal>an_identifier</ogc:Literal></ogc:PropertyIsEqualTo></ogc:And></ogc:Filter></csw:Constraint></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="300" numberOfRecordsReturned="0" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> </csw:SearchResults> </csw:GetRecordsResponse> """) fc = lyr.GetFeatureCount() if fc != 300: gdaltest.post_reason('fail') return 'fail' lyr.SetAttributeFilter("identifier = 'an_identifier' AND " + "references = 'http://foo/' AND " + "anytext LIKE '%%foo%%' AND " + "other_identifiers = '' AND " + "other_subjects = '' AND " + "other_formats = '' AND " + "other_references = '' AND " + "ST_Intersects(boundingbox, ST_MakeEnvelope(2,49,2,49,4326))") lyr.SetAttributeFilter(None) lyr.SetSpatialFilter(None) return 'success'
def test_kmlsuperoverlay_4(): vrt_xml = """<VRTDataset rasterXSize="800" rasterYSize="400"> <SRS>GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]</SRS> <GeoTransform> -1.8000000000000000e+02, 4.5000000000000001e-01, 0.0000000000000000e+00, 9.0000000000000000e+01, 0.0000000000000000e+00, -4.5000000000000001e-01</GeoTransform> <Metadata> <MDI key="AREA_OR_POINT">Area</MDI> </Metadata> <Metadata domain="IMAGE_STRUCTURE"> <MDI key="INTERLEAVE">BAND</MDI> </Metadata> <VRTRasterBand dataType="Byte" band="1"> <Metadata /> <ColorInterp>Red</ColorInterp> <SimpleSource> <SourceFilename relativeToVRT="0">data/small_world.tif</SourceFilename> <SourceBand>1</SourceBand> <SourceProperties RasterXSize="400" RasterYSize="200" DataType="Byte" BlockXSize="400" BlockYSize="20" /> <SrcRect xOff="0" yOff="0" xSize="400" ySize="200" /> <DstRect xOff="0" yOff="0" xSize="800" ySize="400" /> </SimpleSource> </VRTRasterBand> <VRTRasterBand dataType="Byte" band="2"> <Metadata /> <ColorInterp>Green</ColorInterp> <SimpleSource> <SourceFilename relativeToVRT="0">data/small_world.tif</SourceFilename> <SourceBand>2</SourceBand> <SourceProperties RasterXSize="400" RasterYSize="200" DataType="Byte" BlockXSize="400" BlockYSize="20" /> <SrcRect xOff="0" yOff="0" xSize="400" ySize="200" /> <DstRect xOff="0" yOff="0" xSize="800" ySize="400" /> </SimpleSource> </VRTRasterBand> <VRTRasterBand dataType="Byte" band="3"> <Metadata /> <ColorInterp>Blue</ColorInterp> <SimpleSource> <SourceFilename relativeToVRT="0">data/small_world.tif</SourceFilename> <SourceBand>3</SourceBand> <SourceProperties RasterXSize="400" RasterYSize="200" DataType="Byte" BlockXSize="400" BlockYSize="20" /> <SrcRect xOff="0" yOff="0" xSize="400" ySize="200" /> <DstRect xOff="0" yOff="0" xSize="800" ySize="400" /> </SimpleSource> </VRTRasterBand> </VRTDataset>""" gdal.FileFromMemBuffer("/vsimem/src.vrt", vrt_xml) src_ds = gdal.Open("/vsimem/src.vrt") ds = gdal.GetDriverByName('KMLSUPEROVERLAY').CreateCopy( '/vsimem/kmlsuperoverlay_4.kmz', src_ds, options=[ 'FORMAT=PNG', 'NAME=myname', 'DESCRIPTION=mydescription', 'ALTITUDE=10', 'ALTITUDEMODE=absolute' ]) assert ds.GetMetadataItem('NAME') == 'myname' assert ds.GetMetadataItem('DESCRIPTION') == 'mydescription' if ds.GetRasterBand(1).GetOverviewCount() != 1: ds = None src_ds = None gdal.Unlink("/vsimem/src.vrt") gdal.Unlink("/vsimem/kmlsuperoverlay_4.kmz") pytest.fail() if ds.GetRasterBand(1).GetOverview(0).Checksum() != 30111: ds = None src_ds = None gdal.Unlink("/vsimem/src.vrt") gdal.Unlink("/vsimem/kmlsuperoverlay_4.kmz") pytest.fail() if ds.GetRasterBand(1).Checksum() != src_ds.GetRasterBand(1).Checksum(): ds = None src_ds = None gdal.Unlink("/vsimem/src.vrt") gdal.Unlink("/vsimem/kmlsuperoverlay_4.kmz") pytest.fail() # Test fix for #6311 vrt_ds = gdal.GetDriverByName('VRT').CreateCopy('', ds) got_data = vrt_ds.ReadRaster(0, 0, 800, 400, 200, 100) ref_data = ds.ReadRaster(0, 0, 800, 400, 200, 100) vrt_ds = None if got_data != ref_data: ds = None src_ds = None gdal.Unlink("/vsimem/src.vrt") gdal.Unlink("/vsimem/kmlsuperoverlay_4.kmz") pytest.fail() ds = None src_ds = None gdal.Unlink("/vsimem/src.vrt") gdal.Unlink("/vsimem/kmlsuperoverlay_4.kmz")
def vrt_read_10(): src_ds = gdal.Open('data/byte.tif') mem_ds = gdal.GetDriverByName('GTiff').CreateCopy('/vsimem/vrt_read_10.tif', src_ds) vrt_ds = gdal.GetDriverByName('VRT').CreateCopy('/vsimem/vrt_read_10.vrt', mem_ds) vrt_hist = vrt_ds.GetRasterBand(1).GetHistogram() mem_hist = mem_ds.GetRasterBand(1).GetHistogram() mem_ds = None vrt_ds = None f = gdal.VSIFOpenL('/vsimem/vrt_read_10.vrt', 'rb') content = gdal.VSIFReadL(1, 10000, f).decode('ascii') gdal.VSIFCloseL(f) if vrt_hist != mem_hist: gdaltest.post_reason('fail') print(vrt_hist) print(mem_hist) return 'fail' if content.find('<Histograms>') < 0: gdaltest.post_reason('fail') print(content) return 'fail' # Single source optimization for i in range(2): gdal.FileFromMemBuffer('/vsimem/vrt_read_10.vrt', """<VRTDataset rasterXSize="20" rasterYSize="20"> <VRTRasterBand dataType="Byte" band="1"> <SimpleSource> <SourceFilename relativeToVRT="1">vrt_read_10.tif</SourceFilename> </SimpleSource> </VRTRasterBand> </VRTDataset>""") ds = gdal.Open('/vsimem/vrt_read_10.vrt') if i == 0: ds.GetRasterBand(1).GetDefaultHistogram() else: ds.GetRasterBand(1).GetHistogram() ds = None f = gdal.VSIFOpenL('/vsimem/vrt_read_10.vrt', 'rb') content = gdal.VSIFReadL(1, 10000, f).decode('ascii') gdal.VSIFCloseL(f) if content.find('<Histograms>') < 0: gdaltest.post_reason('fail') print(content) return 'fail' # Two sources general case for i in range(2): gdal.FileFromMemBuffer('/vsimem/vrt_read_10.vrt', """<VRTDataset rasterXSize="20" rasterYSize="20"> <VRTRasterBand dataType="Byte" band="1"> <SimpleSource> <SourceFilename relativeToVRT="1">vrt_read_10.tif</SourceFilename> </SimpleSource> <SimpleSource> <SourceFilename relativeToVRT="1">vrt_read_10.tif</SourceFilename> </SimpleSource> </VRTRasterBand> </VRTDataset>""") ds = gdal.Open('/vsimem/vrt_read_10.vrt') if i == 0: ds.GetRasterBand(1).GetDefaultHistogram() else: ds.GetRasterBand(1).GetHistogram() ds = None f = gdal.VSIFOpenL('/vsimem/vrt_read_10.vrt', 'rb') content = gdal.VSIFReadL(1, 10000, f).decode('ascii') gdal.VSIFCloseL(f) if content.find('<Histograms>') < 0: gdaltest.post_reason('fail') print(content) return 'fail' gdal.GetDriverByName('GTiff').Delete('/vsimem/vrt_read_10.tif') gdal.GetDriverByName('VRT').Delete('/vsimem/vrt_read_10.vrt') return 'success'
def ogr_plscenes_3(): if gdaltest.plscenes_drv is None: return 'skip' gdal.FileFromMemBuffer('/vsimem/root', '{"ortho":"/vsimem/root/ortho/"}') # Error case: missing scene gdal.SetConfigOption('PL_URL', '/vsimem/root/') gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=not_existing_scene']) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Error case: invalid scene JSon gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id', """{""") gdal.SetConfigOption('PL_URL', '/vsimem/root/') gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=my_id']) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Error case: missing properties. gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id', """{}""") gdal.SetConfigOption('PL_URL', '/vsimem/root/') gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=my_id']) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id', """{ "type": "Feature", "id": "my_id", "geometry": { "coordinates": [ [ [2,49],[2,50],[3,50],[3,49],[2,49] ] ], "type": "Polygon" }, "properties": { "acquired" : "2015-03-27T12:34:56.123+00", "camera" : { "bit_depth" : 12, "color_mode": "RGB" }, "cloud_cover" : { "estimated" : 0.25 } } }""") # Error case: missing links gdal.SetConfigOption('PL_URL', '/vsimem/root/') gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=my_id']) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id', """{ "type": "Feature", "id": "my_id", "geometry": { "coordinates": [ [ [2,49],[2,50],[3,50],[3,49],[2,49] ] ], "type": "Polygon" }, "properties": { "acquired" : "2015-03-27T12:34:56.123+00", "camera" : { "bit_depth" : 12, "color_mode": "RGB" }, "cloud_cover" : { "estimated" : 0.25 }, "data": { "products": { "visual": { "full": "/vsimem/root/ortho/my_id/full?product=visual" }, "analytic": { "full": "/vsimem/root/ortho/my_id/full?product=analytic" } } }, "links": { "thumbnail": "/vsimem/root/ortho/my_id/thumb" } } }""") # Error case: raster file not accessible gdal.SetConfigOption('PL_URL', '/vsimem/root/') gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=my_id']) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Now everything ok gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id/full?product=visual', open('../gcore/data/byte.tif', 'rb').read()) gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id/full?product=analytic', open('../gcore/data/byte.tif', 'rb').read()) gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id/thumb', open('../gcore/data/byte.tif', 'rb').read()) gdal.SetConfigOption('PL_URL', '/vsimem/root/') gdal.PushErrorHandler() ds = gdal.OpenEx('PLScenes:api_key=foo,scene=my_id,unsupported_option=val', gdal.OF_RASTER) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None or gdal.GetLastErrorMsg().find('Unsupported option unsupported_option') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=my_id']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' md = ds.GetMetadata() if md['id'] != 'my_id': gdaltest.post_reason('fail') return 'fail' ds = None gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=my_id', 'PRODUCT_TYPE=analytic']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' md = ds.GetMetadata() if md['id'] != 'my_id': gdaltest.post_reason('fail') return 'fail' ds = None gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'SCENE=my_id', 'PRODUCT_TYPE=thumb']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = None gdal.Unlink('/vsimem/root') gdal.Unlink('/vsimem/root/ortho/my_id/full?product=visual') gdal.Unlink('/vsimem/root/ortho/my_id/full?product=analytic') gdal.Unlink('/vsimem/root/ortho/my_id/thumb') gdal.Unlink('/vsimem/root/ortho/my_id') return 'success'
def ogr_plscenes_data_v1_errors(): if gdaltest.plscenes_drv is None: return 'skip' # No PL_API_KEY gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') old_key = gdal.GetConfigOption('PL_API_KEY') if old_key: gdal.SetConfigOption('PL_API_KEY', '') with gdaltest.error_handler(): ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options=['VERSION=data_v1']) if old_key: gdal.SetConfigOption('PL_API_KEY', old_key) gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Invalid option gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', '{ "item-types": [] }') with gdaltest.error_handler(): gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:version=data_v1,api_key=foo,invalid=invalid', gdal.OF_VECTOR) gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Invalid JSON gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', '{invalid_json') with gdaltest.error_handler(): gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options=['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Not an object gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', 'false') with gdaltest.error_handler(): gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options=['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Lack of "item_types" gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', '{}') with gdaltest.error_handler(): gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options=['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Invalid catalog objects gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', """{"item_types": [{}, [], null, {"id":null}, {"id":"foo"}]}""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options=['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' # Invalid next URL gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', '{"_links": { "_next": "/vsimem/inexisting" }, "item_types": [{"id": "my_catalog"}]}') gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options=['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) with gdaltest.error_handler(): lyr_count = ds.GetLayerCount() if lyr_count != 1: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', '{"item_types": [{"id": "PSScene3Band"}]}') gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options=['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) lyr = ds.GetLayer(0) # Invalid index ds.GetLayer(-1) ds.GetLayer(1) with gdaltest.error_handler(): ds.GetLayerByName('invalid_name') # Cannot find /vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSScene3Band"],"filter":{"type":"AndFilter","config":[]}} with gdaltest.error_handler(): lyr.GetNextFeature() # Empty object gdal.FileFromMemBuffer('/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSScene3Band"],"filter":{"type":"AndFilter","config":[]}}', '{}') lyr.ResetReading() lyr.GetNextFeature() # null feature gdal.FileFromMemBuffer('/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSScene3Band"],"filter":{"type":"AndFilter","config":[]}}', '{ "features": [ null ] }') lyr.ResetReading() lyr.GetNextFeature() gdal.Unlink('/vsimem/data_v1/item-types') gdal.Unlink('/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSScene3Band"],"filter":{"type":"AndFilter","config":[]}}') return 'success'
def ogr_plscenes_2(): if gdaltest.plscenes_drv is None: return 'skip' gdal.FileFromMemBuffer('/vsimem/root', '{"ortho":"/vsimem/root/ortho/"}') gdal.FileFromMemBuffer('/vsimem/valid_root_but_invalid_child', '{"ortho":"/vsimem/valid_root_but_invalid_child/invalid_child/"}') # Error: no API_KEY gdal.PushErrorHandler() gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR) gdal.SetConfigOption('PL_URL', None) gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') return 'fail' # Error case gdal.PushErrorHandler() gdal.SetConfigOption('PL_URL', '/vsimem/does_not_exist/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') return 'fail' # Error case gdal.SetConfigOption('PL_URL', '/vsimem/valid_root_but_invalid_child/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) gdal.PushErrorHandler() ret = ds.GetLayer(0).GetFeatureCount() gdal.PopErrorHandler() if ret != 0: gdaltest.post_reason('fail') return 'fail' # Error cases for ortho_json in [ """{}""", '"valid_json_but_not_a_json_object"', """{ invalid_json,""", """{ "type": "FeatureCollection" }""", """{ "type": "FeatureCollection", "count": -1 }""", """{ "type": "FeatureCollection", "count": 0 }""", """{ "type": "FeatureCollection", "count": 1 }""", """{ "type": "FeatureCollection", "count": 1, "features": [] }""", ]: gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1000', ortho_json) gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) lyr = ds.GetLayer(0) gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1', """{ "count": 2, }""") gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1000', """{ "type": "FeatureCollection", "count": 2, "features": [ { "type": "Feature", "id": "my_id", "geometry": { "coordinates": [ [ [2,49],[2,50],[3,50],[3,49],[2,49] ] ], "type": "Polygon" }, "properties": { "acquired" : "2015-03-27T12:34:56.123+00", "camera" : { "bit_depth" : 12, "color_mode": "RGB" }, "cloud_cover" : { "estimated" : 0.25 } } } ], "links": { "next" : "/vsimem/root/ortho/?count=1000&page=2" } }""") gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1000&page=2', """{ "type": "FeatureCollection", "count": 1, "features": [ { "type": "Feature", "id": "my_id2", "geometry": null, "properties": {} } ], "links": { "next" : null } }""") my_id_only = """{ "type": "FeatureCollection", "count": 1, "features": [ { "type": "Feature", "id": "my_id", "geometry": { "coordinates": [ [ [2,49],[2,50],[3,50],[3,49],[2,49] ] ], "type": "Polygon" }, "properties": { "acquired" : "2015-03-27T12:34:56.123+00", "camera" : { "bit_depth" : 12, "color_mode": "RGB" }, "cloud_cover" : { "estimated" : 0.25 } } } ], "links": { "next" : null } }""" gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1&intersects=POINT%282.5%2049.5%29', my_id_only) gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1&intersects=POLYGON%20%28%282%2049%2C2%2050%2C3%2050%2C3%2049%2C2%2049%29%29', my_id_only) gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1000&intersects=POINT%282.5%2049.5%29', my_id_only) gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1000&camera.color_mode.eq=RGB&acquired.gte=2015-03-27T12:34:56&acquired.lt=2015-03-27T12:34:57&cloud_cover.estimated.gt=0.20000000&camera.bit_depth.lte=12&camera.bit_depth.gte=12&camera.bit_depth.lt=13', my_id_only) gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1&camera.color_mode.eq=RGB&acquired.gte=2015-03-27T12:34:56&acquired.lt=2015-03-27T12:34:57&cloud_cover.estimated.gt=0.20000000&camera.bit_depth.lte=12&camera.bit_depth.gte=12&camera.bit_depth.lt=13', my_id_only) gdal.SetConfigOption('PL_URL', '/vsimem/root/') gdal.PushErrorHandler() ds = gdal.OpenEx('PLScenes:api_key=foo,unsupported_option=val', gdal.OF_VECTOR) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None or gdal.GetLastErrorMsg().find('Unsupported option unsupported_option') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' if ds.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' if ds.GetLayer(-1) or ds.GetLayer(1): gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayerByName('ortho') if lyr.TestCapability(ogr.OLCFastFeatureCount) != 1 or \ lyr.TestCapability(ogr.OLCStringsAsUTF8) != 1 or \ lyr.TestCapability(ogr.OLCRandomRead) != 0: gdaltest.post_reason('fail') return 'fail' if lyr.GetFeatureCount() != 2: gdaltest.post_reason('fail') return 'fail' ext = lyr.GetExtent() if ext != (2.0, 3.0, 49.0, 50.0): gdaltest.post_reason('fail') print(ext) return 'fail' #lyr.ResetReading() f = lyr.GetNextFeature() if f.id != 'my_id' or f.acquired != '2015/03/27 12:34:56.123+00' or \ f['cloud_cover.estimated'] != 0.25: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' if f.GetGeometryRef().ExportToWkt() != 'MULTIPOLYGON (((2 49,2 50,3 50,3 49,2 49)))': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f.id != 'my_id2': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' if lyr.GetFeatureCount() != 2: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' lyr.SetSpatialFilterRect(-1000,-1000,1000,1000) if lyr.GetFeatureCount() != 2: gdaltest.post_reason('fail') return 'fail' lyr.SetSpatialFilterRect(2.5,49.5,2.5,49.5) f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' lyr.SetSpatialFilter(None) # Filter that can be passed to server side filterstring = "\"camera.color_mode\" = 'RGB' AND acquired = '2015/03/27 12:34:56' AND \"cloud_cover.estimated\" > 0.2 AND \"camera.bit_depth\" <= 12 AND \"camera.bit_depth\" >= 12 AND \"camera.bit_depth\" < 13" lyr.SetAttributeFilter(filterstring) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Same but invert GetNextFeature() and GetFeatureCount() lyr.SetAttributeFilter(filterstring) f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Filter that can be - partly - passed to server side filterstring = "fid = 1 AND \"camera.color_mode\" = 'RGB' AND acquired = '2015/03/27 12:34:56' AND \"cloud_cover.estimated\" > 0.2 AND \"camera.bit_depth\" <= 12 AND \"camera.bit_depth\" >= 12 AND \"camera.bit_depth\" < 13" lyr.SetAttributeFilter(filterstring) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Same but invert GetNextFeature() and GetFeatureCount() lyr.SetAttributeFilter(filterstring) f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' # Filter that cannot be passed to server side filterstring = "fid = 1" lyr.SetAttributeFilter(filterstring) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Same but invert GetNextFeature() and GetFeatureCount() lyr.SetAttributeFilter(filterstring) f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' # Filter on id gdal.FileFromMemBuffer('/vsimem/root/ortho/my_id', """{ "type": "Feature", "id": "my_id", "geometry": { "coordinates": [ [ [2,49],[2,50],[3,50],[3,49],[2,49] ] ], "type": "Polygon" }, "properties": { "acquired" : "2015-03-27T12:34:56.123+00", "camera" : { "bit_depth" : 12, "color_mode": "RGB" }, "cloud_cover" : { "estimated" : 0.25 } } }""") filterstring = "id = 'my_id'" lyr.SetAttributeFilter(filterstring) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Same but invert GetNextFeature() and GetFeatureCount() lyr.SetAttributeFilter(filterstring) f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' gdal.ErrorReset() lyr.SetAttributeFilter("id = 'non_existing_id'") if lyr.GetFeatureCount() != 0 or gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is not None or gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Unset attribute filter lyr.SetAttributeFilter(None) f = lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Regular ExecuteSQL sql_lyr = ds.ExecuteSQL("select * from ortho") f = sql_lyr.GetNextFeature() if f.id != 'my_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' ds.ReleaseResultSet(sql_lyr) # Test ordered by optimization gdal.FileFromMemBuffer('/vsimem/root/ortho/?count=1000&order_by=acquired%20asc', """{ "type": "FeatureCollection", "count": 2, "features": [ { "type": "Feature", "id": "my_id2", "geometry": null, "properties": {} } ], "links": { "next" : null } }""") sql_lyr = ds.ExecuteSQL("select * from ortho order by acquired asc") f = sql_lyr.GetNextFeature() if f.id != 'my_id2': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' ds.ReleaseResultSet(sql_lyr) # Test spat option ds = None gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:spat=2 49 3 50', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) lyr = ds.GetLayer(0) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' lyr.SetSpatialFilterRect(2.5,49.5,2.5,49.5) if lyr.GetFeatureCount() != 1 and gdal.GetLastErrorMsg().find('GEOS support not enabled') < 0: gdaltest.post_reason('fail') return 'fail' ds = None gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:spat=2.5 49.5 2.5 49.5', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) lyr = ds.GetLayer(0) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' ds = None gdal.Unlink('/vsimem/root') gdal.Unlink('/vsimem/valid_root_but_invalid_child') gdal.Unlink('/vsimem/root/ortho/?count=1') gdal.Unlink('/vsimem/root/ortho/?count=1000') gdal.Unlink('/vsimem/root/ortho/?count=1000&page=2') gdal.Unlink('/vsimem/root/ortho/?count=1&intersects=POINT%282.5%2049.5%29') gdal.Unlink('/vsimem/root/ortho/?count=1&intersects=POLYGON%20%28%282%2049%2C2%2050%2C3%2050%2C3%2049%2C2%2049%29%29') gdal.Unlink('/vsimem/root/ortho/?count=1000&intersects=POINT%282.5%2049.5%29') gdal.Unlink('/vsimem/root/ortho/?count=1&camera.color_mode.eq=RGB&acquired.gte=2015-03-27T12:34:56&acquired.lt=2015-03-27T12:34:57&cloud_cover.estimated.gt=0.20000000&camera.bit_depth.lte=12&camera.bit_depth.gte=12&camera.bit_depth.lt=13') gdal.Unlink('/vsimem/root/ortho/?count=1000&camera.color_mode.eq=RGB&acquired.gte=2015-03-27T12:34:56&acquired.lt=2015-03-27T12:34:57&cloud_cover.estimated.gt=0.20000000&camera.bit_depth.lte=12&camera.bit_depth.gte=12&camera.bit_depth.lt=13') gdal.Unlink('/vsimem/root/ortho/?count=1000&order_by=acquired%20asc') gdal.Unlink('/vsimem/root/ortho/my_id') return 'success'
def vsicrypt_2(): if not gdaltest.has_vsicrypt: return 'skip' # Missing key with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Invalid file with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'wb') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Invalid file with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'rb') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Invalid file with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'ab') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Invalid access with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'foo') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Key to short with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=a,file=/vsimem/file.bin', 'wb+') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Invalid signature gdal.FileFromMemBuffer('/vsimem/file.bin', 'foo') with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Generate empty file fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'wb') gdal.VSIFCloseL(fp) fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb') if fp is None: gdaltest.post_reason('fail') return 'fail' gdal.VSIFCloseL(fp) fp = gdal.VSIFOpenL('/vsimem/file.bin', 'rb') header = gdal.VSIFReadL(1, 1000, fp) gdal.VSIFCloseL(fp) if len(header) != 46: gdaltest.post_reason('fail') print(len(header)) return 'fail' # Test shortening header for i in range(46): fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb') gdal.VSIFWriteL(header, 1, 46 - 1 - i, fp) gdal.VSIFCloseL(fp) with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Test corrupting all bytes of header for i in range(46): for val in (0, 127, 255): fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb') try: new_byte = chr(val).encode('latin1') except: new_byte = chr(val) header_new = header[0:i] + new_byte + header[i + 1:] gdal.VSIFWriteL(header_new, 1, 46, fp) gdal.VSIFCloseL(fp) with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=' '/vsimem/file.bin', 'rb') if fp is not None: gdal.VSIFCloseL(fp) gdal.SetConfigOption('VSICRYPT_IV', 'TOO_SHORT') with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=' '/vsimem/file.bin', 'wb') gdal.SetConfigOption('VSICRYPT_IV', None) if fp is not None: gdal.VSIFCloseL(fp) # Inconsistent initial vector. header = struct.pack('B' * 38, 86, 83, 73, 67, 82, 89, 80, 84, # signature 38, 0, # header size 1, # major 0, # minor 0, 2, # sector size 0, # alg 0, # mode 8, # size of IV (should be 16) 32, 13, 169, 71, 154, 208, 22, 32, # IV 0, 0, # size of free text 0, # size of key check 0, 0, 0, 0, 0, 0, 0, 0, # size of unencrypted file 0, 0 # size of extra content ) fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb') gdal.VSIFWriteL(header, 1, len(header), fp) gdal.VSIFCloseL(fp) with gdaltest.error_handler(): fp = gdal.VSIFOpenL( '/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Inconsistent initial vector with key check. header = struct.pack('B' * 39, 86, 83, 73, 67, 82, 89, 80, 84, # signature 39, 0, # header size 1, # major 0, # minor 0, 2, # sector size 0, # alg 0, # mode 8, # size of IV (should be 16) 32, 13, 169, 71, 154, 208, 22, 32, # IV 0, 0, # size of free text 1, # size of key check 0, # key check 0, 0, 0, 0, 0, 0, 0, 0, # size of unencrypted file 0, 0 # size of extra content ) fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb') gdal.VSIFWriteL(header, 1, len(header), fp) gdal.VSIFCloseL(fp) with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Test reading with wrong key fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'wb') gdal.VSIFWriteL('hello', 1, 5, fp) gdal.VSIFCloseL(fp) fp = gdal.VSIFOpenL('/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'rb') content = gdal.VSIFReadL(1, 5, fp).decode('latin1') gdal.VSIFCloseL(fp) if content == 'hello': gdaltest.post_reason('fail') return 'fail' with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=short_key,file=/vsimem/file.bin', 'ab') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Test reading with wrong key with add_key_check fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,add_key_check=yes,file=/vsimem/file.bin', 'wb') gdal.VSIFWriteL('hello', 1, 5, fp) gdal.VSIFCloseL(fp) with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'rb') if fp is not None: gdaltest.post_reason('fail') return 'fail' with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=short_key,file=/vsimem/file.bin', 'ab') if fp is not None: gdaltest.post_reason('fail') return 'fail' with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'ab') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Test creating with potentially not build-in alg: with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/alg=blowfish,key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'wb') if fp is not None: gdal.VSIFCloseL(fp) # Invalid sector_size with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,sector_size=1,file=/vsimem/file.bin', 'wb') if fp is not None: gdaltest.post_reason('fail') return 'fail' # Sector size (16) should be at least twice larger than the block size (16) in CBC_CTS with gdaltest.error_handler(): fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,sector_size=16,mode=CBC_CTS,file=/vsimem/file.bin', 'wb') if fp is not None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/file.bin') return 'success'
def ogr_plscenes_4(): if gdaltest.plscenes_drv is None: return 'skip' gdal.FileFromMemBuffer('/vsimem/root', '{"ortho":"/vsimem/root/ortho/"}') gdal.FileFromMemBuffer('/vsimem/root/another_layer/?count=10', """{ "type": "FeatureCollection", "count": 1, "features": [ { "type": "Feature", "id": "my_id", "properties": { "prop_10": "prop_10", "prop_1" : "prop_1" } } ] }""") gdal.FileFromMemBuffer('/vsimem/root/another_layer/?count=1000', """{ "type": "FeatureCollection", "count": 1, "features": [ { "type": "Feature", "id": "my_id", "properties": { "prop_10": "prop_10", "prop_1" : "prop_1" } } ] }""") gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' if ds.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayerByName('another_layer') if lyr is None: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' if f['prop_1'] != 'prop_1' or f['prop_10'] != 'prop_10': gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() lyr = ds.GetLayerByName('does_not_exist') gdal.PopErrorHandler() if lyr is not None: gdaltest.post_reason('fail') return 'fail' ds = None gdal.FileFromMemBuffer('/vsimem/root', '{"another_layer":"/vsimem/root/another_layer/"}') gdal.SetConfigOption('PL_URL', '/vsimem/root/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) lyr = ds.GetLayer(0) f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' if f['prop_1'] != 'prop_1' or f['prop_10'] != 'prop_10': gdaltest.post_reason('fail') return 'fail' ds = None gdal.Unlink('/vsimem/root') gdal.Unlink('/vsimem/root/another_layer/?count=10') gdal.Unlink('/vsimem/root/another_layer/?count=1000') return 'success'
def test_rasterio_14(): gdal.FileFromMemBuffer( '/vsimem/rasterio_14.asc', """ncols 6 nrows 6 xllcorner 0 yllcorner 0 cellsize 0 0 0 100 0 0 0 0 100 0 0 0 100 0 0 0 0 100 0 100 0 100 0 0 0 0 100 0 100 0 0 0 0 0 0 0 100""") ds = gdal.Translate('/vsimem/rasterio_14_out.asc', '/vsimem/rasterio_14.asc', options='-of AAIGRID -r average -outsize 50% 50%') cs = ds.GetRasterBand(1).Checksum() assert cs == 110, ds.ReadAsArray() gdal.Unlink('/vsimem/rasterio_14.asc') gdal.Unlink('/vsimem/rasterio_14_out.asc') ds = gdal.GetDriverByName('MEM').Create('', 1000000, 1) ds.GetRasterBand(1).WriteRaster(ds.RasterXSize - 1, 0, 1, 1, struct.pack('B' * 1, 100)) data = ds.ReadRaster(buf_xsize=int(ds.RasterXSize / 2), buf_ysize=1, resample_alg=gdal.GRIORA_Average) data = struct.unpack('B' * int(ds.RasterXSize / 2), data) assert data[-1:][0] == 50 data = ds.ReadRaster(ds.RasterXSize - 2, 0, 2, 1, buf_xsize=1, buf_ysize=1, resample_alg=gdal.GRIORA_Average) data = struct.unpack('B' * 1, data) assert data[0] == 50 ds = gdal.GetDriverByName('MEM').Create('', 1, 1000000) ds.GetRasterBand(1).WriteRaster(0, ds.RasterYSize - 1, 1, 1, struct.pack('B' * 1, 100)) data = ds.ReadRaster(buf_xsize=1, buf_ysize=int(ds.RasterYSize / 2), resample_alg=gdal.GRIORA_Average) data = struct.unpack('B' * int(ds.RasterYSize / 2), data) assert data[-1:][0] == 50 data = ds.ReadRaster(0, ds.RasterYSize - 2, 1, 2, buf_xsize=1, buf_ysize=1, resample_alg=gdal.GRIORA_Average) data = struct.unpack('B' * 1, data) assert data[0] == 50
def get_2d_dataset(self, layers, srs, bbox, width, height, styles=None, bgcolor=None, transparent=False, time=None, elevation=None, v=None, filter=None, **kwargs): pyramid = Pyramid.objects(name=layers[0]).first() minx, miny, maxx, maxy = bbox # see if we need to transform the dataset default_srid = Pyramid.objects(name=layers[0]).first().srs s_srs = djgdal.SpatialReference(default_srid.decode('ascii')) print srs if not srs: t_srs = djgdal.SpatialReference(default_srid.decode('ascii')) else: t_srs = djgdal.SpatialReference(srs) #print t_srs, s_srs s_mins = Point(minx, miny, srid=t_srs.wkt) s_maxs = Point(maxx, maxy, srid=t_srs.wkt) s_mins.transform(s_srs.srid) s_maxs.transform(s_srs.srid) # figure out the pixel size. if we're only after one pixel, like for GetFeatureInfo, # it's likely that pxsize will be 0. In that case, set the pixel size to be the # smallest available. tminx, tminy, tmaxx, tmaxy = bbox minx = s_mins.x maxx = s_maxs.x miny = s_mins.y maxy = s_maxs.y pxsize = (maxx - minx) / width # case of only looking for one pixel if pxsize == 0: pxsize = pyramid.pxsize_at_levels[-1] maxx = maxx + pxsize maxy = maxy + pxsize # start with the furthest zoomed out tiles and work our way in, searching for the # nearest level to the query l = 0 p = 999999999 for level, px in enumerate(pyramid.pxsize_at_levels): if abs(px - pxsize) < p: l = level p = abs(px - pxsize) # open the index for that level, filter it by our bounding box. dataset = ogr.Open(pyramid.indices[l].encode('ascii')) index = dataset.GetLayer(0) index.ResetReading() index.SetSpatialFilterRect(minx, miny, maxx, maxy) f = index.GetNextFeature() # retrieve tiles tiles = [] while f: Tile.objects.ensure_index('tile_name') Tile.objects.ensure_index('pyramid', 'tile_name') t = Tile.objects(pyramid=pyramid, level=l, tile_name=f.location).first() fl = gdal.FileFromMemBuffer('/vsimem/' + t.tile_name, t.data) tiles.append(gdal.Open('/vsimem/' + t.tile_name)) f = index.GetNextFeature() del index # close the index # create our output dataset and stitch tiles into it by projecting them in. output = gdal.GetDriverByName('MEM').Create("TEMP", width, height, pyramid.raster_count, pyramid.data_type, []) output.SetGeoTransform([ tminx, (tmaxx - tminx) / width, 0, tmaxy, 0, (tminy - tmaxy) / height ]) output.SetProjection(t_srs.wkt) #print output.GetGeoTransform() for tile in tiles: #print tile.GetGeoTransform() #print tile.GetProjection() #print output.GetProjection() gdal.ReprojectImage(tile, output, None, None, gdal.GRA_Bilinear) return output
def ogr_plscenes_data_v1_nominal(): if gdaltest.plscenes_drv is None: return 'skip' gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', """{ "item_types": [ {"display_description" : "display_description", "display_name" : "display_name", "id": "PSOrthoTile"} ]}""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:version=data_v1,api_key=foo,FOLLOW_LINKS=YES', gdal.OF_VECTOR) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayer(0) if lyr.GetName() != 'PSOrthoTile': gdaltest.post_reason('fail') return 'fail' if lyr.TestCapability(ogr.OLCFastFeatureCount) != 1 or \ lyr.TestCapability(ogr.OLCStringsAsUTF8) != 1 or \ lyr.TestCapability(ogr.OLCRandomRead) != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/data_v1/stats&POSTFIELDS={"interval":"year","item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"RangeFilter","field_name":"cloud_cover","config":{"gte":0.000000}}]}}""", """{ "buckets": [ { "count": 1 }, { "count": 1} ] }""") gdal.FileFromMemBuffer("""/vsimem/data_v1/stats&POSTFIELDS={"interval":"year","item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"RangeFilter","field_name":"cloud_cover","config":{"gte":0}}]}}""", """{ "buckets": [ { "count": 1 }, { "count": 1} ] }""") if lyr.GetFeatureCount() != 2: gdaltest.post_reason('fail') return 'fail' if lyr.GetGeomType() != ogr.wkbMultiPolygon: gdaltest.post_reason('fail') return 'fail' ext = lyr.GetExtent() if ext != (-180.0, 180.0, -90.0, 90.0): gdaltest.post_reason('fail') print(ext) return 'fail' field_count = lyr.GetLayerDefn().GetFieldCount() if field_count != 67: gdaltest.post_reason('fail') print(field_count) return 'fail' # Regular /items/ fetching gdal.FileFromMemBuffer("""/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[]}}""", """{ "_links": { "_next": "/vsimem/data_v1/quick-search?page=2" }, "features" : [ { "id": "id", "_links" : { "_self" : "self", "assets" : "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets" }, "_permissions" : [ "download" ], "properties": { "acquired": "2016/02/11 12:34:56.789+00", "anomalous_pixels": 1.23, "columns": 1, "item_type": "foo" }, "geometry": { "type": "Polygon", "coordinates" : [ [ [2,49],[2,49.1],[2.1,49.1],[2.1,49],[2,49] ] ] } } ] }""") gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets', """{ "analytic" : { "_permissions": ["download"], "_links": { "_self": "analytic_links_self", "activate": "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/activate", }, "location": "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/my.tiff", "status": "active", "expires_at": "2016-02-11T12:34:56.789" } }""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'FOLLOW_LINKS=YES']) gdal.SetConfigOption('PL_URL', None) lyr = ds.GetLayer(0) f = lyr.GetNextFeature() if f.GetFID() != 1 or f['id'] != 'id' or f['self_link'] != 'self' or \ f['assets_link'] != '/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets' or \ f['acquired'] != '2016/02/11 12:34:56.789+00' or \ f['anomalous_pixels'] != 1.23 or f['item_type'] != 'foo' or f['columns'] != 1 or \ f['asset_analytic_self_link'] != 'analytic_links_self' or \ f['asset_analytic_activate_link'] != '/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/activate' or \ f['asset_analytic_permissions'] != ['download'] or \ f['asset_analytic_expires_at'] != '2016/02/11 12:34:56.789' or \ f['asset_analytic_location'] != '/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/my.tiff' or \ f['asset_analytic_status'] != 'active' or \ f.GetGeometryRef().ExportToWkt() != 'MULTIPOLYGON (((2 49,2.0 49.1,2.1 49.1,2.1 49.0,2 49)))': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' lyr.ResetReading() f = lyr.GetNextFeature() if f.GetFID() != 1: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' gdal.FileFromMemBuffer('/vsimem/data_v1/quick-search?page=2', """{ "features" : [ { "id": "id2" } ] }""") f = lyr.GetNextFeature() if f.GetFID() != 2 or f['id'] != 'id2': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' lyr.ResetReading() f = lyr.GetNextFeature() if f.GetFID() != 1: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f.GetFID() != 2: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer("""/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"GeometryFilter","field_name":"geometry","config":{"type":"Point","coordinates":[2.0,49.0]}}]}}""", """{"features" : [ { "id": "id3", "geometry": { "type": "Point", "coordinates": [2,49]} } ] }""") # POINT spatial filter lyr.SetSpatialFilterRect(2,49,2,49) f = lyr.GetNextFeature() if f['id'] != 'id3': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Cannot find /vsimem/data_v1/stats&POSTFIELDS={"interval":"year","item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"GeometryFilter","field_name":"geometry","config":{"type":"Point","coordinates":[2.0,49.0]}}]}} with gdaltest.error_handler(): if lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' # Reset spatial filter lyr.SetSpatialFilter(0, None) f = lyr.GetNextFeature() if f['id'] != 'id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Test attribute filter on id lyr.SetAttributeFilter("id = 'filtered_id'") gdal.FileFromMemBuffer("""/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"StringInFilter","field_name":"id","config":["filtered_id"]}]}}""", """{ "id": "filtered_id", "properties": {} }""") f = lyr.GetNextFeature() if f['id'] != 'filtered_id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Test attribute filter fully evaluated on server side. lyr.SetAttributeFilter("id != 'a' AND acquired >= '2016/02/11' AND acquired <= '2016/02/12' AND acquired > '1970/01/01 01:23:45' AND acquired < '2100/01/01 01:23:45' AND anomalous_pixels = 1.234567 AND (NOT id = 'b') AND columns > 0 AND columns < 2 AND columns = 1 AND columns IN (1, 2) AND (id IN ('filtered_2') OR id = 'foo') AND permissions = 'download' AND permissions IN ('download')") content = """{ "features" : [ { "id": "filtered_2", "_links" : { "_self" : "self", "assets" : "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets" }, "_permissions" : [ "download" ], "properties": { "acquired": "2016/02/11 12:34:56.789+00", "anomalous_pixels": 1.23, "columns": 1, "item_type": "foo" }, "geometry": { "type": "Polygon", "coordinates" : [ [ [2,49],[2,49.1],[2.1,49.1],[2.1,49],[2,49] ] ] } } ] }""" gdal.FileFromMemBuffer("""/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"NotFilter","config":{"type":"StringInFilter","field_name":"id","config":["a"]}},{"type":"DateRangeFilter","field_name":"acquired","config":{"gte":"2016-02-11T00:00:00Z"}}]},{"type":"DateRangeFilter","field_name":"acquired","config":{"lte":"2016-02-12T00:00:00Z"}}]},{"type":"DateRangeFilter","field_name":"acquired","config":{"gt":"1970-01-01T01:23:45Z"}}]},{"type":"DateRangeFilter","field_name":"acquired","config":{"lt":"2100-01-01T01:23:45Z"}}]},{"type":"RangeFilter","field_name":"anomalous_pixels","config":{"gte":1.234567,"lte":1.234567}}]},{"type":"NotFilter","config":{"type":"StringInFilter","field_name":"id","config":["b"]}}]},{"type":"RangeFilter","field_name":"columns","config":{"gt":0}}]},{"type":"RangeFilter","field_name":"columns","config":{"lt":2}}]},{"type":"NumberInFilter","field_name":"columns","config":[1]}]},{"type":"NumberInFilter","field_name":"columns","config":[1,2]}]},{"type":"OrFilter","config":[{"type":"StringInFilter","field_name":"id","config":["filtered_2"]},{"type":"StringInFilter","field_name":"id","config":["foo"]}]}]},{"type":"PermissionFilter","config":["download"]}]},{"type":"PermissionFilter","config":["download"]}]}]}}""",content) gdal.FileFromMemBuffer("""/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"AndFilter","config":[{"type":"NotFilter","config":{"type":"StringInFilter","field_name":"id","config":["a"]}},{"type":"DateRangeFilter","field_name":"acquired","config":{"gte":"2016-02-11T00:00:00Z"}}]},{"type":"DateRangeFilter","field_name":"acquired","config":{"lte":"2016-02-12T00:00:00Z"}}]},{"type":"DateRangeFilter","field_name":"acquired","config":{"gt":"1970-01-01T01:23:45Z"}}]},{"type":"DateRangeFilter","field_name":"acquired","config":{"lt":"2100-01-01T01:23:45Z"}}]},{"type":"RangeFilter","field_name":"anomalous_pixels","config":{"gte":1.23456699,"lte":1.2345670099999999}}]},{"type":"NotFilter","config":{"type":"StringInFilter","field_name":"id","config":["b"]}}]},{"type":"RangeFilter","field_name":"columns","config":{"gt":0}}]},{"type":"RangeFilter","field_name":"columns","config":{"lt":2}}]},{"type":"NumberInFilter","field_name":"columns","config":[1]}]},{"type":"NumberInFilter","field_name":"columns","config":[1,2]}]},{"type":"OrFilter","config":[{"type":"StringInFilter","field_name":"id","config":["filtered_2"]},{"type":"StringInFilter","field_name":"id","config":["foo"]}]}]},{"type":"PermissionFilter","config":["download"]}]},{"type":"PermissionFilter","config":["download"]}]}]}}""", content) f = lyr.GetNextFeature() if f['id'] != 'filtered_2': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Partly server / partly client lyr.SetAttributeFilter("id = 'filtered_3' AND id > 'a'") gdal.FileFromMemBuffer("""/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"StringInFilter","field_name":"id","config":["filtered_3"]}]}}""", """{ "features" : [ { "id": "filtered_3", "properties": { } } ] }""") f = lyr.GetNextFeature() if f['id'] != 'filtered_3': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' lyr.SetAttributeFilter("id > 'a' AND id = 'filtered_3'") f = lyr.GetNextFeature() if f['id'] != 'filtered_3': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Completely client side lyr.SetAttributeFilter("id > 'a' OR id = 'id'") f = lyr.GetNextFeature() if f['id'] != 'id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Completely client side lyr.SetAttributeFilter("NOT id > 'z'") f = lyr.GetNextFeature() if f['id'] != 'id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Reset attribute filter lyr.SetAttributeFilter(None) f = lyr.GetNextFeature() if f['id'] != 'id': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Try raster access # Missing catalog gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'SCENE=id']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None or gdal.GetLastErrorMsg().find('Missing catalog') < 0: gdaltest.post_reason('fail') return 'fail' # Invalid catalog gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=invalid', 'SCENE=id']) gdal.SetConfigOption('PL_URL', None) # visual not an object gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets', """{ "visual": false }""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None: gdaltest.post_reason('fail') return 'fail' # Inactive file, and activation link not working gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets', """{ "analytic" : { "_links": { "_self": "analytic_links_self", "activate": "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/activate", }, "_permissions": ["download"], "status": "inactive", } }""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=analytic']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None: gdaltest.post_reason('fail') return 'fail' # File in activation gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets', """{ "analytic" : { "_links": { "_self": "analytic_links_self", "activate": "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/activate", }, "_permissions": ["download"], "status": "activating", } }""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=analytic']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets', """{ "analytic" : { "_permissions": ["download"], "_links": { "_self": "analytic_links_self", "activate": "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/activate", }, "location": "/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/my.tiff", "status": "active", "expires_at": "2016-02-11T12:34:56.789" } }""") # Missing /vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/my.tiff gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=analytic']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None: gdaltest.post_reason('fail') return 'fail' # JSon content for /vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/my.tiff gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/my.tiff', """{}""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=analytic']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None: gdaltest.post_reason('fail') return 'fail' # Missing metadata gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile/items/id/assets/analytic/my.tiff', open('../gcore/data/byte.tif', 'rb').read()) gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=analytic']) gdal.SetConfigOption('PL_URL', None) if ds_raster is None: gdaltest.post_reason('fail') return 'fail' ds_raster = None # Failed filter by scene id gdal.FileFromMemBuffer('/vsimem/data_v1/item-types/PSOrthoTile', """{"id": "PSOrthoTile"}""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=analytic']) gdal.SetConfigOption('PL_URL', None) if ds_raster is None: gdaltest.post_reason('fail') return 'fail' ds_raster = None # Test metadata items attached to dataset gdal.FileFromMemBuffer("""/vsimem/data_v1/quick-search?_page_size=250&POSTFIELDS={"item_types":["PSOrthoTile"],"filter":{"type":"AndFilter","config":[{"type":"StringInFilter","field_name":"id","config":["id"]}]}}""", """{ "id": "id", "properties": { "anomalous_pixels": 1.23 }, }""") gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=analytic']) gdal.SetConfigOption('PL_URL', None) if ds_raster is None: gdaltest.post_reason('fail') return 'fail' if ds_raster.GetMetadataItem('anomalous_pixels') != '1.23': gdaltest.post_reason('fail') return 'fail' ds_raster = None # Test invalid ASSET gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id', 'ACTIVATION_TIMEOUT=1', 'ASSET=invalid']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None: gdaltest.post_reason('fail') return 'fail' # Test subdatasets gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds_raster = gdal.OpenEx('PLScenes:', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'ASSET=list', 'SCENE=id']) gdal.SetConfigOption('PL_URL', None) if len(ds_raster.GetSubDatasets()) != 1: gdaltest.post_reason('fail') return 'fail' ds_raster = None # Unsupported option gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds_raster = gdal.OpenEx('PLScenes:unsupported=yes', gdal.OF_RASTER, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile', 'SCENE=id']) gdal.SetConfigOption('PL_URL', None) if ds_raster is not None: gdaltest.post_reason('fail') return 'fail' # Test catalog with vector access gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds2 = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=PSOrthoTile']) gdal.SetConfigOption('PL_URL', None) if ds2 is None or ds2.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') with gdaltest.error_handler(): ds2 = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['VERSION=data_v1', 'API_KEY=foo', 'ITEMTYPES=invalid']) gdal.SetConfigOption('PL_URL', None) if ds2 is not None: gdaltest.post_reason('fail') return 'fail' fl = gdal.ReadDir('/vsimem/data_v1') for filename in fl: gdal.Unlink(filename) return 'success'
def eedai_geotiff(): if gdaltest.eedai_drv is None: return 'skip' gdal.FileFromMemBuffer( '/vsimem/ee/assets/image', json.dumps({ 'type': 'IMAGE', 'bands': [{ "id": "B1", "dataType": { "precision": "INTEGER", "range": { "max": 65535 } }, "grid": { "crsCode": "EPSG:32610", "affineTransform": { "translateX": 499980, "translateY": 4200000, "scaleX": 60, "scaleY": -60 }, "dimensions": { "width": 1830, "height": 1831 } } }] })) gdal.SetConfigOption('EEDA_BEARER', 'mybearer') gdal.SetConfigOption('EEDA_URL', '/vsimem/ee/') ds = gdal.Open('EEDAI:image') gdal.SetConfigOption('EEDA_URL', None) mem_ds = gdal.GetDriverByName('MEM').Create('', 256, 256, 1, gdal.GDT_UInt16) mem_ds.GetRasterBand(1).Fill(12345) gdal.GetDriverByName('GTiff').CreateCopy('/vsimem/out.tif', mem_ds) f = gdal.VSIFOpenL('/vsimem/out.tif', 'rb') data = gdal.VSIFReadL(1, 1000000, f) gdal.VSIFCloseL(f) gdal.Unlink('/vsimem/out.tif') gdal.FileFromMemBuffer( '/vsimem/ee/assets:getPixels&CUSTOMREQUEST=POST&POSTFIELDS={ "path": "image", "encoding": "GEO_TIFF", "bandIds": [ "B1" ], "grid": { "origin": { "x": 499980.0, "y": 4200000.0 }, "pixelSize": { "x": 60.0, "y": -60.0 }, "dimensions": { "width": 256, "height": 256 } } }', data) got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 1, 1) got_data = struct.unpack('H', got_data)[0] if got_data != 12345: gdaltest.post_reason('fail') print(got_data) return 'fail' ds = None gdal.SetConfigOption('EEDA_BEARER', None) return 'success'