[GRASS-SVN] r53975 - in grass/trunk/lib/python/pygrass: . raster
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Nov 23 03:45:24 PST 2012
Author: lucadelu
Date: 2012-11-23 03:45:23 -0800 (Fri, 23 Nov 2012)
New Revision: 53975
Modified:
grass/trunk/lib/python/pygrass/env.py
grass/trunk/lib/python/pygrass/errors.py
grass/trunk/lib/python/pygrass/raster/__init__.py
grass/trunk/lib/python/pygrass/raster/abstract.py
Log:
fix must_be_open; add must_be_open to raster methods; moved generic functions to env
Modified: grass/trunk/lib/python/pygrass/env.py
===================================================================
--- grass/trunk/lib/python/pygrass/env.py 2012-11-23 11:28:45 UTC (rev 53974)
+++ grass/trunk/lib/python/pygrass/env.py 2012-11-23 11:45:23 UTC (rev 53975)
@@ -4,8 +4,11 @@
@author: pietro
"""
+
+import fnmatch
+
import grass.lib.gis as libgis
-import fnmatch
+import grass.lib.raster as libraster
from grass.script import core as grasscore
@@ -54,4 +57,39 @@
mapset = get_mapset_vector(mapname, mapset)
if mapset:
return True
- return False
\ No newline at end of file
+ return False
+
+
+def clean_map_name(name):
+ name.strip()
+ for char in ' @#^?°,;%&/':
+ name = name.replace(char, '')
+ return name
+
+
+def coor2pixel((east, north), region):
+ """Convert coordinates into a pixel row and col ::
+
+ >>> from pygrass.region import Region
+ >>> reg = Region()
+ >>> coor2pixel((reg.west, reg.north), reg)
+ (0.0, 0.0)
+ >>> coor2pixel((reg.east, reg.south), reg) == (reg.cols, reg.rows)
+ True
+ """
+ return (libraster.Rast_northing_to_row(north, region.c_region),
+ libraster.Rast_easting_to_col(east, region.c_region))
+
+
+def pixel2coor((col, row), region):
+ """Convert row and col of a pixel into a coordinates ::
+
+ >>> from pygrass.region import Region
+ >>> reg = Region()
+ >>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
+ True
+ >>> pixel2coor((reg.cols, reg.rows), reg) == (reg.east, reg.south)
+ True
+ """
+ return (libraster.Rast_row_to_northing(row, region.c_region),
+ libraster.Rast_col_to_easting(col, region.c_region))
\ No newline at end of file
Modified: grass/trunk/lib/python/pygrass/errors.py
===================================================================
--- grass/trunk/lib/python/pygrass/errors.py 2012-11-23 11:28:45 UTC (rev 53974)
+++ grass/trunk/lib/python/pygrass/errors.py 2012-11-23 11:45:23 UTC (rev 53975)
@@ -22,10 +22,11 @@
def __str__(self):
return repr(self.value)
+
def must_be_open(method):
- def wrapper(self):
+ def wrapper(self, *args, **kargs):
if self.is_open():
- return method(self)
+ return method(self, *args, **kargs)
else:
warning(_("The map is close!"))
return wrapper
\ No newline at end of file
Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py 2012-11-23 11:28:45 UTC (rev 53974)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py 2012-11-23 11:45:23 UTC (rev 53975)
@@ -22,7 +22,7 @@
#
# import pygrass modules
#
-from pygrass.errors import OpenError
+from pygrass.errors import OpenError, must_be_open
from pygrass.region import Region
#
@@ -37,34 +37,6 @@
from history import History
-def coor2pixel((north, east), region):
- """Convert coordinates into a pixel row and col ::
-
- >>> from pygrass import Region
- >>> reg = Region()
- >>> coor2pixel((reg.north, reg.west), reg)
- (0.0, 0.0)
- >>> coor2pixel((reg.south, reg.east), reg) == (reg.rows, reg.cols)
- True
- """
- return (libraster.Rast_northing_to_row(north, region.c_region),
- libraster.Rast_easting_to_col(east, region.c_region))
-
-
-def pixel2coor((row, col), region):
- """Convert row and col of a pixel into a coordinates ::
-
- >>> from pygrass import Region
- >>> reg = Region()
- >>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
- True
- >>> pixel2coor((reg.rows, reg.cols), reg) == (reg.south, reg.east)
- True
- """
- return (libraster.Rast_row_to_northing(row, region.c_region),
- libraster.Rast_col_to_easting(col, region.c_region))
-
-
class RasterRow(RasterAbstractBase):
"""Raster_row_access": Inherits: "Raster_abstract_base" and implements
the default row access of the Rast library.
@@ -134,6 +106,7 @@
super(RasterRow, self).__init__(name, *args, **kargs)
# mode = "r", method = "row",
+ @must_be_open
def get_row(self, row, row_buffer=None):
"""Private method that return the row using the read mode
call the `Rast_get_row` C function.
@@ -153,6 +126,7 @@
libraster.Rast_get_row(self._fd, row_buffer.p, row, self._gtype)
return row_buffer
+ @must_be_open
def put_row(self, row):
"""Private method to write the row sequentially.
"""
@@ -223,24 +197,23 @@
super(RasterRowIO, self).open(mode, mtype, overwrite)
self.rowio.open(self._fd, self.rows, self.cols, self.mtype)
+ @must_be_open
def close(self):
- if self.is_open():
- self.rowio.release()
- libraster.Rast_close(self._fd)
- # update rows and cols attributes
- self._rows = None
- self._cols = None
- self._fd = None
- else:
- warning(_("The map is already close!"))
+ self.rowio.release()
+ libraster.Rast_close(self._fd)
+ # update rows and cols attributes
+ self._rows = None
+ self._cols = None
+ self._fd = None
+ @must_be_open
def get_row(self, row, row_buffer=None):
- """Private method that return the row using:
+ """This method returns the row using:
* the read mode and
* `rowcache` method
- not implemented yet!"""
+ """
if row_buffer is None:
row_buffer = Buffer((self._cols,), self.mtype)
rowio_buf = librowio.Rowio_get(ctypes.byref(self.rowio.crowio), row)
@@ -298,27 +271,28 @@
else:
raise TypeError("Invalid argument type.")
+ @must_be_open
def map2segment(self):
"""Transform an existing map to segment file.
"""
- if self.is_open():
- row_buffer = Buffer((self._cols), self.mtype)
- for row in xrange(self._rows):
- libraster.Rast_get_row(
- self._fd, row_buffer.p, row, self._gtype)
- libseg.segment_put_row(ctypes.byref(self.segment.cseg),
- row_buffer.p, row)
+ row_buffer = Buffer((self._cols), self.mtype)
+ for row in xrange(self._rows):
+ libraster.Rast_get_row(
+ self._fd, row_buffer.p, row, self._gtype)
+ libseg.segment_put_row(ctypes.byref(self.segment.cseg),
+ row_buffer.p, row)
+ @must_be_open
def segment2map(self):
"""Transform the segment file to a map.
"""
- if self.is_open():
- row_buffer = Buffer((self._cols), self.mtype)
- for row in xrange(self._rows):
- libseg.segment_get_row(ctypes.byref(self.segment.cseg),
- row_buffer.p, row)
- libraster.Rast_put_row(self._fd, row_buffer.p, self._gtype)
+ row_buffer = Buffer((self._cols), self.mtype)
+ for row in xrange(self._rows):
+ libseg.segment_get_row(ctypes.byref(self.segment.cseg),
+ row_buffer.p, row)
+ libraster.Rast_put_row(self._fd, row_buffer.p, self._gtype)
+ @must_be_open
def get_row(self, row, row_buffer=None):
"""Return the row using the `segment.get_row` method
@@ -336,6 +310,7 @@
ctypes.byref(self.segment.cseg), row_buffer.p, row)
return row_buffer
+ @must_be_open
def put_row(self, row, row_buffer):
"""Write the row using the `segment.put_row` method
@@ -350,6 +325,7 @@
libseg.segment_put_row(ctypes.byref(self.segment.cseg),
row_buffer.p, row)
+ @must_be_open
def get(self, row, col):
"""Return the map value using the `segment.get` method
@@ -365,6 +341,7 @@
ctypes.byref(self.segment.val), row, col)
return self.segment.val.value
+ @must_be_open
def put(self, row, col, val):
"""Write the value to the map using the `segment.put` method
@@ -445,6 +422,7 @@
self.segment.open(self)
self._fd = libraster.Rast_open_new(self.name, self._gtype)
+ @must_be_open
def close(self, rm_temp_files=True):
"""Close the map, copy the segment files to the map.
@@ -454,21 +432,18 @@
rm_temp_files: bool
If True all the segments file will be removed.
"""
- if self.is_open():
- if self.mode == "w" or self.mode == "rw":
- self.segment.flush()
- self.segment2map()
- if rm_temp_files:
- self.segment.close()
- else:
- self.segment.release()
- libraster.Rast_close(self._fd)
- # update rows and cols attributes
- self._rows = None
- self._cols = None
- self._fd = None
+ if self.mode == "w" or self.mode == "rw":
+ self.segment.flush()
+ self.segment2map()
+ if rm_temp_files:
+ self.segment.close()
else:
- warning(_("The map is already close!"))
+ self.segment.release()
+ libraster.Rast_close(self._fd)
+ # update rows and cols attributes
+ self._rows = None
+ self._cols = None
+ self._fd = None
FLAGS = {1: {'b': 'i', 'i': 'i', 'u': 'i'},
Modified: grass/trunk/lib/python/pygrass/raster/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/abstract.py 2012-11-23 11:28:45 UTC (rev 53974)
+++ grass/trunk/lib/python/pygrass/raster/abstract.py 2012-11-23 11:45:23 UTC (rev 53975)
@@ -36,13 +36,6 @@
INDXOUTRANGE = "The index (%d) is out of range, have you open the map?."
-def clean_map_name(name):
- name.strip()
- for char in ' @#^?°,;%&/':
- name = name.replace(char, '')
- return name
-
-
class RasterAbstractBase(object):
"""Raster_abstract_base: The base class from which all sub-classes
inherit. It does not implement any row or map access methods:
@@ -128,7 +121,7 @@
def _set_name(self, newname):
"""Private method to change the Raster name"""
#import pdb; pdb.set_trace()
- cleanname = clean_map_name(newname)
+ cleanname = env.clean_map_name(newname)
if self.exist():
self.rename(cleanname)
self._name = cleanname
More information about the grass-commit
mailing list