60 likes | 151 Views
ABT 182 / HYD 182 Environmental Analysis using GIS Week 8-2. OOP Object Oriented Programming. import numpy class Extent: def __init__(self, xmn =0, xmx =1, ymn =0, ymx =1): self.xmin = xmn self.xmax = xmx self.ymin = ymn self.ymax = ymx
E N D
ABT 182 / HYD 182 Environmental Analysis using GIS Week 8-2 OOP Object Oriented Programming
import numpy class Extent: def __init__(self, xmn=0, xmx=1, ymn=0, ymx=1): self.xmin = xmn self.xmax = xmx self.ymin = ymn self.ymax = ymx def setExtent(self, xmn, xmx, ymn, ymx): if xmn >= xmx | ymn >= ymx: print('error; invalid extent') else: self.xmin = xmn self.xmax = xmx self.ymin = ymn self.ymax = ymx def get(self): return( [self.xmin, self.xmax, self.ymin, self.ymax] )
class Spatial(Extent): def __init__(self, xmn=0, xmx=1, ymn=0, ymx=1, crs=None ): self.extent = Extent(xmn, xmx, ymn, ymx) self.crs = crs def extent(self, extent): self.extent = extent def setCRS(self, crs): self.crs = crs def show(self): print 'Extent: ', self.extent.get() print 'Coordinate reference system: ', self.crs
class Raster(Spatial): def __init__(self, xmn=-180, xmx=180, ymn=-90, ymx=90,ncols=1, nrows=1, crs="+proj=longlat +datum=WGS84"): self.spatial = Spatial(xmn, xmx, ymn, ymx, crs) self.crs = crs self.ncol = ncols self.nrow = nrows self.data = None self.cells = self.ncells() self.values = numpy.zeros(cells) def show(self): self.spatial.show() print 'ncols: ', self.ncol print 'nrows: ', self.nrow print 'ncells: ', self.ncells() def ncells(self): return( self.nrow * self.ncol) .. continued on next page ..
def setValues(self, v): if len(v) == self.ncells(): self.values = v else: print 'not good' def add(self, x): self.values += x
r = Raster() r.show() r = Raster(ncols=10, nrows=10) r.show() r.setValues(numpy.zeros(r.cells)) #r.setValues(numpy.arange(100)) print r.sumCells() r.add(10) print r.sumCells()