I know this is an old post, but if anyone is looking to do this with ArcMap 10x you can cast your raster as a Raster Object and use the uncompressedSize property to get the size on disk.
That would the maximum possible size -- as you note, almost always smaller due to compression.
Python has functions in the os module to get file sizes off the operating system, so one could write a Python function to measure the size of the raster files using os.path.getsize(). This could be quite involved because you'd have to look at all the files. But it could be done for file-based rasters (not for rasters in the gdb, which are not visible from the OS).
I use the uncompressed size method in the tool validation just as a quick way to automatically select the largest raster in a TOC which is usually the watershed wide Lidar.
To get the actual size of a raster with it's associated files such as .ovr and .aux.xml using the os library I've used this code:
import os raster = # Path to raster file basename = os.path.basename(raster).split(".")[0] rootFolder = os.path.dirname(raster) associatedFiles = [os.path.join(rootFolder,f) for f in next(os.walk(os.path.dirname(raster)))[2] if f.split(".")[0] == basename] if len(os.path.basename(raster).split(".")) == 1: fileList = next(os.walk(raster))[2] dirSize = sum([os.path.getsize(os.path.join(raster,f)) for f in fileList]) rasSize = sum([os.path.getsize(f) for f in associatedFiles]) + dirSize else: rasSize = sum([os.path.getsize(f) for f in associatedFiles]) def convertSize(size,precision=2): suffixes=['B','KB','MB','GB','TB'] suffixIndex = 0 while size > 1024 and suffixIndex < 4: suffixIndex += 1 #increment the index of the suffix size = size/1024.0 #apply the division return "%.*f %s"%(precision,size,suffixes[suffixIndex]) print convertSize(rasSize)
The conversion part of the script was found here. The os.getsize function only returns the size of the file not the size on disk which incorporates the allocated size on the hard drive for the file metadata as seen in the properties of a file.
Here's a function implementation that returns a number in bytes and has the option of returning something similar to uncompressedSize. (There is a bug BUG-000110272 with the uncompressedSize property of the Raster object with large rasters that this works around.)
def raster_size(filepath, size_type="DISK"):
"""Return size of file-based rasters
filepath
path to raster
size_type
"DISK" - file size on disk from os.path.getsize() on files (default)
"UNCOMPRESSED" - file size based on rows x columns x 4 bytes
(estimate for Esri grid format, 32 bits/gridcell)
"CELLS" - number of gridcells
returns: file size as long integer
"""
import os
filepath = os.path.realpath(filepath)
if not os.path.exists(filepath):
raise Exception("{} not found".format(filepath))
skey = str(size_type)[:3].upper()
if skey not in ["UNC", "CEL"]:
raster = filepath
basename = os.path.basename(raster).split(".")[0]
rootFolder = os.path.dirname(raster)
associatedFiles = [os.path.join(rootFolder,f)
for f in next(os.walk(os.path.dirname(raster)))[2]
if f.split(".")[0] == basename]
if len(os.path.basename(raster).split(".")) == 1:
# .tif, .jpg etc
fileList = next(os.walk(raster))[2]
dirSize = sum([os.path.getsize(os.path.join(raster,f)) for f in fileList])
rasSize = sum([os.path.getsize(f) for f in associatedFiles]) + dirSize
else:
# Esri grid format
rasSize = sum([os.path.getsize(f) for f in associatedFiles])
else:
from arcpy.sa import Raster
r = Raster(filepath)
if skey == "UNC":
rasSize = r.width * r.height * 4 * r.bandCount
elif skey == "CEL":
rasSize = r.width * r.height
return rasSize
I reformatted the code snip.
def raster_size(filepath, size_type="DISK"):
"""Return size of file-based rasters
filepath path to raster
size_type "DISK" - file size on disk from os.path.getsize() on files (default)
"UNCOMPRESSED" - file size based on rows x columns x 4 bytes
(estimate for Esri grid format, 32 bits/gridcell)
"CELLS" - number of gridcells
returns: file size as long integer """
import os
filepath = os.path.realpath(filepath)
if not os.path.exists(filepath):
raise Exception("{} not found".format(filepath))
skey = str(size_type)[:3].upper()
if skey not in ["UNC", "CEL"]:
raster = filepath
basename = os.path.basename(raster).split(".")[0]
rootFolder = os.path.dirname(raster)
associatedFiles = [os.path.join(rootFolder,f) for f in next(os.walk(os.path.dirname(raster)))[2] if f.split(".")[0] == basename]
if len(os.path.basename(raster).split(".")) == 1:
# .tif, .jpg etc
fileList = next(os.walk(raster))[2]
dirSize = sum([os.path.getsize(os.path.join(raster,f)) for f in fileList])
rasSize = sum([os.path.getsize(f) for f in associatedFiles]) + dirSize
else:
# Esri grid format
rasSize = sum([os.path.getsize(f) for f in associatedFiles])
else:
from arcpy.sa import Raster
r = Raster(filepath)
if skey == "UNC":
rasSize = r.width * r.height * 4 * r.bandCount
elif skey == "CEL":
rasSize = r.width * r.height
return rasSize