Skip to content

Commit

Permalink
Local Spatial autocorrelation measures from PySal
Browse files Browse the repository at this point in the history
  • Loading branch information
ozak committed Dec 26, 2016
1 parent 4081bba commit e76a24e
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions georasters/georasters.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ def __init__(self, raster, geot, nodata_value=np.nan, projection=None, datatype=
self.Join_Counts = None
self.Moran = None
self.Geary = None
self.Moran_Local = None

def __getitem__(self, indx):
rast = self.raster.__getitem__(indx)
Expand Down Expand Up @@ -976,7 +977,7 @@ def pysal_Moran(self, **kwargs):
Usage:
geo.pysal_Moran(permutations = 1000, rook=True)
arguments passed to raster_weights() and pysal.G
arguments passed to raster_weights() and pysal.Moran
See help(gr.raster_weights), help(pysal.Moran) for options
"""
if self.weights is None:
Expand All @@ -993,8 +994,8 @@ def pysal_Geary(self, **kwargs):
Usage:
geo.pysal_C(permutations = 1000, rook=True)
arguments passed to raster_weights() and pysal.G
See help(gr.raster_weights), help(pysal.C) for options
arguments passed to raster_weights() and pysal.Geary
See help(gr.raster_weights), help(pysal.Geary) for options
"""
if self.weights is None:
self.raster_weights(**kwargs)
Expand All @@ -1003,6 +1004,65 @@ def pysal_Geary(self, **kwargs):
self.Geary = pysal.Geary(rasterf, self.weights, **kwargs)
pass

def pysal_Moran_Local(self, **kwargs):
"""
Compute Local Moran's I measure of local spatial autocorrelation for GeoRaster
Usage:
geo.pysal_Moran_Local(permutations = 1000, rook=True)
arguments passed to raster_weights() and pysal.Moran_Local
See help(gr.raster_weights), help(pysal.Moran_Local) for options
"""
if self.weights is None:
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.Moran_Local = pysal.Moran_Local(rasterf, self.weights, **kwargs)
for i in self.Moran_Local.__dict__.keys():
if (isinstance(getattr(self.Moran_Local, i), np.ma.masked_array) or
(isinstance(getattr(self.Moran_Local, i), np.ndarray)) and
len(getattr(self.Moran_Local, i).shape) == 1):
setattr(self.Moran_Local, i, self.map_vector(getattr(self.Moran_Local, i)))
pass

def pysal_G_Local(self, star=False, **kwargs):
"""
Compute Local G or G* measures of local spatial autocorrelation for GeoRaster
Usage:
geo.pysal_Moran(permutations = 1000, rook=True)
arguments passed to raster_weights() and pysal.G_Local
See help(gr.raster_weights), help(pysal.G_Local) for options
"""
if self.weights is None:
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.G_Local = pysal.G_Local(rasterf, self.weights, **kwargs)
for i in self.G_Local.__dict__.keys():
if (isinstance(getattr(self.G_Local, i), np.ma.masked_array) or
(isinstance(getattr(self.G_Local, i), np.ndarray)) and
len(getattr(self.G_Local, i).shape) == 1):
setattr(self.G_Local, i, self.map_vector(getattr(self.G_Local, i)))
pass

def map_vector(self, x, **kvars):
"""
Create new GeoRaster, which has its data replaced by x
Useful to map output of PySal analyses, e.g. spatial autocorrelation values, etc.
Usage: raster2 = map_vector(x, raster)
where
raster: GeoRaster
x: Numpy array of data with same length as non-missing values in raster,
i.e., len(x) == np.sum(raster.mask==False)
"""
y = self.copy()
y.raster[y.raster.mask == False] = x
return y

# Setup Graph for distance computations and provide distance functions
def mcp(self, *args, **kwargs):
"""
Expand Down

1 comment on commit e76a24e

@ozak
Copy link
Owner Author

@ozak ozak commented on e76a24e Dec 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added local autocorrelation measures from PySal. See #13

Please sign in to comment.