1

I'm trying to get the value of a pixel from a coordinate.

The problem is when I use a large image, so this error appears

Traceback (most recent call last):
File "D:\algoritmo\teste_read_2.py", line 61, in
teste = band1.ReadAsArray()
File "C:\Python27\lib\site-packages\osgeo\gdal.py", line 2449, in ReadAsArray
callback_data = callback_data)
File "C:\Python27\lib\site-packages\osgeo\gdal_array.py", line 339, in BandReadAsArray
buf_obj = numpy.empty([buf_ysize,buf_xsize], dtype = typecode)
ValueError: array is too big; arr.size * arr.dtype.itemsize is larger than the maximum possible size.

Follow my code below:

myshape1 = shapefile.Reader('TEMP_APP_BDG_TODAS_2018Point_selec_utm') 
pontos = myshape1.shapes()
lon = []
lat = []
for p in pontos:
        xy = p.points[0]
        lon.append(xy[0])
        lat.append(xy[1]) 
 plt.figure(1)
 plt.plot(lon,lat,'r*',ms=15)
 alt_srtm = []
 arquivo = "mosaico_srtm_utm_5m"
 image = gdal.Open(arquivo+".tif")
 band1 = image.GetRasterBand(1)
 cols = image.RasterXSize
 rows = image.RasterYSize
 print rows,cols
 geo = image.GetGeoTransform()
 print geo 
 lat1 = geo[3] # Pego a latitude do primeiro pixel
 lon1 = geo[0] # Pego a longitude do primeiro pixel
 pixel_i = geo[1]
 pixel_j = geo[5] * -1
 print lat[0],lon[0]
 teste = band1.ReadAsArray()
 for k in range(0,len(lat)):
        i = int(((lat1 - lat[k])/pixel_i))
        j = int(((lon[k] - lon1)/pixel_j))
        value = teste[i][j]
        alt_srtm.append(value)
  • 1
    you're loading the whole of the image into memory in one go. You can pass parameters to ReadAsArray() to specify a small region (even a single cell). See this related question on SO – Steven Kay Sep 11 '18 at 18:32

Your Answer

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

Browse other questions tagged or ask your own question.