5
img=gdal.Open("D:\data\sub_66")
inputArray=img.ReadAsArray()

gives Error: ValueError: array is too big. What is the problem with this?

  • 1
    Your raster is too big to fit into memory as one block. Use ReadRaster to read the image in sections. Is that really your image path? Your file should have an extension, even GRID has .adf as a file extension. – Michael Stimson Mar 28 '17 at 5:46
  • @Michael Miles-Stimson-Please give example of ReadRaster if possible.My imagepath is correct.My image has 775 columns and 12159 rows.How to read this?Please give an idea. – Earth Patel Mar 28 '17 at 6:14
  • 1
    @MichaelMiles-Stimson, gdal can read grids with .adf extension or just the directory. E.g gdalinfo somegrid and gdalinfo somegrid/w001001.adf will both work. – user2856 Mar 28 '17 at 23:28
  • That's good to know @Luke, I've always used the hdr.adf to open a GRID with GDAL. I use the existence of that file to determine that the folder is a raster and not just a folder so have then used the same to open it. – Michael Stimson Mar 28 '17 at 23:53
5

Here's a simple example of ReadRaster:

import os, sys
from osgeo import gdal

Image  = gdal.Open(r'D:\Testing\test.img')
Band   = Image.GetRasterBand(1) # 1 based, for this example only the first
NoData = Band.GetNoDataValue()  # this might be important later

nBands = Image.RasterCount      # how many bands, to help you loop
nRows  = Image.RasterYSize      # how many rows
nCols  = Image.RasterXSize      # how many columns
dType  = Band.DataType          # the datatype for this band

RowRange = range(nRows)
for ThisRow in RowRange:
    # read a single line from this band
    ThisLine = Band.ReadRaster(0,ThisRow,nCols,1,nCols,1,dType)

    if ThisRow % 100 == 0: # report every 100 lines
        print "Scanning %d of %d" % (ThisRow,nRows)

    for Val in ThisLine: # some simple test on the values
        if Val == 65535:
            print 'Bad value'

Image = None # close the raster

Note that if you're having issues with memory the ReadAsArray method also allows for reading a single scan line:

Cells    = range(nCols)

for ThisRow in RowRange:
    ThisLine = Band.ReadAsArray(0,ThisRow,nCols,1)

    if ThisRow % 100 == 0: # report every 100 lines
        print "Scanning %d of %d" % (ThisRow,nRows)

    for ThisCell in Cells:
        Val = ThisLine[0].item(ThisCell)
        if Val == 65535:
            print 'Bad value'

If you get used to reading the raster line-by-line or block-by-block regardless of the size of the input raster then your scripts will not fail on potentially large rasters which will save you a lot of rewriting in the end.

It is also conceivable by jigging the iterators to read the raster on a cell-by-cell iteration with a band loop; considering the unusually large number of bands this might be an option for you.

1

Assuming the error is related to the wrong filepath, this code should solve your issue:

from osgeo import gdal
import sys
import numpy as np

img = gdal.Open( "D:\data\sub_66.tif" ) # This is an example, please use the real extension of the image file instead of '.tif'

for band in range( img.RasterCount ):
    band += 1
    print "[ GETTING BAND ]: ", band
    srcband = img.GetRasterBand(band)
    inputArray = np.array(img.GetRasterBand(1).ReadAsArray())
    print "[ ARRAY ]: ", inputArray

It will return something like this:

[ GETTING BAND ]:  1
[ ARRAY ]:  [[1900 1900 1898 1895 1892 1887 1879 1871 1863 1852 1845 1837 1824 1802
  1743 1725 1713 1705 1699 1693 1687 1683]
 [1897 1896 1894 1892 1890 1884 1877 1869 1862 1854 1847 1838 1820 1800
  1745 1729 1719 1712 1706 1701 1696 1695]
 [1892 1891 1890 1888 1885 1881 1875 1868 1861 1855 1849 1837 1817 1794
  1747 1732 1725 1720 1714 1710 1707 1706]
 [1887 1885 1884 1882 1880 1878 1873 1867 1860 1855 1849 1833 1815 1789
  1749 1738 1732 1728 1723 1720 1718 1715]
 [1882 1880 1878 1876 1875 1873 1871 1866 1861 1855 1849 1832 1817 1795
  1756 1744 1740 1737 1733 1730 1728 1725]
 [1880 1877 1874 1873 1870 1868 1867 1865 1860 1855 1850 1841 1834 1817
  1795 1769 1749 1746 1743 1740 1736 1731]
 [1880 1876 1873 1870 1869 1866 1863 1862 1859 1856 1852 1847 1843 1841
  1824 1812 1802 1775 1758 1747 1740 1733]
 [1879 1876 1873 1870 1869 1866 1863 1860 1858 1855 1852 1850 1847 1843
  1831 1819 1803 1782 1763 1747 1738 1730]
 [1879 1877 1874 1872 1869 1866 1864 1861 1858 1855 1852 1850 1850 1848
  1836 1816 1794 1775 1754 1744 1736 1728]
 [1880 1877 1875 1872 1869 1867 1864 1862 1858 1854 1850 1848 1850 1850
  1840 1806 1786 1767 1749 1742 1734 1726]
 [1881 1879 1876 1873 1870 1866 1864 1861 1857 1851 1843 1840 1841 1850
  1827 1797 1782 1769 1752 1742 1733 1723]
 [1882 1879 1876 1873 1870 1867 1864 1861 1855 1848 1839 1835 1833 1836
  1810 1794 1783 1771 1758 1747 1737 1729]
 [1882 1880 1876 1873 1869 1866 1862 1858 1854 1849 1838 1833 1826 1814
  1800 1792 1782 1773 1762 1752 1742 1733]
 [1881 1878 1874 1870 1867 1863 1860 1856 1853 1849 1840 1835 1821 1813
  1798 1790 1783 1774 1766 1757 1748 1738]]
  • @Michael Miles-Stimson-No,Imagepath is correct and this gives the result with the another small size file which i have of (425 bands,210 column and 295 rows). – Earth Patel Mar 28 '17 at 6:59
  • @EarthPatel Did you try using the current extension for your image? – mgri Mar 28 '17 at 9:11
  • @Michael Miles-Stimson-Both the file(small size and currently using) has the same type.It has not extension but the TYPE:File. – Earth Patel Mar 28 '17 at 9:43
  • @EarthPatel Ok, I don't know how to manage a file type. So, the next question is: did you try using the "D:\data\sub_66" filepath? But the most important thing is: I'm not Michael Miles-Stimson. :) – mgri Mar 28 '17 at 9:48
  • 1
    6 - yes,I try it.The file which is small(425 bands,210 column and 295 rows) it's running perfectly.but,the file(image) which has (425 bands,775 cols,12159 rows) gives valueerror:array is too big. – Earth Patel Mar 28 '17 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.