[GIS] Moran’s I on 2D arrays/rasters

autocorrelationmoran-indexpysalpythonraster

I would like to apply Moran's I spatial auto-correlation measure to 2D arrays or raster files in Python and I am wondering if there is an available implementation for it.

I know Pysal offers an implementation but I was not able to figure how to apply it to 2D data.

Any other libraries/implementations that would allow me to do that?

Best Answer

Eventually, with the help of Serge Rey's answer and this link in the documentation, I ended up using pysal's implementation as follows:

import pysal as ps
w = ps.lat2W(input_img.shape[0],input_img.shape[1], rook=False, id_type="int")
np.random.seed(12345)
lm = ps.Moran_Local(input_img,w)
moran_significance = np.reshape(lm.p_sim, input_img.shape)

In the last line I reshape the array of pseudo p_values (which indicate the significance of the local indicators of spatial association (LISAs) ) for visualization purposes:

enter image description here

I just leave it here as an answer in case someone finds it useful in the future.

Related Question