I ran into this issue so I wrote an iterator class to do this. In my case I wanted to return the X,Y,Z values but you could easily modify to return the numpy array.
It can be used simply like this:
<SPAN class="keyword token">for</SPAN> block <SPAN class="keyword token">in</SPAN> RasterBlockIterator<SPAN class="punctuation token">(</SPAN>raster<SPAN class="punctuation token">,</SPAN> no_data_val<SPAN class="operator token">=</SPAN>self<SPAN class="punctuation token">.</SPAN>no_data_val<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> point <SPAN class="keyword token">in</SPAN> block<SPAN class="punctuation token">:</SPAN> x <SPAN class="operator token">=</SPAN> point<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> y <SPAN class="operator token">=</SPAN> point<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> z <SPAN class="operator token">=</SPAN> point<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The iterator class. If you want the numpy array just return the arr on line 62. By default it works in blocks of 1000*1000
<SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">RasterBlockIterator</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""Iterator that extracts tiles of data from a Esri Raster."""</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">__init__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> raster<SPAN class="punctuation token">:</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">,</SPAN> tile_size<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">1000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1000</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> no_data_val<SPAN class="operator token">=</SPAN>np<SPAN class="punctuation token">.</SPAN>nan<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" :param raster: Raster to iterate :param tile_size: size of each block/tile to extrac to numpy array :param no_data_val: value in the raster which should be treated as no data """</SPAN> self<SPAN class="punctuation token">.</SPAN>tile_size <SPAN class="operator token">=</SPAN> tile_size self<SPAN class="punctuation token">.</SPAN>raster <SPAN class="operator token">=</SPAN> raster self<SPAN class="punctuation token">.</SPAN>no_data_val <SPAN class="operator token">=</SPAN> no_data_val self<SPAN class="punctuation token">.</SPAN>cell_size <SPAN class="operator token">=</SPAN> raster<SPAN class="punctuation token">.</SPAN>meanCellHeight self<SPAN class="punctuation token">.</SPAN>pos <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="comment token"># work out the size of all the block to create. They won't be extracted at this point</SPAN> t_rows <SPAN class="operator token">=</SPAN> raster<SPAN class="punctuation token">.</SPAN>height t_cols <SPAN class="operator token">=</SPAN> raster<SPAN class="punctuation token">.</SPAN>width x_min <SPAN class="operator token">=</SPAN> raster<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMin y_min <SPAN class="operator token">=</SPAN> raster<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMin self<SPAN class="punctuation token">.</SPAN>tiles <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> t_rows<SPAN class="punctuation token">,</SPAN> tile_size<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> col <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> t_cols<SPAN class="punctuation token">,</SPAN> tile_size<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> colend <SPAN class="operator token">=</SPAN> min<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>t_cols<SPAN class="punctuation token">,</SPAN> col <SPAN class="operator token">+</SPAN> tile_size<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> rowend <SPAN class="operator token">=</SPAN> min<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>t_rows<SPAN class="punctuation token">,</SPAN> row <SPAN class="operator token">+</SPAN> tile_size<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> xstart <SPAN class="operator token">=</SPAN> x_min <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>col <SPAN class="operator token">*</SPAN> self<SPAN class="punctuation token">.</SPAN>cell_size<SPAN class="punctuation token">)</SPAN> ystart <SPAN class="operator token">=</SPAN> y_min <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>row <SPAN class="operator token">*</SPAN> self<SPAN class="punctuation token">.</SPAN>cell_size<SPAN class="punctuation token">)</SPAN> self<SPAN class="punctuation token">.</SPAN>tiles<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>namedtuple<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'tile'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'colstart colend rowstart rowend xstart ystart'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">(</SPAN> colstart<SPAN class="operator token">=</SPAN>col<SPAN class="punctuation token">,</SPAN> colend<SPAN class="operator token">=</SPAN>colend<SPAN class="punctuation token">,</SPAN> rowstart<SPAN class="operator token">=</SPAN>row<SPAN class="punctuation token">,</SPAN> rowend<SPAN class="operator token">=</SPAN>rowend<SPAN class="punctuation token">,</SPAN> xstart<SPAN class="operator token">=</SPAN>xstart<SPAN class="punctuation token">,</SPAN> ystart<SPAN class="operator token">=</SPAN>ystart <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">__iter__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> self <SPAN class="keyword token">def</SPAN> <SPAN class="token function">__len__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> len<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">.</SPAN>tiles<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">__next__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN> List<SPAN class="punctuation token">[</SPAN>Tuple<SPAN class="punctuation token">[</SPAN>float<SPAN class="punctuation token">,</SPAN> float<SPAN class="punctuation token">,</SPAN> float<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> tile <SPAN class="operator token">=</SPAN> self<SPAN class="punctuation token">.</SPAN>tiles<SPAN class="punctuation token">[</SPAN>self<SPAN class="punctuation token">.</SPAN>pos<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">except</SPAN> IndexError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">raise</SPAN> StopIteration lower_l <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>tile<SPAN class="punctuation token">.</SPAN>xstart<SPAN class="punctuation token">,</SPAN> tile<SPAN class="punctuation token">.</SPAN>ystart<SPAN class="punctuation token">)</SPAN> ncols <SPAN class="operator token">=</SPAN> tile<SPAN class="punctuation token">.</SPAN>colend <SPAN class="operator token">-</SPAN> tile<SPAN class="punctuation token">.</SPAN>colstart rnrows <SPAN class="operator token">=</SPAN> tile<SPAN class="punctuation token">.</SPAN>rowend <SPAN class="operator token">-</SPAN> tile<SPAN class="punctuation token">.</SPAN>rowstart arr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RasterToNumPyArray<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">.</SPAN>raster<SPAN class="punctuation token">,</SPAN> nodata_to_value<SPAN class="operator token">=</SPAN>np<SPAN class="punctuation token">.</SPAN>nan<SPAN class="punctuation token">,</SPAN> lower_left_corner<SPAN class="operator token">=</SPAN>lower_l<SPAN class="punctuation token">,</SPAN> ncols<SPAN class="operator token">=</SPAN>ncols<SPAN class="punctuation token">,</SPAN> nrows<SPAN class="operator token">=</SPAN>rnrows<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Loop over rows and extract X,Y,Z</SPAN> output_data <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="punctuation token">(</SPAN>j <SPAN class="operator token">*</SPAN> self<SPAN class="punctuation token">.</SPAN>cell_size <SPAN class="operator token">+</SPAN> tile<SPAN class="punctuation token">.</SPAN>xstart<SPAN class="punctuation token">,</SPAN> i <SPAN class="operator token">*</SPAN> self<SPAN class="punctuation token">.</SPAN>cell_size <SPAN class="operator token">+</SPAN> tile<SPAN class="punctuation token">.</SPAN>ystart<SPAN class="punctuation token">,</SPAN> value<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> i<SPAN class="punctuation token">,</SPAN> row <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>arr<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> j<SPAN class="punctuation token">,</SPAN> value <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> self<SPAN class="punctuation token">.</SPAN>no_data_val <SPAN class="operator token">==</SPAN> value <SPAN class="operator token">and</SPAN> <SPAN class="operator token">not</SPAN> np<SPAN class="punctuation token">.</SPAN>isnan<SPAN class="punctuation token">(</SPAN>value<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">]</SPAN> self<SPAN class="punctuation token">.</SPAN>pos <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="keyword token">return</SPAN> output_data<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
```
Do I loop for rows and cols?
ncols=67000 nrows=58000 cellsize=2 xmin=0 ymax=58000 ## ^^ Get all this info from your original raster tile_size=(128,128) #GDB rasters=(128,128), tif=(ncols,1), GRID=(256,4) ntiles = 4 #no. of tiles to process at a time #arcpy.env.Extent=in_raster #arcpy.env.outputCoordinateSystem=in_raster #arcpy.env.cellSize=in_raster #new_raster=arcpy.CreateRasterDataset(out_path, out_name, cellsize, pixel_type, # spatial_reference) for row in range(0,nrows,tile_size[1]): for col in range(0,ncols,tile_size[0]*ntiles): colend=min([ncols,col+tile_size[0]*ntiles-1]) rowend=min([nrows,row+tile_size[1]-1]) xstart=xmin+(cellsize*col) ystart=ymax-(cellsize*(rowend+1)) print 'Processing cols',col,':',colend print 'Processing rows',row,':',rowend print 'Extracting lower left corner:',xstart,ystart #array=RasterToNumPyArray (in_raster, arcpy.Point(xmin,ystart), ncols, step) #Do your processing #out_raster=NumPyArrayToRaster(array, arcpy.Point(xmin,ystart), cellsize, cellsize) #arcpy.Mosaic_management(out_raster,new_raster)
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.