Skip to content

Commit

Permalink
Updated to pysal 2 frunctions
Browse files Browse the repository at this point in the history
Backward compatible
  • Loading branch information
ozak committed Nov 7, 2019
1 parent 0612bd9 commit 40f42fc
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions georasters/georasters.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@
from affine import Affine
from rasterstats import zonal_stats
import pysal
if pysal.__version__.startswith('2'):
from pysal.explore.esda import G as pysal_G
from pysal.explore.esda import G_Local as pysal_G_Local
from pysal.explore.esda import Gamma as pysal_Gamma
from pysal.explore.esda import Join_Counts as pysal_Join_Counts
from pysal.explore.esda import Moran as pysal_Moran
from pysal.explore.esda import Moran_Local as pysal_Moran_Local
from pysal.explore.esda import Geary as pysal_Geary
from pysal.lib.weights import lat2W as pysal_lat2W
from pysal.lib.weights import W as pysal_W
else:
from pysal import G as pysal_G
from pysal import G_Local as pysal_G_Local
from pysal import Gamma as pysal_Gamma
from pysal import Join_Counts as pysal_Join_Counts
from pysal import Moran as pysal_Moran
from pysal import Moran_Local as pysal_Moran_Local
from pysal import Geary as pysal_Geary
from pysal import lat2W as pydal_lat2W
from pysal import W as pysal_W

# Function to read the original file's projection:
def get_geo_info(filename, band=1):
Expand Down Expand Up @@ -960,14 +980,14 @@ def pysal_G(self, **kwargs):
Usage:
geo.pysal_G(permutations = 1000, rook=True)
arguments passed to raster_weights() and pysal.G
See help(gr.raster_weights), help(pysal.G) for options
arguments passed to raster_weights() and pysal_G
See help(gr.raster_weights), help(pysal_G) for options
"""
if self.weights is None:
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.G = pysal.G(rasterf, self.weights, **kwargs)
self.G = pysal_G(rasterf, self.weights, **kwargs)
pass

def pysal_Gamma(self, **kwargs):
Expand All @@ -984,7 +1004,7 @@ def pysal_Gamma(self, **kwargs):
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.Gamma = pysal.Gamma(rasterf, self.weights, **kwargs)
self.Gamma = pysal_Gamma(rasterf, self.weights, **kwargs)
pass

def pysal_Join_Counts(self, **kwargs):
Expand All @@ -1001,7 +1021,7 @@ def pysal_Join_Counts(self, **kwargs):
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.Join_Counts = pysal.Join_Counts(rasterf, self.weights, **kwargs)
self.Join_Counts = pysal_Join_Counts(rasterf, self.weights, **kwargs)
pass

def pysal_Moran(self, **kwargs):
Expand All @@ -1018,7 +1038,7 @@ def pysal_Moran(self, **kwargs):
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.Moran = pysal.Moran(rasterf, self.weights, **kwargs)
self.Moran = pysal_Moran(rasterf, self.weights, **kwargs)
pass

def pysal_Geary(self, **kwargs):
Expand All @@ -1035,7 +1055,7 @@ def pysal_Geary(self, **kwargs):
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.Geary = pysal.Geary(rasterf, self.weights, **kwargs)
self.Geary = pysal_Geary(rasterf, self.weights, **kwargs)
pass

def pysal_Moran_Local(self, **kwargs):
Expand All @@ -1052,7 +1072,7 @@ def pysal_Moran_Local(self, **kwargs):
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.Moran_Local = pysal.Moran_Local(rasterf, self.weights, **kwargs)
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
Expand All @@ -1074,7 +1094,7 @@ def pysal_G_Local(self, star=False, **kwargs):
self.raster_weights(**kwargs)
rasterf = self.raster.flatten()
rasterf = rasterf[rasterf.mask==False]
self.G_Local = pysal.G_Local(rasterf, self.weights, **kwargs)
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
Expand Down Expand Up @@ -1444,7 +1464,7 @@ def raster_weights(raster, rook=False, transform='r', **kwargs):
shape = (np.sqrt(raster.shape[0]) * np.array([1,1])).astype(int)
else:
shape = raster.shape
w = pysal.lat2W(*shape, rook=rook, **kwargs)
w = pysal_lat2W(*shape, rook=rook, **kwargs)

# Identify missing/no data
if isinstance(rasterf, np.ma.core.MaskedArray):
Expand All @@ -1458,7 +1478,7 @@ def raster_weights(raster, rook=False, transform='r', **kwargs):
if key not in missn:
value = list(set(value).difference(missn))
cneighbors[key] = value
w = pysal.W(cneighbors)
w = pysal_W(cneighbors)
w.transform = transform
return w

Expand Down

0 comments on commit 40f42fc

Please sign in to comment.