Skip to content

Instantly share code, notes, and snippets.

@RutgerK
Created June 3, 2015 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RutgerK/27c4af235035621fb609 to your computer and use it in GitHub Desktop.
Save RutgerK/27c4af235035621fb609 to your computer and use it in GitHub Desktop.
GDAL_filesize
{
"metadata": {
"name": "",
"signature": "sha256:839d2d22433f5f0016acc0cfc6567424967435252e116ea22b40127793c2bbd7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import gdal\n",
"import osr\n",
"import numpy as np\n",
"import subprocess\n",
"import os\n",
"import glob"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gdal.VersionInfo()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"'1110100'"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"driver = gdal.GetDriverByName('GTIFF')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"xsize = 2048\n",
"ysize = 2048\n",
"n_bands = 5\n",
"size = n_bands * ysize * xsize\n",
"\n",
"#outdir = r'D:\\Temp'\n",
"outdir = os.getcwd()\n",
"\n",
"# outfile, gdal dtype, numpy dtype, co options\n",
"runs = [('gdal_float32_pb.tif', gdal.GDT_Float32, np.float32, ['COMPRESS=PACKBITS']),\n",
" ('gdal_float32_pb_tiled.tif', gdal.GDT_Float32, np.float32, ['COMPRESS=PACKBITS', 'TILED=YES']),\n",
" ('gdal_float32_pb_tiled_512.tif', gdal.GDT_Float32, np.float32, ['COMPRESS=PACKBITS', 'TILED=YES', 'BLOCKXSIZE=512', 'BLOCKYSIZE=512']),\n",
" ('gdal_float32_pb_tiled_128.tif', gdal.GDT_Float32, np.float32, ['COMPRESS=PACKBITS', 'TILED=YES', 'BLOCKXSIZE=128', 'BLOCKYSIZE=128']),\n",
" ('gdal_float32_lzw.tif', gdal.GDT_Float32, np.float32, ['COMPRESS=LZW']),\n",
" ('gdal_float32_deflate.tif', gdal.GDT_Float32, np.float32, ['COMPRESS=DEFLATE']),\n",
" ('gdal_float32.tif', gdal.GDT_Float32, np.float32, []),\n",
" ('gdal_float64_packbits.tif', gdal.GDT_Float64, np.float64, ['COMPRESS=PACKBITS']),\n",
" ('gdal_float64.tif', gdal.GDT_Float64, np.float64, []),\n",
" ('gdal_uint16_packbits.tif', gdal.GDT_UInt16, np.uint16, ['COMPRESS=PACKBITS']),\n",
" ('gdal_uint16_lzw.tif', gdal.GDT_UInt16, np.uint16, ['COMPRESS=LZW']),\n",
" ('gdal_uint8_packbits.tif', gdal.GDT_Byte, np.uint8, ['COMPRESS=PACKBITS']),\n",
" ('gdal_uint8_deflate.tif', gdal.GDT_Byte, np.uint8, ['COMPRESS=DEFLATE']), \n",
" ('gdal_uint8.tif', gdal.GDT_Byte, np.uint8, [])]\n",
" \n",
"# dummy data\n",
"data = np.random.randint(0,255, size=size).reshape(n_bands, ysize, xsize)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('{filename: <35}{pysize: >15}{gtsize: >15}{diff: >15}'.format(filename='File',\n",
" pysize='Python',\n",
" gtsize='Gdal_trans',\n",
" diff='Difference'))\n",
" \n",
" \n",
"for outfile, gdaltype, nptype, options in runs:\n",
"\n",
" outfile = os.path.join(outdir, outfile)\n",
" outfile_gdaltranslate = outfile.replace('.tif', '_gdal_translate.tif')\n",
"\n",
" # file creation with Python bindings\n",
" dst_ds = driver.Create(outfile, xsize, ysize, n_bands, gdaltype, options)\n",
"\n",
" dst_ds.SetGeoTransform([ 444720, 30, 0, 3751320, 0, -30 ])\n",
" srs = osr.SpatialReference()\n",
" srs.SetUTM(11, 1)\n",
" srs.SetWellKnownGeogCS('NAD27')\n",
" dst_ds.SetProjection(srs.ExportToWkt())\n",
"\n",
" for i in range(n_bands):\n",
" dst_ds.GetRasterBand(i+1).WriteArray(data[i,:,:].astype(nptype))\n",
"\n",
" dst_ds = None\n",
" \n",
" # convert with gdal_translate as a reference \n",
" if len(options) > 0:\n",
" res = subprocess.check_output('gdal_translate -co \"%s\" %s %s' % ('\" -co \"'.join(options),\n",
" outfile, \n",
" outfile_gdaltranslate))\n",
" else:\n",
" res = subprocess.check_output('gdal_translate %s %s' % (outfile,\n",
" outfile_gdaltranslate))\n",
" \n",
" # get file sizes\n",
" py_size = os.path.getsize(outfile) / 1024**2\n",
" gt_size = os.path.getsize(outfile_gdaltranslate) / 1024**2\n",
" difference = py_size - gt_size\n",
" \n",
" print('{filename: <35}{pysize:12.1f} MB{gtsize:12.1f} MB{dif:12.1f} MB'.format(filename=os.path.basename(outfile),\n",
" pysize=py_size,\n",
" gtsize=gt_size,\n",
" dif=difference))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"File Python Gdal_trans Difference\n",
"gdal_float32_pb.tif 132.1 MB 80.3 MB 51.8 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float32_pb_tiled.tif 132.1 MB 80.3 MB 51.8 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float32_pb_tiled_512.tif 132.1 MB 80.3 MB 51.8 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float32_pb_tiled_128.tif 132.2 MB 80.3 MB 51.9 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float32_lzw.tif 60.3 MB 35.2 MB 25.1 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float32_deflate.tif 54.4 MB 31.4 MB 23.1 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float32.tif 80.0 MB 80.0 MB 0.0 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float64_packbits.tif 249.0 MB 113.2 MB 135.8 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_float64.tif 160.0 MB 160.0 MB 0.0 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_uint16_packbits.tif 40.3 MB 40.3 MB 0.0 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_uint16_lzw.tif 29.3 MB 29.3 MB 0.0 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_uint8_packbits.tif 20.2 MB 20.2 MB 0.0 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_uint8_deflate.tif 20.0 MB 20.0 MB 0.0 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"gdal_uint8.tif 20.0 MB 20.0 MB 0.0 MB"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# check if the properties/options are actually used\n",
"# seems all right\n",
"\n",
"def get_props(infile):\n",
" \n",
" ds = gdal.Open(infile) \n",
" meta = ds.GetMetadata('IMAGE_STRUCTURE')\n",
" bs = ds.GetRasterBand(1).GetBlockSize()\n",
" \n",
" ds = None \n",
" \n",
" return meta, bs\n",
"\n",
"print('{filename: <47}{compression: >12}{xblock: >12}{yblock: >12}'.format(filename='filename',\n",
" compression='Compression',\n",
" xblock='x-block',\n",
" yblock='y-block'))\n",
"\n",
"for gdal_file in glob.glob(os.path.join(outdir, '*_gdal_translate.tif')):\n",
" \n",
" meta, bs = get_props(gdal_file)\n",
" filename = os.path.basename(gdal_file).replace('.tif', '')\n",
" print('{filename: <47}{compression: >12}{xblock:12.0f}{yblock:12.0f}'.format(filename=filename,\n",
" compression=meta.get('COMPRESSION', '(none)'),\n",
" xblock=bs[1],\n",
" yblock=bs[0]))\n",
" \n",
" python_file = gdal_file.replace('_gdal_translate', '')\n",
" meta, bs = get_props(python_file)\n",
" filename = os.path.basename(python_file).replace('.tif', '')\n",
" print('{filename: <47}{compression: >12}{xblock:12.0f}{yblock:12.0f}'.format(filename=filename,\n",
" compression=meta.get('COMPRESSION', '(none)'),\n",
" xblock=bs[1],\n",
" yblock=bs[0]))\n",
" \n",
" print('\\n')\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"filename Compression x-block y-block\n",
"gdal_float32_deflate_gdal_translate DEFLATE 1 2048\n",
"gdal_float32_deflate DEFLATE 1 2048\n",
"\n",
"\n",
"gdal_float32_deflate_tiled_gdal_translate DEFLATE 1 2048\n",
"gdal_float32_deflate_tiled DEFLATE 1 2048\n",
"\n",
"\n",
"gdal_float32_gdal_translate (none) 1 2048\n",
"gdal_float32 (none) 1 2048\n",
"\n",
"\n",
"gdal_float32_lzw_gdal_translate LZW 1 2048\n",
"gdal_float32_lzw LZW 1 2048\n",
"\n",
"\n",
"gdal_float32_lzw_tiled_gdal_translate LZW 1 2048\n",
"gdal_float32_lzw_tiled LZW 1 2048\n",
"\n",
"\n",
"gdal_float32_pb_gdal_translate PACKBITS 1 2048\n",
"gdal_float32_pb PACKBITS 1 2048\n",
"\n",
"\n",
"gdal_float32_pb_tiled_128_gdal_translate PACKBITS 128 128\n",
"gdal_float32_pb_tiled_128 PACKBITS 128 128\n",
"\n",
"\n",
"gdal_float32_pb_tiled_512_gdal_translate PACKBITS 512 512\n",
"gdal_float32_pb_tiled_512 PACKBITS 512 512\n",
"\n",
"\n",
"gdal_float32_pb_tiled_gdal_translate PACKBITS 256 256\n",
"gdal_float32_pb_tiled PACKBITS 256 256\n",
"\n",
"\n",
"gdal_float64_gdal_translate (none) 1 2048\n",
"gdal_float64 (none) 1 2048\n",
"\n",
"\n",
"gdal_float64_packbits_gdal_translate PACKBITS 1 2048\n",
"gdal_float64_packbits PACKBITS 1 2048\n",
"\n",
"\n",
"gdal_uint16_lzw_gdal_translate LZW 1 2048\n",
"gdal_uint16_lzw LZW 1 2048\n",
"\n",
"\n",
"gdal_uint16_packbits_gdal_translate PACKBITS 1 2048\n",
"gdal_uint16_packbits PACKBITS 1 2048\n",
"\n",
"\n",
"gdal_uint8_deflate_gdal_translate DEFLATE 1 2048\n",
"gdal_uint8_deflate DEFLATE 1 2048\n",
"\n",
"\n",
"gdal_uint8_gdal_translate (none) 1 2048\n",
"gdal_uint8 (none) 1 2048\n",
"\n",
"\n",
"gdal_uint8_packbits_gdal_translate PACKBITS 1 2048\n",
"gdal_uint8_packbits PACKBITS 1 2048\n",
"\n",
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment