I'm having trouble converting a Raster to an Array
<p>I'm currently having some trouble converting a raster to an array. Ultimately I would like to convert a raster from an integer to a float32 so that I can run a gdal_calc however, I could not change the type properly in order to do this. </p>

<p>SO, I would appreciate any help that someone could provide. Here is my script, very short.</p>

<pre><code>import gdal
import numpy as np
import math
import osgeo
import os
import scipy


# Open Rasters 

hvRaster = gdal.Open("C:\\Users\\moses\\Desktop\\Calc_Test\\IMG-HV-ALOS2110871010-160611-HBQR1.5RUA.img")
vhRaster = gdal.Open("C:\\Users\\moses\\Desktop\\Calc_Test\\IMG-VH-ALOS2110871010-160611-HBQR1.5RUA.img")


# Get Raster Band

hvRasterBand = hvRaster.GetRasterBand(1)
vhRasterBand = vhRaster.GetRasterBand(1)

# Convert Raster to Array

hvArray = np.array(hvRaster.GetRasterBand(1).ReadAsArray())
vhArray = np.array(vhRaster.GetRasterBand(1).ReadAsArray())

print hvArray
print vhArray
</code></pre>

<p>Thank you in advance,</p>

<p>Moses</p>

<p>The method ReadAsArray() will create a numpy.ndarray with a dtype of the raster dataset. Your goal is to transform an integer dtype to a float32. The simplest way to do this is to use the <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html" rel="nofollow">astype()</a> method for ndarray.</p>

<pre><code># Convert Raster to Array

hvArray = hvRaster.GetRasterBand(1).ReadAsArray().astype(np.float32)
vhArray = vhRaster.GetRasterBand(1).ReadAsArray().astype(np.float32)
</code></pre>

python arrays numpy raster gdal